refacto: more Entities work

This commit is contained in:
Laureηt 2021-04-22 17:29:49 +02:00
parent 463e513d71
commit 6cd727c09d
7 changed files with 58 additions and 107 deletions

View file

@ -188,14 +188,23 @@ public class Arrow extends BaseActor {
@Override @Override
protected String getInfo() { protected String getInfo() {
return
"TTL=" + (int)TTL + "\n" if (landed) {
+ "pos=" + (int)getX() + "," + (int)getY() + "\n" return
+ "speed=" + (int)velocity.x + "," + (int)velocity.y + "\n" "TTL=" + (int)TTL + "\n"
+ "accel=" + (int)acceleration.x + "," + (int)acceleration.y +"\n" + "pos=" + (int)getX() + "," + (int)getY() + "\n"
+ "force=" + (int)force.x + "," + (int)force.y + "\n" + "angle=" + (int)getRotation()
+ "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()
;
}
} }
} }

View file

@ -1,6 +1,5 @@
package sagittarius.model; package sagittarius.model;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
@ -39,13 +38,7 @@ public abstract class BaseActor extends Actor {
protected void drawDebugBounds(ShapeRenderer shapes) { protected void drawDebugBounds(ShapeRenderer shapes) {
if (!getDebug()) return; if (!getDebug()) return;
shapes.set(ShapeType.Line); shapes.set(ShapeType.Line);
if (getStage() != null) shapes.setColor(getStage().getDebugColor()); if (getStage() != null) shapes.setColor(getColor());
// shapes.rect(getX() - getOriginX(), getY() - getOriginY(),
// getOriginX(), getOriginY(),
// getWidth(), getHeight(),
// getScaleX(), getScaleY(),
// getRotation());
shapes.setColor(Color.RED);
shapes.polygon(test()); shapes.polygon(test());
} }

View file

@ -5,7 +5,6 @@ import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; 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.Polygon;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
@ -16,12 +15,19 @@ public abstract class Entity extends Actor {
protected static BitmapFont fontDebug = new BitmapFont(); 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 angle;
protected float mass; protected float mass;
protected Color color; protected Polygon hitbox;
protected Vector2 position;
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
@ -34,8 +40,9 @@ public abstract class Entity extends Actor {
public Entity(float angle, float mass, Color color, Vector2 position) { public Entity(float angle, float mass, Color color, Vector2 position) {
this.angle = angle; this.angle = angle;
this.mass = mass; this.mass = mass;
this.color = color; this.setColor(color);
this.position = position.cpy(); this.setPosition(position);
this.hitbox = new Polygon(getHitbox());
} }
// ---------- GETTERs ---------- // ---------- GETTERs ----------
@ -54,18 +61,11 @@ public abstract class Entity extends Actor {
return this.mass; return this.mass;
} }
/**
* @return color of the {@link #Entity}.
*/
public Color getColor() {
return this.color;
}
/** /**
* @return position of the {@link #Entity}. * @return position of the {@link #Entity}.
*/ */
public Vector2 getPosition() { public Vector2 getPosition() {
return this.position.cpy(); return new Vector2(getX(), getY());
} }
// ---------- SETTERs ---------- // ---------- SETTERs ----------
@ -88,22 +88,14 @@ public abstract class Entity extends Actor {
this.mass = mass; 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}. * Sets the Position of the {@link #Entity}.
* *
* @param position new position. * @param position new position.
*/ */
public void setPosition(Vector2 position) { public void setPosition(Vector2 position) {
this.position = position.cpy(); this.setX(position.x);
this.setY(position.y);
} }
// ---------- METHODs ---------- // ---------- METHODs ----------
@ -128,15 +120,16 @@ public abstract class Entity extends Actor {
@Override @Override
public void draw(Batch batch, float parentAlpha) { public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha); super.draw(batch, parentAlpha);
if (getDebug()) fontDebug.draw(batch, getInfo(), getX(), getY()); //if (getDebug())
fontDebug.draw(batch, getInfo(), getX(), getY());
} }
@Override @Override
protected void drawDebugBounds(ShapeRenderer shapes) { protected void drawDebugBounds(ShapeRenderer shapes) {
if (!getDebug()) return; //if (!getDebug()) return;
shapes.set(ShapeType.Line); shapes.set(ShapeType.Line);
if (getStage() != null) shapes.setColor(getStage().getDebugColor()); if (getStage() != null) shapes.setColor(this.getColor());
shapes.polygon(getHitbox()); shapes.polygon(hitbox.getVertices());
} }
} }

View file

@ -1,7 +1,6 @@
package sagittarius.model; package sagittarius.model;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
@ -46,22 +45,14 @@ public abstract class EntityCircle extends Entity {
// ---------- METHODs ---------- // ---------- METHODs ----------
@Override @Override
protected float[] getHitbox() { protected float[] getHitbox() { // peut être optimisé
int segments = Math.max(1, (int)(6 * (float)Math.cbrt(this.radius))); int segments = 128;
float[] vertices = new float[2*segments]; float[] vertices = new float[2*segments];
float angle = 2 * MathUtils.PI / segments; float theta = 360.0f / segments;
float cos = MathUtils.cos(angle);
float sin = MathUtils.sin(angle);
float cx = radius, cy = 0;
for (int i = 0; i < segments; i++) { for (int i = 0; i < segments; i++) {
vertices[i] = this.position.x + cx; vertices[2*i] = getX() + MathUtils.cosDeg(theta * (i+1))*radius;
vertices[i+1] = this.position.y + cy; vertices[2*i+1] = getY() + MathUtils.sinDeg(theta * (i+1))*radius;
float temp = cx;
cx = cos * cx - sin * cy;
cy = sin * temp + cos * cy;
} }
return vertices; return vertices;

View file

@ -1,7 +1,6 @@
package sagittarius.model; package sagittarius.model;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
@ -37,8 +36,8 @@ public abstract class EntityQuad extends Entity {
fy2 *= getScaleY(); fy2 *= getScaleY();
} }
float worldOriginX = this.position.x; float worldOriginX = getX();
float worldOriginY = this.position.y; float worldOriginY = getY();
float x1 = cos * fx - sin * fy + worldOriginX; float x1 = cos * fx - sin * fy + worldOriginX;
float y1 = sin * fx + cos * fy + worldOriginY; float y1 = sin * fx + cos * fy + worldOriginY;

View file

@ -17,17 +17,7 @@ public class Moon extends Planet {
this.altitude = altitude; this.altitude = altitude;
} }
// ---------- METHODs ---------- // ---------- GETTERs ----------
@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()));
}
/** /**
* @return the Planet which the Moon orbits. * @return the Planet which the Moon orbits.
@ -36,4 +26,13 @@ public class Moon extends Planet {
return this.sun; 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);
}
} }

View file

@ -1,51 +1,18 @@
package sagittarius.model; package sagittarius.model;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
public class Planet extends BaseActor { public class Planet extends EntityCircle {
// ---------- ATTRIBUTEs ----------
private float radius;
private float mass;
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
public Planet(Vector2 position, float mass, float radius, Color color) { public Planet(Vector2 position, float mass, float radius, Color color) {
super(); super(0, mass, color, position, radius);
this.setPosition(position.x, position.y);
this.radius = radius;
this.mass = mass;
this.setColor(color);
} }
// ---------- METHODs ---------- // ---------- 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 @Override
protected String getInfo() { protected String getInfo() {
return return