diff --git a/core/src/sagittarius/Arrow.java b/core/src/sagittarius/Arrow.java index 1acd073..8b8a867 100644 --- a/core/src/sagittarius/Arrow.java +++ b/core/src/sagittarius/Arrow.java @@ -18,6 +18,10 @@ class Arrow extends Entity { float TTL = 20; private final float length = 100; + private boolean active = true; + private Planet crash; + private Vector2 offset = new Vector2(); + // ---------- CONSTRUCTORs ---------- 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() ); 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; } - private boolean hasLanded() { + private void verifyActivity() { for (Planet planet : SagittariusGame.planetList) { 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) { - if (!this.hasLanded()) { + if (this.active) { + verifyActivity(); integrationVerlet(deltaTime); this.TTL -= deltaTime; this.angle = this.velocity.angleDeg(); + } else { + this.position = crash.position.cpy().add(offset); } } @@ -61,10 +81,6 @@ class Arrow extends Entity { // Verlet integration // 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.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) { // 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, "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); @@ -100,7 +116,8 @@ class Arrow extends Entity { ArrayList path = new ArrayList(); Arrow dummyArrow = new Arrow(angle, power, shooter); for (int i = 0; i < iterations; i++) { - if (dummyArrow.hasLanded()) { break; } + dummyArrow.verifyActivity(); + if (!dummyArrow.active) { break; } dummyArrow.integrationVerlet(timeStep); path.add(dummyArrow.position.x); path.add(dummyArrow.position.y); diff --git a/core/src/sagittarius/Bow.java b/core/src/sagittarius/Bow.java index 2116db3..f704e92 100644 --- a/core/src/sagittarius/Bow.java +++ b/core/src/sagittarius/Bow.java @@ -53,7 +53,7 @@ class Bow { power = MathUtils.clamp(aim.len(), 0, 1000); } - public void render(ShapeRenderer shapeRenderer) { + void render(ShapeRenderer shapeRenderer) { if (pressed) { shapeRenderer.line(this.anchor, SagittariusGame.worldCursor); float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f); diff --git a/core/src/sagittarius/Entity.java b/core/src/sagittarius/Entity.java index b69dab6..09cac56 100644 --- a/core/src/sagittarius/Entity.java +++ b/core/src/sagittarius/Entity.java @@ -15,6 +15,8 @@ abstract class Entity { // ---------- CONSTRUCTORs ---------- + // TODO : reorganize all constructors + protected Entity(Vector2 position, float mass) { this.position = position.cpy(); this.mass = mass; diff --git a/core/src/sagittarius/GameScreen.java b/core/src/sagittarius/GameScreen.java index f0e870d..ce93f92 100644 --- a/core/src/sagittarius/GameScreen.java +++ b/core/src/sagittarius/GameScreen.java @@ -27,7 +27,7 @@ class GameScreen extends ScreenAdapter { // camera stuff private Viewport viewport; - public static Camera camera; + protected static Camera camera; // TODO: categorize better ? private HUD hud = new HUD(); @@ -61,6 +61,11 @@ class GameScreen extends ScreenAdapter { for (Planet planet : SagittariusGame.planetList) { planet.renderDebug(batch, font); } + + // moons + for (Moon moon : SagittariusGame.moonList) { + moon.renderDebug(batch, font); + } // players for (Player player : SagittariusGame.playerList) { @@ -93,6 +98,11 @@ class GameScreen extends ScreenAdapter { arrow.render(shapeRenderer); } + // moons + for (Moon moon : SagittariusGame.moonList) { + moon.render(shapeRenderer); + } + // float[] vertices = {100, 100, 200, 200, 1000, 500}; // shapeRenderer.polyline(vertices); diff --git a/core/src/sagittarius/HUD.java b/core/src/sagittarius/HUD.java index c33b9bc..a99ed1e 100644 --- a/core/src/sagittarius/HUD.java +++ b/core/src/sagittarius/HUD.java @@ -17,11 +17,11 @@ class HUD implements Disposable { // ---------- METHODs ---------- - public void update() { + void update() { frameRate = Gdx.graphics.getFramesPerSecond(); } - public void render() { + void render() { batch.begin(); // framerate diff --git a/core/src/sagittarius/Moon.java b/core/src/sagittarius/Moon.java new file mode 100644 index 0000000..d371d2e --- /dev/null +++ b/core/src/sagittarius/Moon.java @@ -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; + } + +} diff --git a/core/src/sagittarius/SagittariusGame.java b/core/src/sagittarius/SagittariusGame.java index 0aac0f5..7f7e557 100644 --- a/core/src/sagittarius/SagittariusGame.java +++ b/core/src/sagittarius/SagittariusGame.java @@ -22,6 +22,7 @@ public class SagittariusGame extends Game { // Entities static ArrayList planetList; + static ArrayList moonList; static ArrayList playerList; static ArrayList 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(1000, 400), 1000, 100, Color.CYAN) ); + moonList = new ArrayList(); + moonList.add( new Moon(planetList.get(1), 100, 20, 300) ); + playerList = new ArrayList(); playerList.add( new Player(planetList.get(0)) ); @@ -53,6 +57,11 @@ public class SagittariusGame extends Game { player.update(deltaTime); } + // moons + for (Moon moon : moonList) { + moon.update(deltaTime); + } + // arrows for (Iterator it = arrowList.iterator(); it.hasNext(); ) { Arrow arrow = it.next();