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 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.

View file

@ -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,7 +35,6 @@ 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();
@ -46,19 +42,15 @@ public class Bow extends Actor {
} else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) {
computeArrow();
} else if (pressed) {
// Sagittarius.arrowList.add(getArrow());
GameScreen.arrows.addActor(getArrow());
pressed = false;
playing = 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;
}
}

View file

@ -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()
;
}
}

View file

@ -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;
}
}

View file

@ -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;