This commit is contained in:
Kristian Knarvik 2020-04-16 15:33:37 +02:00
commit 1dab8c6e1d
18 changed files with 379 additions and 63 deletions

View File

@ -22,5 +22,10 @@ public enum GameState {
//Indicates that the user is in the process of sending their cards to the server //Indicates that the user is in the process of sending their cards to the server
SENDING_CARDS, SENDING_CARDS,
//Indicates that the game is won by a player //Indicates that the game is won by a player
GAME_IS_WON GAME_IS_WON,
JUST_BEFORE_CHOOSING_CARDS,
LOADING
} }

View File

@ -14,6 +14,15 @@ public class ScreenManager {
private IPAddressScreen ipAddressScreen; private IPAddressScreen ipAddressScreen;
private LobbyScreen lobbyScreen; private LobbyScreen lobbyScreen;
private WinnerScreen winnerScreen; private WinnerScreen winnerScreen;
private CardChoiceScreen cardChoiceScreen;
public synchronized CardChoiceScreen getCardChoiceScreen(RoboRallyWrapper roboRallyWrapper) {
if (this.cardChoiceScreen == null) {
this.cardChoiceScreen = new CardChoiceScreen(roboRallyWrapper);
}
return cardChoiceScreen;
}
/** /**
* Gets an instance of the winner screen * Gets an instance of the winner screen

View File

@ -80,6 +80,9 @@ public class BoardActiveScreen extends AbstractScreen implements InputProcessor
if (roboRallyWrapper.roboRallyGame.getGameState() == GameState.GAME_IS_WON) { if (roboRallyWrapper.roboRallyGame.getGameState() == GameState.GAME_IS_WON) {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getWinnerScreen(roboRallyWrapper)); roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getWinnerScreen(roboRallyWrapper));
} }
else if (roboRallyWrapper.roboRallyGame.getGameState() == GameState.CHOOSING_STAY_IN_POWER_DOWN){
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getPowerDownScreen(roboRallyWrapper));
}
} }
@Override @Override

View File

@ -15,6 +15,7 @@ import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport; import com.badlogic.gdx.utils.viewport.Viewport;
import inf112.fiasko.roborally.elementproperties.GameState;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper; import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
import inf112.fiasko.roborally.gamewrapper.SimpleButton; import inf112.fiasko.roborally.gamewrapper.SimpleButton;
import inf112.fiasko.roborally.objects.IDeck; import inf112.fiasko.roborally.objects.IDeck;
@ -51,8 +52,8 @@ public class CardChoiceScreen extends InputAdapter implements Screen {
* Instantiates a new card choice screen * Instantiates a new card choice screen
* @param roboRallyWrapper The Robo Rally wrapper which is parent of this screen * @param roboRallyWrapper The Robo Rally wrapper which is parent of this screen
*/ */
public CardChoiceScreen(final RoboRallyWrapper roboRallyWrapper, ProgrammingCardDeck deck) { public CardChoiceScreen(final RoboRallyWrapper roboRallyWrapper) {
this.deck = deck; this.deck = roboRallyWrapper.roboRallyGame.getPlayerHand();
this.roboRallyWrapper = roboRallyWrapper; this.roboRallyWrapper = roboRallyWrapper;
camera = new OrthographicCamera(); camera = new OrthographicCamera();
int applicationWidth = 600; int applicationWidth = 600;
@ -66,12 +67,13 @@ public class CardChoiceScreen extends InputAdapter implements Screen {
inputMultiplexer = new InputMultiplexer(); inputMultiplexer = new InputMultiplexer();
try { try {
generateCards(); generateCards(deck);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
this.chosenCards = new ArrayList<>(); this.chosenCards = new ArrayList<>();
this.maxCards = 5;
this.maxCards = roboRallyWrapper.roboRallyGame.getProgramSize();
stage = new Stage(); stage = new Stage();
TextButton confirmCards = new SimpleButton("Confirm cards", roboRallyWrapper.font).getButton(); TextButton confirmCards = new SimpleButton("Confirm cards", roboRallyWrapper.font).getButton();
@ -83,7 +85,10 @@ public class CardChoiceScreen extends InputAdapter implements Screen {
@Override @Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
if (chosenCards.size() == maxCards) { if (chosenCards.size() == maxCards) {
System.out.println("Lock cards!");
roboRallyWrapper.roboRallyGame.setProgram(getCards());
roboRallyWrapper.roboRallyGame.setGameState(GameState.CHOOSING_POWER_DOWN);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getPowerDownScreen(roboRallyWrapper));
return true; return true;
} }
return false; return false;
@ -93,16 +98,19 @@ public class CardChoiceScreen extends InputAdapter implements Screen {
inputMultiplexer.addProcessor(this); inputMultiplexer.addProcessor(this);
inputMultiplexer.addProcessor(stage); inputMultiplexer.addProcessor(stage);
} }
private List<ProgrammingCard> getCards(){
List<ProgrammingCard> program = new ArrayList<>();
chosenCards.forEach((cardRectangle -> program.add(cardRectangle.card)));
return program;
}
/** /**
* Generates some placeholder cards for testing * Generates some placeholder cards for testing
* @throws IOException If programming cards cannot be loaded * @throws IOException If programming cards cannot be loaded
*/ */
private void generateCards() throws IOException { private void generateCards(ProgrammingCardDeck deck) throws IOException {
IDeck<ProgrammingCard> deck = DeckLoaderUtil.loadProgrammingCardsDeck();
deck.shuffle();
//Get 9 programming cards //Get 9 programming cards
List<ProgrammingCard> cardList = deck.getCards().subList(0, 9); List<ProgrammingCard> cardList = deck.getCards();
float cardWidth = viewport.getWorldWidth() / 3; float cardWidth = viewport.getWorldWidth() / 3;
float cardHeight = (viewport.getWorldHeight() - 30) / 3; float cardHeight = (viewport.getWorldHeight() - 30) / 3;
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {

View File

@ -43,19 +43,26 @@ public class LoadingScreen extends AbstractScreen {
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Loading...", applicationWidth/2f-380/2f, roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Loading...", applicationWidth/2f-380/2f,
applicationHeight / 2f,380, 1, true); applicationHeight / 2f,380, 1, true);
roboRallyWrapper.batch.end(); roboRallyWrapper.batch.end();
long time = System.currentTimeMillis();
//TODO: Allow to set any condition and next screen //if (roboRallyWrapper.roboRallyGame != null){
if (roboRallyWrapper.roboRallyGame != null && roboRallyWrapper.roboRallyGame.getGameState() != initialGameState) { // System.out.println(roboRallyWrapper.roboRallyGame.getGameState());
//}
if (roboRallyWrapper.roboRallyGame != null && roboRallyWrapper.roboRallyGame.getGameState() != GameState.LOADING) {
handleScreenChange(); handleScreenChange();
} }
} }
private void handleScreenChange() { private void handleScreenChange() {
switch (initialGameState) { switch (roboRallyWrapper.roboRallyGame.getGameState()) {
case SENDING_CARDS: case RUNNING_PROGRAMS:
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getBoardActiveScreen(this.roboRallyWrapper)); roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getBoardActiveScreen(this.roboRallyWrapper));
break; break;
} case INITIAL_SETUP:
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(this.roboRallyWrapper));
break;
case CHOOSING_CARDS:
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getCardChoiceScreen(this.roboRallyWrapper));
break; }
} }
@Override @Override

View File

@ -9,8 +9,15 @@ import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport; import com.badlogic.gdx.utils.viewport.Viewport;
import inf112.fiasko.roborally.elementproperties.Action;
import inf112.fiasko.roborally.elementproperties.GameState;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper; import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
import inf112.fiasko.roborally.gamewrapper.SimpleButton; import inf112.fiasko.roborally.gamewrapper.SimpleButton;
import inf112.fiasko.roborally.networking.containers.ProgramAndPowerdownRequest;
import inf112.fiasko.roborally.objects.ProgrammingCard;
import java.util.ArrayList;
import java.util.List;
/** /**
* This screen is used for asking players whether they want to power down * This screen is used for asking players whether they want to power down
@ -42,8 +49,8 @@ public class PowerDownScreen extends AbstractScreen {
powerDownButton.addListener(new InputListener() { powerDownButton.addListener(new InputListener() {
@Override @Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(roboRallyWrapper)); sendPowerdownStatus(true);
return true;//her we do stuff return true;
} }
}); });
} }
@ -68,10 +75,29 @@ public class PowerDownScreen extends AbstractScreen {
stage.draw(); stage.draw();
if (elapsedTime > 10) { if (elapsedTime > 10) {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(this.roboRallyWrapper)); sendPowerdownStatus( false);
} }
} }
public void sendPowerdownStatus (boolean bool){
if(roboRallyWrapper.roboRallyGame.getGameState()== GameState.CHOOSING_STAY_IN_POWER_DOWN){
roboRallyWrapper.roboRallyGame.setGameState(GameState.TURN_CLEANUP);
roboRallyWrapper.client.sendElement(bool);
}
else if (roboRallyWrapper.roboRallyGame.getGameState()==GameState.CHOOSING_POWER_DOWN){
roboRallyWrapper.roboRallyGame.setGameState(GameState.LOADING);
roboRallyWrapper.client.sendElement(new ProgramAndPowerdownRequest(bool,
roboRallyWrapper.roboRallyGame.getProgram()));
}
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(this.roboRallyWrapper));
}
@Override @Override
public void resize(int width, int height) { public void resize(int width, int height) {
viewport.update(width, height); viewport.update(width, height);

View File

@ -30,9 +30,10 @@ public class RoboRallyClient {
* Sends something to the server * Sends something to the server
* @param object The object to send to the server * @param object The object to send to the server
*/ */
public void sendElement(Object object) { public void sendElement(Object object) {try{
client.sendTCP(object); client.sendTCP(object);}
} catch(Exception e){e.printStackTrace();}
}
} }

