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,6 +188,14 @@ public class Arrow extends BaseActor {
@Override
protected String getInfo() {
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"
@ -197,5 +205,6 @@ public class Arrow extends BaseActor {
+ "angle=" + (int)getRotation()
;
}
}
}

View file

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

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.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());
}
}

View file

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

View file

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

View file

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

View file

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