From 2c5b66eb65021f473b2229f0e27e3f61afb7b3ad Mon Sep 17 00:00:00 2001 From: Damien Date: Tue, 13 Apr 2021 19:45:49 +0200 Subject: [PATCH] Ca marche ? --- core/src/sagittarius/SagittariusGame.java | 7 +- core/src/sagittarius/model/Arrow.java | 74 ++++++++++++---- core/src/sagittarius/model/Bow.java | 54 +++++++++--- core/src/sagittarius/model/Player.java | 100 ++++++++++++++++------ core/src/sagittarius/view/GameScreen.java | 42 ++++++++- 5 files changed, 214 insertions(+), 63 deletions(-) diff --git a/core/src/sagittarius/SagittariusGame.java b/core/src/sagittarius/SagittariusGame.java index 1a89227..7ae50e6 100644 --- a/core/src/sagittarius/SagittariusGame.java +++ b/core/src/sagittarius/SagittariusGame.java @@ -3,13 +3,15 @@ package sagittarius; import com.badlogic.gdx.Game; import sagittarius.view.BaseScreen; -import sagittarius.view.StartScreen; +import sagittarius.view.GameScreen; public class SagittariusGame extends Game { // Constants public static final int G = 100; + // Game + public static GameScreen gameScreen; private static SagittariusGame game; public SagittariusGame() { @@ -26,6 +28,7 @@ public class SagittariusGame extends Game { @Override public void create() { - this.setScreen(new StartScreen()); + gameScreen = new GameScreen(); + this.setScreen(gameScreen); } } \ No newline at end of file diff --git a/core/src/sagittarius/model/Arrow.java b/core/src/sagittarius/model/Arrow.java index 49da985..e19b1f9 100644 --- a/core/src/sagittarius/model/Arrow.java +++ b/core/src/sagittarius/model/Arrow.java @@ -2,6 +2,8 @@ package sagittarius.model; import java.util.ArrayList; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.MathUtils; @@ -11,7 +13,9 @@ import com.badlogic.gdx.scenes.scene2d.Actor; import sagittarius.SagittariusGame; import sagittarius.view.GameScreen; -public class Arrow extends BaseActor { +public class Arrow extends Actor { + + private static final float initTTL = 20; // ---------- ATTRIBUTEs ---------- @@ -19,7 +23,10 @@ public class Arrow extends BaseActor { private Vector2 acceleration; private Vector2 force; - private float TTL = 20; + private float TTL = initTTL; + private boolean landed; + + private BitmapFont fontDebug = new BitmapFont(); // ---------- CONSTRUCTORs ---------- @@ -39,6 +46,7 @@ public class Arrow extends BaseActor { this.setOrigin(80, 2); this.setSize(100, 4); this.force = computeForce(); + this.landed = false; } // ---------- METHODs ---------- @@ -46,16 +54,29 @@ public class Arrow extends BaseActor { @Override public void act(float dt) { - integrationVerlet(dt); - this.TTL -= dt; - this.setRotation(this.velocity.angleDeg()); + if (!landed) { + integrationVerlet(dt); + this.TTL -= dt; + this.setRotation(this.velocity.angleDeg()); - if (TTL <= 0) { - GameScreen.arrows.removeActor(this); + if (TTL <= 0) { + GameScreen.arrows.removeActor(this); + } } } + @Override + public void draw(Batch batch, float parentAlpha) { + super.draw(batch, parentAlpha); + fontDebug.draw(batch, "TTL=" + (int)TTL, getX(), getY() + fontDebug.getCapHeight()*5); + fontDebug.draw(batch, "pos=" + (int)getX() + "," + (int)getY(), getX(), getY() + fontDebug.getCapHeight()*4); + fontDebug.draw(batch, "speed=" + (int)velocity.x + "," + (int)velocity.y, getX(), getY() + fontDebug.getCapHeight()*3); + fontDebug.draw(batch, "accel=" + (int)acceleration.x + "," + (int)acceleration.y, getX(), getY() + fontDebug.getCapHeight()*2); + fontDebug.draw(batch, "force=" + (int)force.x + "," + (int)force.y, getX(), getY() + fontDebug.getCapHeight()*1); + fontDebug.draw(batch, "angle=" + (int)getRotation(), getX(), getY()); + } + @Override public void drawDebug(ShapeRenderer shapes) { super.drawDebug(shapes); @@ -116,6 +137,33 @@ 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; @@ -155,16 +203,4 @@ public class Arrow extends BaseActor { return new Vector2( getX(), getY() ); } - @Override - protected String getInfo() { - return - "TTL=" + (int)TTL + "\n" - + "pos=" + (int)getX() + "," + (int)getY() + "\n" - + "speed=" + (int)velocity.x + "," + (int)velocity.y + "\n" - + "accel=" + (int)acceleration.x + "," + (int)acceleration.y +"\n" - + "force=" + (int)force.x + "," + (int)force.y + "\n" - + "angle=" + (int)getRotation() - ; - } - } diff --git a/core/src/sagittarius/model/Bow.java b/core/src/sagittarius/model/Bow.java index 9eac5de..e6f6de6 100644 --- a/core/src/sagittarius/model/Bow.java +++ b/core/src/sagittarius/model/Bow.java @@ -9,7 +9,7 @@ import com.badlogic.gdx.scenes.scene2d.Actor; import sagittarius.view.GameScreen; -class Bow extends Actor { +public class Bow extends Actor { // ---------- ATTRIBUTEs ---------- @@ -23,10 +23,13 @@ class Bow extends Actor { private float power; + private boolean playing; + // ---------- CONSTRUCTORs ---------- Bow(Player shooter, boolean aimAssist) { super(); + this.playing = false; this.shooter = shooter; this.aimAssist = aimAssist; } @@ -35,16 +38,19 @@ class Bow extends Actor { @Override public void act(float dt) { - - // 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; + 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; + } } } @@ -52,7 +58,7 @@ class Bow extends Actor { @Override public void drawDebug(ShapeRenderer shapes) { super.drawDebug(shapes); - if (pressed) { + if (pressed && playing) { shapes.line(this.anchor, GameScreen.worldCursor); if (aimAssist) { float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f); @@ -82,4 +88,28 @@ class Bow extends Actor { return new Arrow(angle, power, shooter); } + + /** + * Indiquer que c'est a son tour de jouer. + */ + public void startPlaying() { + playing = true; + } + + + /** + * Indique que ce n'est plus son tour de jouer. + */ + public void stopPlaying() { + playing = false; + } + + + /** + * Savoir si il est entrain de jouer. + */ + public boolean isPlaying() { + return playing; + } + } diff --git a/core/src/sagittarius/model/Player.java b/core/src/sagittarius/model/Player.java index f7174c7..c369167 100644 --- a/core/src/sagittarius/model/Player.java +++ b/core/src/sagittarius/model/Player.java @@ -3,72 +3,120 @@ package sagittarius.model; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.math.MathUtils; +import com.badlogic.gdx.scenes.scene2d.Actor; -public class Player extends BaseActor { +public class Player extends Actor { // ---------- ATTRIBUTEs ---------- private Planet home; private Bow bow; + private boolean playing; + private BitmapFont fontDebug = new BitmapFont(); // ---------- CONSTRUCTORs ---------- public Player(Planet home, Color color) { super(); - - this.setSize(50, 100); - this.setOrigin(getWidth()/2, getHeight()/2); + this.setSize(100, 50); // TODO: fix this ? (width & height inverted) + this.setRotation(MathUtils.random(360)); this.setColor(color); - - this.angle = MathUtils.random(360); - this.setRotation(angle-90); - + this.setOrigin(0, getHeight()/2); this.home = home; this.bow = new Bow(this, true); } // ---------- METHODs ---------- + @Override + public void draw(Batch batch, float parentAlpha) { + super.draw(batch, parentAlpha); + fontDebug.draw(batch, "pos=" + (int)getX() + "," + (int)getY() , getX(), getY()); + } + @Override public void drawDebug(ShapeRenderer shapes) { super.drawDebug(shapes); - shapes.line(home.getX(), home.getY(), getX(), getY()); bow.drawDebug(shapes); } + @Override + protected void drawDebugBounds(ShapeRenderer shapes) { + if (!getDebug()) return; + shapes.set(ShapeType.Line); + if (getStage() != null) shapes.setColor(getStage().getDebugColor()); + + shapes.rect(getX(), getY() - getOriginY(), + getOriginX(), getOriginY(), + getWidth(), getHeight(), + getScaleX(), getScaleY(), + getRotation()); + shapes.line(home.getX(), home.getY(), getX(), getY()); + } + @Override public void act(float dt) { super.act(dt); - if (Gdx.input.isKeyPressed(Keys.LEFT)) { - this.angle += 100.0f / home.getRadius(); - } - if (Gdx.input.isKeyPressed(Keys.RIGHT)) { - this.angle -= 100.0f / home.getRadius(); + if (playing) { + if (Gdx.input.isKeyPressed(Keys.LEFT)) { + rotateBy( 100.0f / home.getRadius()); + } + if (Gdx.input.isKeyPressed(Keys.RIGHT)) { + rotateBy(- 100.0f / home.getRadius()); + } } - setX(home.getX() + (home.getRadius() + getHeight()/2)*MathUtils.cosDeg(angle)); - setY(home.getY() + (home.getRadius() + getHeight()/2)*MathUtils.sinDeg(angle)); - - this.setRotation(angle-90); + setX(home.getX() + home.getRadius()*MathUtils.cosDeg(getRotation())); + setY(home.getY() + home.getRadius()*MathUtils.sinDeg(getRotation())); bow.act(dt); } - @Override - protected String getInfo() { - return - "pos=" + (int)getX() + "," + (int)getY() - ; - } - /** * @return the Player's home Planet. */ - Planet getHome() { + public Planet getHome() { return this.home; } + + /** + * @return the player's bow. + */ + public Bow getBow() { + return bow; + } + + + /** + * Indiquer que c'est son tour de jouer. + */ + public void startPlaying() { + bow.startPlaying(); + playing = true; + } + + + /** + * Indique la fin de son tour de jeu. + */ + public void stopPlaying() { + bow.stopPlaying(); + playing = false; + } + + + /** + * Savoir si il est entrain de jouer. + */ + public boolean isPlaying() { + return playing; + } + } diff --git a/core/src/sagittarius/view/GameScreen.java b/core/src/sagittarius/view/GameScreen.java index 401dd12..b36d7ca 100644 --- a/core/src/sagittarius/view/GameScreen.java +++ b/core/src/sagittarius/view/GameScreen.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; +import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Group; import sagittarius.model.*; @@ -20,6 +21,9 @@ public class GameScreen extends BaseScreen { // Groups // TODO: move this in SagittariusGame ? public static Group attractors; public static Group arrows; + public static Group players; + + private int playerTurn; // ---------- METHODs ---------- @@ -41,11 +45,17 @@ public class GameScreen extends BaseScreen { mainStage.addActor(attractors); // players + players = new Group(); + Player player1 = new Player(planet1, Color.WHITE); - mainStage.addActor(player1); + players.addActor(player1); Player player2 = new Player(planet2, Color.WHITE); - mainStage.addActor(player2); + players.addActor(player2); + + mainStage.addActor(players); + playerTurn = 0; + player1.startPlaying(); // arrows arrows = new Group(); @@ -59,7 +69,6 @@ public class GameScreen extends BaseScreen { uiStage.addActor(mouseInfo); mainStage.setDebugAll(true); // TODO: disable later - uiStage.setDebugAll(true); // TODO: disable later } @@ -70,6 +79,31 @@ public class GameScreen extends BaseScreen { unprojectedCursor = mainStage.getCamera().unproject(new Vector3(screenCursor, 0)); worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y); + Player actualPlayer = (Player) players.getChild(playerTurn); + if (actualPlayer.isPlaying() && !actualPlayer.getBow().isPlaying()) { + actualPlayer.stopPlaying(); + playerTurn++; + playerTurn %= players.getChildren().size; + actualPlayer = (Player) players.getChild(playerTurn); + actualPlayer.startPlaying(); + } + } -} + + public void removePlayer(Player player) { + int index = players.getChildren().indexOf(player, true); + if (index < playerTurn) { + players.removeActor(player); + playerTurn++; + playerTurn %= players.getChildren().size; + } else if (index == playerTurn) { + players.removeActor(player); + playerTurn %= players.getChildren().size; + Player actualPlayer = (Player) players.getChild(playerTurn); + actualPlayer.startPlaying(); + } else { + players.removeActor(player); + } + } +} \ No newline at end of file