refacto: verifyLanding of Arrow

This commit is contained in:
Laureηt 2021-04-13 21:55:14 +02:00
parent 6b74d3ad7c
commit f57d2da53d
5 changed files with 57 additions and 86 deletions

View file

@ -22,6 +22,9 @@ public class Arrow extends BaseActor {
private float TTL = initTTL; private float TTL = initTTL;
private boolean landed; private boolean landed;
private Planet crash;
private Vector2 offset;
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
/** /**
@ -51,10 +54,14 @@ public class Arrow extends BaseActor {
integrationVerlet(dt); integrationVerlet(dt);
this.TTL -= dt; this.TTL -= dt;
this.setRotation(this.velocity.angleDeg()); this.setRotation(this.velocity.angleDeg());
verifyLanding();
if (TTL <= 0) { if (TTL <= 0) {
GameScreen.arrows.removeActor(this); 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 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 gravityX = coeff * dx;
float gravityY = coeff * dy; float gravityY = coeff * dy;
@ -106,39 +113,30 @@ public class Arrow extends BaseActor {
this.moveBy(dt * ( this.velocity.x + dt * this.acceleration.x / 2 ), this.moveBy(dt * ( this.velocity.x + dt * this.acceleration.x / 2 ),
dt * ( this.velocity.y + dt * this.acceleration.y / 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.force = computeForce();
this.velocity.x += dt * ( this.acceleration.x + this.force.x ) / 2; this.velocity.x += dt * ( this.acceleration.x + this.force.x ) / 2;
this.velocity.y += dt * ( this.acceleration.y + this.force.y ) / 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 /** // TODO : pass directly an Arrow instead of 3 arguments
* Computes the trajectory of an Arrow, * Computes the trajectory of an Arrow,
* given its initial conditions. * given its initial conditions.

View file

@ -23,13 +23,10 @@ public class Bow extends Actor {
private float power; private float power;
private boolean playing;
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
Bow(Player shooter, boolean aimAssist) { public Bow(Player shooter, boolean aimAssist) {
super(); super();
this.playing = false;
this.shooter = shooter; this.shooter = shooter;
this.aimAssist = aimAssist; this.aimAssist = aimAssist;
} }
@ -38,7 +35,6 @@ public class Bow extends Actor {
@Override @Override
public void act(float dt) { public void act(float dt) {
if (playing) {
// TODO: probably can do better with an eventListener // TODO: probably can do better with an eventListener
if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) { if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) {
this.anchor = GameScreen.worldCursor.cpy(); this.anchor = GameScreen.worldCursor.cpy();
@ -46,19 +42,15 @@ public class Bow extends Actor {
} else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) { } else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) {
computeArrow(); computeArrow();
} else if (pressed) { } else if (pressed) {
// Sagittarius.arrowList.add(getArrow());
GameScreen.arrows.addActor(getArrow()); GameScreen.arrows.addActor(getArrow());
pressed = false; pressed = false;
playing = false;
} }
} }
}
@Override @Override
public void drawDebug(ShapeRenderer shapes) { public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes); super.drawDebug(shapes);
if (pressed && playing) { if (pressed) {
shapes.line(this.anchor, GameScreen.worldCursor); shapes.line(this.anchor, GameScreen.worldCursor);
if (aimAssist) { if (aimAssist) {
float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f); 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); return new Arrow(angle, power, shooter);
} }
/** public void changeShooter(Player newShooter) {
* Change the active state of the {@link Bow}. this.shooter = newShooter;
* @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;
} }
} }

View file

@ -11,7 +11,6 @@ public class Player extends BaseActor {
// ---------- ATTRIBUTEs ---------- // ---------- ATTRIBUTEs ----------
private Planet home; private Planet home;
private Bow bow;
private boolean playing; private boolean playing;
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
@ -27,7 +26,6 @@ public class Player extends BaseActor {
this.setRotation(angle-90); this.setRotation(angle-90);
this.home = home; this.home = home;
this.bow = new Bow(this, true);
} }
// ---------- METHODs ---------- // ---------- METHODs ----------
@ -36,7 +34,6 @@ public class Player extends BaseActor {
public void drawDebug(ShapeRenderer shapes) { public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes); super.drawDebug(shapes);
shapes.line(home.getX(), home.getY(), getX(), getY()); shapes.line(home.getX(), home.getY(), getX(), getY());
bow.drawDebug(shapes);
} }
@Override @Override
@ -56,8 +53,13 @@ public class Player extends BaseActor {
setY(home.getY() + (home.getRadius() + getHeight()/2)*MathUtils.sinDeg(angle)); setY(home.getY() + (home.getRadius() + getHeight()/2)*MathUtils.sinDeg(angle));
this.setRotation(angle-90); 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 this.home;
} }
/**
* @return the player's bow.
*/
public Bow getBow() {
return bow;
}
/** /**
* Change the active state of the {@link Player}. * Change the active state of the {@link Player}.
* @param bool true or false * @param bool true or false
*/ */
public void setPlaying(boolean bool) { public void setPlaying(boolean bool) {
bow.setPlaying(bool);
playing = bool; playing = bool;
} }
@ -90,11 +84,4 @@ public class Player extends BaseActor {
return playing; return playing;
} }
@Override
protected String getInfo() {
return
"pos=" + (int)getX() + "," + (int)getY()
;
}
} }

View file

@ -3,6 +3,9 @@ package sagittarius.view;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30; import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.Stage;
import sagittarius.model.BaseActor;
import com.badlogic.gdx.Screen; import com.badlogic.gdx.Screen;
public abstract class BaseScreen implements Screen { public abstract class BaseScreen implements Screen {
@ -52,8 +55,4 @@ public abstract class BaseScreen implements Screen {
@Override public void show() {} @Override public void show() {}
@Override public void hide() {} @Override public void hide() {}
public Stage getUIStage() {
return uiStage;
}
} }

View file

@ -53,13 +53,15 @@ public class GameScreen extends BaseScreen {
players.addActor(player2); players.addActor(player2);
mainStage.addActor(players); mainStage.addActor(players);
playerTurn = 0;
player1.setPlaying(true);
// arrows // arrows
arrows = new Group(); arrows = new Group();
mainStage.addActor(arrows); mainStage.addActor(arrows);
// The one and only Bow
Bow bow = new Bow(player1, true);
mainStage.addActor(bow);
// others // others
FPS fpsCounter = new FPS(uiStage); FPS fpsCounter = new FPS(uiStage);
uiStage.addActor(fpsCounter); uiStage.addActor(fpsCounter);
@ -70,6 +72,10 @@ public class GameScreen extends BaseScreen {
mainStage.setDebugAll(true); // TODO: disable later mainStage.setDebugAll(true); // TODO: disable later
uiStage.setDebugAll(true); // TODO: disable later uiStage.setDebugAll(true); // TODO: disable later
// game turns
playerTurn = 0;
player1.setPlaying(true);
} }
@Override @Override
@ -80,7 +86,7 @@ public class GameScreen extends BaseScreen {
worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y); worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y);
Player actualPlayer = (Player) players.getChild(playerTurn); Player actualPlayer = (Player) players.getChild(playerTurn);
if (actualPlayer.isPlaying() && !actualPlayer.getBow().isPlaying()) { if (actualPlayer.isPlaying()) {
actualPlayer.setPlaying(true); actualPlayer.setPlaying(true);
playerTurn++; playerTurn++;
playerTurn %= players.getChildren().size; playerTurn %= players.getChildren().size;