diff --git a/core/src/sagittarius/model/Arrow.java b/core/src/sagittarius/model/Arrow.java index cd8a405..0d5fdb2 100644 --- a/core/src/sagittarius/model/Arrow.java +++ b/core/src/sagittarius/model/Arrow.java @@ -3,6 +3,7 @@ package sagittarius.model; import java.util.ArrayList; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Intersector; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.scenes.scene2d.Actor; @@ -51,15 +52,16 @@ public class Arrow extends EntityQuad { integrationVerlet(dt); this.TTL -= dt; this.setRotation(this.velocity.angleDeg()); + verifyLanding(); verifyHitting(); if (TTL <= 0) { GameScreen.arrows.removeActor(this); + GameScreen.nextPlayer(); } } else { - this.setX( crash.getX() + this.offset.x ); - this.setY( crash.getY() + this.offset.y ); + this.setPosition(crash.getPosition().cpy().add(offset)); } } @@ -81,18 +83,10 @@ public class Arrow extends EntityQuad { private Vector2 computeForce() { Vector2 force = new Vector2(); for (Actor actor : GameScreen.attractors.getChildren()) { - - float dx = actor.getX() - this.getX(); - float dy = actor.getY() - this.getY(); - - float len2 = dx*dx + dy*dy; - - float coeff = SagittariusGame.G * ((Planet) actor).getMass() * (float) Math.pow(len2, -3/2); - - float gravityX = coeff * dx; - float gravityY = coeff * dy; - - force.add(gravityX, gravityY); + Planet attractor = (Planet) actor; + Vector2 diff = attractor.getPosition().cpy().sub(this.getPosition()); + Vector2 attraction = diff.scl( SagittariusGame.G * attractor.mass / diff.len2() ); + force.add(attraction); } return force; } @@ -123,36 +117,53 @@ public class Arrow extends EntityQuad { */ private void verifyLanding() { for (Actor actor : GameScreen.attractors.getChildren()) { - Planet planet = (Planet) actor; - float dist = this.getPosition().sub( planet.getPosition() ).len(); - - if (dist <= planet.getRadius()) { // TODO: change with overlap in the future ? + if (planet.hitbox.contains(this.getPosition())) { landed = true; this.crash = planet; this.offset = this.getPosition().sub( planet.getPosition() ); + GameScreen.nextPlayer(); break; } } } + private boolean hasLanded() { + for (Actor actor : GameScreen.attractors.getChildren()) { + Planet planet = (Planet) actor; + if (planet.hitbox.contains(this.getPosition())) { + return true; + } + } + return false; + } + /** * TODO */ private void verifyHitting() { for (Actor actor : GameScreen.players.getChildren()) { - Player player = (Player) actor; - - if (TTL < 19 && player.hitbox.contains(getX(), getY())) { + if (TTL < 19 && Intersector.overlapConvexPolygons(player.hitbox, this.hitbox)) { landed = true; GameScreen.removePlayer(player); GameScreen.arrows.removeActor(this); + GameScreen.nextPlayer(); break; } } } + private boolean hasHit() { + for (Actor actor : GameScreen.players.getChildren()) { + Player player = (Player) actor; + if (TTL < 19 && Intersector.overlapConvexPolygons(player.hitbox, this.hitbox)) { + return true; + } + } + return false; + } + /** // TODO : pass directly an Arrow instead of 3 arguments * Computes the trajectory of an Arrow, * given its initial conditions. @@ -166,8 +177,7 @@ public class Arrow extends EntityQuad { Arrow dummyArrow = new Arrow(angle, power, shooter); for (int i = 0; i < iterations; i++) { dummyArrow.integrationVerlet(timeStep); - // dummyArrow.verifyLanding(); - // dummyArrow.verifyHittingPlayer(); // fix -> return booleans, take action after + if (dummyArrow.hasLanded() || dummyArrow.hasHit() ) { break; } path.add(dummyArrow.getX()); path.add(dummyArrow.getY()); } diff --git a/core/src/sagittarius/model/Bow.java b/core/src/sagittarius/model/Bow.java index 3964b37..49259ed 100644 --- a/core/src/sagittarius/model/Bow.java +++ b/core/src/sagittarius/model/Bow.java @@ -13,7 +13,6 @@ public class Bow extends Actor { // ---------- ATTRIBUTEs ---------- - private Player shooter; private boolean aimAssist = false; private boolean pressed = false; private float angle; @@ -35,18 +34,17 @@ public class Bow extends Actor { @Override public void act(float dt) { super.act(dt); - this.shooter = GameScreen.actualPlayer; - - // TODO: probably can do better with an eventListener - if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) { + if (GameScreen.playerCurrent.isActive() && Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) { this.anchor = GameScreen.worldCursor.cpy(); pressed = true; } else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) { - computeArrow(); + aim = this.anchor.cpy().sub(GameScreen.worldCursor); + angle = aim.angleDeg(); + power = MathUtils.clamp(aim.len(), 0, 1000); } else if (pressed) { pressed = false; - GameScreen.arrows.addActor(getArrow()); - GameScreen.nextPlayer(); + GameScreen.playerCurrent.setActive(false); + GameScreen.arrows.addActor(new Arrow(angle, power, GameScreen.playerCurrent)); } } @@ -56,7 +54,7 @@ public class Bow extends Actor { if (pressed) { shapes.line(this.anchor, GameScreen.worldCursor); if (aimAssist) { - float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f); + float[] traj = Arrow.traj(angle, power, GameScreen.playerCurrent, 400, 0.05f); if (traj.length > 2) { shapes.polyline(traj); } @@ -64,23 +62,4 @@ public class Bow extends Actor { } } - /** - * Converts the Bow's attributes to apply it - * to the Arrow's constructor. - */ - private void computeArrow() { - aim = this.anchor.cpy().sub(GameScreen.worldCursor); - angle = aim.angleDeg(); - power = MathUtils.clamp(aim.len(), 0, 1000); - } - - /** - * Generates an Arrow according to the Bow's attributes. - * - * @return an Arrow. - */ - private Arrow getArrow() { - return new Arrow(angle, power, shooter); - } - } diff --git a/core/src/sagittarius/model/Moon.java b/core/src/sagittarius/model/Moon.java index af6e0b8..a422698 100644 --- a/core/src/sagittarius/model/Moon.java +++ b/core/src/sagittarius/model/Moon.java @@ -1,6 +1,7 @@ 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; @@ -35,4 +36,12 @@ public class Moon extends Planet { this.setY(sun.getY() + this.altitude*MathUtils.sinDeg(this.getRotation())); super.act(dt); } + + @Override + public void drawDebug(ShapeRenderer shapes) { + super.drawDebug(shapes); + if (getStage() != null) shapes.setColor(getStage().getDebugColor()); + shapes.line(getX(), getY(), sun.getX(), sun.getY()); + } + } \ No newline at end of file diff --git a/core/src/sagittarius/view/GameScreen.java b/core/src/sagittarius/view/GameScreen.java index 13c5cb0..7b3cace 100644 --- a/core/src/sagittarius/view/GameScreen.java +++ b/core/src/sagittarius/view/GameScreen.java @@ -26,8 +26,8 @@ public class GameScreen extends BaseScreen { public static Group arrows; public static Group players; - public static int playerTurn; - public static Player actualPlayer; + public static int playerIndex; + public static Player playerCurrent; // ---------- METHODs ---------- @@ -78,26 +78,22 @@ public class GameScreen extends BaseScreen { uiStage.setDebugAll(SagittariusGame.debugMode); // game turns - playerTurn = 0; + playerIndex = 0; player1.setActive(true); + playerCurrent = player1; } @Override public void update(float dt) { - screenCursor = new Vector2(Gdx.input.getX(), Gdx.input.getY()); + screenCursor = new Vector2(Gdx.input.getX(), Gdx.input.getY()); // utiliser les trucs fournis par libGDX unprojectedCursor = mainStage.getCamera().unproject(new Vector3(screenCursor, 0)); worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y); - actualPlayer = (Player) players.getChild(playerTurn); - // if (actualPlayer.isActive()) { - // actualPlayer.setActive(false); - // playerTurn++; - // playerTurn %= players.getChildren().size; - // actualPlayer = (Player) players.getChild(playerTurn); - // actualPlayer.setActive(true); - // } + if (players.getChildren().size <= 1) { + SagittariusGame.setActiveScreen( new StartScreen() ); + } } @@ -106,26 +102,27 @@ public class GameScreen extends BaseScreen { * @param player */ public static void removePlayer(Player player) { + player.setActive(false); int index = players.getChildren().indexOf(player, true); - if (index < playerTurn) { + if (index < playerIndex) { players.removeActor(player); - playerTurn++; - playerTurn %= players.getChildren().size; - } else if (index == playerTurn) { + //playerIndex++; // ? keeping ? + playerIndex %= players.getChildren().size; + } else if (index == playerIndex) { players.removeActor(player); - playerTurn %= players.getChildren().size; - actualPlayer = (Player) players.getChild(playerTurn); - actualPlayer.setActive(true); + playerIndex %= players.getChildren().size; + playerCurrent = (Player) players.getChild(playerIndex); + playerCurrent.setActive(true); } else { players.removeActor(player); } } public static void nextPlayer() { - actualPlayer.setActive(false); - playerTurn++; - playerTurn %= players.getChildren().size; - actualPlayer = (Player) players.getChild(playerTurn); - actualPlayer.setActive(true); + playerCurrent.setActive(false); + playerIndex++; + playerIndex %= players.getChildren().size; + playerCurrent = (Player) players.getChild(playerIndex); + playerCurrent.setActive(true); } } \ No newline at end of file