From 939ceaed4ae07c6798c665d0b159bee8f7043b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Fri, 23 Apr 2021 17:03:40 +0200 Subject: [PATCH] feat!: camera now follows arrows and players --- core/src/sagittarius/SagittariusGame.java | 7 ++--- core/src/sagittarius/model/Arrow.java | 3 +++ core/src/sagittarius/model/Bow.java | 8 +++--- core/src/sagittarius/view/GameScreen.java | 31 ++++++++++++++++++++--- core/src/sagittarius/view/MouseInfo.java | 5 ++-- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/core/src/sagittarius/SagittariusGame.java b/core/src/sagittarius/SagittariusGame.java index 1c8195b..f07989a 100644 --- a/core/src/sagittarius/SagittariusGame.java +++ b/core/src/sagittarius/SagittariusGame.java @@ -3,10 +3,7 @@ package sagittarius; import com.badlogic.gdx.Game; import com.kotcrab.vis.ui.VisUI; -import sagittarius.view.BaseScreen; -import sagittarius.view.GameScreen; -import sagittarius.view.SettingsScreen; -import sagittarius.view.StartScreen; +import sagittarius.view.*; public class SagittariusGame extends Game { @@ -31,7 +28,7 @@ public class SagittariusGame extends Game { @Override public void create() { VisUI.load(); - game.setScreen(new GameScreen()); + game.setScreen(new StartScreen()); } } diff --git a/core/src/sagittarius/model/Arrow.java b/core/src/sagittarius/model/Arrow.java index 0d5fdb2..f2ece14 100644 --- a/core/src/sagittarius/model/Arrow.java +++ b/core/src/sagittarius/model/Arrow.java @@ -59,6 +59,7 @@ public class Arrow extends EntityQuad { if (TTL <= 0) { GameScreen.arrows.removeActor(this); GameScreen.nextPlayer(); + GameScreen.setFocus(GameScreen.playerCurrent); } } else { this.setPosition(crash.getPosition().cpy().add(offset)); @@ -123,6 +124,7 @@ public class Arrow extends EntityQuad { this.crash = planet; this.offset = this.getPosition().sub( planet.getPosition() ); GameScreen.nextPlayer(); + GameScreen.setFocus(GameScreen.playerCurrent); break; } } @@ -149,6 +151,7 @@ public class Arrow extends EntityQuad { GameScreen.removePlayer(player); GameScreen.arrows.removeActor(this); GameScreen.nextPlayer(); + GameScreen.setFocus(GameScreen.playerCurrent); break; } } diff --git a/core/src/sagittarius/model/Bow.java b/core/src/sagittarius/model/Bow.java index 2f52ebf..82cb030 100644 --- a/core/src/sagittarius/model/Bow.java +++ b/core/src/sagittarius/model/Bow.java @@ -35,17 +35,19 @@ public class Bow extends Actor { @Override public void act(float dt) { super.act(dt); - if (GameScreen.playerCurrent.isActive() && Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) { + if (GameScreen.playerCurrent.isActive() && Gdx.input.isButtonJustPressed(Buttons.RIGHT) && !pressed) { this.anchor = GameScreen.worldCursor.cpy(); pressed = true; - } else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) { + } else if (Gdx.input.isButtonPressed(Buttons.RIGHT) && pressed) { aim = this.anchor.cpy().sub(GameScreen.worldCursor); angle = aim.angleDeg(); power = MathUtils.clamp(aim.len(), 0, 1000); } else if (pressed && power > 50) { pressed = false; GameScreen.playerCurrent.setActive(false); - GameScreen.arrows.addActor(new Arrow(angle, power, GameScreen.playerCurrent)); + Arrow arrowShot = new Arrow(angle, power, GameScreen.playerCurrent); + GameScreen.setFocus(arrowShot); // do not use constructor 2 times + GameScreen.arrows.addActor(arrowShot); } else { pressed = false; } diff --git a/core/src/sagittarius/view/GameScreen.java b/core/src/sagittarius/view/GameScreen.java index ebf9a7c..e9231e4 100644 --- a/core/src/sagittarius/view/GameScreen.java +++ b/core/src/sagittarius/view/GameScreen.java @@ -7,6 +7,8 @@ import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.scenes.scene2d.Group; +import org.omg.CORBA.Current; + import sagittarius.SagittariusGame; import sagittarius.model.*; @@ -26,9 +28,16 @@ public class GameScreen extends BaseScreen { public static Group arrows; public static Group players; + // turn system stuff public static int playerIndex; public static Player playerCurrent; + // camera stuff + private Vector3 mainCameraPosition; + private final float speed = 0.05f; + private final float ispeed = 1.0f - speed; + private static Entity focus; + // ---------- METHODs ---------- @Override @@ -40,7 +49,7 @@ public class GameScreen extends BaseScreen { Planet planet1 = new Planet(new Vector2(300, 200), 1000, 50, Color.BLUE); attractors.addActor(planet1); - Planet planet2 = new Planet(new Vector2(1200, 700), 4000, 200, Color.ORANGE); + Planet planet2 = new Planet(new Vector2(1200, 700), 3000, 200, Color.ORANGE); attractors.addActor(planet2); Moon moon2_1 = new Moon(planet2, 100, 70, 500, Color.MAGENTA); @@ -88,6 +97,10 @@ public class GameScreen extends BaseScreen { player1.setActive(true); playerCurrent = player1; + // camera stuff + mainCameraPosition = mainStage.getCamera().position; + focus = playerCurrent; + } @Override @@ -101,11 +114,16 @@ public class GameScreen extends BaseScreen { SagittariusGame.setActiveScreen( new StartScreen() ); } + // camera follow focus + mainCameraPosition.scl(ispeed); + mainCameraPosition.add(new Vector3(focus.getPosition(), 0).scl(speed)); + } /** - * TODO - * @param player + * Removes a Player from the Game. + * + * @param player Player to remove from the active list. */ public static void removePlayer(Player player) { player.setActive(false); @@ -124,6 +142,9 @@ public class GameScreen extends BaseScreen { } } + /** + * Used to allows the next Player in the list to play. + */ public static void nextPlayer() { playerCurrent.setActive(false); playerIndex++; @@ -131,4 +152,8 @@ public class GameScreen extends BaseScreen { playerCurrent = (Player) players.getChild(playerIndex); playerCurrent.setActive(true); } + + public static void setFocus(Entity entity) { + focus = entity; + } } \ No newline at end of file diff --git a/core/src/sagittarius/view/MouseInfo.java b/core/src/sagittarius/view/MouseInfo.java index aa4cba8..221f1e1 100644 --- a/core/src/sagittarius/view/MouseInfo.java +++ b/core/src/sagittarius/view/MouseInfo.java @@ -1,5 +1,6 @@ package sagittarius.view; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.scenes.scene2d.Actor; @@ -10,8 +11,8 @@ public class MouseInfo extends Actor { @Override public void act(float dt) { super.act(dt); - setX(GameScreen.worldCursor.x); // use direct method - setY(GameScreen.worldCursor.y); + setX(GameScreen.screenCursor.x); + setY(Gdx.graphics.getHeight() - GameScreen.screenCursor.y); } @Override