Implemented a Thread that listens for changes in day/night in gameStateModel to simplify and reduce the tasks of client thread
This commit is contained in:
parent
277eada934
commit
c6f82fd7c1
@ -3,6 +3,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client;
|
|||||||
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
|
||||||
import ch.unibas.dmi.dbis.cs108.gamelogic.ClientGameInfoHandler;
|
import ch.unibas.dmi.dbis.cs108.gamelogic.ClientGameInfoHandler;
|
||||||
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel;
|
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.DayNightChangeListener;
|
||||||
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.GUI;
|
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.GUI;
|
||||||
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.GameStateModel;
|
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.GameStateModel;
|
||||||
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ChatApp;
|
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ChatApp;
|
||||||
@ -350,23 +351,12 @@ public class Client {
|
|||||||
switch (parameter) {
|
switch (parameter) {
|
||||||
case GuiParameters.night: //ClientGameInfoHandler;
|
case GuiParameters.night: //ClientGameInfoHandler;
|
||||||
gameStateModel.setDayClone(false);
|
gameStateModel.setDayClone(false);
|
||||||
LOGGER.debug("----------------Night, Your role is:" + gameStateModel.getYourRole() + gameStateModel);
|
|
||||||
Sprites.setNightSprites(gameStateModel.getPassengerTrainClone()[1], GameController.getGameStateModel().getKickedOff());
|
|
||||||
chatApp.getGameController().setNoiseButtonInvisible();
|
|
||||||
chatApp.getGameController().setVoteButtonVisibilityNight(gameStateModel);
|
|
||||||
break;
|
break;
|
||||||
case GuiParameters.day: //ClientGameInfoHandler
|
case GuiParameters.day: //ClientGameInfoHandler
|
||||||
gameStateModel.setDayClone(true);
|
gameStateModel.setDayClone(true);
|
||||||
LOGGER.debug("----------------Day, Your role is:" + gameStateModel.getYourRole()+ gameStateModel);
|
|
||||||
Sprites.setDaySprites(gameStateModel.getPassengerTrainClone()[1], GameController.getGameStateModel().getKickedOff());
|
|
||||||
chatApp.getGameController().setNoiseButtonVisible();
|
|
||||||
chatApp.getGameController().setVoteButtonVisibilityDay(gameStateModel);
|
|
||||||
break;
|
break;
|
||||||
case GuiParameters.updateGameState:
|
case GuiParameters.updateGameState:
|
||||||
gameStateModel.setGSFromString(data);
|
gameStateModel.setGSFromString(data);
|
||||||
chatApp.getGameController().updateGameSprites(LoungeSceneViewController.getTrainAnimationDayController());
|
|
||||||
chatApp.getGameController().updateRoomLabels();
|
|
||||||
gameStateModel.setRoleFromPosition(position);
|
|
||||||
break;
|
break;
|
||||||
case GuiParameters.noiseHeardAtPosition:
|
case GuiParameters.noiseHeardAtPosition:
|
||||||
try {
|
try {
|
||||||
@ -388,13 +378,12 @@ public class Client {
|
|||||||
break;
|
break;
|
||||||
case GuiParameters.viewChangeToGame:
|
case GuiParameters.viewChangeToGame:
|
||||||
chatApp.getLoungeSceneViewController().addGameView();
|
chatApp.getLoungeSceneViewController().addGameView();
|
||||||
//TODO
|
gameStateModel.setGameOver(false);
|
||||||
|
new Thread(new DayNightChangeListener(gameStateModel, chatApp, position)).start();
|
||||||
break;
|
break;
|
||||||
/*case GuiParameters.viewChangeToStart:
|
|
||||||
//TODO
|
|
||||||
break;*/
|
|
||||||
case GuiParameters.viewChangeToLobby:
|
case GuiParameters.viewChangeToLobby:
|
||||||
chatApp.getLoungeSceneViewController().removeGameView();
|
chatApp.getLoungeSceneViewController().removeGameView();
|
||||||
|
gameStateModel.setGameOver(true);
|
||||||
//TODO
|
//TODO
|
||||||
break;
|
break;
|
||||||
case GuiParameters.addNewMemberToLobby:
|
case GuiParameters.addNewMemberToLobby:
|
||||||
|
|||||||
@ -0,0 +1,54 @@
|
|||||||
|
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui;
|
||||||
|
|
||||||
|
|
||||||
|
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game.GameController;
|
||||||
|
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge.LoungeSceneViewController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class adds methods to listen if there is a change in the day&night state and calls methods accordingly
|
||||||
|
*/
|
||||||
|
public class DayNightChangeListener implements Runnable {
|
||||||
|
|
||||||
|
private GameStateModel gameStateModel;
|
||||||
|
private ChatApp chatApp;
|
||||||
|
private int position;
|
||||||
|
|
||||||
|
public DayNightChangeListener(GameStateModel gameStateModel, ChatApp chatApp, int position) {
|
||||||
|
this.gameStateModel = gameStateModel;
|
||||||
|
this.chatApp = chatApp;
|
||||||
|
this.position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try{
|
||||||
|
while(!gameStateModel.isGameOver()) {
|
||||||
|
if(gameStateModel.getDayClone()) { //its Day
|
||||||
|
LoungeSceneViewController.getTrainAnimationDayController().dontShowFullWagon();
|
||||||
|
Sprites.setDaySprites(gameStateModel.getPassengerTrainClone()[1], GameController.getGameStateModel().getKickedOff());
|
||||||
|
chatApp.getGameController().setNoiseButtonVisible();
|
||||||
|
chatApp.getGameController().setVoteButtonVisibilityDay(gameStateModel);
|
||||||
|
} else { //its night
|
||||||
|
if (gameStateModel.getYourRole().equals("h")) {
|
||||||
|
LoungeSceneViewController.getTrainAnimationDayController().showFullWagon();
|
||||||
|
}
|
||||||
|
Sprites.setNightSprites(gameStateModel.getPassengerTrainClone()[1], GameController.getGameStateModel().getKickedOff());
|
||||||
|
chatApp.getGameController().setNoiseButtonInvisible();
|
||||||
|
chatApp.getGameController().setVoteButtonVisibilityNight(gameStateModel);
|
||||||
|
}
|
||||||
|
chatApp.getGameController().updateGameSprites(LoungeSceneViewController.getTrainAnimationDayController());
|
||||||
|
chatApp.getGameController().updateRoomLabels();
|
||||||
|
gameStateModel.setRoleFromPosition(position);
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -19,6 +19,11 @@ public class GameStateModel {
|
|||||||
*/
|
*/
|
||||||
private boolean isDayClone;
|
private boolean isDayClone;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* true if the game is over
|
||||||
|
*/
|
||||||
|
private boolean gameOver = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* can take the values h/g/s for human/ghost/spectator. Safes the role the client this GamesStateModel
|
* can take the values h/g/s for human/ghost/spectator. Safes the role the client this GamesStateModel
|
||||||
* lives on currently has
|
* lives on currently has
|
||||||
@ -48,6 +53,15 @@ public class GameStateModel {
|
|||||||
isDayClone = false;
|
isDayClone = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setGameOver(boolean gameOver) {
|
||||||
|
this.gameOver = gameOver;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isGameOver() {
|
||||||
|
return gameOver;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the passengerTrainClone
|
* Updates the passengerTrainClone
|
||||||
* @param names an array of the names of the players
|
* @param names an array of the names of the players
|
||||||
|
|||||||
@ -95,6 +95,24 @@ public class TrainAnimationDayController implements Initializable {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void showFullWagon() {
|
||||||
|
Platform.runLater(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
wagonFullImageView.setVisible(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dontShowFullWagon() {
|
||||||
|
Platform.runLater(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
wagonFullImageView.setVisible(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds the gameView to the existing LobbyView
|
* Adds the gameView to the existing LobbyView
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user