From 6cd727c09d924549a88fbfb2ede255ced656f401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Thu, 22 Apr 2021 17:29:49 +0200 Subject: [PATCH] refacto: more Entities work --- core/src/sagittarius/model/Arrow.java | 25 ++++++---- core/src/sagittarius/model/BaseActor.java | 9 +--- core/src/sagittarius/model/Entity.java | 49 +++++++++----------- core/src/sagittarius/model/EntityCircle.java | 19 ++------ core/src/sagittarius/model/EntityQuad.java | 5 +- core/src/sagittarius/model/Moon.java | 21 ++++----- core/src/sagittarius/model/Planet.java | 37 +-------------- 7 files changed, 58 insertions(+), 107 deletions(-) diff --git a/core/src/sagittarius/model/Arrow.java b/core/src/sagittarius/model/Arrow.java index 68fa99b..0172b1f 100644 --- a/core/src/sagittarius/model/Arrow.java +++ b/core/src/sagittarius/model/Arrow.java @@ -188,14 +188,23 @@ public class Arrow extends BaseActor { @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() - ; + + if (landed) { + return + "TTL=" + (int)TTL + "\n" + + "pos=" + (int)getX() + "," + (int)getY() + "\n" + + "angle=" + (int)getRotation() + ; + } else { + 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 index 4707fc0..5b3db67 100644 --- a/core/src/sagittarius/model/BaseActor.java +++ b/core/src/sagittarius/model/BaseActor.java @@ -1,6 +1,5 @@ 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; @@ -39,13 +38,7 @@ public abstract class BaseActor extends Actor { protected void drawDebugBounds(ShapeRenderer shapes) { if (!getDebug()) return; shapes.set(ShapeType.Line); - if (getStage() != null) shapes.setColor(getStage().getDebugColor()); - // shapes.rect(getX() - getOriginX(), getY() - getOriginY(), - // getOriginX(), getOriginY(), - // getWidth(), getHeight(), - // getScaleX(), getScaleY(), - // getRotation()); - shapes.setColor(Color.RED); + if (getStage() != null) shapes.setColor(getColor()); shapes.polygon(test()); } diff --git a/core/src/sagittarius/model/Entity.java b/core/src/sagittarius/model/Entity.java index c738b97..54be816 100644 --- a/core/src/sagittarius/model/Entity.java +++ b/core/src/sagittarius/model/Entity.java @@ -5,7 +5,6 @@ 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.math.Polygon; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.scenes.scene2d.Actor; @@ -16,12 +15,19 @@ public abstract class Entity extends Actor { protected static BitmapFont fontDebug = new BitmapFont(); - protected Polygon hitbox = new Polygon(getHitbox()); + // private @Null String name; + // private Touchable touchable = Touchable.enabled; + // private boolean visible = true, debug; + // float x, y; + // float width, height; + // float originX, originY; + // float scaleX = 1, scaleY = 1; + // float rotation; + // final Color color = new Color(1, 1, 1, 1); protected float angle; protected float mass; - protected Color color; - protected Vector2 position; + protected Polygon hitbox; // ---------- CONSTRUCTORs ---------- @@ -34,8 +40,9 @@ public abstract class Entity extends Actor { public Entity(float angle, float mass, Color color, Vector2 position) { this.angle = angle; this.mass = mass; - this.color = color; - this.position = position.cpy(); + this.setColor(color); + this.setPosition(position); + this.hitbox = new Polygon(getHitbox()); } // ---------- GETTERs ---------- @@ -54,18 +61,11 @@ public abstract class Entity extends Actor { return this.mass; } - /** - * @return color of the {@link #Entity}. - */ - public Color getColor() { - return this.color; - } - /** * @return position of the {@link #Entity}. */ public Vector2 getPosition() { - return this.position.cpy(); + return new Vector2(getX(), getY()); } // ---------- SETTERs ---------- @@ -88,22 +88,14 @@ public abstract class Entity extends Actor { this.mass = mass; } - /** - * Sets the color of the {@link #Entity}. - * - * @param color new color. - */ - public void setColor(Color color) { - this.color = color; - } - /** * Sets the Position of the {@link #Entity}. * * @param position new position. */ public void setPosition(Vector2 position) { - this.position = position.cpy(); + this.setX(position.x); + this.setY(position.y); } // ---------- METHODs ---------- @@ -128,15 +120,16 @@ 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()) + fontDebug.draw(batch, getInfo(), getX(), getY()); } @Override protected void drawDebugBounds(ShapeRenderer shapes) { - if (!getDebug()) return; + //if (!getDebug()) return; shapes.set(ShapeType.Line); - if (getStage() != null) shapes.setColor(getStage().getDebugColor()); - shapes.polygon(getHitbox()); + if (getStage() != null) shapes.setColor(this.getColor()); + shapes.polygon(hitbox.getVertices()); } } diff --git a/core/src/sagittarius/model/EntityCircle.java b/core/src/sagittarius/model/EntityCircle.java index a716dc0..5f64f9f 100644 --- a/core/src/sagittarius/model/EntityCircle.java +++ b/core/src/sagittarius/model/EntityCircle.java @@ -1,7 +1,6 @@ package sagittarius.model; import com.badlogic.gdx.graphics.Color; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; @@ -46,22 +45,14 @@ public abstract class EntityCircle extends Entity { // ---------- METHODs ---------- @Override - protected float[] getHitbox() { - int segments = Math.max(1, (int)(6 * (float)Math.cbrt(this.radius))); + protected float[] getHitbox() { // peut ĂȘtre optimisĂ© + int segments = 128; float[] vertices = new float[2*segments]; - float angle = 2 * MathUtils.PI / segments; - float cos = MathUtils.cos(angle); - float sin = MathUtils.sin(angle); - float cx = radius, cy = 0; - + float theta = 360.0f / segments; for (int i = 0; i < segments; i++) { - vertices[i] = this.position.x + cx; - vertices[i+1] = this.position.y + cy; - - float temp = cx; - cx = cos * cx - sin * cy; - cy = sin * temp + cos * cy; + vertices[2*i] = getX() + MathUtils.cosDeg(theta * (i+1))*radius; + vertices[2*i+1] = getY() + MathUtils.sinDeg(theta * (i+1))*radius; } return vertices; diff --git a/core/src/sagittarius/model/EntityQuad.java b/core/src/sagittarius/model/EntityQuad.java index 2f03060..811a849 100644 --- a/core/src/sagittarius/model/EntityQuad.java +++ b/core/src/sagittarius/model/EntityQuad.java @@ -1,7 +1,6 @@ package sagittarius.model; import com.badlogic.gdx.graphics.Color; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; @@ -37,8 +36,8 @@ public abstract class EntityQuad extends Entity { fy2 *= getScaleY(); } - float worldOriginX = this.position.x; - float worldOriginY = this.position.y; + float worldOriginX = getX(); + float worldOriginY = getY(); float x1 = cos * fx - sin * fy + worldOriginX; float y1 = sin * fx + cos * fy + worldOriginY; diff --git a/core/src/sagittarius/model/Moon.java b/core/src/sagittarius/model/Moon.java index 5952118..af6e0b8 100644 --- a/core/src/sagittarius/model/Moon.java +++ b/core/src/sagittarius/model/Moon.java @@ -17,17 +17,7 @@ public class Moon extends Planet { this.altitude = altitude; } -// ---------- METHODs ---------- - - @Override - public void act(float dt) { - - this.rotateBy(10.0f / this.altitude); - - this.setX(sun.getX() + this.altitude*MathUtils.cosDeg(this.getRotation())); - this.setY(sun.getY() + this.altitude*MathUtils.sinDeg(this.getRotation())); - - } +// ---------- GETTERs ---------- /** * @return the Planet which the Moon orbits. @@ -36,4 +26,13 @@ public class Moon extends Planet { return this.sun; } +// ---------- METHODs ---------- + + @Override + public void act(float dt) { + this.rotateBy(10.0f / this.altitude); + this.setX(sun.getX() + this.altitude*MathUtils.cosDeg(this.getRotation())); + this.setY(sun.getY() + this.altitude*MathUtils.sinDeg(this.getRotation())); + super.act(dt); + } } \ No newline at end of file diff --git a/core/src/sagittarius/model/Planet.java b/core/src/sagittarius/model/Planet.java index 7e4cf32..b15b859 100644 --- a/core/src/sagittarius/model/Planet.java +++ b/core/src/sagittarius/model/Planet.java @@ -1,51 +1,18 @@ package sagittarius.model; import com.badlogic.gdx.graphics.Color; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.Vector2; -public class Planet extends BaseActor { - -// ---------- ATTRIBUTEs ---------- - - private float radius; - private float mass; +public class Planet extends EntityCircle { // ---------- CONSTRUCTORs ---------- public Planet(Vector2 position, float mass, float radius, Color color) { - super(); - this.setPosition(position.x, position.y); - this.radius = radius; - this.mass = mass; - this.setColor(color); + super(0, mass, color, position, radius); } // ---------- METHODs ---------- - @Override - public void act(float dt) { - super.act(dt); - } - - @Override - public void drawDebug(ShapeRenderer shapes) { - super.drawDebug(shapes); - shapes.setColor(this.getColor()); - shapes.circle(this.getX(), this.getY(), this.radius); - } - - /** - * @return the radius of the Planet. - */ - public float getRadius() { - return this.radius; - } - - public float getMass() { - return mass; - } - @Override protected String getInfo() { return