ftea: added Moons

This commit is contained in:
Laureηt 2021-04-06 22:59:10 +02:00
parent a2b1108ad5
commit 60fd51a774
7 changed files with 90 additions and 14 deletions

View file

@ -18,6 +18,10 @@ class Arrow extends Entity {
float TTL = 20; float TTL = 20;
private final float length = 100; private final float length = 100;
private boolean active = true;
private Planet crash;
private Vector2 offset = new Vector2();
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
Arrow(float angle, float power, Player shooter) { Arrow(float angle, float power, Player shooter) {
@ -36,24 +40,40 @@ class Arrow extends Entity {
Vector2 attraction = diff.scl( SagittariusGame.G * attractor.mass / diff.len2() ); Vector2 attraction = diff.scl( SagittariusGame.G * attractor.mass / diff.len2() );
force.add(attraction); force.add(attraction);
} }
for (Moon attractor : SagittariusGame.moonList) {
Vector2 diff = attractor.position.cpy().sub(this.position);
Vector2 attraction = diff.scl( SagittariusGame.G * attractor.mass / diff.len2() );
force.add(attraction);
}
return force; return force;
} }
private boolean hasLanded() { private void verifyActivity() {
for (Planet planet : SagittariusGame.planetList) { for (Planet planet : SagittariusGame.planetList) {
if (this.distanceTo(planet) < planet.getRadius()) { if (this.distanceTo(planet) < planet.getRadius()) {
return true; this.active = false;
this.crash = planet;
this.offset = this.position.cpy().sub(planet.position);
}
}
for (Moon moon : SagittariusGame.moonList) {
if (this.distanceTo(moon) < moon.getRadius()) {
this.active = false;
this.crash = moon;
this.offset = this.position.cpy().sub(moon.position);
} }
} }
return false;
} }
void update(float deltaTime) { void update(float deltaTime) {
if (!this.hasLanded()) { if (this.active) {
verifyActivity();
integrationVerlet(deltaTime); integrationVerlet(deltaTime);
this.TTL -= deltaTime; this.TTL -= deltaTime;
this.angle = this.velocity.angleDeg(); this.angle = this.velocity.angleDeg();
} else {
this.position = crash.position.cpy().add(offset);
} }
} }
@ -61,10 +81,6 @@ class Arrow extends Entity {
// Verlet integration // Verlet integration
// https://gamedev.stackexchange.com/questions/15708/how-can-i-implement-gravity/41917#41917 // https://gamedev.stackexchange.com/questions/15708/how-can-i-implement-gravity/41917#41917
// TODO : vectorialiser
// System.out.println(deltaTime);
this.acceleration = this.force.cpy(); this.acceleration = this.force.cpy();
this.position.x += deltaTime * ( this.velocity.x + deltaTime * this.acceleration.x / 2 ); this.position.x += deltaTime * ( this.velocity.x + deltaTime * this.acceleration.x / 2 );
@ -84,7 +100,7 @@ class Arrow extends Entity {
void renderDebug(Batch batch, BitmapFont font) { void renderDebug(Batch batch, BitmapFont font) {
// TODO : dirty, do it in an other way // TODO : dirty, do it in an other way
if (!hasLanded()) { if (active) {
font.draw(batch, "TTL = " + this.TTL, this.position.x, this.position.y + font.getCapHeight()*5); font.draw(batch, "TTL = " + this.TTL, this.position.x, this.position.y + font.getCapHeight()*5);
font.draw(batch, "pos = " + this.position, this.position.x, this.position.y + font.getCapHeight()*4); font.draw(batch, "pos = " + this.position, this.position.x, this.position.y + font.getCapHeight()*4);
font.draw(batch, "speed = " + this.velocity, this.position.x, this.position.y + font.getCapHeight()*3); font.draw(batch, "speed = " + this.velocity, this.position.x, this.position.y + font.getCapHeight()*3);
@ -100,7 +116,8 @@ class Arrow extends Entity {
ArrayList<Float> path = new ArrayList<Float>(); ArrayList<Float> path = new ArrayList<Float>();
Arrow dummyArrow = new Arrow(angle, power, shooter); Arrow dummyArrow = new Arrow(angle, power, shooter);
for (int i = 0; i < iterations; i++) { for (int i = 0; i < iterations; i++) {
if (dummyArrow.hasLanded()) { break; } dummyArrow.verifyActivity();
if (!dummyArrow.active) { break; }
dummyArrow.integrationVerlet(timeStep); dummyArrow.integrationVerlet(timeStep);
path.add(dummyArrow.position.x); path.add(dummyArrow.position.x);
path.add(dummyArrow.position.y); path.add(dummyArrow.position.y);

View file

@ -53,7 +53,7 @@ class Bow {
power = MathUtils.clamp(aim.len(), 0, 1000); power = MathUtils.clamp(aim.len(), 0, 1000);
} }
public void render(ShapeRenderer shapeRenderer) { void render(ShapeRenderer shapeRenderer) {
if (pressed) { if (pressed) {
shapeRenderer.line(this.anchor, SagittariusGame.worldCursor); shapeRenderer.line(this.anchor, SagittariusGame.worldCursor);
float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f); float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f);

View file

@ -15,6 +15,8 @@ abstract class Entity {
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
// TODO : reorganize all constructors
protected Entity(Vector2 position, float mass) { protected Entity(Vector2 position, float mass) {
this.position = position.cpy(); this.position = position.cpy();
this.mass = mass; this.mass = mass;

View file

@ -27,7 +27,7 @@ class GameScreen extends ScreenAdapter {
// camera stuff // camera stuff
private Viewport viewport; private Viewport viewport;
public static Camera camera; protected static Camera camera;
// TODO: categorize better ? // TODO: categorize better ?
private HUD hud = new HUD(); private HUD hud = new HUD();
@ -61,6 +61,11 @@ class GameScreen extends ScreenAdapter {
for (Planet planet : SagittariusGame.planetList) { for (Planet planet : SagittariusGame.planetList) {
planet.renderDebug(batch, font); planet.renderDebug(batch, font);
} }
// moons
for (Moon moon : SagittariusGame.moonList) {
moon.renderDebug(batch, font);
}
// players // players
for (Player player : SagittariusGame.playerList) { for (Player player : SagittariusGame.playerList) {
@ -93,6 +98,11 @@ class GameScreen extends ScreenAdapter {
arrow.render(shapeRenderer); arrow.render(shapeRenderer);
} }
// moons
for (Moon moon : SagittariusGame.moonList) {
moon.render(shapeRenderer);
}
// float[] vertices = {100, 100, 200, 200, 1000, 500}; // float[] vertices = {100, 100, 200, 200, 1000, 500};
// shapeRenderer.polyline(vertices); // shapeRenderer.polyline(vertices);

View file

@ -17,11 +17,11 @@ class HUD implements Disposable {
// ---------- METHODs ---------- // ---------- METHODs ----------
public void update() { void update() {
frameRate = Gdx.graphics.getFramesPerSecond(); frameRate = Gdx.graphics.getFramesPerSecond();
} }
public void render() { void render() {
batch.begin(); batch.begin();
// framerate // framerate

View file

@ -0,0 +1,38 @@
package sagittarius;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.MathUtils;
class Moon extends Planet {
private Planet planet;
private float altitude;
// ---------- CONSTRUCTORs ----------
Moon(Planet planet, float mass, float radius, float altitude) {
super(planet.position, mass, radius);
this.planet = planet;
this.altitude = altitude;
}
Moon(Planet planet, float mass, float radius, float altitude, Color color) {
super(planet.position, mass, radius, color);
}
// ---------- METHODs ----------
void update(double deltaTime) {
this.angle += 10.0f / altitude;
this.position.x = planet.position.x + altitude*MathUtils.cosDeg(angle);
this.position.y = planet.position.y + altitude*MathUtils.sinDeg(angle);
}
Planet getPlanet() {
return this.planet;
}
}

View file

@ -22,6 +22,7 @@ public class SagittariusGame extends Game {
// Entities // Entities
static ArrayList<Planet> planetList; static ArrayList<Planet> planetList;
static ArrayList<Moon> moonList;
static ArrayList<Player> playerList; static ArrayList<Player> playerList;
static ArrayList<Arrow> arrowList; static ArrayList<Arrow> arrowList;
@ -35,6 +36,9 @@ public class SagittariusGame extends Game {
planetList.add( new Planet(new Vector2(400, 400), 1000, 50) ); planetList.add( new Planet(new Vector2(400, 400), 1000, 50) );
planetList.add( new Planet(new Vector2(1000, 400), 1000, 100, Color.CYAN) ); planetList.add( new Planet(new Vector2(1000, 400), 1000, 100, Color.CYAN) );
moonList = new ArrayList<Moon>();
moonList.add( new Moon(planetList.get(1), 100, 20, 300) );
playerList = new ArrayList<Player>(); playerList = new ArrayList<Player>();
playerList.add( new Player(planetList.get(0)) ); playerList.add( new Player(planetList.get(0)) );
@ -53,6 +57,11 @@ public class SagittariusGame extends Game {
player.update(deltaTime); player.update(deltaTime);
} }
// moons
for (Moon moon : moonList) {
moon.update(deltaTime);
}
// arrows // arrows
for (Iterator<Arrow> it = arrowList.iterator(); it.hasNext(); ) { for (Iterator<Arrow> it = arrowList.iterator(); it.hasNext(); ) {
Arrow arrow = it.next(); Arrow arrow = it.next();