From f57d2da53d82a2d988e5672dcab7036fd6cce77c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Tue, 13 Apr 2021 21:55:14 +0200 Subject: [PATCH] refacto: verifyLanding of Arrow --- core/src/sagittarius/model/Arrow.java | 54 +++++++++++------------ core/src/sagittarius/model/Bow.java | 45 ++++++------------- core/src/sagittarius/model/Player.java | 25 +++-------- core/src/sagittarius/view/BaseScreen.java | 7 ++- core/src/sagittarius/view/GameScreen.java | 12 +++-- 5 files changed, 57 insertions(+), 86 deletions(-) diff --git a/core/src/sagittarius/model/Arrow.java b/core/src/sagittarius/model/Arrow.java index 20b17d5..ab3d5e3 100644 --- a/core/src/sagittarius/model/Arrow.java +++ b/core/src/sagittarius/model/Arrow.java @@ -22,6 +22,9 @@ public class Arrow extends BaseActor { private float TTL = initTTL; private boolean landed; + private Planet crash; + private Vector2 offset; + // ---------- CONSTRUCTORs ---------- /** @@ -51,10 +54,14 @@ public class Arrow extends BaseActor { integrationVerlet(dt); this.TTL -= dt; this.setRotation(this.velocity.angleDeg()); + verifyLanding(); if (TTL <= 0) { GameScreen.arrows.removeActor(this); } + } else { + this.setX( crash.getX() + this.offset.x ); + this.setY( crash.getY() + this.offset.y ); } } @@ -81,7 +88,7 @@ public class Arrow extends BaseActor { float len2 = dx*dx + dy*dy; - float coeff = (float) (SagittariusGame.G * ((Planet) actor).getMass() * Math.pow(len2, -3/2)); + float coeff = SagittariusGame.G * ((Planet) actor).getMass() * (float) Math.pow(len2, -3/2); float gravityX = coeff * dx; float gravityY = coeff * dy; @@ -106,39 +113,30 @@ public class Arrow extends BaseActor { this.moveBy(dt * ( this.velocity.x + dt * this.acceleration.x / 2 ), dt * ( this.velocity.y + dt * this.acceleration.y / 2 )); - for (Actor actor : GameScreen.attractors.getChildren()) { - - float dx = actor.getX() - this.getX(); - float dy = actor.getY() - this.getY(); - - float len2 = dx*dx + dy*dy; - - if (len2 < ((Planet) actor).getRadius()*((Planet) actor).getRadius()) { - landed = true; - break; - } - } - - if (TTL < initTTL - 0.5) { - for (Actor actor : GameScreen.players.getChildren()) { - - float dx = actor.getX() - this.getX(); - float dy = actor.getY() - this.getY(); - - float len2 = dx*dx + dy*dy; - - if (len2 < 200) { - SagittariusGame.gameScreen.removePlayer((Player) actor); - } - } - } - this.force = computeForce(); this.velocity.x += dt * ( this.acceleration.x + this.force.x ) / 2; this.velocity.y += dt * ( this.acceleration.y + this.force.y ) / 2; } + /** + * TODO + */ + 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 ? + landed = true; + this.crash = planet; + this.offset = this.getPosition().sub( planet.getPosition() ); + break; + } + } + } + /** // TODO : pass directly an Arrow instead of 3 arguments * Computes the trajectory of an Arrow, * given its initial conditions. diff --git a/core/src/sagittarius/model/Bow.java b/core/src/sagittarius/model/Bow.java index 75b4b0a..c4643c2 100644 --- a/core/src/sagittarius/model/Bow.java +++ b/core/src/sagittarius/model/Bow.java @@ -23,13 +23,10 @@ public class Bow extends Actor { private float power; - private boolean playing; - // ---------- CONSTRUCTORs ---------- - Bow(Player shooter, boolean aimAssist) { + public Bow(Player shooter, boolean aimAssist) { super(); - this.playing = false; this.shooter = shooter; this.aimAssist = aimAssist; } @@ -38,27 +35,22 @@ public class Bow extends Actor { @Override public void act(float dt) { - if (playing) { - // TODO: probably can do better with an eventListener - if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) { - this.anchor = GameScreen.worldCursor.cpy(); - pressed = true; - } else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) { - computeArrow(); - } else if (pressed) { - // Sagittarius.arrowList.add(getArrow()); - GameScreen.arrows.addActor(getArrow()); - pressed = false; - playing = false; - } + // TODO: probably can do better with an eventListener + if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) { + this.anchor = GameScreen.worldCursor.cpy(); + pressed = true; + } else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) { + computeArrow(); + } else if (pressed) { + GameScreen.arrows.addActor(getArrow()); + pressed = false; } - } @Override public void drawDebug(ShapeRenderer shapes) { super.drawDebug(shapes); - if (pressed && playing) { + if (pressed) { shapes.line(this.anchor, GameScreen.worldCursor); if (aimAssist) { float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f); @@ -88,19 +80,8 @@ public class Bow extends Actor { return new Arrow(angle, power, shooter); } - /** - * Change the active state of the {@link Bow}. - * @param bool true or false - */ - public void setPlaying(boolean bool) { - playing = bool; - } - - /** - * @return the active state of the {@link Bow}. - */ - public boolean isPlaying() { - return playing; + public void changeShooter(Player newShooter) { + this.shooter = newShooter; } } diff --git a/core/src/sagittarius/model/Player.java b/core/src/sagittarius/model/Player.java index 3460316..5d0ea6f 100644 --- a/core/src/sagittarius/model/Player.java +++ b/core/src/sagittarius/model/Player.java @@ -11,7 +11,6 @@ public class Player extends BaseActor { // ---------- ATTRIBUTEs ---------- private Planet home; - private Bow bow; private boolean playing; // ---------- CONSTRUCTORs ---------- @@ -27,7 +26,6 @@ public class Player extends BaseActor { this.setRotation(angle-90); this.home = home; - this.bow = new Bow(this, true); } // ---------- METHODs ---------- @@ -36,7 +34,6 @@ public class Player extends BaseActor { public void drawDebug(ShapeRenderer shapes) { super.drawDebug(shapes); shapes.line(home.getX(), home.getY(), getX(), getY()); - bow.drawDebug(shapes); } @Override @@ -56,8 +53,13 @@ public class Player extends BaseActor { setY(home.getY() + (home.getRadius() + getHeight()/2)*MathUtils.sinDeg(angle)); this.setRotation(angle-90); + } - bow.act(dt); + @Override + protected String getInfo() { + return + "pos=" + (int)getX() + "," + (int)getY() + ; } /** @@ -67,19 +69,11 @@ public class Player extends BaseActor { return this.home; } - /** - * @return the player's bow. - */ - public Bow getBow() { - return bow; - } - /** * Change the active state of the {@link Player}. * @param bool true or false */ public void setPlaying(boolean bool) { - bow.setPlaying(bool); playing = bool; } @@ -90,11 +84,4 @@ public class Player extends BaseActor { return playing; } - @Override - protected String getInfo() { - return - "pos=" + (int)getX() + "," + (int)getY() - ; - } - } diff --git a/core/src/sagittarius/view/BaseScreen.java b/core/src/sagittarius/view/BaseScreen.java index 7e29a1b..2621cd0 100644 --- a/core/src/sagittarius/view/BaseScreen.java +++ b/core/src/sagittarius/view/BaseScreen.java @@ -3,6 +3,9 @@ package sagittarius.view; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL30; import com.badlogic.gdx.scenes.scene2d.Stage; + +import sagittarius.model.BaseActor; + import com.badlogic.gdx.Screen; public abstract class BaseScreen implements Screen { @@ -52,8 +55,4 @@ public abstract class BaseScreen implements Screen { @Override public void show() {} @Override public void hide() {} - public Stage getUIStage() { - return uiStage; - } - } \ No newline at end of file diff --git a/core/src/sagittarius/view/GameScreen.java b/core/src/sagittarius/view/GameScreen.java index d683462..9461359 100644 --- a/core/src/sagittarius/view/GameScreen.java +++ b/core/src/sagittarius/view/GameScreen.java @@ -53,13 +53,15 @@ public class GameScreen extends BaseScreen { players.addActor(player2); mainStage.addActor(players); - playerTurn = 0; - player1.setPlaying(true); // arrows arrows = new Group(); mainStage.addActor(arrows); + // The one and only Bow + Bow bow = new Bow(player1, true); + mainStage.addActor(bow); + // others FPS fpsCounter = new FPS(uiStage); uiStage.addActor(fpsCounter); @@ -70,6 +72,10 @@ public class GameScreen extends BaseScreen { mainStage.setDebugAll(true); // TODO: disable later uiStage.setDebugAll(true); // TODO: disable later + // game turns + playerTurn = 0; + player1.setPlaying(true); + } @Override @@ -80,7 +86,7 @@ public class GameScreen extends BaseScreen { worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y); Player actualPlayer = (Player) players.getChild(playerTurn); - if (actualPlayer.isPlaying() && !actualPlayer.getBow().isPlaying()) { + if (actualPlayer.isPlaying()) { actualPlayer.setPlaying(true); playerTurn++; playerTurn %= players.getChildren().size;