Skip to content
Snippets Groups Projects
Commit 2e3ac92a authored by Petter Selfors Rølvåg's avatar Petter Selfors Rølvåg
Browse files

Merge branch 'main-game-transition' into 'dev'

Main game transition

See merge request !10
parents c5e29e01 56010f36
No related branches found
No related tags found
2 merge requests!13Dev,!10Main game transition
package com.mygdx.game.controller.commands;
import com.esotericsoftware.kryonet.Connection;
import com.mygdx.game.controller.NetworkController;
import com.mygdx.game.model.Lobby;
public class cSetPlayerReady extends Command{
public cSetPlayerReady() { super("cSetPlayerReady"); }
private int lobbyID;
private int playerID;
public cSetPlayerReady(boolean isPlayerReady, int lobbyID) {
super("cSetPlayerReady", isPlayerReady);
this.lobbyID = lobbyID;
this.playerID = playerID;
}
public cSetPlayerReady(boolean isPlayerReady, int lobbyID, int playerID) {
super("cSetPlayerReady", isPlayerReady);
this.lobbyID = lobbyID;
this.playerID = playerID;
}
@Override
public void execute(NetworkController net, Connection connection){
if(data instanceof Boolean){
boolean isPlayerReady = (Boolean) data;
net.getPlayer(connection.getID()).setIsPlayerReady(isPlayerReady);
Lobby lobby = net.getLobby(lobbyID);
for (Connection c : net.getConnections(lobby)) {
c.sendTCP(new cSetPlayerReady(isPlayerReady, lobbyID, connection.getID()));
}
if(lobby.isFullAndReady()){
for (Connection c : net.getConnections(lobby)) {
c.sendTCP(new cStartGame());
}
System.out.printf("Lobby %b is about to start. \n", lobbyID);
}
System.out.printf("Player status updated. Is player %d currently ready? %b \n", playerID, isPlayerReady);
}
}
}
\ No newline at end of file
package com.mygdx.game.controller.commands;
import com.esotericsoftware.kryonet.Connection;
import com.mygdx.game.controller.NetworkController;
import com.mygdx.game.model.Lobby;
public class cStartGame extends Command{
public cStartGame() { super("cStartGame"); }
public cStartGame(int id) { super("cStartGame", (Integer) id); }
@Override
public void execute(NetworkController net, Connection connection){
}
}
......@@ -50,8 +50,42 @@ public class Lobby {
return set;
}
public Player getPlayerByID(int playerID){
Player returnPlayer = null;
for (Player player : players){
if (player.getID() == playerID) returnPlayer = player;
}
return returnPlayer;
}
public ArrayList<Player> getPlayers(){
return players;
}
public int getPlayersCount(){ return players.size(); }
public boolean isFullAndReady(){
if(MAX_PLAYERS == getPlayersCount()){
for(Player player : players){
if(!player.getIsPlayerReady()) {
System.out.println("This should run only once.");
return false;
}
}
System.out.println("This should run a few times.");
return true;
}
else{
System.out.println("This should many times.");
return false;
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
......
......@@ -5,6 +5,7 @@ public class Player {
private int ID;
private int indexAvatar;
private String playerName;
private boolean isPlayerReady;
public Player(){}
......@@ -43,4 +44,16 @@ public class Player {
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public String getPlayerName(){
return this.playerName;
}
public void setIsPlayerReady(boolean isPlayerReady) {
this.isPlayerReady = isPlayerReady;
}
public boolean getIsPlayerReady(){
return this.isPlayerReady;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment