Legger til valg av lokale servere Closes #60

Fjerner LOADING GameState
Gjør det enklere å endre port for server og klient
This commit is contained in:
2020-04-23 22:07:13 +02:00
parent 03f7090e3b
commit d9dd9d7948
14 changed files with 100 additions and 45 deletions

View File

@@ -18,6 +18,8 @@ public class RoboRallyWrapper extends Game {
public RoboRallyGame roboRallyGame;
public RoboRallyServer server;
public RoboRallyClient client;
public int defaultTCPPort = 54555;
public int discoverUDPPort = 54777;
@Override
public void create() {

View File

@@ -1,22 +1,25 @@
package inf112.fiasko.roborally.gamewrapper.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
import inf112.fiasko.roborally.networking.RoboRallyClient;
import javax.swing.*;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* This screen allows the user to enter the ip address to connect to
@@ -38,6 +41,20 @@ public class IPAddressScreen extends AbstractScreen {
TextButton joinButton = new TextButton("Join", skin);
joinButton.setSize(300, 60);
joinButton.setPosition(applicationWidth / 2f - joinButton.getWidth() / 2f, 300);
roboRallyWrapper.client = new RoboRallyClient(roboRallyWrapper);
List<InetAddress> lanServers = roboRallyWrapper.client.getLanServers();
Set<String> validHosts = new HashSet<>();
for (InetAddress address : lanServers) {
validHosts.add(address.getHostAddress());
}
final SelectBox<String> selectBox = new SelectBox<>(skin);
selectBox.setItems(validHosts.toArray(new String[0]));
selectBox.setPosition(-80 + (applicationWidth - selectBox.getWidth()) / 2f, 200);
selectBox.setSize(200, 50);
stage.addActor(selectBox);
joinButton.addListener(new ClickListener() {
@Override
public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
@@ -47,10 +64,16 @@ public class IPAddressScreen extends AbstractScreen {
@Override
public void touchUp(InputEvent e, float x, float y, int point, int button) {
try {
roboRallyWrapper.client = new RoboRallyClient(textInput.getText(), roboRallyWrapper);
String serverIp;
String writtenIP = textInput.getText();
if (writtenIP.isEmpty()) {
serverIp = selectBox.getSelected();
} else {
serverIp = writtenIP;
}
roboRallyWrapper.client.connect(serverIp, roboRallyWrapper.defaultTCPPort, roboRallyWrapper.discoverUDPPort);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getUsernameScreen(roboRallyWrapper));
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Could not connect to the server."
+ " Please make sure the ip address you typed is correct, and that the server is online.");
}
@@ -80,7 +103,8 @@ public class IPAddressScreen extends AbstractScreen {
roboRallyWrapper.batch.setProjectionMatrix(camera.combined);
roboRallyWrapper.batch.begin();
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Enter IP address and click the button to join a server",
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Enter or select IP address and click the button to " +
"join a server",
applicationWidth / 2f - 380 / 2f, applicationHeight / 2f + 100, 380, 1,
true);
roboRallyWrapper.batch.end();

View File

@@ -44,9 +44,7 @@ public class LoadingScreen extends AbstractScreen {
if (roboRallyWrapper.roboRallyGame != null) {
GameState gameState = roboRallyWrapper.roboRallyGame.getGameState();
if (gameState != GameState.LOADING) {
handleScreenChange(gameState);
}
handleScreenChange(gameState);
}
}
@@ -73,12 +71,12 @@ public class LoadingScreen extends AbstractScreen {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getPowerDownScreen(this.roboRallyWrapper));
break;
case SKIP_POWER_DOWN_SCREEN:
roboRallyWrapper.roboRallyGame.setGameState(GameState.LOADING);
roboRallyWrapper.roboRallyGame.setGameState(GameState.WAITING_FOR_OTHER_PLAYERS_PROGRAMS);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(this.roboRallyWrapper));
roboRallyWrapper.client.sendElement(new ProgramAndPowerdownRequest(false, new ArrayList<>()));
break;
default:
System.out.println("The loading screen doesn't know what to do with " + gameState);
//Ignored
break;
}
}

View File

@@ -56,9 +56,9 @@ public class PowerDownScreen extends AbstractScreen {
roboRallyWrapper.batch.setProjectionMatrix(camera.combined);
String descriptionText;
if (roboRallyWrapper.roboRallyGame.getGameState() == GameState.CHOOSING_POWER_DOWN) {
descriptionText = "Click the button to enter power down next round";
descriptionText = "Click the button to enter power down next turn";
} else {
descriptionText = "Click the button to continue your power down the next round";
descriptionText = "Click the button to continue your power down the next turn";
}
int elapsedTime = (int) Math.floor((System.currentTimeMillis() - startTime) / 1000f);
@@ -90,7 +90,7 @@ public class PowerDownScreen extends AbstractScreen {
roboRallyWrapper.client.sendElement(bool);
break;
case CHOOSING_POWER_DOWN:
roboRallyWrapper.roboRallyGame.setGameState(GameState.LOADING);
roboRallyWrapper.roboRallyGame.setGameState(GameState.WAITING_FOR_OTHER_PLAYERS_PROGRAMS);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(this.roboRallyWrapper));
roboRallyWrapper.client.sendElement(new ProgramAndPowerdownRequest(bool,
roboRallyWrapper.roboRallyGame.getProgram()));

View File

@@ -39,11 +39,11 @@ public class StartMenuScreen extends AbstractScreen {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
try {
roboRallyWrapper.server = new RoboRallyServer();
roboRallyWrapper.client = new RoboRallyClient("127.0.0.1", roboRallyWrapper);
roboRallyWrapper.server = new RoboRallyServer(roboRallyWrapper.defaultTCPPort, roboRallyWrapper.discoverUDPPort);
roboRallyWrapper.client = new RoboRallyClient(roboRallyWrapper);
roboRallyWrapper.client.connect("127.0.0.1", roboRallyWrapper.defaultTCPPort, roboRallyWrapper.discoverUDPPort);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getUsernameScreen(roboRallyWrapper));
} catch (IOException e) {
e.printStackTrace();
//Hard fail
roboRallyWrapper.quit("Server could not be started");
}

View File

@@ -1,7 +1,6 @@
package inf112.fiasko.roborally.gamewrapper.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
@@ -47,12 +46,12 @@ public class UsernameScreen extends AbstractScreen {
JOptionPane.showMessageDialog(null, "Username cannot be empty.");
return;
}
roboRallyWrapper.client.sendElement(userName);
if (roboRallyWrapper.server == null) {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(roboRallyWrapper));
} else {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLobbyScreen(roboRallyWrapper));
}
roboRallyWrapper.client.sendElement(userName);
}
});
textInput = new TextField("", skin);