diff options
Diffstat (limited to 'core/src/main')
4 files changed, 175 insertions, 39 deletions
diff --git a/core/src/main/java/org/snoopdesigns/endless/screen/main/CameraDebug.java b/core/src/main/java/org/snoopdesigns/endless/screen/main/CameraDebug.java index 123f5bc..84d7a0e 100644 --- a/core/src/main/java/org/snoopdesigns/endless/screen/main/CameraDebug.java +++ b/core/src/main/java/org/snoopdesigns/endless/screen/main/CameraDebug.java @@ -26,16 +26,26 @@ public class CameraDebug implements Renderable {      @Override      public void render(float delta, RenderContext renderContext) {          batch.begin(); -        font.draw(batch, formatDebugText(renderContext), 100f, 100f); +        font.draw(batch, formatDebugText(renderContext), 100f, 200f);          batch.end();      }      private static String formatDebugText(final RenderContext renderContext) { -        return "cam: %f %f\nviewport: %f %f".formatted( +        final var minX = renderContext.getCamera().position.x - +                renderContext.getCamera().viewportWidth / 2; +        final var maxX = renderContext.getCamera().position.x + +                renderContext.getCamera().viewportWidth / 2; +        final var minY = renderContext.getCamera().position.y - +                renderContext.getCamera().viewportHeight / 2; +        final var maxY = renderContext.getCamera().position.y + +                renderContext.getCamera().viewportHeight / 2; +        return "cam: %f %f\nviewport: %f %f\nX: %f %f\nY: %f %f".formatted(                  renderContext.getCameraPosition().x,                  renderContext.getCameraPosition().y,                  renderContext.getCamera().viewportWidth, -                renderContext.getCamera().viewportHeight +                renderContext.getCamera().viewportHeight, +                minX, maxX, +                minY, maxY          );      }  } diff --git a/core/src/main/java/org/snoopdesigns/endless/screen/main/MainScreen.java b/core/src/main/java/org/snoopdesigns/endless/screen/main/MainScreen.java index 5f162b7..ed26ef6 100644 --- a/core/src/main/java/org/snoopdesigns/endless/screen/main/MainScreen.java +++ b/core/src/main/java/org/snoopdesigns/endless/screen/main/MainScreen.java @@ -6,44 +6,60 @@ import com.badlogic.gdx.InputAdapter;  import com.badlogic.gdx.Screen;  import com.badlogic.gdx.graphics.Color;  import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.math.MathUtils;  import com.badlogic.gdx.utils.ScreenUtils;  import org.snoopdesigns.endless.render.RenderContext;  import org.snoopdesigns.endless.render.Renderable;  public class MainScreen implements Screen { +    public final float WORLD_MULTIPLIER = 5f; + +    private float screenWidth; +    private float screenHeight; +      private Renderable starField;      private Renderable cameraDebug; +    private Renderable playerShip;      private OrthographicCamera camera;      private RenderContext renderContext;      @Override      public void show() { -        float screenWidth = Gdx.graphics.getWidth(); -        float screenHeight = Gdx.graphics.getHeight(); +        screenWidth = Gdx.graphics.getWidth(); +        screenHeight = Gdx.graphics.getHeight();          camera = new OrthographicCamera(                  screenWidth,                  screenWidth * (screenHeight / screenWidth)); +        // Set initial camera position +        camera.position.x = 0; +        camera.position.y = 0; +          Gdx.input.setInputProcessor(new InputAdapter() {              @Override              public boolean scrolled(float amountX, float amountY) { -                final var scale = 1.0f + (0.1f * amountY); +                final var scale = 1.0f + (0.05f * amountY);                  camera.viewportWidth *= scale; -                camera.viewportHeight *= scale; +                camera.viewportHeight = camera.viewportWidth * +                        (screenHeight / screenWidth);                  normalizeCamera();                  return true;              }              private void normalizeCamera() { -                //camera.zoom = MathUtils.clamp(camera.zoom, 0.1f, -                //WORLD_WIDTH / camera.viewportWidth); +                camera.viewportWidth = MathUtils.clamp(camera.viewportWidth, +                        screenWidth, +                        WORLD_MULTIPLIER * screenWidth); +                camera.viewportHeight = camera.viewportWidth * +                        (screenHeight / screenWidth);              }          });          starField = new StarField();          cameraDebug = new CameraDebug(); +        playerShip = new PlayerShip();          renderContext = new RenderContext(camera);      } @@ -57,6 +73,7 @@ public class MainScreen implements Screen {          starField.render(delta, renderContext);          cameraDebug.render(delta, renderContext); +        playerShip.render(delta, renderContext);      }      @Override @@ -80,17 +97,18 @@ public class MainScreen implements Screen {      }      private void handleInput() { +        final var cameraBoost = 10f;          if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { -            camera.translate(-3, 0, 0); +            camera.translate(-1 * cameraBoost, 0, 0);          }          if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { -            camera.translate(3, 0, 0); +            camera.translate(cameraBoost, 0, 0);          }          if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) { -            camera.translate(0, -3, 0); +            camera.translate(0, -1 * cameraBoost, 0);          }          if (Gdx.input.isKeyPressed(Input.Keys.UP)) { -            camera.translate(0, 3, 0); +            camera.translate(0, cameraBoost, 0);          }      }  } diff --git a/core/src/main/java/org/snoopdesigns/endless/screen/main/PlayerShip.java b/core/src/main/java/org/snoopdesigns/endless/screen/main/PlayerShip.java new file mode 100644 index 0000000..356b366 --- /dev/null +++ b/core/src/main/java/org/snoopdesigns/endless/screen/main/PlayerShip.java @@ -0,0 +1,32 @@ +package org.snoopdesigns.endless.screen.main; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Sprite; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import org.snoopdesigns.endless.render.RenderContext; +import org.snoopdesigns.endless.render.Renderable; + +public class PlayerShip implements Renderable { + +    private final SpriteBatch batch; +    private final Sprite sprite; + +    public PlayerShip() { +        batch = new SpriteBatch(); +        sprite = new Sprite(new Texture(Gdx.files.internal("ship/sparrow.png"))); +    } + +    @Override +    public void render(float delta, RenderContext renderContext) { +        batch.setProjectionMatrix(renderContext.getProjectionMatrix()); + +        sprite.setPosition( +                renderContext.getCameraPosition().x, +                renderContext.getCameraPosition().y); + +        batch.begin(); +        sprite.draw(batch); +        batch.end(); +    } +} diff --git a/core/src/main/java/org/snoopdesigns/endless/screen/main/StarField.java b/core/src/main/java/org/snoopdesigns/endless/screen/main/StarField.java index e5c059b..994b3fa 100644 --- a/core/src/main/java/org/snoopdesigns/endless/screen/main/StarField.java +++ b/core/src/main/java/org/snoopdesigns/endless/screen/main/StarField.java @@ -1,11 +1,13 @@  package org.snoopdesigns.endless.screen.main;  import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Color;  import com.badlogic.gdx.graphics.GL30;  import com.badlogic.gdx.graphics.Texture;  import com.badlogic.gdx.graphics.g2d.Sprite;  import com.badlogic.gdx.graphics.g2d.SpriteBatch;  import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Vector2;  import com.badlogic.gdx.math.Vector3;  import org.snoopdesigns.endless.render.RenderContext;  import org.snoopdesigns.endless.render.Renderable; @@ -16,29 +18,33 @@ import java.util.List;  public class StarField implements Renderable { -    private final float STAR_RADIUS = 1.0f; -    private final float TILE_AMOUNT = 100; -    private final float TILE_SIZE = 60; -    private final float BRIGHTNESS_FACTOR = 0.6f; -    private final int HAZE_AMOUNT = 16; +    private static final boolean DEBUG = true; +    private static final float VISUAL_SCREEN_BOUNDARY = DEBUG ? 0.9f : 1.0f; + +    private static final float STAR_RADIUS = 1.0f; +    private static final float TILE_AMOUNT = 3; +    private static final float TILE_SIZE = 4000; +    private static final int STARS_PER_TILE = 900; +    private static final float BRIGHTNESS_FACTOR = 0.9f; +    private static final int HAZE_AMOUNT = 16;      private final ShapeRenderer shapeRenderer;      private final SpriteBatch batch;      private final SecureRandom random = new SecureRandom();      private final List<Vector3> stars = new ArrayList<>(); +    private final List<Vector2> tiles = new ArrayList<>();      private final List<Sprite> hazes = new ArrayList<>();      public StarField() {          shapeRenderer = new ShapeRenderer();          batch = new SpriteBatch(); -        final var displacement = (TILE_AMOUNT * TILE_SIZE) / -2.0f;          for (int i = 0; i < TILE_AMOUNT; i++) {              for (int j = 0; j < TILE_AMOUNT; j++) { -                stars.add(new Vector3( -                        displacement + TILE_SIZE * i + random.nextFloat(TILE_SIZE), -                        displacement + TILE_SIZE * j + random.nextFloat(TILE_SIZE), -                        random.nextFloat(BRIGHTNESS_FACTOR))); +                tiles.add(new Vector2( +                        TILE_SIZE * i, +                        TILE_SIZE * j)); +                generateStars();              }          } @@ -52,32 +58,102 @@ public class StarField implements Renderable {          }      } +    private void generateStars() { +        for (int k = 0; k < STARS_PER_TILE; k++) { +            stars.add(new Vector3( +                    random.nextFloat(TILE_SIZE), +                    random.nextFloat(TILE_SIZE), +                    random.nextFloat(BRIGHTNESS_FACTOR))); +        } +    } +      @Override      public void render(float delta, RenderContext renderContext) {          final var projectionMatrix = renderContext.getProjectionMatrix(); -        final var cameraPosition = renderContext.getCameraPosition(); -          batch.setProjectionMatrix(projectionMatrix); -        batch.begin(); -        hazes.forEach(haze -> { -            haze.draw(batch); -        }); -        batch.end(); +        shapeRenderer.setProjectionMatrix(projectionMatrix); + +        final var minX = renderContext.getCamera().position.x - +                renderContext.getCamera().viewportWidth / 2 * VISUAL_SCREEN_BOUNDARY; +        final var maxX = renderContext.getCamera().position.x + +                renderContext.getCamera().viewportWidth / 2 * VISUAL_SCREEN_BOUNDARY; +        final var minY = renderContext.getCamera().position.y - +                renderContext.getCamera().viewportHeight / 2 * VISUAL_SCREEN_BOUNDARY; +        final var maxY = renderContext.getCamera().position.y + +                renderContext.getCamera().viewportHeight / 2 * VISUAL_SCREEN_BOUNDARY;          Gdx.gl.glEnable(GL30.GL_BLEND);          Gdx.gl.glBlendFunc(GL30.GL_SRC_ALPHA, GL30.GL_ONE_MINUS_SRC_ALPHA); -        shapeRenderer.setProjectionMatrix(projectionMatrix); -        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); +        // Haze +        batch.begin(); +        hazes.forEach(haze -> +                haze.draw(batch)); +        batch.end(); -        stars.forEach(star -> { -            shapeRenderer.setColor(1, 1, 1, star.z); -            shapeRenderer.circle( -                    star.x + cameraPosition.x * star.z, -                    star.y + cameraPosition.y * star.z, STAR_RADIUS); -        }); +        if (DEBUG) { +            shapeRenderer.begin(ShapeRenderer.ShapeType.Line); +            shapeRenderer.setColor(Color.GREEN); +            shapeRenderer.rect(minX, minY, maxX - minX, maxY - minY); +            shapeRenderer.end(); +        } + +        // Tiles +        for (int tileIndex = 0; tileIndex < TILE_AMOUNT * TILE_AMOUNT; tileIndex++) { +            final Vector2 tileCoords = tiles.get(tileIndex); +            final Vector2 adjustedCoords = new Vector2(tileCoords.x, tileCoords.y); +            while (adjustedCoords.x > maxX) { +                adjustedCoords.x -= TILE_SIZE * TILE_AMOUNT; +            } +            while (adjustedCoords.x < minX - TILE_SIZE) { +                adjustedCoords.x += TILE_SIZE * TILE_AMOUNT; +            } +            while (adjustedCoords.y > maxY) { +                adjustedCoords.y -= TILE_SIZE * TILE_AMOUNT; +            } +            while (adjustedCoords.y < minY - TILE_SIZE) { +                adjustedCoords.y += TILE_SIZE * TILE_AMOUNT; +            } + +            if (DEBUG) { +                shapeRenderer.begin(ShapeRenderer.ShapeType.Line); +                shapeRenderer.setColor(Color.YELLOW); +                if (isVisible(adjustedCoords, minX, maxX, minY, maxY)) { +                    shapeRenderer.rect(adjustedCoords.x, +                            adjustedCoords.y, +                            TILE_SIZE, +                            TILE_SIZE); +                } +                shapeRenderer.end(); +            } + +            if (isVisible(adjustedCoords, minX, maxX, minY, maxY)) { +                shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); +                for (int starIndex = tileIndex * STARS_PER_TILE; +                     starIndex < tileIndex * STARS_PER_TILE + STARS_PER_TILE; +                     starIndex++) { +                    final Vector3 star = stars.get(starIndex); +                    shapeRenderer.setColor(1, 1, 1, star.z); +                    shapeRenderer.circle( +                            adjustedCoords.x + star.x, +                            adjustedCoords.y + star.y, +                            STAR_RADIUS); +                } +            } +            shapeRenderer.end(); +        } -        shapeRenderer.end();          Gdx.gl.glDisable(GL30.GL_BLEND);      } + +    private static boolean isVisible(final Vector2 coords, +                                     final float minX, +                                     final float maxX, +                                     final float minY, +                                     final float maxY) { +        return coords.x + TILE_SIZE > minX && +                coords.x < maxX && +                coords.y + TILE_SIZE > minY && +                coords.y < maxY; +    }  }  | 
