Refining multiplayer: wait screen for clients to join the server. TODO: Fix name stuff and multiple players from the same machine
parent
5b512c77bb
commit
01f38a09cb
|
@ -19,6 +19,8 @@ public class GameManager {
|
|||
|
||||
public boolean gameStarted = false;
|
||||
public boolean showGame = true;
|
||||
public boolean anyoneWon = false;
|
||||
|
||||
Random rand = new Random();
|
||||
|
||||
GameType type = GameType.LOCAL;
|
||||
|
@ -32,12 +34,12 @@ public class GameManager {
|
|||
|
||||
type = t;
|
||||
setShowGame(type != GameType.SERVER);
|
||||
|
||||
|
||||
mazeGen = new MazeGenerator(main, MazeSettings.MAZEX, MazeSettings.MAZEZ);
|
||||
}
|
||||
|
||||
boolean anyoneWon = false;
|
||||
ArrayList<MazePlayer> toDelete = new ArrayList<MazePlayer>();
|
||||
|
||||
public void generateMaze(Set<MazePlayer> pl, int todraw[][]) {
|
||||
main.setScreen(null);
|
||||
anyoneWon = false;
|
||||
|
@ -89,7 +91,7 @@ public class GameManager {
|
|||
}
|
||||
|
||||
public void update() {
|
||||
if (gameStarted) {
|
||||
if (gameStarted && !anyoneWon) {
|
||||
main.world.cam.position.set(mazeGen.w / 2, (MazeSettings.MAZEX + MazeSettings.MAZEZ) * 0.45f,
|
||||
mazeGen.h / 2);
|
||||
main.world.cam.lookAt(MazeSettings.MAZEX / 2, 0, MazeSettings.MAZEX / 2);
|
||||
|
@ -104,7 +106,7 @@ public class GameManager {
|
|||
p.render(main.world.modelBatch, main.world.environment);
|
||||
|
||||
anyoneWon = false;
|
||||
if(type != GameType.CLIENT) {
|
||||
if (type != GameType.CLIENT) {
|
||||
if (checkWin(p)) {
|
||||
anyoneWon = true;
|
||||
gameStarted = false;
|
||||
|
@ -114,21 +116,27 @@ public class GameManager {
|
|||
}
|
||||
}
|
||||
|
||||
if (anyoneWon)
|
||||
main.setScreen(main.uiManager.playersScreen);
|
||||
if (anyoneWon) {
|
||||
System.out.println("Game Finished! " + type);
|
||||
if (type == GameType.LOCAL) {
|
||||
main.setScreen(main.uiManager.playersScreen);
|
||||
}else if(type == GameType.SERVER) {
|
||||
main.uiManager.preGameScreen.setGameType(GameType.SERVER);
|
||||
main.setScreen(main.uiManager.preGameScreen);
|
||||
}
|
||||
}
|
||||
main.world.modelBatch.end();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void spreadPlayers() {
|
||||
for (MazePlayer p : players) {
|
||||
int x = 1, z = 1;
|
||||
do {
|
||||
x = (Math.abs(rand.nextInt() - 1) % (mazeGen.w));
|
||||
z = (Math.abs(rand.nextInt() - 1) % (mazeGen.h));
|
||||
System.out.println(thereIsPlayerInPos(x, z) + " - " + mazeGen.occupiedSpot(x,
|
||||
z) + " --- " + x + ", " + z);
|
||||
System.out.println(
|
||||
thereIsPlayerInPos(x, z) + " - " + mazeGen.occupiedSpot(x, z) + " --- " + x + ", " + z);
|
||||
} while (thereIsPlayerInPos(x, z) || mazeGen.occupiedSpot(x, z));
|
||||
p.setPlaying();
|
||||
p.setPos(x + 0.5f, 2f, z + 0.5f);
|
||||
|
@ -152,7 +160,6 @@ public class GameManager {
|
|||
return new Player(kup, kdown, ksx, kdx, x + 0.5f, 4f, z + 0.5f, name);
|
||||
}
|
||||
|
||||
|
||||
public boolean checkWin(MazePlayer p) {
|
||||
if ((int) p.getPos().x == mazeGen.WINX && (int) p.getPos().z == mazeGen.WINZ) {
|
||||
System.out.println(p.getName() + " won");
|
||||
|
|
|
@ -2,7 +2,6 @@ package com.emamaker.amazeing.manager.network;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Hashtable;
|
||||
|
||||
|
@ -13,6 +12,7 @@ import com.emamaker.amazeing.AMazeIng;
|
|||
import com.emamaker.amazeing.manager.GameManager;
|
||||
import com.emamaker.amazeing.manager.GameType;
|
||||
import com.emamaker.amazeing.manager.network.NetworkCommon.AddNewPlayer;
|
||||
import com.emamaker.amazeing.manager.network.NetworkCommon.EndGame;
|
||||
import com.emamaker.amazeing.manager.network.NetworkCommon.LoginAO;
|
||||
import com.emamaker.amazeing.manager.network.NetworkCommon.LoginAO2;
|
||||
import com.emamaker.amazeing.manager.network.NetworkCommon.RemovePlayer;
|
||||
|
@ -35,6 +35,7 @@ public class GameClient {
|
|||
public int port;
|
||||
|
||||
boolean startGame = false;
|
||||
boolean showPreGame = false;
|
||||
String map = "";
|
||||
|
||||
// Hashtable of remote players present in the match. This will be used to update
|
||||
|
@ -42,13 +43,12 @@ public class GameClient {
|
|||
Hashtable<String, MazePlayerRemote> remotePlayers = new Hashtable<>();
|
||||
// Hashtable<String, MazePlayerLocal> localPlayers = new Hashtable<>();
|
||||
MazePlayerLocal player;
|
||||
|
||||
ArrayList<MazePlayer> players = new ArrayList<MazePlayer>();
|
||||
public ArrayList<MazePlayer> players = new ArrayList<MazePlayer>();
|
||||
|
||||
volatile HashSet<String> toAdd = new HashSet<>();
|
||||
volatile HashSet<String> toRemove = new HashSet<>();
|
||||
|
||||
GameManager gameManager;
|
||||
public GameManager gameManager;
|
||||
Client client;
|
||||
// UUID is represented using a string, for kryonet ease of use
|
||||
String uuid = "";
|
||||
|
@ -80,6 +80,11 @@ public class GameClient {
|
|||
uuid = ((LoginAO2) object).uuid;
|
||||
client.sendTCP(object);
|
||||
System.out.println("Received UUID " + uuid.toString() + " from server, giving confirmation!");
|
||||
|
||||
// When we receive the connection accept from the server, we can show the
|
||||
// pre-game screen listing the players' names, setting this flag to let the main
|
||||
// thread to it
|
||||
showPreGame = true;
|
||||
} else if (object instanceof AddNewPlayer) {
|
||||
AddNewPlayer msg = (AddNewPlayer) object;
|
||||
if ((!msg.uuid.equals(uuid))) {
|
||||
|
@ -110,8 +115,8 @@ public class GameClient {
|
|||
remotePlayers.get(msg.uuid).setPlaying();
|
||||
remotePlayers.get(msg.uuid).setTransform(msg.tx, msg.ty, msg.tz, msg.rx, msg.ry, msg.rz,
|
||||
msg.rw);
|
||||
System.out.println("R: " + msg.tx + ", " + msg.ty + ", " + msg.tz);
|
||||
System.out.println("Updating remote player with uuid " + msg.uuid.toString());
|
||||
// System.out.println("R: " + msg.tx + ", " + msg.ty + ", " + msg.tz);
|
||||
// System.out.println("Updating remote player with uuid " + msg.uuid.toString());
|
||||
}
|
||||
} else if (object instanceof UpdateMap) {
|
||||
map = ((UpdateMap) object).map;
|
||||
|
@ -120,6 +125,13 @@ public class GameClient {
|
|||
startGame = true;
|
||||
map = ((StartGame) object).map;
|
||||
System.out.println("Starting the online game!");
|
||||
} else if (object instanceof EndGame) {
|
||||
System.out.println("EndGame Received!");
|
||||
if (gameManager != null) {
|
||||
gameManager.gameStarted = false;
|
||||
gameManager.anyoneWon = true;
|
||||
showPreGame = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -164,17 +176,29 @@ public class GameClient {
|
|||
|
||||
}
|
||||
|
||||
for (MazePlayerRemote p : remotePlayers.values())
|
||||
if (!players.contains(p))
|
||||
players.add(p);
|
||||
|
||||
// for (MazePlayerLocal p : localPlayers.values())
|
||||
// if (!players.contains(p))
|
||||
// players.add(p);
|
||||
if (!players.contains(player))
|
||||
players.add(player);
|
||||
|
||||
if (showPreGame) {
|
||||
// We are taking care of specifying what type of game we are running. Server is
|
||||
// the server is running in the same instance, client if not
|
||||
// In this way server host is shown the start game button.
|
||||
if (!main.server.isRunning())
|
||||
main.uiManager.preGameScreen.setGameType(GameType.CLIENT);
|
||||
main.setScreen(main.uiManager.preGameScreen);
|
||||
System.out.println("Game ended!");
|
||||
showPreGame = false;
|
||||
}
|
||||
|
||||
if (startGame) {
|
||||
gameManager = new GameManager(main, GameType.CLIENT);
|
||||
for (MazePlayerRemote p : remotePlayers.values())
|
||||
if (!players.contains(p))
|
||||
players.add(p);
|
||||
|
||||
// for (MazePlayerLocal p : localPlayers.values())
|
||||
// if (!players.contains(p))
|
||||
// players.add(p);
|
||||
|
||||
players.add(player);
|
||||
|
||||
if (main.getScreen() != null) {
|
||||
main.getScreen().hide();
|
||||
|
@ -183,7 +207,6 @@ public class GameClient {
|
|||
|
||||
for (MazePlayer p : players)
|
||||
p.setPlaying();
|
||||
System.out.println(Arrays.toString(players.toArray()));
|
||||
|
||||
gameManager.generateMaze(new HashSet<MazePlayer>(players));
|
||||
startGame = false;
|
||||
|
@ -216,6 +239,10 @@ public class GameClient {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return clientRunning;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
if (clientRunning) {
|
||||
client.stop();
|
||||
|
|
|
@ -11,11 +11,14 @@ import com.emamaker.amazeing.AMazeIng;
|
|||
import com.emamaker.amazeing.manager.GameManager;
|
||||
import com.emamaker.amazeing.manager.GameType;
|
||||
import com.emamaker.amazeing.manager.network.NetworkCommon.AddNewPlayer;
|
||||
import com.emamaker.amazeing.manager.network.NetworkCommon.ConnectionRefused;
|
||||
import com.emamaker.amazeing.manager.network.NetworkCommon.EndGame;
|
||||
import com.emamaker.amazeing.manager.network.NetworkCommon.LoginAO;
|
||||
import com.emamaker.amazeing.manager.network.NetworkCommon.LoginAO2;
|
||||
import com.emamaker.amazeing.manager.network.NetworkCommon.RemovePlayer;
|
||||
import com.emamaker.amazeing.manager.network.NetworkCommon.StartGame;
|
||||
import com.emamaker.amazeing.manager.network.NetworkCommon.UpdatePlayerTransform;
|
||||
import com.emamaker.amazeing.maze.settings.MazeSettings;
|
||||
import com.emamaker.amazeing.player.MazePlayer;
|
||||
import com.emamaker.amazeing.player.MazePlayerRemote;
|
||||
import com.esotericsoftware.kryonet.Connection;
|
||||
|
@ -27,15 +30,16 @@ public class GameServer {
|
|||
public AMazeIng main;
|
||||
|
||||
volatile boolean serverRunning = false;
|
||||
boolean endGameCalled = false;
|
||||
public int port;
|
||||
UUID uuid;
|
||||
|
||||
GameManager gameManager;
|
||||
public GameManager gameManager;
|
||||
Server server;
|
||||
|
||||
// Hashtable of remote players present in the match. This will be used to update
|
||||
// other players' transform when server reports about it
|
||||
Hashtable<String, MazePlayerRemote> remotePlayers = new Hashtable<>();
|
||||
public Hashtable<String, MazePlayerRemote> remotePlayers = new Hashtable<>();
|
||||
|
||||
public GameServer(AMazeIng main_) {
|
||||
main = main_;
|
||||
|
@ -80,18 +84,27 @@ public class GameServer {
|
|||
// Ignore is there's no uuid or it's different from the login message one
|
||||
if (connection.uuid == null || !connection.uuid.equals(((LoginAO2) object).uuid))
|
||||
return;
|
||||
// Otherwise add a new player and notify all clients about it
|
||||
remotePlayers.put(((LoginAO2) object).uuid,
|
||||
new MazePlayerRemote(main, ((LoginAO2) object).uuid, false));
|
||||
AddNewPlayer response = new AddNewPlayer();
|
||||
response.uuid = ((LoginAO2) object).uuid;
|
||||
|
||||
System.out.println("Client with UUID " + response.uuid + " is connected and ready to play :)");
|
||||
server.sendToAllTCP(response);
|
||||
// If there's still space left for players to join
|
||||
if (remotePlayers.values().size() < MazeSettings.MAXPLAYERS) {
|
||||
// Otherwise add a new player and notify all clients about it
|
||||
remotePlayers.put(((LoginAO2) object).uuid,
|
||||
new MazePlayerRemote(main, ((LoginAO2) object).uuid, false));
|
||||
|
||||
AddNewPlayer response = new AddNewPlayer();
|
||||
response.uuid = ((LoginAO2) object).uuid;
|
||||
|
||||
for (String s : remotePlayers.keySet()) {
|
||||
response.uuid = s;
|
||||
c.sendTCP(response);
|
||||
System.out.println(
|
||||
"Client with UUID " + response.uuid + " is connected and ready to play :)");
|
||||
server.sendToAllTCP(response);
|
||||
|
||||
for (String s : remotePlayers.keySet()) {
|
||||
response.uuid = s;
|
||||
c.sendTCP(response);
|
||||
}
|
||||
} else {
|
||||
// Send connection refused
|
||||
c.sendTCP(new ConnectionRefused());
|
||||
}
|
||||
|
||||
} else if (object instanceof RemovePlayer) {
|
||||
|
@ -139,9 +152,14 @@ public class GameServer {
|
|||
server.bind(port);
|
||||
server.start();
|
||||
System.out.println("Server registered and running on port " + port);
|
||||
|
||||
//Also launch the client to have a player play on host
|
||||
|
||||
// Also launch the client to have a player play on host
|
||||
main.client.start("localhost", port);
|
||||
|
||||
// If the server and the client have been started successfully, we can show the
|
||||
// joining screen
|
||||
main.uiManager.preGameScreen.setGameType(GameType.SERVER);
|
||||
main.setScreen(main.uiManager.preGameScreen);
|
||||
System.out.println("Local client ready to play!");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -154,6 +172,10 @@ public class GameServer {
|
|||
if (serverRunning) {
|
||||
if (gameManager != null) {
|
||||
gameManager.update();
|
||||
if (gameManager.anyoneWon && !endGameCalled) {
|
||||
server.sendToAllTCP(new EndGame());
|
||||
endGameCalled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -168,6 +190,8 @@ public class GameServer {
|
|||
// Start game stuff
|
||||
this.gameManager = new GameManager(main, GameType.SERVER);
|
||||
this.gameManager.generateMaze(new HashSet<MazePlayer>(remotePlayers.values()));
|
||||
endGameCalled = false;
|
||||
|
||||
StartGame request = new StartGame();
|
||||
request.map = this.gameManager.mazeGen.runLenghtEncode();
|
||||
server.sendToAllTCP(request);
|
||||
|
@ -175,6 +199,11 @@ public class GameServer {
|
|||
if (gameManager.gameStarted)
|
||||
for (String p : remotePlayers.keySet())
|
||||
updatePlayer(p, remotePlayers.get(p), true);
|
||||
|
||||
if (main.getScreen() != null) {
|
||||
main.setScreen(null);
|
||||
main.getScreen().hide();
|
||||
}
|
||||
} else {
|
||||
System.out.println("Server not started yet, game cannot start");
|
||||
}
|
||||
|
@ -222,6 +251,10 @@ public class GameServer {
|
|||
serverRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return serverRunning;
|
||||
}
|
||||
}
|
||||
|
||||
class ConnectionPlayer extends Connection {
|
||||
|
|
|
@ -11,12 +11,14 @@ public class NetworkCommon {
|
|||
Kryo kryo = endPoint.getKryo();
|
||||
kryo.register(LoginAO.class);
|
||||
kryo.register(LoginAO2.class);
|
||||
kryo.register(ConnectionRefused.class);
|
||||
kryo.register(LoginUUID.class);
|
||||
kryo.register(AddNewPlayer.class);
|
||||
kryo.register(RemovePlayer.class);
|
||||
kryo.register(UpdatePlayerTransform.class);
|
||||
kryo.register(UpdatePlayerTransformServer.class);
|
||||
kryo.register(StartGame.class);
|
||||
kryo.register(EndGame.class);
|
||||
kryo.register(UpdateMap.class);
|
||||
}
|
||||
|
||||
|
@ -26,6 +28,9 @@ public class NetworkCommon {
|
|||
static public class LoginAO2 {
|
||||
String uuid;
|
||||
}
|
||||
static public class ConnectionRefused {
|
||||
String uuid;
|
||||
}
|
||||
static public class LoginUUID {
|
||||
String uuid;
|
||||
}
|
||||
|
@ -45,13 +50,17 @@ public class NetworkCommon {
|
|||
String uuid;
|
||||
float tx, ty, tz, rx, ry, rz, rw;
|
||||
}
|
||||
|
||||
|
||||
static public class StartGame{
|
||||
//Use this to notify clients of a newly started game
|
||||
//A Run-lenght-encoded representation of the map can be appended, this can be avoided but it's not recommended
|
||||
String map;
|
||||
}
|
||||
static public class EndGame{
|
||||
//Use this to notify clients when a game ends
|
||||
}
|
||||
static public class UpdateMap{
|
||||
//Use this to notify clients of a modification of the map
|
||||
//Run-lenght-encoded representation of the map
|
||||
String map;
|
||||
}
|
||||
|
|
|
@ -112,9 +112,7 @@ public class MazeGenerator {
|
|||
* player
|
||||
*/
|
||||
public void setupEndPoint() {
|
||||
// Randomly spawns all the players
|
||||
// For a spawn location to be valid, it has to be free from both walls and
|
||||
// players
|
||||
// Randomly places the end point
|
||||
int x = 0, y = 0;
|
||||
|
||||
do {
|
||||
|
@ -125,7 +123,7 @@ public class MazeGenerator {
|
|||
WINX = x;
|
||||
WINZ = y;
|
||||
todraw[x][y] = 2;
|
||||
main.world.worldManager.setCell(WINX, 0, WINZ, CellId.ID_WOOD);
|
||||
show(todraw);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -245,10 +243,15 @@ public class MazeGenerator {
|
|||
|
||||
main.world.worldManager.setCell(i, 0, j, CellId.ID_GRASS);
|
||||
|
||||
if (todraw[i][j] == 1)
|
||||
main.world.worldManager.setCell(i, 1, j, CellId.ID_LEAVES);
|
||||
if (todraw[i][j] == 2)
|
||||
// if (todraw[i][j] == 1)
|
||||
// main.world.worldManager.setCell(i, 1, j, CellId.ID_LEAVES);
|
||||
if (todraw[i][j] == 2) {
|
||||
WINX = i;
|
||||
WINZ = j;
|
||||
|
||||
System.out.println("Win position in: " + i + ", " + j);
|
||||
main.world.worldManager.setCell(i, 0, j, CellId.ID_WOOD);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ public abstract class MazePlayer {
|
|||
MeshPartBuilder meshBuilder;
|
||||
static int meshAttr = VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal;
|
||||
public GameObject obj;
|
||||
String name;
|
||||
String name = "";
|
||||
boolean disposing = false;
|
||||
boolean playing = false;
|
||||
boolean show = true;
|
||||
|
@ -100,6 +100,7 @@ public abstract class MazePlayer {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void buildModel() {
|
||||
modelBuilder.begin();
|
||||
Node n = modelBuilder.node();
|
||||
|
|
|
@ -139,14 +139,23 @@ public class MazePlayerLocal extends MazePlayer {
|
|||
// be rendered correctly)
|
||||
ghostObject.getWorldTransform(characterTransform);
|
||||
|
||||
if (pressed)
|
||||
if (pressed) {
|
||||
System.out.println("Player in: " + getPos());
|
||||
main.client.updateLocalPlayer(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
inputs();
|
||||
}
|
||||
@Override
|
||||
public Vector3 getPos() {
|
||||
if (!disposing) {
|
||||
return ghostObject.getWorldTransform().getTranslation(new Vector3());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPos(Vector3 v) {
|
||||
|
|
|
@ -11,7 +11,6 @@ public class MazePlayerRemote extends MazePlayer{
|
|||
|
||||
static Random rand = new Random();
|
||||
|
||||
String name;
|
||||
AMazeIng main;
|
||||
//UUID is stored a string, for kryonet ease of use
|
||||
public String uuid;
|
||||
|
@ -27,14 +26,6 @@ public class MazePlayerRemote extends MazePlayer{
|
|||
uuid = u;
|
||||
}
|
||||
|
||||
public void setName(String name_) {
|
||||
this.name = name_;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.badlogic.gdx.Gdx;
|
|||
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
|
||||
import com.emamaker.amazeing.AMazeIng;
|
||||
import com.emamaker.amazeing.ui.screens.PlayerChooseScreen;
|
||||
import com.emamaker.amazeing.ui.screens.PreGameScreen;
|
||||
import com.emamaker.amazeing.ui.screens.ServerJoinScreen;
|
||||
import com.emamaker.amazeing.ui.screens.ServerLaunchScreen;
|
||||
import com.emamaker.amazeing.ui.screens.SettingsScreen;
|
||||
|
@ -20,6 +21,7 @@ public class UIManager {
|
|||
public SettingsScreen setScreen;
|
||||
public ServerJoinScreen srvJoinScreen;
|
||||
public ServerLaunchScreen srvLaunchScreen;
|
||||
public PreGameScreen preGameScreen;
|
||||
|
||||
public UIManager(Game main_) {
|
||||
main = (AMazeIng)main_;
|
||||
|
@ -32,6 +34,7 @@ public class UIManager {
|
|||
setScreen = new SettingsScreen(this);
|
||||
srvJoinScreen = new ServerJoinScreen(this);
|
||||
srvLaunchScreen = new ServerLaunchScreen(this);
|
||||
preGameScreen = new PreGameScreen(this);
|
||||
|
||||
main.setScreen(titleScreen);
|
||||
}
|
||||
|
|
|
@ -129,7 +129,7 @@ public class PlayerChooseScreen implements Screen {
|
|||
|
||||
// Labels to know if players joined
|
||||
for (int i = 0; i < labels.length; i++) {
|
||||
labels[i] = new Label("Not joined yet", uiManager.skin);
|
||||
labels[i] = new Label("-- empty slot --", uiManager.skin);
|
||||
}
|
||||
|
||||
// Add actions to the buttons
|
||||
|
|
|
@ -0,0 +1,197 @@
|
|||
package com.emamaker.amazeing.ui.screens;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Screen;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.scenes.scene2d.InputEvent;
|
||||
import com.badlogic.gdx.scenes.scene2d.InputListener;
|
||||
import com.badlogic.gdx.scenes.scene2d.Stage;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Container;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Label;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Table;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
|
||||
import com.badlogic.gdx.utils.Align;
|
||||
import com.badlogic.gdx.utils.viewport.ScreenViewport;
|
||||
import com.emamaker.amazeing.manager.GameType;
|
||||
import com.emamaker.amazeing.maze.settings.MazeSettings;
|
||||
import com.emamaker.amazeing.player.MazePlayer;
|
||||
import com.emamaker.amazeing.ui.UIManager;
|
||||
|
||||
public class PreGameScreen implements Screen {
|
||||
|
||||
UIManager uiManager;
|
||||
Stage stage;
|
||||
Table table;
|
||||
|
||||
Label[] labels;
|
||||
MazePlayer[] players, tmp;
|
||||
|
||||
// GameType we are runnig. assuming server for default. If client, the StartGame
|
||||
// button shouldn't appear
|
||||
GameType type = GameType.SERVER;
|
||||
|
||||
Screen thisScreen;
|
||||
|
||||
public PreGameScreen(UIManager uiManager_) {
|
||||
uiManager = uiManager_;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
thisScreen = this;
|
||||
|
||||
labels = new Label[MazeSettings.MAXPLAYERS];
|
||||
players = new MazePlayer[MazeSettings.MAXPLAYERS];
|
||||
tmp = new MazePlayer[MazeSettings.MAXPLAYERS];
|
||||
|
||||
stage = new Stage(new ScreenViewport());
|
||||
Container<Table> tableContainer = new Container<Table>();
|
||||
Table table = new Table();
|
||||
|
||||
float cw = stage.getWidth();
|
||||
float ch = stage.getHeight();
|
||||
|
||||
tableContainer.setSize(cw, ch);
|
||||
tableContainer.setPosition(0, 0);
|
||||
|
||||
Label instLab = new Label("Waiting for players to join...", uiManager.skin);
|
||||
TextButton backBtn = new TextButton("Back", uiManager.skin);
|
||||
TextButton setBtn = new TextButton("Settings", uiManager.skin);
|
||||
TextButton helpBtn = new TextButton("?", uiManager.skin);
|
||||
TextButton playBtn = new TextButton("Start the match!", uiManager.skin);
|
||||
|
||||
if(type == GameType.CLIENT) instLab.setText("Waiting for server to start the game...");
|
||||
|
||||
// Labels to know if players joined
|
||||
for (int i = 0; i < labels.length; i++) {
|
||||
labels[i] = new Label("-- empty slot --", uiManager.skin);
|
||||
}
|
||||
|
||||
// Add actions to the buttons
|
||||
backBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
hide();
|
||||
uiManager.main.setScreen(type == GameType.SERVER ? uiManager.srvLaunchScreen : uiManager.srvJoinScreen);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
if (type == GameType.SERVER) {
|
||||
// Add actions to the buttons
|
||||
playBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
hide();
|
||||
uiManager.main.server.startGame();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
// Add actions to the buttons
|
||||
setBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
uiManager.setScreen.setPrevScreen(thisScreen);
|
||||
uiManager.main.setScreen(uiManager.setScreen);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
// Add actions to the buttons
|
||||
helpBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
System.out.println("Make this appear help dialog (TODO)");
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
Table firstRowTable = new Table();
|
||||
|
||||
firstRowTable.add(backBtn).width(50).height(50).fillX().expandX().space(cw * 0.005f);
|
||||
firstRowTable.add(instLab).height(50).fillX().expandX().space(cw * 0.25f);
|
||||
if (type == GameType.SERVER)
|
||||
firstRowTable.add(setBtn).height(50).fillX().expandX().space(cw * 0.005f);
|
||||
firstRowTable.add(helpBtn).width(50).height(50).fillX().expandX().space(cw * 0.005f);
|
||||
firstRowTable.setOrigin(Align.center | Align.top);
|
||||
table.row().colspan(4);
|
||||
|
||||
table.add(firstRowTable);
|
||||
|
||||
for (int i = 0; i < labels.length; i++) {
|
||||
if (i % 4 == 0)
|
||||
table.row().expandY().fillY();
|
||||
table.add(labels[i]).space(1);
|
||||
}
|
||||
|
||||
if (type == GameType.SERVER) {
|
||||
table.row().colspan(4);
|
||||
table.add(playBtn).fillX().width(cw * 0.06f);
|
||||
}
|
||||
|
||||
tableContainer.setActor(table);
|
||||
stage.addActor(tableContainer);
|
||||
|
||||
uiManager.main.multiplexer.addProcessor(stage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hide() {
|
||||
uiManager.main.multiplexer.removeProcessor(stage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(float delta) {
|
||||
Gdx.gl.glClearColor(0, 0, 0, 0);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
stage.act();
|
||||
stage.draw();
|
||||
|
||||
// Constantly update player labels, comparing with the remote players present on
|
||||
// server
|
||||
tmp = type == GameType.SERVER
|
||||
? Arrays.copyOf(uiManager.main.server.remotePlayers.values().toArray(),
|
||||
uiManager.main.server.remotePlayers.values().size(), MazePlayer[].class)
|
||||
: Arrays.copyOf(uiManager.main.client.players.toArray(), uiManager.main.client.players.size(),
|
||||
MazePlayer[].class);
|
||||
if (!(Arrays.equals(players, tmp))) {
|
||||
// Update Labels
|
||||
for (int i = 0; i < tmp.length; i++) {
|
||||
players[i] = tmp[i];
|
||||
labels[i].setText(players[i].getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setGameType(GameType t) {
|
||||
type = t;
|
||||
show();
|
||||
}
|
||||
|
||||
public GameType getGameType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(int width, int height) {
|
||||
stage.getViewport().update(width, height, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pause() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resume() {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
stage.dispose();
|
||||
}
|
||||
|
||||
}
|
|
@ -38,8 +38,10 @@ public class ServerJoinScreen implements Screen {
|
|||
TextButton backBtn = new TextButton("Main menu", uiManager.skin);
|
||||
TextButton connectBtn = new TextButton("Connect to the server!", uiManager.skin);
|
||||
TextButton helpBtn = new TextButton("?", uiManager.skin);
|
||||
final TextArea srvIp = new TextArea("localhost", uiManager.skin);
|
||||
final TextArea srvPort = new TextArea("9999", uiManager.skin);
|
||||
Label srvIpL = new Label("Server IP: ", uiManager.skin);
|
||||
final TextArea srvIp = new TextArea("", uiManager.skin);
|
||||
Label srvPortL = new Label("Server Port: ", uiManager.skin);
|
||||
final TextArea srvPort = new TextArea("", uiManager.skin);
|
||||
|
||||
// Add actions to the buttons
|
||||
backBtn.addListener(new InputListener() {
|
||||
|
@ -80,9 +82,11 @@ public class ServerJoinScreen implements Screen {
|
|||
table.row().colspan(4);
|
||||
table.add(firstRowTable);
|
||||
|
||||
table.row().colspan(4);
|
||||
table.add(srvIp).fillX().expandX();
|
||||
table.row().colspan(4);
|
||||
table.row().colspan(2);
|
||||
table.add(srvIpL).expandX();
|
||||
table.add(srvIp).expandX();
|
||||
table.row().colspan(2);
|
||||
table.add(srvPortL).fillX().expandX();
|
||||
table.add(srvPort).fillX().expandX();
|
||||
|
||||
table.row().colspan(4);
|
||||
|
|
|
@ -13,17 +13,20 @@ import com.badlogic.gdx.scenes.scene2d.ui.TextArea;
|
|||
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
|
||||
import com.badlogic.gdx.utils.Align;
|
||||
import com.badlogic.gdx.utils.viewport.ScreenViewport;
|
||||
import com.emamaker.amazeing.manager.GameType;
|
||||
import com.emamaker.amazeing.ui.UIManager;
|
||||
|
||||
public class ServerLaunchScreen implements Screen {
|
||||
|
||||
Stage stage;
|
||||
UIManager uiManager;
|
||||
Screen thisScreen;
|
||||
|
||||
public ServerLaunchScreen(UIManager uiManager_) {
|
||||
|
||||
uiManager = uiManager_;
|
||||
|
||||
thisScreen = this;
|
||||
|
||||
stage = new Stage(new ScreenViewport());
|
||||
Container<Table> tableContainer = new Container<Table>();
|
||||
Table table = new Table();
|
||||
|
@ -34,12 +37,13 @@ public class ServerLaunchScreen implements Screen {
|
|||
tableContainer.setSize(cw, ch);
|
||||
tableContainer.setPosition(0, 0);
|
||||
|
||||
Label instLab = new Label("Enter the port the server should start on", uiManager.skin);
|
||||
TextButton backBtn = new TextButton("Main menu and quit server", uiManager.skin);
|
||||
Label instLab = new Label("Enter the port the server must start on", uiManager.skin);
|
||||
TextButton backBtn = new TextButton("<", uiManager.skin);
|
||||
TextButton connectBtn = new TextButton("Launch the server!", uiManager.skin);
|
||||
TextButton gameBtn = new TextButton("Launch the game!", uiManager.skin);
|
||||
TextButton setBtn = new TextButton("Settings", uiManager.skin);
|
||||
TextButton helpBtn = new TextButton("?", uiManager.skin);
|
||||
final TextArea srvPort = new TextArea("9999", uiManager.skin);
|
||||
Label srvPortL = new Label("Port: ", uiManager.skin);
|
||||
final TextArea srvPort = new TextArea("", uiManager.skin);
|
||||
|
||||
// Add actions to the buttons
|
||||
backBtn.addListener(new InputListener() {
|
||||
|
@ -58,6 +62,15 @@ public class ServerLaunchScreen implements Screen {
|
|||
return true;
|
||||
}
|
||||
});
|
||||
setBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
hide();
|
||||
uiManager.setScreen.prevScreen = thisScreen;
|
||||
uiManager.main.setScreen(uiManager.setScreen);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
connectBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
|
@ -65,32 +78,24 @@ public class ServerLaunchScreen implements Screen {
|
|||
return true;
|
||||
}
|
||||
});
|
||||
gameBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
uiManager.main.server.startGame();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Table firstRowTable = new Table();
|
||||
firstRowTable.add(backBtn).fillX().expandX().space(cw * 0.005f);
|
||||
firstRowTable.add(instLab).height(50).fillX().expandX().space(cw * 0.25f);
|
||||
firstRowTable.add(setBtn).width(50).height(50).fillX().expandX().space(cw * 0.005f);
|
||||
firstRowTable.add(helpBtn).width(50).height(50).fillX().expandX().space(cw * 0.005f);
|
||||
firstRowTable.setOrigin(Align.center | Align.top);
|
||||
|
||||
table.row().colspan(4);
|
||||
table.add(firstRowTable);
|
||||
|
||||
table.row().colspan(4);
|
||||
table.add(srvPort).fillX().expandX();
|
||||
table.row().colspan(2);
|
||||
table.add(srvPortL).expandX();
|
||||
table.add(srvPort).expandX();
|
||||
|
||||
table.row().colspan(4);
|
||||
table.add(connectBtn).fillX().expandX();
|
||||
|
||||
table.row().colspan(4);
|
||||
table.add(gameBtn).fillX().expandX();
|
||||
|
||||
tableContainer.setActor(table);
|
||||
stage.addActor(tableContainer);
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ public class SettingsScreen implements Screen{
|
|||
tableContainer.setPosition(0, 0);
|
||||
|
||||
Label instLab = new Label("Here you can customize game settings!", uiManager.skin);
|
||||
TextButton backBtn = new TextButton("Back", uiManager.skin);
|
||||
TextButton backBtn = new TextButton("<", uiManager.skin);
|
||||
TextButton helpBtn = new TextButton("?", uiManager.skin);
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue