From 463e513d71e2f6d617f3cd10677501d2dddb848a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Thu, 22 Apr 2021 16:11:50 +0200 Subject: [PATCH] feat: created Entities --- core/src/sagittarius/model/Arrow.java | 2 +- core/src/sagittarius/model/Entity.java | 44 ++++++++++-- core/src/sagittarius/model/EntityCircle.java | 70 ++++++++++++++++++++ core/src/sagittarius/model/EntityQuad.java | 58 ++++++++++++++++ 4 files changed, 168 insertions(+), 6 deletions(-) create mode 100644 core/src/sagittarius/model/EntityCircle.java create mode 100644 core/src/sagittarius/model/EntityQuad.java diff --git a/core/src/sagittarius/model/Arrow.java b/core/src/sagittarius/model/Arrow.java index b6f5d22..68fa99b 100644 --- a/core/src/sagittarius/model/Arrow.java +++ b/core/src/sagittarius/model/Arrow.java @@ -177,7 +177,7 @@ public class Arrow extends BaseActor { // TODO : optimize, lots of useless stuff + change name // or ignore and use adapters - final float[] arr = new float[path.size()]; + final float[] arr = new float[path.size()]; // utiliser le count de la fonction, faire tab size iteration*2, et draw avec count int index = 0; for (final Float value: path) { arr[index++] = value; diff --git a/core/src/sagittarius/model/Entity.java b/core/src/sagittarius/model/Entity.java index 7d071d5..c738b97 100644 --- a/core/src/sagittarius/model/Entity.java +++ b/core/src/sagittarius/model/Entity.java @@ -1,18 +1,26 @@ package sagittarius.model; -import java.awt.Color; - -import com.badlogic.gdx.math.Vector; +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.math.Polygon; import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.scenes.scene2d.Actor; -public abstract class Entity { +public abstract class Entity extends Actor { // ---------- ATTRIBUTEs ---------- + protected static BitmapFont fontDebug = new BitmapFont(); + + protected Polygon hitbox = new Polygon(getHitbox()); + protected float angle; protected float mass; protected Color color; - protected Vector2 position; // ---------- CONSTRUCTORs ---------- @@ -105,4 +113,30 @@ public abstract class Entity { */ protected abstract String getInfo(); + /** + * inpired from {@link ShapeRenderer#rect} + * @return vertices of the hitbox polygon + */ + protected abstract float[] getHitbox(); + + @Override + public void act(float delta) { + super.act(delta); + hitbox.setVertices(getHitbox()); + } + + @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(getStage().getDebugColor()); + shapes.polygon(getHitbox()); + } + } diff --git a/core/src/sagittarius/model/EntityCircle.java b/core/src/sagittarius/model/EntityCircle.java new file mode 100644 index 0000000..a716dc0 --- /dev/null +++ b/core/src/sagittarius/model/EntityCircle.java @@ -0,0 +1,70 @@ +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; + +public abstract class EntityCircle extends Entity { + +// ---------- ATTRIBUTEs ---------- + + protected float radius; + +// ---------- CONSTRUCTORs ---------- + + /** Creates an {@link #Entity}. + * @param angle angle associated to the {@link #Entity}. + * @param mass mass of the {@link #Entity}. + * @param color color of the {@link #Entity}. + * @param position position of the {@link #Entity}. + */ + public EntityCircle(float angle, float mass, Color color, Vector2 position, float radius) { + super(angle, mass, color, position); + this.radius = radius; + } + +// ---------- GETTERs ---------- + + /** + * @return radius of the {@link #EntityCircle}. + */ + public float getRadius() { + return this.radius; + } + +// ---------- SETTERs ---------- + + /** + * Sets the radius of the {@link #EntityCircle}. + * + * @param radius new radius. + */ + public void setRadius(float radius) { + this.radius = radius; + } +// ---------- METHODs ---------- + + @Override + protected float[] getHitbox() { + int segments = Math.max(1, (int)(6 * (float)Math.cbrt(this.radius))); + 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; + + 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; + } + + return vertices; + } + +} diff --git a/core/src/sagittarius/model/EntityQuad.java b/core/src/sagittarius/model/EntityQuad.java new file mode 100644 index 0000000..2f03060 --- /dev/null +++ b/core/src/sagittarius/model/EntityQuad.java @@ -0,0 +1,58 @@ +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; + +public abstract class EntityQuad extends Entity { + +// ---------- CONSTRUCTORs ---------- + + /** Creates an {@link #Entity}. + * @param angle angle associated to the {@link #Entity}. + * @param mass mass of the {@link #Entity}. + * @param color color of the {@link #Entity}. + * @param position position of the {@link #Entity}. + */ + public EntityQuad(float angle, float mass, Color color, Vector2 position) { + super(angle, mass, color, position); + } + +// ---------- METHODs ---------- + + @Override + protected float[] getHitbox() { + 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 = this.position.x; + float worldOriginY = this.position.y; + + 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 }; + } + +}