From b279519b55183d519c7409f1da79a48ba3092a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Tue, 13 Apr 2021 18:43:41 +0200 Subject: [PATCH] feat: created BaseActor + factorized some code --- core/src/sagittarius/SagittariusGame.java | 4 ++-- core/src/sagittarius/model/Arrow.java | 29 ++++++++++------------- core/src/sagittarius/model/BaseActor.java | 21 ++++++++++++++++ core/src/sagittarius/model/Bow.java | 1 - core/src/sagittarius/model/Planet.java | 24 ++++++++----------- core/src/sagittarius/model/Player.java | 21 +++++++--------- core/src/sagittarius/view/GameScreen.java | 3 +++ 7 files changed, 58 insertions(+), 45 deletions(-) create mode 100644 core/src/sagittarius/model/BaseActor.java diff --git a/core/src/sagittarius/SagittariusGame.java b/core/src/sagittarius/SagittariusGame.java index 4cc4ce6..1a89227 100644 --- a/core/src/sagittarius/SagittariusGame.java +++ b/core/src/sagittarius/SagittariusGame.java @@ -3,7 +3,7 @@ package sagittarius; import com.badlogic.gdx.Game; import sagittarius.view.BaseScreen; -import sagittarius.view.GameScreen; +import sagittarius.view.StartScreen; public class SagittariusGame extends Game { @@ -26,6 +26,6 @@ public class SagittariusGame extends Game { @Override public void create() { - this.setScreen(new GameScreen()); + this.setScreen(new StartScreen()); } } \ No newline at end of file diff --git a/core/src/sagittarius/model/Arrow.java b/core/src/sagittarius/model/Arrow.java index 14a2b2f..49da985 100644 --- a/core/src/sagittarius/model/Arrow.java +++ b/core/src/sagittarius/model/Arrow.java @@ -2,8 +2,6 @@ package sagittarius.model; import java.util.ArrayList; -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.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.MathUtils; @@ -13,7 +11,7 @@ import com.badlogic.gdx.scenes.scene2d.Actor; import sagittarius.SagittariusGame; import sagittarius.view.GameScreen; -public class Arrow extends Actor { +public class Arrow extends BaseActor { // ---------- ATTRIBUTEs ---------- @@ -23,8 +21,6 @@ public class Arrow extends Actor { private float TTL = 20; - private BitmapFont fontDebug = new BitmapFont(); - // ---------- CONSTRUCTORs ---------- /** @@ -60,17 +56,6 @@ public class Arrow extends Actor { } - @Override - public void draw(Batch batch, float parentAlpha) { - super.draw(batch, parentAlpha); - fontDebug.draw(batch, "TTL=" + (int)TTL, getX(), getY() + fontDebug.getCapHeight()*5); - fontDebug.draw(batch, "pos=" + (int)getX() + "," + (int)getY(), getX(), getY() + fontDebug.getCapHeight()*4); - fontDebug.draw(batch, "speed=" + (int)velocity.x + "," + (int)velocity.y, getX(), getY() + fontDebug.getCapHeight()*3); - fontDebug.draw(batch, "accel=" + (int)acceleration.x + "," + (int)acceleration.y, getX(), getY() + fontDebug.getCapHeight()*2); - fontDebug.draw(batch, "force=" + (int)force.x + "," + (int)force.y, getX(), getY() + fontDebug.getCapHeight()*1); - fontDebug.draw(batch, "angle=" + (int)getRotation(), getX(), getY()); - } - @Override public void drawDebug(ShapeRenderer shapes) { super.drawDebug(shapes); @@ -170,4 +155,16 @@ public class Arrow extends Actor { return new Vector2( getX(), getY() ); } + @Override + protected String getInfo() { + return + "TTL=" + (int)TTL + "\n" + + "pos=" + (int)getX() + "," + (int)getY() + "\n" + + "speed=" + (int)velocity.x + "," + (int)velocity.y + "\n" + + "accel=" + (int)acceleration.x + "," + (int)acceleration.y +"\n" + + "force=" + (int)force.x + "," + (int)force.y + "\n" + + "angle=" + (int)getRotation() + ; + } + } diff --git a/core/src/sagittarius/model/BaseActor.java b/core/src/sagittarius/model/BaseActor.java new file mode 100644 index 0000000..bbca534 --- /dev/null +++ b/core/src/sagittarius/model/BaseActor.java @@ -0,0 +1,21 @@ +package sagittarius.model; + +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.scenes.scene2d.Actor; + +public abstract class BaseActor extends Actor { + + static private BitmapFont fontDebug = new BitmapFont(); + + protected abstract String getInfo(); + + protected float angle; + + @Override + public void draw(Batch batch, float parentAlpha) { + super.draw(batch, parentAlpha); + fontDebug.draw(batch, getInfo(), getX(), getY()); + } + +} diff --git a/core/src/sagittarius/model/Bow.java b/core/src/sagittarius/model/Bow.java index c540d81..9eac5de 100644 --- a/core/src/sagittarius/model/Bow.java +++ b/core/src/sagittarius/model/Bow.java @@ -43,7 +43,6 @@ class Bow extends Actor { } else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) { computeArrow(); } else if (pressed) { - // Sagittarius.arrowList.add(getArrow()); GameScreen.arrows.addActor(getArrow()); pressed = false; } diff --git a/core/src/sagittarius/model/Planet.java b/core/src/sagittarius/model/Planet.java index 275cb1f..7b5dae5 100644 --- a/core/src/sagittarius/model/Planet.java +++ b/core/src/sagittarius/model/Planet.java @@ -1,18 +1,13 @@ package sagittarius.model; 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; -import com.badlogic.gdx.scenes.scene2d.Actor; -import com.badlogic.gdx.scenes.scene2d.Group; -public class Planet extends Actor { +public class Planet extends BaseActor { // ---------- ATTRIBUTEs ---------- - private BitmapFont fontDebug = new BitmapFont(); private float radius; private float mass; @@ -33,14 +28,6 @@ public class Planet extends Actor { super.act(dt); } - @Override - public void draw(Batch batch, float parentAlpha) { - super.draw(batch, parentAlpha); - fontDebug.draw(batch, "radius=" + (int)radius, getX(), getY() + fontDebug.getCapHeight()*2); - fontDebug.draw(batch, "mass=" + (int)mass, getX(), getY() + fontDebug.getCapHeight()); - fontDebug.draw(batch, "pos=" + (int)getX() + "," + (int)getY(), getX(), getY()); - } - @Override public void drawDebug(ShapeRenderer shapes) { super.drawDebug(shapes); @@ -63,4 +50,13 @@ public class Planet extends Actor { return new Vector2( getX(), getY() ); } + @Override + protected String getInfo() { + return + "radius=" + (int)radius + "\n" + + "mass=" + (int)mass + "\n" + + "pos=" + (int)getX() + "," + (int)getY() + ; + } + } diff --git a/core/src/sagittarius/model/Player.java b/core/src/sagittarius/model/Player.java index a032183..ad73cf1 100644 --- a/core/src/sagittarius/model/Player.java +++ b/core/src/sagittarius/model/Player.java @@ -3,26 +3,22 @@ package sagittarius.model; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input.Keys; 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.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.MathUtils; -import com.badlogic.gdx.scenes.scene2d.Actor; -public class Player extends Actor { +public class Player extends BaseActor { // ---------- ATTRIBUTEs ---------- private Planet home; private Bow bow; - private BitmapFont fontDebug = new BitmapFont(); // ---------- CONSTRUCTORs ---------- public Player(Planet home, Color color) { super(); - this.setSize(100, 50); // TODO: fix this ? (width & height inverted) + this.setSize(100, 50); // TODO: fix this ? (width & height inverted) => use BaseActor.angle this.setRotation(MathUtils.random(360)); this.setColor(color); this.setOrigin(0, getHeight()/2); @@ -32,12 +28,6 @@ public class Player extends Actor { // ---------- METHODs ---------- - @Override - public void draw(Batch batch, float parentAlpha) { - super.draw(batch, parentAlpha); - fontDebug.draw(batch, "pos=" + (int)getX() + "," + (int)getY() , getX(), getY()); - } - @Override public void drawDebug(ShapeRenderer shapes) { super.drawDebug(shapes); @@ -82,4 +72,11 @@ public class Player extends Actor { return this.home; } + @Override + protected String getInfo() { + return + "pos=" + (int)getX() + "," + (int)getY() + ; + } + } diff --git a/core/src/sagittarius/view/GameScreen.java b/core/src/sagittarius/view/GameScreen.java index f1c712d..90c313c 100644 --- a/core/src/sagittarius/view/GameScreen.java +++ b/core/src/sagittarius/view/GameScreen.java @@ -44,6 +44,9 @@ public class GameScreen extends BaseScreen { Player player1 = new Player(planet1, Color.WHITE); mainStage.addActor(player1); + Player player2 = new Player(planet2, Color.WHITE); + mainStage.addActor(player2); + // arrows arrows = new Group(); mainStage.addActor(arrows);