From ea826d2ca7edc2d467ab86c2fa6886ae265d025b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Thu, 22 Apr 2021 17:53:00 +0200 Subject: [PATCH] feat: remove BaseActor, some cleaning necessary --- core/src/sagittarius/model/Arrow.java | 1 + core/src/sagittarius/model/BaseActor.java | 95 ----------------------- core/src/sagittarius/model/Entity.java | 9 +-- core/src/sagittarius/model/FPS.java | 11 ++- core/src/sagittarius/model/MouseInfo.java | 10 ++- core/src/sagittarius/model/Player.java | 1 + core/src/sagittarius/view/GameScreen.java | 3 + 7 files changed, 28 insertions(+), 102 deletions(-) delete mode 100644 core/src/sagittarius/model/BaseActor.java diff --git a/core/src/sagittarius/model/Arrow.java b/core/src/sagittarius/model/Arrow.java index daf9b9b..cd8a405 100644 --- a/core/src/sagittarius/model/Arrow.java +++ b/core/src/sagittarius/model/Arrow.java @@ -67,6 +67,7 @@ public class Arrow extends EntityQuad { @Override public void drawDebug(ShapeRenderer shapes) { super.drawDebug(shapes); + if (getStage() != null) shapes.setColor(getStage().getDebugColor()); for (Actor actor : GameScreen.attractors.getChildren()) { shapes.line(getX(), getY(), actor.getX(), actor.getY()); } diff --git a/core/src/sagittarius/model/BaseActor.java b/core/src/sagittarius/model/BaseActor.java deleted file mode 100644 index 1fb10ee..0000000 --- a/core/src/sagittarius/model/BaseActor.java +++ /dev/null @@ -1,95 +0,0 @@ -package sagittarius.model; - -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.Intersector; -import com.badlogic.gdx.math.MathUtils; -import com.badlogic.gdx.math.Polygon; -import com.badlogic.gdx.math.Vector2; -import com.badlogic.gdx.scenes.scene2d.Actor; - -public abstract class BaseActor extends Actor { - -// ---------- ATTRIBUTEs ---------- - - static private BitmapFont fontDebug = new BitmapFont(); - - protected Polygon hitbox = new Polygon(test()); - - protected float angle; // encapsulate ? - -// ---------- METHODs ---------- - - @Override - public void act(float dt) { - super.act(dt); - hitbox.setVertices(test()); - } - - @Override - public void draw(Batch batch, float parentAlpha) { - super.draw(batch, parentAlpha); - if (getDebug()) fontDebug.draw(batch, getInfo(), getX(), getY()); - } - - @Override - protected void drawDebugBounds(ShapeRenderer shapes) { - if (!getDebug()) return; - shapes.set(ShapeType.Line); - if (getStage() != null) shapes.setColor(getColor()); - shapes.polygon(test()); - } - - /** - * {@link ShapeRenderer#rect} - * @return vertices of the hitbox polygon - */ - protected float[] test() { // convert position + rotation and other shit to polygon - float cos = MathUtils.cosDeg(getRotation()); - float sin = MathUtils.sinDeg(getRotation()); - float fx = -getOriginX(); - float fy = -getOriginY(); - float fx2 = getWidth() - getOriginX(); - float fy2 = getHeight() - getOriginY(); - - if (getScaleX() != 1 || getScaleX() != 1) { - fx *= getScaleX(); - fy *= getScaleY(); - fx2 *= getScaleX(); - fy2 *= getScaleY(); - } - - float worldOriginX = getX(); - float worldOriginY = getY(); - - float x1 = cos * fx - sin * fy + worldOriginX; - float y1 = sin * fx + cos * fy + worldOriginY; - - float x2 = cos * fx2 - sin * fy + worldOriginX; - float y2 = sin * fx2 + cos * fy + worldOriginY; - - float x3 = cos * fx2 - sin * fy2 + worldOriginX; - float y3 = sin * fx2 + cos * fy2 + worldOriginY; - - float x4 = x1 + (x3 - x2); - float y4 = y3 - (y2 - y1); - - return new float[] { x1, y1, x2, y2, x3, y3, x4, y4 }; - } - - /** - * @return debug String displayed in {@link #draw()} - */ - protected abstract String getInfo(); - - protected boolean overlaps(BaseActor other) { - return Intersector.overlapConvexPolygons(hitbox, other.hitbox); - } - - public Vector2 getPosition() { - return new Vector2( getX(), getY() ); - } - -} diff --git a/core/src/sagittarius/model/Entity.java b/core/src/sagittarius/model/Entity.java index 17163ce..05c856c 100644 --- a/core/src/sagittarius/model/Entity.java +++ b/core/src/sagittarius/model/Entity.java @@ -2,19 +2,18 @@ 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.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.Polygon; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.scenes.scene2d.Actor; +import sagittarius.view.GameScreen; + public abstract class Entity extends Actor { // ---------- ATTRIBUTEs ---------- - protected static BitmapFont fontDebug = new BitmapFont(); - // private @Null String name; // private Touchable touchable = Touchable.enabled; // private boolean visible = true, debug; @@ -120,14 +119,14 @@ public abstract class Entity extends Actor { @Override public void draw(Batch batch, float parentAlpha) { super.draw(batch, parentAlpha); - if (getDebug()) fontDebug.draw(batch, getInfo(), getX(), getY()); + if (getDebug()) GameScreen.fontDebug.draw(batch, getInfo(), getX(), getY()); } @Override protected void drawDebugBounds(ShapeRenderer shapes) { if (!getDebug()) return; shapes.set(ShapeType.Line); - if (getStage() != null) shapes.setColor(this.getColor()); + shapes.setColor(this.getColor()); shapes.polygon(hitbox.getVertices()); } diff --git a/core/src/sagittarius/model/FPS.java b/core/src/sagittarius/model/FPS.java index 496bb3e..69f0f07 100644 --- a/core/src/sagittarius/model/FPS.java +++ b/core/src/sagittarius/model/FPS.java @@ -1,9 +1,13 @@ package sagittarius.model; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Stage; -public class FPS extends BaseActor { +import sagittarius.view.GameScreen; + +public class FPS extends Actor { // ---------- ATTRIBUTEs ---------- @@ -23,6 +27,11 @@ public class FPS extends BaseActor { } @Override + public void draw(Batch batch, float parentAlpha) { + super.draw(batch, parentAlpha); + if (getDebug()) GameScreen.fontDebug.draw(batch, getInfo(), getX(), getY()); + } + protected String getInfo() { return frameRate + " fps"; } diff --git a/core/src/sagittarius/model/MouseInfo.java b/core/src/sagittarius/model/MouseInfo.java index 025d9ba..fa2fafc 100644 --- a/core/src/sagittarius/model/MouseInfo.java +++ b/core/src/sagittarius/model/MouseInfo.java @@ -1,8 +1,11 @@ package sagittarius.model; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.scenes.scene2d.Actor; + import sagittarius.view.GameScreen; -public class MouseInfo extends BaseActor { +public class MouseInfo extends Actor { // ---------- METHODs ---------- @@ -14,6 +17,11 @@ public class MouseInfo extends BaseActor { } @Override + public void draw(Batch batch, float parentAlpha) { + super.draw(batch, parentAlpha); + if (getDebug()) GameScreen.fontDebug.draw(batch, getInfo(), getX(), getY()); + } + protected String getInfo() { return "screen=" + (int)GameScreen.screenCursor.x + "," + (int)GameScreen.screenCursor.y + "\n" diff --git a/core/src/sagittarius/model/Player.java b/core/src/sagittarius/model/Player.java index fc4ab48..3abe8a8 100644 --- a/core/src/sagittarius/model/Player.java +++ b/core/src/sagittarius/model/Player.java @@ -30,6 +30,7 @@ public class Player extends EntityQuad { @Override public void drawDebug(ShapeRenderer shapes) { super.drawDebug(shapes); + if (getStage() != null) shapes.setColor(getStage().getDebugColor()); shapes.line(home.getX(), home.getY(), getX(), getY()); } diff --git a/core/src/sagittarius/view/GameScreen.java b/core/src/sagittarius/view/GameScreen.java index 105fd82..13c5cb0 100644 --- a/core/src/sagittarius/view/GameScreen.java +++ b/core/src/sagittarius/view/GameScreen.java @@ -2,6 +2,7 @@ package sagittarius.view; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.scenes.scene2d.Group; @@ -13,6 +14,8 @@ public class GameScreen extends BaseScreen { // ---------- ATTRIBUTEs ---------- + public static BitmapFont fontDebug = new BitmapFont(); + // Cursors public static Vector2 screenCursor; private static Vector3 unprojectedCursor;