projet-programmation-orient.../core/src/sagittarius/view/GameScreen.java
2021-04-23 17:32:17 +02:00

167 lines
4.8 KiB
Java

package sagittarius.view;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Group;
import sagittarius.SagittariusGame;
import sagittarius.model.*;
public class GameScreen extends BaseScreen {
// ---------- ATTRIBUTEs ----------
public static BitmapFont fontDebug = new BitmapFont();
// Cursors
public static Vector2 screenCursor;
private static Vector3 unprojectedCursor;
public static Vector2 worldCursor;
// Groups // TODO: move this in SagittariusGame ?
public static Group attractors;
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
public void initialize() {
Gdx.input.setInputProcessor(null); // on a pas de boutons
// planets & moons
attractors = new Group();
Planet planet1 = new Planet(new Vector2(300, 200), 1000, 50, Color.BLUE);
attractors.addActor(planet1);
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);
attractors.addActor(moon2_1);
Planet planet3 = new Planet(new Vector2(1500, 100), 1000, 70, Color.PINK);
attractors.addActor(planet3);
mainStage.addActor(attractors);
// players
players = new Group();
Player player1 = new Player(planet1, Color.RED);
players.addActor(player1);
Player player2 = new Player(planet2, Color.WHITE);
players.addActor(player2);
Player player3 = new Player(moon2_1, Color.YELLOW);
players.addActor(player3);
mainStage.addActor(players);
// arrows
arrows = new Group();
mainStage.addActor(arrows);
// The one and only Bow
Bow bow = new Bow(true);
mainStage.addActor(bow);
// others
FPS fpsCounter = new FPS(uiStage);
uiStage.addActor(fpsCounter);
MouseInfo mouseInfo = new MouseInfo();
uiStage.addActor(mouseInfo);
mainStage.setDebugAll(SagittariusGame.debugMode);
uiStage.setDebugAll(SagittariusGame.debugMode);
// game turns
playerIndex = 0;
player1.setActive(true);
playerCurrent = player1;
// camera stuff
mainCameraPosition = mainStage.getCamera().position;
focus = playerCurrent;
}
@Override
public void update(float dt) {
screenCursor = new Vector2(Gdx.input.getX(), Gdx.input.getY()); // utiliser les trucs fournis par libGDX
unprojectedCursor = mainStage.getCamera().unproject(new Vector3(screenCursor, 0));
worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y);
if (players.getChildren().size <= 1) {
SagittariusGame.setActiveScreen( new StartScreen() );
}
// camera follow focus
mainCameraPosition.scl(ispeed);
mainCameraPosition.add(new Vector3(focus.getPosition(), 0).scl(speed));
if (Gdx.input.isButtonPressed(Buttons.LEFT)) {
((OrthographicCamera) mainStage.getCamera()).zoom = 2f;
} else {
((OrthographicCamera) mainStage.getCamera()).zoom = 1f;
}
}
/**
* Removes a Player from the Game.
*
* @param player Player to remove from the active list.
*/
public static void removePlayer(Player player) {
player.setActive(false);
int index = players.getChildren().indexOf(player, true);
if (index < playerIndex) {
players.removeActor(player);
playerIndex++;
playerIndex %= players.getChildren().size;
} else if (index == playerIndex) {
players.removeActor(player);
playerIndex %= players.getChildren().size;
playerCurrent = (Player) players.getChild(playerIndex);
playerCurrent.setActive(true);
} else {
players.removeActor(player);
}
}
/**
* Used to allows the next Player in the list to play.
*/
public static void nextPlayer() {
playerCurrent.setActive(false);
playerIndex++;
playerIndex %= players.getChildren().size;
playerCurrent = (Player) players.getChild(playerIndex);
playerCurrent.setActive(true);
}
public static void setFocus(Entity entity) {
focus = entity;
}
}