From 4b36703287c3136d5cdbae90c1e1b8a3274f5018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Mon, 5 Apr 2021 18:45:57 +0200 Subject: [PATCH] feat: basic drawing --- core/src/bzh/fainsin/sagittarius/Entity.java | 58 ++++++++++ .../bzh/fainsin/sagittarius/GameScreen.java | 105 ++++++++++++++++++ core/src/bzh/fainsin/sagittarius/HUD.java | 23 ++++ core/src/bzh/fainsin/sagittarius/Planet.java | 42 +++++++ .../fainsin/sagittarius/SagittariusGame.java | 51 +++++---- 5 files changed, 257 insertions(+), 22 deletions(-) create mode 100644 core/src/bzh/fainsin/sagittarius/Entity.java create mode 100644 core/src/bzh/fainsin/sagittarius/GameScreen.java create mode 100644 core/src/bzh/fainsin/sagittarius/HUD.java create mode 100644 core/src/bzh/fainsin/sagittarius/Planet.java diff --git a/core/src/bzh/fainsin/sagittarius/Entity.java b/core/src/bzh/fainsin/sagittarius/Entity.java new file mode 100644 index 0000000..9e6d110 --- /dev/null +++ b/core/src/bzh/fainsin/sagittarius/Entity.java @@ -0,0 +1,58 @@ +package bzh.fainsin.sagittarius; + +import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.math.Vector2; + +abstract class Entity { + +// ---------- ATTRIBUTs ---------- + + protected Vector2 position; + + protected float mass = 1.0f; + protected float angle; + protected Color color = Color.WHITE; + +// ---------- CONSTRUCTORs ---------- + + protected Entity(Vector2 position, float mass) { + this.position = position; + this.mass = mass; + } + + protected Entity(Vector2 position, float mass, Color color) { + this(position, mass); + this.color = color; + } + +// ---------- GETs ---------- + + + Vector2 getPosition() { + return this.position; + } + + float getMass() { + return this.mass; + } + + float getAngle() { + return this.angle; + } + + Color getColor() { + return this.color; + } + +// ---------- METHODs ---------- + + /** + * Return the euclidian distance to another entity + * @param entity + * @return distance to entity + */ + float distanceTo(Entity entity) { + return this.position.sub(entity.getPosition()).len(); + } + +} diff --git a/core/src/bzh/fainsin/sagittarius/GameScreen.java b/core/src/bzh/fainsin/sagittarius/GameScreen.java new file mode 100644 index 0000000..0b92aa3 --- /dev/null +++ b/core/src/bzh/fainsin/sagittarius/GameScreen.java @@ -0,0 +1,105 @@ +package bzh.fainsin.sagittarius; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.ScreenAdapter; +import com.badlogic.gdx.graphics.Camera; +import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.OrthographicCamera; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.utils.viewport.FitViewport; +import com.badlogic.gdx.utils.viewport.Viewport; + +class GameScreen extends ScreenAdapter { + +// ---------- ATTRIBUTES ---------- + + // screen size + static final int WIDTH = 1920; + static final int HEIGHT = 1080; + + // drawing stuff + private SpriteBatch batch; + private ShapeRenderer shapeRenderer; + private BitmapFont font; + + // camera stuff + private Viewport viewport; + public static Camera camera; + + // TODO: categorize better ? + private HUD hud = new HUD(); + +// ---------- METHODS ---------- + + private void clearScreen() { + Gdx.gl.glClearColor(Color.BLACK.r, Color.BLACK.g, Color.BLACK.b, Color.BLACK.a); + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + } + + private void update(float delta) { + SagittariusGame.update(delta); + hud.update(); + camera.update(); + } + + @Override + public void render(float delta) { + + update(delta); + + clearScreen(); + + // ---------- batch draw ---------- + batch.setProjectionMatrix(camera.combined); + batch.begin(); + + // ---------- HUD ---------- + hud.render(batch, font); + + batch.end(); + + // ---------- shapeRenderer draw ---------- + shapeRenderer.setProjectionMatrix(camera.combined); + shapeRenderer.begin(ShapeRenderer.ShapeType.Line); + + // ---------- planets ---------- + for (Planet planet : SagittariusGame.planetList) { + planet.render(shapeRenderer); + } + + shapeRenderer.end(); + + } + + @Override + public void show() { + camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); + camera.position.set(WIDTH / 2, HEIGHT / 2, 0); + camera.update(); + + viewport = new FitViewport(WIDTH, HEIGHT, camera); + + shapeRenderer = new ShapeRenderer(); + batch = new SpriteBatch(); + font = new BitmapFont(); + } + + @Override + public void resize(int width, int height) { + viewport.update(width, height); + camera.update(); + batch.setProjectionMatrix(camera.combined); + shapeRenderer.setProjectionMatrix(camera.combined); + } + + @Override + public void dispose() { + batch.dispose(); + font.dispose(); + shapeRenderer.dispose(); + } + +} diff --git a/core/src/bzh/fainsin/sagittarius/HUD.java b/core/src/bzh/fainsin/sagittarius/HUD.java new file mode 100644 index 0000000..574a32a --- /dev/null +++ b/core/src/bzh/fainsin/sagittarius/HUD.java @@ -0,0 +1,23 @@ +package bzh.fainsin.sagittarius; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.BitmapFont; + +class HUD { + +// ATTRIBUTS + + private int frameRate; + +// MÉTHODES + + public void update() { + frameRate = Gdx.graphics.getFramesPerSecond(); + } + + public void render(Batch batch, BitmapFont font) { + font.draw(batch, frameRate + " fps", 3, GameScreen.HEIGHT - 3); + } + +} diff --git a/core/src/bzh/fainsin/sagittarius/Planet.java b/core/src/bzh/fainsin/sagittarius/Planet.java new file mode 100644 index 0000000..aef6924 --- /dev/null +++ b/core/src/bzh/fainsin/sagittarius/Planet.java @@ -0,0 +1,42 @@ +package bzh.fainsin.sagittarius; + +import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Vector2; + +class Planet extends Entity { + +// ---------- ATTRIBUTEs ---------- + + private float radius = 10; + +// ---------- CONSTRUCTORs ---------- + + Planet(Vector2 position, float mass, float radius) { + super(position, mass); + this.radius = radius; + } + + Planet(Vector2 position, float mass, float radius, Color color) { + super(position, mass, color); + this.radius = radius; + } + +// ---------- METHODS ---------- + + void render(ShapeRenderer shapeRenderer) { + shapeRenderer.setColor(color); + shapeRenderer.circle(this.position.x, this.position.y, radius); + } + + void renderDebug(Batch batch, BitmapFont font) { + font.draw(batch, "x = " + (int) position.x + ", y = " + (int) position.y, position.x, position.y); + } + + float getRadius() { + return this.radius; + } + +} diff --git a/core/src/bzh/fainsin/sagittarius/SagittariusGame.java b/core/src/bzh/fainsin/sagittarius/SagittariusGame.java index e73489e..2eabd58 100644 --- a/core/src/bzh/fainsin/sagittarius/SagittariusGame.java +++ b/core/src/bzh/fainsin/sagittarius/SagittariusGame.java @@ -1,33 +1,40 @@ package bzh.fainsin.sagittarius; -import com.badlogic.gdx.ApplicationAdapter; +import java.util.ArrayList; + +import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.GL20; -import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.math.Vector3; -public class SagittariusGame extends ApplicationAdapter { - SpriteBatch batch; - Texture img; +public class SagittariusGame extends Game { + +// ---------- ATTRIBUTEs ---------- + + // Vectors + static Vector2 screenCursor; + static Vector2 worldCursor; + + // Planets + static ArrayList planetList; - @Override - public void create () { - batch = new SpriteBatch(); - img = new Texture("badlogic.jpg"); - } +// ---------- METHODs ---------- @Override - public void render () { - Gdx.gl.glClearColor(1, 0, 0, 1); - Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - batch.begin(); - batch.draw(img, 0, 0); - batch.end(); + public void create() { + setScreen(new GameScreen()); + + planetList = new ArrayList(); + planetList.add( new Planet(new Vector2(100, 100), 1, 50) ); } + + static void update(float delta) { + + // ---------- cursors ---------- + screenCursor = new Vector2(Gdx.input.getX(), Gdx.input.getY()); + Vector3 unprojectedCursor = GameScreen.camera.unproject(new Vector3(screenCursor, 0)); + worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y); - @Override - public void dispose () { - batch.dispose(); - img.dispose(); } + }