View File

@ -2,13 +2,18 @@ package inf112.fiasko.roborally.networking;
import com.esotericsoftware.kryonet.Connection; import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener; import com.esotericsoftware.kryonet.Listener;
import inf112.fiasko.roborally.elementproperties.GameState;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper; import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
import inf112.fiasko.roborally.gamewrapper.screens.CardChoiceScreen; import inf112.fiasko.roborally.gamewrapper.screens.CardChoiceScreen;
import inf112.fiasko.roborally.networking.containers.ErrorResponse; import inf112.fiasko.roborally.networking.containers.ErrorResponse;
import inf112.fiasko.roborally.networking.containers.GameStartInfo; import inf112.fiasko.roborally.networking.containers.GameStartInfo;
import inf112.fiasko.roborally.networking.containers.PowerdownContainer;
import inf112.fiasko.roborally.networking.containers.ProgamsContainer;
import inf112.fiasko.roborally.objects.ProgrammingCardDeck; import inf112.fiasko.roborally.objects.ProgrammingCardDeck;
import inf112.fiasko.roborally.objects.RoboRallyGame; import inf112.fiasko.roborally.objects.RoboRallyGame;
import java.util.concurrent.TimeUnit;
/** /**
* This listener handles all receiving from the server * This listener handles all receiving from the server
*/ */
@ -33,11 +38,22 @@ class RoboRallyClientListener extends Listener {
GameStartInfo info = (GameStartInfo) object; GameStartInfo info = (GameStartInfo) object;
wrapper.roboRallyGame = new RoboRallyGame(info.getPlayerList(), info.getBoardName(), wrapper.roboRallyGame = new RoboRallyGame(info.getPlayerList(), info.getBoardName(),
wrapper.server != null, info.getPlayerName(), wrapper.client, wrapper.server); wrapper.server != null, info.getPlayerName(), wrapper.client, wrapper.server);
wrapper.setScreen(wrapper.screenManager.getLoadingScreen(wrapper));
} }
else if(object instanceof ProgrammingCardDeck){ else if(object instanceof ProgrammingCardDeck){
wrapper.setScreen(new CardChoiceScreen(wrapper,(ProgrammingCardDeck) object)); wrapper.roboRallyGame.setGameState(GameState.CHOOSING_CARDS);
wrapper.roboRallyGame.setPlayerHand((ProgrammingCardDeck) object);
} }
else if(object instanceof ProgamsContainer){
try {
wrapper.roboRallyGame.reciveAllProgrammes((ProgamsContainer) object);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else if (object instanceof PowerdownContainer){
wrapper.roboRallyGame.recivedStayInPowerdown((PowerdownContainer) object);
}
} }
} }

