UI: fixed some missing hide() statements and now there are help and errors dialogs appearing

master
EmaMaker 2020-04-26 14:37:57 +02:00
parent fb708dfd9c
commit b43a438f07
7 changed files with 153 additions and 44 deletions

View File

@ -57,7 +57,7 @@ public class GameClient {
main = main_;
}
public void start(String addr_, int port_) {
public boolean start(String addr_, int port_) {
port = port_;
addr = addr_;
@ -149,11 +149,13 @@ public class GameClient {
} else {
System.out.println("Already connected, to need to connect again");
}
return true;
// Server communication after connection can go here, or in
// Listener#connected().
} catch (IOException ex) {
ex.printStackTrace();
}
return false;
}
// Update must be called from Main thread and used for applications on main

View File

@ -46,7 +46,8 @@ public class GameServer {
uuid = UUID.randomUUID();
}
public void startServer(int port_) {
//Returns true if the server started successfully
public boolean startServer(int port_) {
port = port_;
serverRunning = true;
try {
@ -157,17 +158,18 @@ public class GameServer {
server.start();
System.out.println("Server registered and running on port " + port);
// 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!");
// Also launch the client to have a player play on host. We return the result of starting, so server doesn't start if local client has problems
if (main.client.start("localhost", port))
return true;
else {
server.stop();
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
// Update must be called from Main thread and used for applications on main
@ -205,8 +207,8 @@ public class GameServer {
updatePlayer(p, remotePlayers.get(p), true);
if (main.getScreen() != null) {
main.setScreen(null);
main.getScreen().hide();
main.setScreen(null);
}
} else {
System.out.println("Server not started yet, game cannot start");

View File

@ -11,6 +11,7 @@ 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.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
@ -122,15 +123,28 @@ public class PlayerChooseScreen implements Screen {
tableContainer.setPosition(0, 0);
Label instLab = new Label("Use WASD, ARROWS, or button on controller to join the match", uiManager.skin);
TextButton backBtn = new TextButton("Back", uiManager.skin);
TextButton backBtn = new TextButton("<", uiManager.skin);
TextButton setBtn = new TextButton("Settings", uiManager.skin);
TextButton helpBtn = new TextButton("?", uiManager.skin);
TextButton playBtn = new TextButton("Play", uiManager.skin);
TextButton playBtn = new TextButton("Play!", uiManager.skin);
// Labels to know if players joined
for (int i = 0; i < labels.length; i++) {
labels[i] = new Label("-- empty slot --", uiManager.skin);
}
final Dialog helpDlg = new Dialog("Help", uiManager.skin);
/* HELP DIALOG */
helpDlg.text("Here you can start a singleplayer or multiplayer game on the local machine:\n"
+ "For keyboard players, pressing W,A,S,D or the directional arrows will toggle two different players.\n"
+ "Pressing a button on a controller will toggle a player.\n"
+ "You can edit game settings from the \"Settings\" menu or use the \"<\" button to go back to the main menu\n"
+ "Press the \"Play!\" button to start the game with the players that have currently joined.\n"
+ "Once a game is finished you will go back to this menu");
TextButton helpDlgOkBtn = new TextButton("OK", uiManager.skin);
helpDlg.button(helpDlgOkBtn);
helpDlgOkBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
helpDlg.hide();
return true;
}
});
// Add actions to the buttons
backBtn.addListener(new InputListener() {
@ -141,7 +155,6 @@ public class PlayerChooseScreen implements Screen {
return true;
}
});
// Add actions to the buttons
playBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
@ -152,24 +165,30 @@ public class PlayerChooseScreen implements Screen {
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) {
hide();
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)");
helpDlg.show(stage);
return true;
}
});
// Labels to know if players joined
for (int i = 0; i < labels.length; i++) {
labels[i] = new Label("-- empty slot --", uiManager.skin);
}
/* BUILD UP TABLE */
Table firstRowTable = new Table();
firstRowTable.add(backBtn).width(50).height(50).fillX().expandX().space(cw * 0.005f);
@ -225,6 +244,7 @@ public class PlayerChooseScreen implements Screen {
p = new MazePlayerLocal(uiManager.main, Keys.UP, Keys.DOWN, Keys.LEFT, Keys.RIGHT);
togglePlayer(p);
}
// for (Controller c : Controllers.getControllers()) {
// if (c.getButton(Xbox.Y)) {
// p = getPlayerWithCtrl(c);
@ -233,17 +253,19 @@ public class PlayerChooseScreen implements Screen {
// togglePlayer(p);
// }
// }
//Update labels
for(int i = 0; i < labels.length; i++) {
labels[i].setText(i < players.size() ? "-- Player Ready! --" : "-- empty slot --" );
}
}
public void togglePlayer(MazePlayerLocal p) {
try {
if (alreadyAddedPlayer(p)) {
players.get(p).setText("Not Joined Yet");
if (alreadyAddedPlayer(p))
players.remove(p);
} else {
else
players.put(p, labels[players.size()]);
players.get(p).setText("Player Read");
}
} catch (Exception e) {
System.out.println("All players already joined");
}
@ -259,11 +281,12 @@ public class PlayerChooseScreen implements Screen {
public MazePlayerLocal getPlayerWithKeys(int... keys) {
for (MazePlayer p : players.keySet()) {
if(p instanceof MazePlayerLocal) {
for (int k : keys) {
if (((MazePlayerLocal)p).kup == k || ((MazePlayerLocal)p).kdown == k || ((MazePlayerLocal)p).ksx == k || ((MazePlayerLocal)p).kdx == k)
return (MazePlayerLocal)p;
}
if (p instanceof MazePlayerLocal) {
for (int k : keys) {
if (((MazePlayerLocal) p).kup == k || ((MazePlayerLocal) p).kdown == k
|| ((MazePlayerLocal) p).ksx == k || ((MazePlayerLocal) p).kdx == k)
return (MazePlayerLocal) p;
}
}
}
return null;
@ -276,7 +299,7 @@ public class PlayerChooseScreen implements Screen {
public MazePlayerLocal getPlayerWithCtrl(Controller ctrl) {
for (MazePlayer p : players.keySet()) {
if (p instanceof MazePlayerLocal) {
if (((MazePlayerLocal)p).ctrl == ctrl)
if (((MazePlayerLocal) p).ctrl == ctrl)
return (MazePlayerLocal) p;
}
}

View File

@ -100,6 +100,7 @@ public class PreGameScreen implements Screen {
setBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
hide();
uiManager.setScreen.setPrevScreen(thisScreen);
uiManager.main.setScreen(uiManager.setScreen);
return true;

View File

@ -7,6 +7,7 @@ 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.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextArea;
@ -39,10 +40,38 @@ public class ServerJoinScreen implements Screen {
TextButton connectBtn = new TextButton("Connect to the server!", uiManager.skin);
TextButton helpBtn = new TextButton("?", 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 srvIp = new TextArea("", uiManager.skin);
final TextArea srvPort = new TextArea("", uiManager.skin);
final Dialog helpDlg = new Dialog("Help", uiManager.skin);
/* HELP DIALOG */
helpDlg.text("Here you can connect to a server to play with your friends over the network.\n"
+ "The server host should provide you with address and port info to connect to the server");
TextButton helpDlgOkBtn = new TextButton("OK", uiManager.skin);
helpDlg.button(helpDlgOkBtn);
helpDlgOkBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
helpDlg.hide();
return true;
}
});
final Dialog failDlg = new Dialog("Server start-up failed", uiManager.skin);
/* HELP DIALOG */
failDlg.text("Connection to the server failed. Check your internet connection and address/port combination.\n"
+ "Or Pheraps there's no server running there?");
TextButton failDlgOkBtn = new TextButton("OK", uiManager.skin);
failDlg.button(failDlgOkBtn);
failDlg.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
failDlg.hide();
return true;
}
});
// Add actions to the buttons
backBtn.addListener(new InputListener() {
@Override
@ -56,19 +85,15 @@ public class ServerJoinScreen implements Screen {
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)");
helpDlg.show(stage);
return true;
}
});
connectBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
try {
uiManager.main.client.start(srvIp.getText(), Integer.valueOf(srvPort.getText()));
} catch (Exception e) {
System.out.println("Please input a valid ip address and port");
}
if (!uiManager.main.client.start(srvIp.getText(), Integer.valueOf(srvPort.getText())))
failDlg.show(stage);
return true;
}
});

View File

@ -7,12 +7,14 @@ 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.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
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 {
@ -44,6 +46,35 @@ public class ServerLaunchScreen implements Screen {
Label srvPortL = new Label("Port: ", uiManager.skin);
final TextArea srvPort = new TextArea("", uiManager.skin);
final Dialog helpDlg = new Dialog("Help", uiManager.skin);
/* HELP DIALOG */
helpDlg.text("Here you can start a server to play with your friends over the network.\n"
+ "Choose a network port to start the server on and start the server.\n"
+ "In the next screen you will be given the address and port other players have to connect to play on this server.\n"
+ "The port must not being used by another program at the same time, or the server start-up will fail");
TextButton helpDlgOkBtn = new TextButton("OK", uiManager.skin);
helpDlg.button(helpDlgOkBtn);
helpDlgOkBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
helpDlg.hide();
return true;
}
});
final Dialog failDlg = new Dialog("Server start-up failed", uiManager.skin);
/* HELP DIALOG */
failDlg.text("Server start-up failed. Pheraps the port is already being used?");
TextButton failDlgOkBtn = new TextButton("OK", uiManager.skin);
failDlg.button(failDlgOkBtn);
failDlg.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
failDlg.hide();
return true;
}
});
// Add actions to the buttons
backBtn.addListener(new InputListener() {
@Override
@ -57,7 +88,7 @@ public class ServerLaunchScreen implements Screen {
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)");
helpDlg.show(stage);
return true;
}
});
@ -73,7 +104,15 @@ public class ServerLaunchScreen implements Screen {
connectBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
uiManager.main.server.startServer(Integer.valueOf(srvPort.getText()));
if(uiManager.main.server.startServer(Integer.valueOf(srvPort.getText()))) {
// If the server and the client have been started successfully, we can show the
// joining screen
uiManager.preGameScreen.setGameType(GameType.SERVER);
uiManager.main.setScreen(uiManager.preGameScreen);
}else {
//Show the dialog to say there's was something wrong
failDlg.show(stage);
}
return true;
}
});

View File

@ -7,6 +7,7 @@ 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.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
@ -42,7 +43,21 @@ public class SettingsScreen implements Screen{
Label instLab = new Label("Here you can customize game settings!", uiManager.skin);
TextButton backBtn = new TextButton("<", uiManager.skin);
TextButton helpBtn = new TextButton("?", uiManager.skin);
final Dialog helpDlg = new Dialog("Help", uiManager.skin);
/* HELP DIALOG */
helpDlg.text("Here you can customize can settings:\n"
+ "Maze Size: changes the size of the maze. Mazes are always squares. This affects both local and online games.\n"
+ "Max. Players: changes the max number of players that can join the game. This affects both local and online games.");
TextButton helpDlgOkBtn = new TextButton("OK", uiManager.skin);
helpDlg.button(helpDlgOkBtn);
helpDlgOkBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
helpDlg.hide();
return true;
}
});
// Add actions to the buttons
backBtn.addListener(new InputListener() {
@ -57,13 +72,15 @@ public class SettingsScreen implements Screen{
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)");
helpDlg.show(stage);
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);