feat!:turn system finally finished

returned to old computeForce using Vectors2
This commit is contained in:
Laureηt 2021-04-23 15:53:10 +02:00
parent f82147cbcc
commit 5b591e3f8e
4 changed files with 70 additions and 75 deletions

View file

@ -3,6 +3,7 @@ package sagittarius.model;
import java.util.ArrayList; import java.util.ArrayList;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
@ -51,15 +52,16 @@ public class Arrow extends EntityQuad {
integrationVerlet(dt); integrationVerlet(dt);
this.TTL -= dt; this.TTL -= dt;
this.setRotation(this.velocity.angleDeg()); this.setRotation(this.velocity.angleDeg());
verifyLanding(); verifyLanding();
verifyHitting(); verifyHitting();
if (TTL <= 0) { if (TTL <= 0) {
GameScreen.arrows.removeActor(this); GameScreen.arrows.removeActor(this);
GameScreen.nextPlayer();
} }
} else { } else {
this.setX( crash.getX() + this.offset.x ); this.setPosition(crash.getPosition().cpy().add(offset));
this.setY( crash.getY() + this.offset.y );
} }
} }
@ -81,18 +83,10 @@ public class Arrow extends EntityQuad {
private Vector2 computeForce() { private Vector2 computeForce() {
Vector2 force = new Vector2(); Vector2 force = new Vector2();
for (Actor actor : GameScreen.attractors.getChildren()) { for (Actor actor : GameScreen.attractors.getChildren()) {
Planet attractor = (Planet) actor;
float dx = actor.getX() - this.getX(); Vector2 diff = attractor.getPosition().cpy().sub(this.getPosition());
float dy = actor.getY() - this.getY(); Vector2 attraction = diff.scl( SagittariusGame.G * attractor.mass / diff.len2() );
force.add(attraction);
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);
} }
return force; return force;
} }
@ -123,36 +117,53 @@ public class Arrow extends EntityQuad {
*/ */
private void verifyLanding() { private void verifyLanding() {
for (Actor actor : GameScreen.attractors.getChildren()) { for (Actor actor : GameScreen.attractors.getChildren()) {
Planet planet = (Planet) actor; Planet planet = (Planet) actor;
float dist = this.getPosition().sub( planet.getPosition() ).len(); if (planet.hitbox.contains(this.getPosition())) {
if (dist <= planet.getRadius()) { // TODO: change with overlap in the future ?
landed = true; landed = true;
this.crash = planet; this.crash = planet;
this.offset = this.getPosition().sub( planet.getPosition() ); this.offset = this.getPosition().sub( planet.getPosition() );
GameScreen.nextPlayer();
break; 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 * TODO
*/ */
private void verifyHitting() { private void verifyHitting() {
for (Actor actor : GameScreen.players.getChildren()) { for (Actor actor : GameScreen.players.getChildren()) {
Player player = (Player) actor; Player player = (Player) actor;
if (TTL < 19 && Intersector.overlapConvexPolygons(player.hitbox, this.hitbox)) {
if (TTL < 19 && player.hitbox.contains(getX(), getY())) {
landed = true; landed = true;
GameScreen.removePlayer(player); GameScreen.removePlayer(player);
GameScreen.arrows.removeActor(this); GameScreen.arrows.removeActor(this);
GameScreen.nextPlayer();
break; 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 /** // 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.
@ -166,8 +177,7 @@ public class Arrow extends EntityQuad {
Arrow dummyArrow = new Arrow(angle, power, shooter); Arrow dummyArrow = new Arrow(angle, power, shooter);
for (int i = 0; i < iterations; i++) { for (int i = 0; i < iterations; i++) {
dummyArrow.integrationVerlet(timeStep); dummyArrow.integrationVerlet(timeStep);
// dummyArrow.verifyLanding(); if (dummyArrow.hasLanded() || dummyArrow.hasHit() ) { break; }
// dummyArrow.verifyHittingPlayer(); // fix -> return booleans, take action after
path.add(dummyArrow.getX()); path.add(dummyArrow.getX());
path.add(dummyArrow.getY()); path.add(dummyArrow.getY());
} }

View file

@ -13,7 +13,6 @@ public class Bow extends Actor {
// ---------- ATTRIBUTEs ---------- // ---------- ATTRIBUTEs ----------
private Player shooter;
private boolean aimAssist = false; private boolean aimAssist = false;
private boolean pressed = false; private boolean pressed = false;
private float angle; private float angle;
@ -35,18 +34,17 @@ public class Bow extends Actor {
@Override @Override
public void act(float dt) { public void act(float dt) {
super.act(dt); super.act(dt);
this.shooter = GameScreen.actualPlayer; if (GameScreen.playerCurrent.isActive() && Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) {
// TODO: probably can do better with an eventListener
if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) {
this.anchor = GameScreen.worldCursor.cpy(); this.anchor = GameScreen.worldCursor.cpy();
pressed = true; pressed = true;
} else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) { } 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) { } else if (pressed) {
pressed = false; pressed = false;
GameScreen.arrows.addActor(getArrow()); GameScreen.playerCurrent.setActive(false);
GameScreen.nextPlayer(); GameScreen.arrows.addActor(new Arrow(angle, power, GameScreen.playerCurrent));
} }
} }
@ -56,7 +54,7 @@ public class Bow extends Actor {
if (pressed) { 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, GameScreen.playerCurrent, 400, 0.05f);
if (traj.length > 2) { if (traj.length > 2) {
shapes.polyline(traj); 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);
}
} }

View file

@ -1,6 +1,7 @@
package sagittarius.model; package sagittarius.model;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; 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())); this.setY(sun.getY() + this.altitude*MathUtils.sinDeg(this.getRotation()));
super.act(dt); 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());
}
} }

View file

@ -26,8 +26,8 @@ public class GameScreen extends BaseScreen {
public static Group arrows; public static Group arrows;
public static Group players; public static Group players;
public static int playerTurn; public static int playerIndex;
public static Player actualPlayer; public static Player playerCurrent;
// ---------- METHODs ---------- // ---------- METHODs ----------
@ -78,26 +78,22 @@ public class GameScreen extends BaseScreen {
uiStage.setDebugAll(SagittariusGame.debugMode); uiStage.setDebugAll(SagittariusGame.debugMode);
// game turns // game turns
playerTurn = 0; playerIndex = 0;
player1.setActive(true); player1.setActive(true);
playerCurrent = player1;
} }
@Override @Override
public void update(float dt) { 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)); unprojectedCursor = mainStage.getCamera().unproject(new Vector3(screenCursor, 0));
worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y); worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y);
actualPlayer = (Player) players.getChild(playerTurn); if (players.getChildren().size <= 1) {
// if (actualPlayer.isActive()) { SagittariusGame.setActiveScreen( new StartScreen() );
// actualPlayer.setActive(false); }
// playerTurn++;
// playerTurn %= players.getChildren().size;
// actualPlayer = (Player) players.getChild(playerTurn);
// actualPlayer.setActive(true);
// }
} }
@ -106,26 +102,27 @@ public class GameScreen extends BaseScreen {
* @param player * @param player
*/ */
public static void removePlayer(Player player) { public static void removePlayer(Player player) {
player.setActive(false);
int index = players.getChildren().indexOf(player, true); int index = players.getChildren().indexOf(player, true);
if (index < playerTurn) { if (index < playerIndex) {
players.removeActor(player); players.removeActor(player);
playerTurn++; //playerIndex++; // ? keeping ?
playerTurn %= players.getChildren().size; playerIndex %= players.getChildren().size;
} else if (index == playerTurn) { } else if (index == playerIndex) {
players.removeActor(player); players.removeActor(player);
playerTurn %= players.getChildren().size; playerIndex %= players.getChildren().size;
actualPlayer = (Player) players.getChild(playerTurn); playerCurrent = (Player) players.getChild(playerIndex);
actualPlayer.setActive(true); playerCurrent.setActive(true);
} else { } else {
players.removeActor(player); players.removeActor(player);
} }
} }
public static void nextPlayer() { public static void nextPlayer() {
actualPlayer.setActive(false); playerCurrent.setActive(false);
playerTurn++; playerIndex++;
playerTurn %= players.getChildren().size; playerIndex %= players.getChildren().size;
actualPlayer = (Player) players.getChild(playerTurn); playerCurrent = (Player) players.getChild(playerIndex);
actualPlayer.setActive(true); playerCurrent.setActive(true);
} }
} }