View File

@ -24,7 +24,7 @@ public class RoboRallyServer {
server.start(); server.start();
NetworkUtil.registerClasses(server.getKryo()); NetworkUtil.registerClasses(server.getKryo());
server.bind(54555, 54777); server.bind(54555, 54777);
listener = new RoboRallyServerListener(); listener = new RoboRallyServerListener(this);
server.addListener(listener); server.addListener(listener);
} }

View File

@ -5,12 +5,17 @@ import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener; import com.esotericsoftware.kryonet.Listener;
import inf112.fiasko.roborally.elementproperties.RobotID; import inf112.fiasko.roborally.elementproperties.RobotID;
import inf112.fiasko.roborally.networking.containers.ErrorResponse; import inf112.fiasko.roborally.networking.containers.ErrorResponse;
import inf112.fiasko.roborally.networking.containers.PowerdownContainer;
import inf112.fiasko.roborally.networking.containers.ProgamsContainer;
import inf112.fiasko.roborally.networking.containers.ProgramAndPowerdownRequest;
import inf112.fiasko.roborally.objects.ProgrammingCard;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* This listener handles all sending and responses for the server * This listener handles all sending and responses for the server
@ -20,15 +25,19 @@ class RoboRallyServerListener extends Listener {
private final Map<Connection, RobotID> clients; private final Map<Connection, RobotID> clients;
private final Map<Connection, String> playerNames; private final Map<Connection, String> playerNames;
private final List<Connection> deadPlayers; private final List<Connection> deadPlayers;
private Map<Connection,Boolean> StayInPowerdown = new HashMap<>();
private Map<Connection,ProgramAndPowerdownRequest> programs = new HashMap<>();
private RoboRallyServer server;
/** /**
* Instantiates a new Robo Rally server listener * Instantiates a new Robo Rally server listener
*/ */
RoboRallyServerListener() { RoboRallyServerListener(RoboRallyServer server) {
super(); super();
clients = new HashMap<>(); clients = new HashMap<>();
playerNames = new HashMap<>(); playerNames = new HashMap<>();
deadPlayers = new ArrayList<>(); deadPlayers = new ArrayList<>();
this.server = server;
} }
/** /**
@ -64,16 +73,64 @@ class RoboRallyServerListener extends Listener {
@Override @Override
public void received (Connection connection, Object object) { public void received (Connection connection, Object object) {
if (object instanceof String) { if (object instanceof String) {
String playerName = (String) object; System.out.println((String) object);
if (playerNames.containsValue(playerName)) { recivedString(connection,(String) object);
String errorMessage = "The player name send is already taken."; }
connection.sendTCP(new ErrorResponse(errorMessage, new IllegalArgumentException(errorMessage))); else if(object instanceof Boolean){
} else { recivedContinuePowerdown(connection,(Boolean) object);
playerNames.put(connection, playerName); }
else if(object instanceof ProgramAndPowerdownRequest){
reciveProgamAndPowerdownRequest(connection,(ProgramAndPowerdownRequest) object);
}
}
private void recivedString(Connection connection,String name){
String playerName = name;
if (playerNames.containsValue(playerName)) {
String errorMessage = "The player name send is already taken.";
connection.sendTCP(new ErrorResponse(errorMessage, new IllegalArgumentException(errorMessage)));
} else {
playerNames.put(connection, playerName);
}
}
private void recivedContinuePowerdown(Connection connection,Boolean bool){
StayInPowerdown.put(connection,bool);
if(recivedDataFromAllConnections(StayInPowerdown)){
Map<String,Boolean> powerdowns = new HashMap<>();
for (Connection connected:StayInPowerdown.keySet()) {
powerdowns.put(playerNames.get(connected),StayInPowerdown.get(connected));
} }
server.sendToAllClients(new PowerdownContainer(powerdowns));
StayInPowerdown = new HashMap<>();
} }
} }
private void reciveProgamAndPowerdownRequest(Connection connection,ProgramAndPowerdownRequest request){
programs.put(connection,request);
if(recivedDataFromAllConnections(programs)){
Map<String,Boolean> powerdown = new HashMap<>();
Map<String,List<ProgrammingCard>> program = new HashMap<>();
for (Connection connected:programs.keySet()) {
powerdown.put(playerNames.get(connected),programs.get(connected).getPowerdown());
program.put(playerNames.get(connected),programs.get(connected).getProgram());
}
server.sendToAllClients(new ProgamsContainer(program,powerdown));
programs = new HashMap<>();
}
}
private<K> boolean recivedDataFromAllConnections(Map<Connection,K> data){
Set<Connection> connections = clients.keySet();
connections.removeAll(deadPlayers);
return connections.containsAll(data.keySet()) && data.keySet().containsAll(connections);
}
@Override @Override
public void connected(Connection connection) { public void connected(Connection connection) {
//The first client to connect is assumed to be the host //The first client to connect is assumed to be the host

View File

@ -0,0 +1,15 @@
package inf112.fiasko.roborally.networking.containers;
import java.util.Map;
public class PowerdownContainer {
private Map<String,Boolean> powerdown;
public PowerdownContainer(Map<String, Boolean> powerdown) {
this.powerdown = powerdown;
}
public Map<String, Boolean> getPowerdown() {
return powerdown;
}
}

View File

@ -0,0 +1,24 @@
package inf112.fiasko.roborally.networking.containers;
import inf112.fiasko.roborally.objects.ProgrammingCard;
import java.util.List;
import java.util.Map;
public class ProgamsContainer {
private Map<String, List<ProgrammingCard>> program;
private Map<String, Boolean> powerdown;
public ProgamsContainer(){}
public ProgamsContainer(Map<String, List<ProgrammingCard>> program, Map<String, Boolean> powerdown) {
this.program = program;
this.powerdown = powerdown;
}
public Map<String, List<ProgrammingCard>> getProgram() {
return program;
}
public Map<String, Boolean> getPowerdown() {
return powerdown;
}
}

View File

@ -0,0 +1,24 @@
package inf112.fiasko.roborally.networking.containers;
import inf112.fiasko.roborally.objects.ProgrammingCard;
import java.util.List;
public class ProgramAndPowerdownRequest {
private Boolean powerdown;
private List<ProgrammingCard> program;
public ProgramAndPowerdownRequest(){}
public ProgramAndPowerdownRequest(Boolean powerdown, List<ProgrammingCard> program) {
this.program=program;
this.powerdown = powerdown;
}
public List<ProgrammingCard> getProgram() {
return program;
}
public Boolean getPowerdown() {
return powerdown;
}
}

View File

@ -756,4 +756,31 @@ public class Board {
particles.setElement(positionX, positionY, new Particle(type, laserDirection)); particles.setElement(positionX, positionY, new Particle(type, laserDirection));
} }
/**
* Gets the int corresponding to the flag a robot has last visited
* @param robotID The robot to be checked
* @return The flag last visited in a number
*/
public int getLastFlagVisitedFromRobotID(RobotID robotID) {
return robots.get(robotID).getLastFlagVisited();
}
/**
* Sets a boolean for if the robot has touched a flag this turn
* @param robotID The robot to be checked
* @param hasTouched If the robot has touched a flag this turn
*/
public void setHasTouchedFlagThisTurnFromRobotID(RobotID robotID, boolean hasTouched) {
robots.get(robotID).setHasTouchedFlagThisTurn(hasTouched);
}
/**
* Checks a boolean for if the robot has touched a flag this turn
* @param robotID The robot to be checked
* @return If the robot has touched a flag this turn
*/
public boolean isHasTouchedFlagThisTurnFromRobotID(RobotID robotID) {
return robots.get(robotID).isHasTouchedFlagThisTurn();
}
} }

