feat: created Entities

This commit is contained in:
Laureηt 2021-04-22 16:11:50 +02:00
parent a4ed374a4c
commit 463e513d71
4 changed files with 168 additions and 6 deletions

View file

@ -177,7 +177,7 @@ public class Arrow extends BaseActor {
// TODO : optimize, lots of useless stuff + change name // TODO : optimize, lots of useless stuff + change name
// or ignore and use adapters // 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; int index = 0;
for (final Float value: path) { for (final Float value: path) {
arr[index++] = value; arr[index++] = value;

View file

@ -1,18 +1,26 @@
package sagittarius.model; package sagittarius.model;
import java.awt.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Vector; 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.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
public abstract class Entity { public abstract class Entity extends Actor {
// ---------- ATTRIBUTEs ---------- // ---------- ATTRIBUTEs ----------
protected static BitmapFont fontDebug = new BitmapFont();
protected Polygon hitbox = new Polygon(getHitbox());
protected float angle; protected float angle;
protected float mass; protected float mass;
protected Color color; protected Color color;
protected Vector2 position; protected Vector2 position;
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
@ -105,4 +113,30 @@ public abstract class Entity {
*/ */
protected abstract String getInfo(); 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());
}
} }

View file

@ -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;
}
}

View file

@ -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 };
}
}