2021-04-08 19:34:17 +00:00
|
|
|
package sagittarius.view;
|
2021-04-05 16:45:57 +00:00
|
|
|
|
2021-04-09 13:03:24 +00:00
|
|
|
import com.badlogic.gdx.Gdx;
|
2021-05-03 13:16:10 +00:00
|
|
|
import com.badlogic.gdx.InputProcessor;
|
|
|
|
import com.badlogic.gdx.Input.Keys;
|
2021-04-05 16:45:57 +00:00
|
|
|
import com.badlogic.gdx.graphics.Color;
|
2021-04-23 15:32:17 +00:00
|
|
|
import com.badlogic.gdx.graphics.OrthographicCamera;
|
2021-04-22 15:53:00 +00:00
|
|
|
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
2021-05-03 13:16:10 +00:00
|
|
|
import com.badlogic.gdx.math.MathUtils;
|
2021-04-09 09:57:52 +00:00
|
|
|
import com.badlogic.gdx.math.Vector2;
|
2021-04-09 13:03:24 +00:00
|
|
|
import com.badlogic.gdx.math.Vector3;
|
2021-05-29 10:21:10 +00:00
|
|
|
import com.badlogic.gdx.scenes.scene2d.Actor;
|
2021-04-09 13:49:33 +00:00
|
|
|
import com.badlogic.gdx.scenes.scene2d.Group;
|
2021-04-05 16:45:57 +00:00
|
|
|
|
2021-04-16 11:57:02 +00:00
|
|
|
import sagittarius.SagittariusGame;
|
2021-04-09 09:57:52 +00:00
|
|
|
import sagittarius.model.*;
|
2021-04-05 16:45:57 +00:00
|
|
|
|
2021-05-03 13:16:10 +00:00
|
|
|
public class GameScreen extends BaseScreen implements InputProcessor {
|
2021-04-05 16:45:57 +00:00
|
|
|
|
2021-05-29 10:21:10 +00:00
|
|
|
// ---------- GENERATION ----------
|
|
|
|
|
2021-05-29 10:27:01 +00:00
|
|
|
public static int minDistance = 600;
|
|
|
|
public static int maxDistance = 900;
|
2021-05-29 10:21:10 +00:00
|
|
|
public static int minMass = 500;
|
|
|
|
public static int maxMass = 1000;
|
|
|
|
public static int minRadius = 20;
|
|
|
|
public static int maxRadius = 150;
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-27 09:04:00 +00:00
|
|
|
// ---------- ATTRIBUTEs ----------
|
2021-04-05 16:45:57 +00:00
|
|
|
|
2021-04-22 15:53:00 +00:00
|
|
|
public static BitmapFont fontDebug = new BitmapFont();
|
|
|
|
|
2021-04-09 13:03:24 +00:00
|
|
|
// Cursors
|
|
|
|
public static Vector2 screenCursor;
|
|
|
|
private static Vector3 unprojectedCursor;
|
2021-05-27 09:04:00 +00:00
|
|
|
public static Vector2 worldCursor;
|
2021-04-09 13:03:24 +00:00
|
|
|
|
2021-05-13 18:18:48 +00:00
|
|
|
// Groups
|
2021-04-12 18:45:09 +00:00
|
|
|
public static Group attractors;
|
2021-04-09 14:45:43 +00:00
|
|
|
public static Group arrows;
|
2021-05-13 18:18:48 +00:00
|
|
|
public static Group players; // TODO: move this in SagittariusGame ?
|
2021-04-13 17:45:49 +00:00
|
|
|
|
2021-04-23 15:03:40 +00:00
|
|
|
// turn system stuff
|
2021-04-23 13:53:10 +00:00
|
|
|
public static int playerIndex;
|
|
|
|
public static Player playerCurrent;
|
2021-04-09 14:28:37 +00:00
|
|
|
|
2021-04-23 15:03:40 +00:00
|
|
|
// camera stuff
|
|
|
|
private Vector3 mainCameraPosition;
|
|
|
|
private final float speed = 0.05f;
|
|
|
|
private final float ispeed = 1.0f - speed;
|
|
|
|
private static Entity focus;
|
|
|
|
|
2021-05-03 13:16:10 +00:00
|
|
|
// test
|
|
|
|
private OrthographicCamera gameCam;
|
|
|
|
|
2021-05-27 09:04:00 +00:00
|
|
|
// ---------- METHODs ----------
|
2021-04-05 16:45:57 +00:00
|
|
|
|
|
|
|
@Override
|
2021-04-09 09:57:52 +00:00
|
|
|
public void initialize() {
|
|
|
|
|
2021-05-27 14:11:14 +00:00
|
|
|
if (SagittariusGame.music != null) {
|
2021-05-27 09:04:00 +00:00
|
|
|
SagittariusGame.music.stop();
|
2021-05-27 14:11:14 +00:00
|
|
|
} SagittariusGame.music =
|
|
|
|
Gdx.audio.newMusic(Gdx.files.internal("sounds/music/game_music.mp3"));
|
|
|
|
SagittariusGame.music.setLooping(true);
|
|
|
|
SagittariusGame.music.setVolume(SagittariusGame.musicVolume);
|
|
|
|
if (!SagittariusGame.disableMusic) {
|
2021-05-27 09:04:00 +00:00
|
|
|
SagittariusGame.music.play();
|
2021-05-13 20:39:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 13:16:10 +00:00
|
|
|
Gdx.input.setInputProcessor(this);
|
2021-04-23 15:32:17 +00:00
|
|
|
|
2021-05-27 14:11:14 +00:00
|
|
|
// The one and only Bow
|
|
|
|
Bow bow = new Bow(true);
|
|
|
|
bow.setDebug(true);
|
|
|
|
mainStage.addActor(bow);
|
|
|
|
|
2021-05-12 17:06:26 +00:00
|
|
|
// arrows
|
|
|
|
arrows = new Group();
|
|
|
|
mainStage.addActor(arrows);
|
|
|
|
|
2021-04-12 18:45:09 +00:00
|
|
|
// planets & moons
|
|
|
|
attractors = new Group();
|
2021-04-13 19:24:36 +00:00
|
|
|
|
2021-05-29 10:43:24 +00:00
|
|
|
for (int i = 0; i < SagittariusGame.numberPlanets; i++) {
|
2021-05-29 10:41:40 +00:00
|
|
|
// Position of the new planet
|
2021-05-29 10:21:10 +00:00
|
|
|
Vector2 position;
|
|
|
|
if (i == 0) {
|
|
|
|
position = new Vector2(0, 0);
|
|
|
|
} else {
|
|
|
|
int distance = minDistance + MathUtils.random(maxDistance - minDistance);
|
|
|
|
float angle = 2*MathUtils.PI*MathUtils.random();
|
|
|
|
float shortestDistance;
|
|
|
|
do {
|
|
|
|
int index = MathUtils.random(i-1);
|
|
|
|
Actor planet0 = attractors.getChild(index);
|
|
|
|
position = new Vector2(distance * (float) Math.cos(angle), distance * (float) Math.sin(angle));
|
|
|
|
position.add(planet0.getX(), planet0.getY());
|
|
|
|
|
|
|
|
shortestDistance = maxDistance;
|
|
|
|
for (int j = 0; j < i-1; j++) {
|
|
|
|
Actor planet1 = attractors.getChild(j);
|
|
|
|
float dx = planet1.getX() - position.x;
|
|
|
|
float dy = planet1.getY() - position.y;
|
|
|
|
float distanceToPlanet1 = (float) Math.sqrt(dx*dx + dy*dy);
|
|
|
|
if (distanceToPlanet1 < shortestDistance) {
|
|
|
|
shortestDistance = distanceToPlanet1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (shortestDistance < minDistance);
|
|
|
|
}
|
2021-05-29 10:41:40 +00:00
|
|
|
|
|
|
|
// other attributs
|
2021-05-29 10:21:10 +00:00
|
|
|
int mass = minMass + MathUtils.random(maxMass - minMass);
|
|
|
|
int radius = minRadius + MathUtils.random(maxRadius - minRadius);
|
2021-05-29 10:41:40 +00:00
|
|
|
Color color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random(), 1);
|
|
|
|
|
|
|
|
// create the planet
|
2021-05-29 10:21:10 +00:00
|
|
|
Planet planet = new Planet(position, mass, radius, color);
|
|
|
|
attractors.addActor(planet);
|
|
|
|
}
|
2021-04-23 14:17:09 +00:00
|
|
|
|
2021-05-29 10:41:40 +00:00
|
|
|
// create a moon
|
2021-05-29 10:27:01 +00:00
|
|
|
Moon moon = new Moon(
|
2021-05-29 10:43:24 +00:00
|
|
|
(Planet) attractors.getChild( MathUtils.random(SagittariusGame.numberPlanets-1)),
|
2021-05-29 10:27:01 +00:00
|
|
|
minMass / 2 + MathUtils.random(maxMass - minMass) / 2,
|
|
|
|
minRadius / 2 + MathUtils.random(maxRadius - minRadius) / 2,
|
|
|
|
minDistance / 2,
|
2021-05-29 10:41:40 +00:00
|
|
|
new Color((float) Math.random(), (float) Math.random(), (float) Math.random(), 1)
|
2021-05-29 10:27:01 +00:00
|
|
|
);
|
|
|
|
attractors.addActor(moon);
|
|
|
|
|
2021-04-12 18:45:09 +00:00
|
|
|
mainStage.addActor(attractors);
|
2021-04-09 13:49:33 +00:00
|
|
|
|
|
|
|
// players
|
2021-04-13 17:45:49 +00:00
|
|
|
players = new Group();
|
|
|
|
|
2021-05-29 10:41:40 +00:00
|
|
|
for (int i = 0; i < SagittariusGame.numberPlayers; i++) {
|
|
|
|
Player player = new Player((Planet) attractors.getChild(i), new Color((float) Math.random(), (float) Math.random(), (float) Math.random(), 1));
|
|
|
|
players.addActor(player);
|
|
|
|
}
|
2021-04-23 14:17:09 +00:00
|
|
|
|
2021-04-13 17:45:49 +00:00
|
|
|
mainStage.addActor(players);
|
2021-04-13 16:43:41 +00:00
|
|
|
|
2021-04-09 14:45:43 +00:00
|
|
|
// others
|
2021-04-13 19:08:25 +00:00
|
|
|
FPS fpsCounter = new FPS(uiStage);
|
2021-04-09 13:03:24 +00:00
|
|
|
uiStage.addActor(fpsCounter);
|
|
|
|
|
|
|
|
MouseInfo mouseInfo = new MouseInfo();
|
|
|
|
uiStage.addActor(mouseInfo);
|
2021-04-09 13:10:39 +00:00
|
|
|
|
2021-05-12 17:00:10 +00:00
|
|
|
mainStage.setDebugAll(SagittariusGame.debugMode);
|
|
|
|
uiStage.setDebugAll(SagittariusGame.debugMode);
|
2021-04-10 10:22:48 +00:00
|
|
|
|
2021-04-13 19:55:14 +00:00
|
|
|
// game turns
|
2021-04-23 13:53:10 +00:00
|
|
|
playerIndex = 0;
|
2021-05-29 10:41:40 +00:00
|
|
|
((Player) players.getChild(0)).setActive(true);
|
|
|
|
playerCurrent = (Player) players.getChild(0);
|
2021-04-13 19:55:14 +00:00
|
|
|
|
2021-04-23 15:03:40 +00:00
|
|
|
// camera stuff
|
|
|
|
mainCameraPosition = mainStage.getCamera().position;
|
|
|
|
focus = playerCurrent;
|
2021-05-03 13:16:10 +00:00
|
|
|
gameCam = ((OrthographicCamera) mainStage.getCamera());
|
2021-04-23 15:03:40 +00:00
|
|
|
|
2021-04-05 16:45:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-09 09:57:52 +00:00
|
|
|
public void update(float dt) {
|
2021-04-09 13:03:24 +00:00
|
|
|
|
2021-05-27 14:11:14 +00:00
|
|
|
screenCursor = new Vector2(Gdx.input.getX(), Gdx.input.getY());
|
2021-05-27 09:04:00 +00:00
|
|
|
unprojectedCursor = mainStage.getCamera().unproject(new Vector3(screenCursor, 0));
|
|
|
|
worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y);
|
2021-04-09 13:03:24 +00:00
|
|
|
|
2021-04-23 13:53:10 +00:00
|
|
|
if (players.getChildren().size <= 1) {
|
2021-05-29 09:10:54 +00:00
|
|
|
SagittariusGame.setActiveScreen(new EndScreen());
|
2021-04-23 13:53:10 +00:00
|
|
|
}
|
2021-04-13 17:45:49 +00:00
|
|
|
|
2021-05-03 13:16:10 +00:00
|
|
|
// camera zoom using keys
|
2021-05-27 09:07:49 +00:00
|
|
|
if (Gdx.input.isKeyPressed(SagittariusGame.zoomOutKey)) {
|
2021-05-03 13:16:10 +00:00
|
|
|
gameCam.zoom += dt;
|
|
|
|
}
|
2021-05-27 09:07:49 +00:00
|
|
|
if (Gdx.input.isKeyPressed(SagittariusGame.zoomInKey)) {
|
2021-05-03 13:16:10 +00:00
|
|
|
gameCam.zoom -= dt;
|
|
|
|
}
|
2021-05-27 09:04:00 +00:00
|
|
|
// clamp zoom
|
2021-05-27 08:51:03 +00:00
|
|
|
gameCam.zoom = MathUtils.clamp(gameCam.zoom, 1f, 3f);
|
2021-05-03 13:16:10 +00:00
|
|
|
|
2021-05-27 09:04:00 +00:00
|
|
|
// Pause Menu
|
2021-05-27 08:51:03 +00:00
|
|
|
if (Gdx.input.isKeyPressed(Keys.ESCAPE)) {
|
2021-05-27 09:04:00 +00:00
|
|
|
SagittariusGame.setActiveScreen(new ResumeScreen(this));
|
2021-05-13 21:14:05 +00:00
|
|
|
}
|
2021-05-03 13:16:10 +00:00
|
|
|
|
2021-04-23 15:03:40 +00:00
|
|
|
// camera follow focus
|
|
|
|
mainCameraPosition.scl(ispeed);
|
|
|
|
mainCameraPosition.add(new Vector3(focus.getPosition(), 0).scl(speed));
|
2021-04-05 16:45:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 12:30:25 +00:00
|
|
|
/**
|
2021-04-23 15:03:40 +00:00
|
|
|
* Removes a Player from the Game.
|
|
|
|
*
|
|
|
|
* @param player Player to remove from the active list.
|
2021-04-16 12:30:25 +00:00
|
|
|
*/
|
|
|
|
public static void removePlayer(Player player) {
|
2021-04-23 13:53:10 +00:00
|
|
|
player.setActive(false);
|
2021-04-16 12:30:25 +00:00
|
|
|
int index = players.getChildren().indexOf(player, true);
|
2021-04-23 13:53:10 +00:00
|
|
|
if (index < playerIndex) {
|
2021-04-16 12:30:25 +00:00
|
|
|
players.removeActor(player);
|
2021-04-23 14:26:17 +00:00
|
|
|
playerIndex++;
|
2021-04-23 13:53:10 +00:00
|
|
|
playerIndex %= players.getChildren().size;
|
|
|
|
} else if (index == playerIndex) {
|
2021-04-16 12:30:25 +00:00
|
|
|
players.removeActor(player);
|
2021-04-23 13:53:10 +00:00
|
|
|
playerIndex %= players.getChildren().size;
|
|
|
|
playerCurrent = (Player) players.getChild(playerIndex);
|
|
|
|
playerCurrent.setActive(true);
|
2021-04-16 12:30:25 +00:00
|
|
|
} else {
|
|
|
|
players.removeActor(player);
|
2021-04-13 17:45:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-16 11:43:59 +00:00
|
|
|
|
2021-04-23 15:03:40 +00:00
|
|
|
/**
|
|
|
|
* Used to allows the next Player in the list to play.
|
|
|
|
*/
|
2021-04-16 12:30:25 +00:00
|
|
|
public static void nextPlayer() {
|
2021-04-23 13:53:10 +00:00
|
|
|
playerCurrent.setActive(false);
|
|
|
|
playerIndex++;
|
|
|
|
playerIndex %= players.getChildren().size;
|
|
|
|
playerCurrent = (Player) players.getChild(playerIndex);
|
|
|
|
playerCurrent.setActive(true);
|
2021-04-16 12:30:25 +00:00
|
|
|
}
|
2021-04-23 15:03:40 +00:00
|
|
|
|
|
|
|
public static void setFocus(Entity entity) {
|
|
|
|
focus = entity;
|
|
|
|
}
|
2021-05-03 13:16:10 +00:00
|
|
|
|
2021-05-27 09:04:00 +00:00
|
|
|
// ---------- InputProcessor METHODs ----------
|
2021-05-03 13:16:10 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean keyDown(int keycode) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean keyUp(int keycode) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean keyTyped(char character) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean touchDragged(int screenX, int screenY, int pointer) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean mouseMoved(int screenX, int screenY) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean scrolled(float amountX, float amountY) {
|
|
|
|
gameCam.zoom += 2 * amountY * Gdx.graphics.getDeltaTime();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|