View File

@ -1,6 +1,10 @@
package inf112.fiasko.roborally.objects; package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.elementproperties.GameState; import inf112.fiasko.roborally.elementproperties.GameState;
import inf112.fiasko.roborally.networking.containers.PowerdownContainer;
import inf112.fiasko.roborally.networking.containers.ProgamsContainer;
import java.util.List;
/** /**
* This interface describes * This interface describes
@ -23,4 +27,21 @@ public interface IInteractableGame {
* @return A string of the player name * @return A string of the player name
*/ */
String getWinningPlayerName(); String getWinningPlayerName();
void reciveAllProgrammes(ProgamsContainer programs) throws InterruptedException;
void recivedStayInPowerdown(PowerdownContainer powerdowns);
List<ProgrammingCard> getProgram();
int getProgramSize();
void setPlayerHand(ProgrammingCardDeck playerHand);
ProgrammingCardDeck getPlayerHand();
void setProgram(List<ProgrammingCard> program);
} }

View File

@ -7,8 +7,8 @@ import inf112.fiasko.roborally.elementproperties.Action;
*/ */
public class ProgrammingCard implements Comparable<ProgrammingCard> { public class ProgrammingCard implements Comparable<ProgrammingCard> {
private final int cardPriority; private int cardPriority;
private final Action cardAction; private Action cardAction;
/** /**
* Initializes the priority and the action of the card * Initializes the priority and the action of the card
@ -20,6 +20,10 @@ public class ProgrammingCard implements Comparable<ProgrammingCard> {
this.cardAction = cardAction; this.cardAction = cardAction;
} }
public ProgrammingCard(){
}
/** /**
* Gets the priority of the programming card * Gets the priority of the programming card
* @return The programming card priority * @return The programming card priority

View File

@ -9,6 +9,8 @@ import inf112.fiasko.roborally.elementproperties.RobotID;
import inf112.fiasko.roborally.elementproperties.TileType; import inf112.fiasko.roborally.elementproperties.TileType;
import inf112.fiasko.roborally.networking.RoboRallyClient; import inf112.fiasko.roborally.networking.RoboRallyClient;
import inf112.fiasko.roborally.networking.RoboRallyServer; import inf112.fiasko.roborally.networking.RoboRallyServer;
import inf112.fiasko.roborally.networking.containers.PowerdownContainer;
import inf112.fiasko.roborally.networking.containers.ProgamsContainer;
import inf112.fiasko.roborally.utility.BoardLoaderUtil; import inf112.fiasko.roborally.utility.BoardLoaderUtil;
import inf112.fiasko.roborally.utility.DeckLoaderUtil; import inf112.fiasko.roborally.utility.DeckLoaderUtil;
@ -38,6 +40,24 @@ public class RoboRallyGame implements IRoboRallyGame {
private final RoboRallyClient client; private final RoboRallyClient client;
private RoboRallyServer server; private RoboRallyServer server;
private String winningPlayerName; private String winningPlayerName;
private List<ProgrammingCard> program;
private ProgrammingCardDeck playerHand;
public ProgrammingCardDeck getPlayerHand() {
return playerHand;
}
public void setPlayerHand(ProgrammingCardDeck playerHand) {
this.playerHand = playerHand;
}
public List<ProgrammingCard> getProgram() {
return program;
}
public void setProgram(List<ProgrammingCard> program) {
this.program = program;
}
/** /**
* Instantiates a new Robo Rally game * Instantiates a new Robo Rally game
@ -63,6 +83,9 @@ public class RoboRallyGame implements IRoboRallyGame {
} }
} }
/** /**
* Instantiates a new Robo Rally game * Instantiates a new Robo Rally game
* @param playerList A list of all the players participating in the game * @param playerList A list of all the players participating in the game
@ -247,6 +270,14 @@ public class RoboRallyGame implements IRoboRallyGame {
return null; return null;
} }
public int getProgramSize(){
Player player = getPlayerFromName(playerName);
if (player != null) {
return Math.min(5, 5 - gameBoard.getRobotDamage(player.getRobotID()) + 4);
}
return -1;
}
/** /**
* Runs all the steps of one turn in the game * Runs all the steps of one turn in the game
* @throws InterruptedException If interrupted while trying to sleep * @throws InterruptedException If interrupted while trying to sleep
@ -289,18 +320,40 @@ public class RoboRallyGame implements IRoboRallyGame {
} }
} }
} }
setGameState(GameState.CHOOSING_CARDS); setGameState(GameState.JUST_BEFORE_CHOOSING_CARDS);
// TODO: Make program for this player, if not in power down // TODO: Make program for this player, if not in power down
// TODO: Ask player for new power down // TODO: Ask player for new power down
// Run the phases of the game // Run the phases of the game
while (getGameState()==GameState.CHOOSING_CARDS) {
//loops waiting for the player to be done choosing their cards
// TODO: If this player is in power down, ask if it shall continue
// Respawn dead robots, as long as they have more lives left
}
public void recivedStayInPowerdown(PowerdownContainer powerdowns){
for (Player player:playerList) {
player.setPowerDownNextRound(powerdowns.getPowerdown().get(player.getName()));
} }
runPhase(1); respawnRobots();
runPhase(2); resetHasTouchedFlagThisTurnForAllRobots();
runPhase(3); }
runPhase(4);
runPhase(5); public void reciveAllProgrammes(ProgamsContainer programs) throws InterruptedException {
Map<String,List<ProgrammingCard>> progs = programs.getProgram();
Map<String,Boolean> powerdown = programs.getPowerdown();
String playername;
for (Player player:playerList) {
playername = player.getName();
player.setInProgram(progs.get(playername));
player.setPowerDownNextRound(powerdown.get(playername));
}
setGameState(GameState.RUNNING_PROGRAMS);
runPhase(1);
runPhase(2);
runPhase(3);
runPhase(4);
runPhase(5);
// Repair robots on repair tiles // Repair robots on repair tiles
repairAllRobotsOnRepairTiles(); repairAllRobotsOnRepairTiles();
@ -309,9 +362,8 @@ public class RoboRallyGame implements IRoboRallyGame {
removeNonLockedProgrammingCardsFromPlayers(); removeNonLockedProgrammingCardsFromPlayers();
} }
// TODO: If this player is in power down, ask if it shall continue // TODO: If this player is in power down, ask if it shall continue
// Respawn dead robots, as long as they have more lives left
respawnRobots();
resetHasTouchedFlagThisTurnForAllRobots();
} }
/** /**
@ -582,24 +634,32 @@ public class RoboRallyGame implements IRoboRallyGame {
private void checkAllFlags() { private void checkAllFlags() {
for (BoardElementContainer<Tile> flag : flags) { for (BoardElementContainer<Tile> flag : flags) {
Position flagPosition = flag.getPosition(); Position flagPosition = flag.getPosition();
if (gameBoard.hasRobotOnPosition(flagPosition)) { if (!gameBoard.hasRobotOnPosition(flagPosition)) {
RobotID robotID = gameBoard.getRobotOnPosition(flagPosition); continue;
for (Robot robot : gameBoard.getAliveRobots()) { }
if (robot.getRobotId() != robotID || robot.isHasTouchedFlagThisTurn()) { RobotID robotID = gameBoard.getRobotOnPosition(flagPosition);
continue; if (gameBoard.isHasTouchedFlagThisTurnFromRobotID(robotID)) {
} continue;
gameBoard.updateFlagOnRobot(robotID, flag.getElement().getTileType()); }
robot.setHasTouchedFlagThisTurn(true); gameBoard.updateFlagOnRobot(robotID, flag.getElement().getTileType());
if (victoryCheck(robot.getLastFlagVisited(), flags.size())) { gameBoard.setHasTouchedFlagThisTurnFromRobotID(robotID,true);
for (Player player : playerList) { checkIfPlayerWon(robotID, flags.size());
if (player.getRobotID() != robotID) { }
continue; }
}
setWinningPlayerName(player.getName()); /**
setGameState(GameState.GAME_IS_WON); * Checks if the player won, and shows the victory screen
} * @param robotID The robot to be checked
} * @param numberOfFlags The number of flags on the map
*/
private void checkIfPlayerWon(RobotID robotID, int numberOfFlags) {
if (victoryCheck(gameBoard.getLastFlagVisitedFromRobotID(robotID), numberOfFlags)) {
for (Player player : playerList) {
if (player.getRobotID() != robotID) {
continue;
} }
setWinningPlayerName(player.getName());
setGameState(GameState.GAME_IS_WON);
} }
} }
} }
@ -614,7 +674,6 @@ public class RoboRallyGame implements IRoboRallyGame {
return (lastFlagVisited == lastFlag); return (lastFlagVisited == lastFlag);
} }
/** /**
* Fires all lasers on the game board * Fires all lasers on the game board
*/ */

