Commit b4c379ad authored by Petter Selfors Rølvåg's avatar Petter Selfors Rølvåg
Browse files

lobby players ready state and UI

parent c5e29e01
<component name="libraryTable">
<library name="Gradle: org.javatuples:javatuples:1.2">
<CLASSES>
<root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.javatuples/javatuples/1.2/507312ac4b601204a72a83380badbca82683dd36/javatuples-1.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.javatuples/javatuples/1.2/a7495f5370bdfcf46c6f3c6ed0badf52877aa467/javatuples-1.2-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
......@@ -13,8 +13,6 @@ buildscript {
classpath 'com.android.tools.build:gradle:3.5.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
......@@ -53,6 +51,7 @@ project(":desktop") {
api "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation group: "com.esotericsoftware", name: 'kryonet', version: "2.22.0-RC1"
implementation group: "org.javatuples", name: 'javatuples', version: "1.2"
}
}
......@@ -79,6 +78,7 @@ project(":android") {
api "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation group: "com.esotericsoftware", name: 'kryonet', version: "2.22.0-RC1"
implementation group: "org.javatuples", name: 'javatuples', version: "1.2"
}
}
......@@ -92,5 +92,6 @@ project(":core") {
api "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation group: "com.esotericsoftware", name: 'kryonet', version: "2.22.0-RC1"
implementation group: "org.javatuples", name: 'javatuples', version: "1.2"
}
}
\ No newline at end of file
......@@ -11,6 +11,9 @@ import com.mygdx.game.model.Lobby;
import com.mygdx.game.model.Player;
import com.mygdx.game.views.tokens.PlayerAvatar;
import org.javatuples.Pair;
import org.javatuples.Tuple;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
......@@ -27,7 +30,7 @@ public class NetworkController {
client.start();
try {
//192.168.87.34
String IP4_LAN_ADDRESS = "192.168.10.154";
String IP4_LAN_ADDRESS = "192.168.87.23";
client.connect(10000, IP4_LAN_ADDRESS, 54555, 54777);
} catch (IOException e) {
e.printStackTrace();
......@@ -53,6 +56,8 @@ public class NetworkController {
kryo.register(cLobbyDelete.class, 23);
kryo.register(cLobbyGetList.class, 24);
kryo.register(cSetIndexAvatar.class, 25);
kryo.register(cSetPlayerReady.class, 26);
kryo.register(cSetPlayerName.class, 27);
}
public Client getClient() { return client; }
......
package com.mygdx.game.controllers;
import com.badlogic.gdx.utils.Null;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.mygdx.game.controllers.commands.*;
import com.mygdx.game.model.Lobby;
import com.mygdx.game.model.Player;
import org.javatuples.Pair;
import java.util.ArrayList;
import java.util.LinkedList;
public class PlayerController {
private NetworkController net;
......@@ -45,12 +49,20 @@ public class PlayerController {
public void lobbyGetList(){ sendCommand(new cLobbyGetList()); }
public void setLobbyPlayerReady(boolean isPlayerReady){
sendCommand(new cSetPlayerReady(isPlayerReady, lobby.getID(), player.getID()));
}
public void connectPlayer(int avatarIndex){ sendCommand(new cPlayerJoin(avatarIndex));}
public void createLobby(String name, int MAX_PLAYERS){ sendCommand(new cLobbyCreate(name, MAX_PLAYERS)); }
public void updateIndexAvatar(int indexAvatar){ sendCommand(new cSetIndexAvatar(indexAvatar)); }
public void playerSetName(String name) {
sendCommand(new cSetPlayerName(name));
}
public void deleteLobby(int id){ sendCommand(new cLobbyDelete(id)); }
public void joinLobby(int id){ sendCommand(new cLobbyJoin(id)); }
......@@ -69,6 +81,22 @@ public class PlayerController {
public void setLobbies(ArrayList<Lobby> lobbies){ this.lobbies = lobbies; }
public boolean isOwningPlayerInLobby(){
try{
if(lobby==null){
return false;
}
else if(lobby.getID() == -1){
return false;
}
else{
return true;
}
}catch (NullPointerException e){
return false;
}
}
public PlayerController getPlayerController(){ return this; }
public NetworkController getNetWorkController() { return net; }
......@@ -76,6 +104,4 @@ public class PlayerController {
public Command getLastCommand(){ return receivedCommands.getFirst(); }
}
package com.mygdx.game.controllers.commands;
import com.esotericsoftware.kryonet.Connection;
import com.mygdx.game.controllers.PlayerController;
import com.mygdx.game.model.Lobby;
import com.mygdx.game.model.Player;
import org.javatuples.Pair;
public class cSetPlayerName extends Command{
public cSetPlayerName() { super("cLobbyJoin"); }
public cSetPlayerName(String name) {
super("cLobbyJoin", name);
}
@Override
public void execute(PlayerController playerController, Connection connection){
if(data instanceof String){
String name = (String) data;
playerController.getPlayer().setPlayerName(name);
System.out.printf("Player name is updated on server. %s \n", name);
}
}
}
package com.mygdx.game.controllers.commands;
import com.esotericsoftware.kryonet.Connection;
import com.mygdx.game.controllers.PlayerController;
import com.mygdx.game.model.Lobby;
import com.mygdx.game.model.Player;
import org.javatuples.Pair;
public class cSetPlayerReady extends Command{
public cSetPlayerReady() { super("cLobbyJoin"); }
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(PlayerController playerController, Connection connection){
if(data instanceof Boolean){
boolean isPlayerReady = (Boolean) data;
if(playerID == playerController.getPlayer().getID()) playerController.getPlayer().setIsPlayerReady(isPlayerReady);
playerController.getLobby().getPlayerByID(playerID).setIsPlayerReady(isPlayerReady);
//System.out.println("value of playerID" + playerID);
System.out.printf("Player status updated. Is player %d currently ready? %b \n", playerID, isPlayerReady);
}
}
}
......@@ -34,6 +34,17 @@ public class Lobby {
return name;
}
public Player getPlayerByID(int playerID){
Player returnPlayer = null;
for (Player player : players){
if (player.getID() == playerID) returnPlayer = player;
}
return returnPlayer;
}
public int getMaxPlayers(){
return MAX_PLAYERS;
}
......
......@@ -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;
}
}
......@@ -12,6 +12,7 @@ import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
......@@ -44,12 +45,13 @@ public class LobbyView extends View{
private boolean startRocketTransition;
private Button createLobbyButton;
private Button lobbyListBackButton;
private Button findLobbyButton;
private Button joinLobbyButton;
private Button lobbyBackButton;
private Button lobbyListRefreshButton;
private Button lobbyRefreshButton;
private Button findLobbyButton;
private Button lobbyListBackButton;
private Button playerReadyButton;
private Button setUserNameButton;
private Button startLobbyButton;
......@@ -73,11 +75,12 @@ public class LobbyView extends View{
private TextField lobbyCreateTextField;
private SelectBox<Integer> lobbyCreateSizeSelectBox;
private Image playerReady;
private ImageButton playerReadyCheckButton;
private SelectBox<Integer> lobbyCreateSizeSelectBox;
private ShapeRenderer shapeRenderer;
public LobbyView(GameViewManager gvm, final PlayerController playerController, AssetManager assetManager, final Stage stage, Skin skin, ArrayList<PlayerAvatar> availableAvatars) {
super(gvm, playerController, assetManager, stage, skin);
......@@ -96,16 +99,22 @@ public class LobbyView extends View{
buttonClickAudio = (Sound) assetManager.get(MenuAssets.BUTTON_CLICK_AUDIO.path, MenuAssets.BUTTON_CLICK_AUDIO.classType);
Texture lobbyListTexture = (Texture) assetManager.get(LobbyAssets.LOBBYLIST.path, LobbyAssets.LOBBYLIST.classType);
Image playerReady = new Image(assetManager.get(LobbyAssets.PLAYER_READY.path, Texture.class));
final Image playerReadyCheck = new Image((Texture) assetManager.get(LobbyAssets.PLAYER_READY_CHECK.path, Texture.class));
playerReadyCheckButton = new ImageButton(playerReadyCheck.getDrawable());
playerReadyCheckButton.setPosition(col_width*6, row_height*2);
backgroundImage = new Image(background);
rocketImage = new Image(rocketTexture);
exhaustImage = new Image(exhaustTexture);
rocketImage.setPosition(col_width*8.5f, row_height*4f);
rocketImage.setSize(col_width*1, row_height*2.5f);
exhaustImage = new Image(exhaustTexture);
exhaustImage.setSize(col_width*0.6f, row_height*2f);
exhaustImage.setPosition(rocketImage.getX()+rocketImage.getWidth()/2-exhaustImage.getWidth()/2, rocketImage.getY()-exhaustImage.getHeight());
lobbyListBubbleImage = new Image(lobbyListTexture);
lobbyListBubbleImage.setPosition(col_width*0.25f, row_height*5.5f);
backgroundImage = new Image(background);
outputLabel = new Label("Press a Button",skin,"black");
outputLabel.setSize(Gdx.graphics.getWidth(),row_height);
......@@ -135,6 +144,10 @@ public class LobbyView extends View{
lobbyRefreshButton.setSize(col_width*1f,(float)(row_height*0.65));
lobbyRefreshButton.setPosition(lobbyListBubbleImage.getX()+lobbyListBubbleImage.getWidth()*0.1f,lobbyListBubbleImage.getY()+lobbyListBubbleImage.getHeight()*0.125f+row_height*1);
playerReadyButton = new TextButton("Refresh",skin,"small");
playerReadyButton.setSize(col_width*1f,(float)(row_height*0.65));
playerReadyButton.setPosition(lobbyListBubbleImage.getX()+lobbyListBubbleImage.getWidth()*0.1f,lobbyListBubbleImage.getY()+lobbyListBubbleImage.getHeight()*0.125f+row_height*-1);
lobbyBackButton = new TextButton("Back",skin,"small");
lobbyBackButton.setSize(col_width*1f,(float)(row_height*0.65));
lobbyBackButton.setPosition(lobbyListBubbleImage.getX()+lobbyListBubbleImage.getWidth()*0.6f,lobbyListBubbleImage.getY()+lobbyListBubbleImage.getHeight()*0.125f+row_height*1);
......@@ -203,6 +216,23 @@ public class LobbyView extends View{
outputLabel.setText("createLobbyButton");
}
});
playerReadyCheckButton.addListener(new InputListener(){
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
buttonClickAudio.play(0.8f);
outputLabel.setText("Player Ready");
playerController.setLobbyPlayerReady(true);
}
@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
outputLabel.setText("playerReadyCheckButton");
}
});
findLobbyButton.addListener(new InputListener(){
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
......@@ -351,6 +381,23 @@ public class LobbyView extends View{
}
});
playerReadyButton.addListener(new InputListener(){
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
buttonClickAudio.play(0.8f);
outputLabel.setText("Lobby refreshed!");
lobbyRefresh();
}
@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
outputLabel.setText("lobbyRefreshButton");
}
});
lobbyCreateTextField.addListener(new InputListener(){
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
......@@ -507,6 +554,7 @@ public class LobbyView extends View{
lobbyListRefresh();
}
float[] player1Pos = {col_width*3.75f, row_height*2};
float[] player2Pos = {col_width*3.5f, row_height*1.5f};
......@@ -516,13 +564,32 @@ public class LobbyView extends View{
float[] player5Pos = {col_width*5, row_height*2};
float[] player6Pos = {col_width*4.8f, row_height*1.5f};
float[] player7Pos = {col_width*5.7f, row_height*2};
float[] player8Pos = {col_width*5.5f, row_height*1.5f};
float[] player1LabelPos = {col_width*0.5f, row_height*9.8f};
float[] player2LabelPos = {col_width*0.5f, row_height*9.3f};
float[] player3LabelPos = {col_width*0.5f, row_height*8.8f};
float[] player4LabelPos = {col_width*0.5f, row_height*8.3f};
float[] player5LabelPos = {col_width*0.5f, row_height*7.8f};
float[] player6LabelPos = {col_width*0.5f, row_height*7.3f};
float[] player7LabelPos = {col_width*0.5f, row_height*6.8f};
float[] player8LabelPos = {col_width*0.5f, row_height*6.3f};
private Lobby lobby;
private ArrayList<Player> playersInLobby;
ArrayList<float[]> playerPositions= new ArrayList<>(Arrays.asList(player1Pos, player2Pos, player3Pos, player4Pos, player5Pos, player6Pos));;
ArrayList<float[]> playerPositions= new ArrayList<>(Arrays.asList(player1Pos, player2Pos, player3Pos, player4Pos, player5Pos, player6Pos, player7Pos, player8Pos));;
ArrayList<float[]> playerLabelPositions= new ArrayList<>(Arrays.asList(player1LabelPos, player2LabelPos, player3LabelPos, player4LabelPos, player5LabelPos, player6LabelPos, player7LabelPos, player8LabelPos));;
private final ArrayList<PlayerAvatar> availableAvatars;
private ArrayList<PlayerAvatar> lobbyAvatars = new ArrayList<>();
private ArrayList<Image> playerReadyImages = new ArrayList<>();
private ArrayList<Label> lobbyLabels= new ArrayList<>();
private void lobbyRefresh(){
if(playerController.getLobby() == null) {
System.out.println("Error during lobby refresh: not in a lobby");
......@@ -531,20 +598,53 @@ public class LobbyView extends View{
lobbyAvatars.clear();
lobby = playerController.getLobby();
playersInLobby = lobby.getPlayers();
if(!playerController.isOwningPlayerInLobby() && !playerController.getPlayer().getIsPlayerReady()) playerReadyCheckButton.remove();
else stage.addActor(playerReadyCheckButton);
for (Label label : lobbyLabels) label.remove();
for (Image image : playerReadyImages) image.remove();
//System.out.println("My avatar Index: "+ playerController.getPlayer().getIndexAvatar());
//System.out.println("Number of players in lobby: "+ lobby.getPlayers().size());
System.out.printf(">Current Lobby Size:\t%d\n", lobby.getPlayers().size());
for(int i = 0; i<playersInLobby.size(); i++){
//System.out.println("This players ID: "+ playersInLobby.get(i).getID() + " avatarIndex: "+ playersInLobby.get(i).getIndexAvatar());
PlayerAvatar pa = availableAvatars.get(playersInLobby.get(i).getIndexAvatar());
pa.setScale(0.3f, 0.3f);
pa.setOrigin(0, 0);
pa.setPosition(playerPositions.get(i)[0], playerPositions.get(i)[1]);
lobbyAvatars.add(pa);
Player player = playersInLobby.get(i);
System.out.printf(" >PlayerName:%s\tID:%d\n", player.getPlayerName(), player.getID());
PlayerAvatar playerAvatar =availableAvatars.get(player.getIndexAvatar());
playerAvatar.setScale(0.3f, 0.3f);
playerAvatar.setOrigin(0, 0);
playerAvatar.setPosition(playerPositions.get(i)[0], playerPositions.get(i)[1]);
lobbyAvatars.add(playerAvatar);
Label label = new Label("Player"+i+":\t%d" + player.getPlayerName() + "\t", skin, "black");
label.setColor(1, 0, 0, 1);
label.scaleBy(1.5f);
label.setPosition(playerLabelPositions.get(i)[0], playerLabelPositions.get(i)[1]);
lobbyLabels.add(label);
if(player.getIsPlayerReady()) {
Image image = new Image(assetManager.get(LobbyAssets.PLAYER_READY.path, Texture.class));
image.setPosition(label.getX()+image.getWidth()+col_width*1f, label.getY()-image.getHeight()/2+label.getHeight()/2);
playerReadyImages.add(image);
System.out.println("ready???");
}
else{
System.out.println("Player is not ready..");
}
stage.addActor(lobbyLabels.get(i));
}
for (Image image : playerReadyImages){
stage.addActor(image);
}
}
}
private void showLobbyWindow(){
clearActors();
mainUIActors.add(lobbyBackButton);
......@@ -554,6 +654,7 @@ public class LobbyView extends View{
stage.addActor(actor);
}
}
private void showMainLobbyWindow(){
clearActors();
mainUIActors.add(createLobbyButton);
......@@ -573,6 +674,7 @@ public class LobbyView extends View{
sound.dispose();
*/
}
@Override
public void handleInput() {
if(Gdx.input.isTouched()) {
......@@ -581,13 +683,31 @@ public class LobbyView extends View{
}
}
@Override
public void render(float dt) {
Gdx.gl.glClearColor(1, 1, 1, 1);
stage.draw();
stage.getBatch().begin();
for(PlayerAvatar playerAvatar : lobbyAvatars){
if(updateAvatarTimer>= updateAvatarFrequency){
playerAvatar.updateTexture();
}
playerAvatar.draw(stage.getBatch());
}
if(updateAvatarTimer>= updateAvatarFrequency){
updateAvatarTimer=0;
}
stage.getBatch().end();
drawGrid();
}
float updateAvatarTimer = 0;
float updateAvatarFrequency = 0.120f; //update every 250ms
float refreshLobbyTimer = 1;
@Override
public void render(float dt) {
Gdx.gl.glClearColor(1, 1, 1, 1);
public void update(float dt) {
updateAvatarTimer+= dt;
......@@ -610,23 +730,9 @@ public class LobbyView extends View{
else if(startRocketTransition){
}
stage.draw();
stage.getBatch().begin();
for(PlayerAvatar playerAvatar : lobbyAvatars){
if(updateAvatarTimer>= updateAvatarFrequency){
playerAvatar.updateTexture();
}
playerAvatar.draw(stage.getBatch());
}
if(updateAvatarTimer>= updateAvatarFrequency){
updateAvatarTimer=0;
}
stage.getBatch().end();
drawGrid();
}
@Override
public void update(float dt) {
stage.act(dt);
handleInput();
}
......
......@@ -237,6 +237,7 @@ public class MenuView extends View{
transitionAudio.play(0.50f);
startFadeFromBlankToWhite = true;
whiteImage.setZIndex(stage.getActors().size);
playerController.playerSetName(usernameTextField.getText());
playerController.updateIndexAvatar(currentIndexAvatar);
}
return true;
......@@ -254,8 +255,7 @@ public class MenuView extends View{
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
buttonClickAudio.play(0.8f);
if(usernameTextField.getText().isEmpty()){
playerController.getPlayer().setPlayerName(usernameTextField.getText());
outputLabel.setText("");
outputLabel.setText(usernameTextField.getText());
}
}
});
......
......@@ -7,7 +7,9 @@ public enum LobbyAssets {
LOBBYLIST("Menu/1x/LobbyList.png", Texture.class),
BACKGROUND("Menu/1x/LobbyMenu@1x.png", Texture.class),
ROCKET("Menu/1x/Rocket_Main@1x.png", Texture.class),