View File

@ -5,12 +5,16 @@ import inf112.fiasko.roborally.elementproperties.Action;
import inf112.fiasko.roborally.elementproperties.RobotID; import inf112.fiasko.roborally.elementproperties.RobotID;
import inf112.fiasko.roborally.networking.containers.ErrorResponse; import inf112.fiasko.roborally.networking.containers.ErrorResponse;
import inf112.fiasko.roborally.networking.containers.GameStartInfo; import inf112.fiasko.roborally.networking.containers.GameStartInfo;
import inf112.fiasko.roborally.networking.containers.PowerdownContainer;
import inf112.fiasko.roborally.networking.containers.ProgamsContainer;
import inf112.fiasko.roborally.networking.containers.ProgramAndPowerdownRequest;
import inf112.fiasko.roborally.objects.IDeck; import inf112.fiasko.roborally.objects.IDeck;
import inf112.fiasko.roborally.objects.Player; import inf112.fiasko.roborally.objects.Player;
import inf112.fiasko.roborally.objects.ProgrammingCard; import inf112.fiasko.roborally.objects.ProgrammingCard;
import inf112.fiasko.roborally.objects.ProgrammingCardDeck; import inf112.fiasko.roborally.objects.ProgrammingCardDeck;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
/** /**
* This class helps with networking tasks * This class helps with networking tasks
@ -31,5 +35,11 @@ public final class NetworkUtil {
kryo.register(RobotID.class); kryo.register(RobotID.class);
kryo.register(ProgrammingCardDeck.class); kryo.register(ProgrammingCardDeck.class);
kryo.register(Action.class); kryo.register(Action.class);
kryo.register(ProgramAndPowerdownRequest.class);
kryo.register(ProgamsContainer.class);
kryo.register(PowerdownContainer.class);
kryo.register(HashMap.class);
} }
} }