Ready Up and Clean Up

This commit is contained in:
DerTyp187
2021-12-15 00:18:24 +01:00
parent e8499c91d1
commit 8072265797
9 changed files with 317 additions and 127 deletions

View File

@@ -1,26 +1,62 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using TMPro;
/* TODO:
* - Changing Lobby Title Objects causes bugs on client
*/
/*
* The Lobby Class is used to give all LobbyPlayer an instance above them for managing purposes.
* You could write this into a NetworkManager, but I thought it would be nicer in a seperate script.
*/
public class Lobby : NetworkBehaviour
{
[SerializeField]
public List<LobbyPlayer> LobbyPlayers = new List<LobbyPlayer>();
// Sync Vars
[SyncVar] //A list of all connected player
public List<LobbyPlayer> LobbyPlayers = new List<LobbyPlayer>(); // All player a register themselves when they join (LobbyPlayer.cs)
[SyncVar(hook = "ChangeTitle")]
[SerializeField] private string lobbyTitle;
[SerializeField] string lobbyTitle; // Title/Name of the Lobby; Can only be changed by the host, because of "AuthHost"
public bool AuthHost(LobbyPlayer player)
[SyncVar]
public bool allReady = false; // All players are ready?
void Update()
{
if(LobbyPlayers.IndexOf(player) == 0)
CheckLobbyPlayers(); // Checking the LobbyPlayer List
allReady = CheckAllReady(); // Continous checking if all player are ready
}
public void StartGame() // initializes the In-Game Scene and converts LobbyPlayers to GamePlayers
{
Debug.Log("START");
/* https://youtu.be/HZIzGLe-2f4?t=586
* Start Loading Panel
* Destroy LobbyPlayer
* Instatiate Player Objects and connect them to "conn"
* Switch Scene
*/
}
#region LobbyPlayer Interaction (Public)
/* Public (Where the LobbyPlayer interacts with) */
public bool AuthHost(LobbyPlayer player) // Checks if player is the host
{
// In theory the host should always be the first connected player, which means he is index 0 in the LobbyPlayers-List
if (LobbyPlayers.IndexOf(player) == 0)
{
return true;
}
return false;
}
public void SetTitle(LobbyPlayer player, string text)
public void SetTitle(LobbyPlayer player, string text) // the host can set the LobbyTitle
{
if (AuthHost(player))
{
@@ -28,13 +64,44 @@ public class Lobby : NetworkBehaviour
}
}
public void RegisterPlayer(LobbyPlayer player)
public void RegisterPlayer(LobbyPlayer player) // Where a Player can register himself
{
LobbyPlayers.Add(player);
}
#endregion
public void ChangeTitle(string oldTitle, string newTitle)
#region checks
/* Checks */
bool CheckAllReady() // Checks if all players are ready
{
// Check if all players are ready (if a player is not ready)
foreach (LobbyPlayer player in LobbyPlayers)
{
if (!player.ready)
{
return false;
}
}
return true;
}
void CheckLobbyPlayers() // Checks if all LobbyPlayers in the list are still connected (having a GameObject) -> Clears missing players
{
foreach(LobbyPlayer player in LobbyPlayers)
{
if (player == null)
{
LobbyPlayers.Remove(player);
}
}
}
#endregion
#region hooks
/* HOOKS */
void ChangeTitle(string oldTitle, string newTitle) // Changes the Title Object
{
GameObject.Find("title").GetComponent<TextMeshProUGUI>().text = newTitle;
}
#endregion
}

View File

@@ -1,74 +1,116 @@
using Mirror;
using UnityEngine;
using UnityEngine.UI; // For <Button>
using Mirror;
using TMPro;
using UnityEngine.UI;
/*
* This class manages the LobbyPlayer, which is the player object while the player is connected only to the lobby
* AND is not IN-GAME (just a text-based player)!
* this LobbyPlayer will get converted into a Player/GamePlayer for in-game use.
*/
public class LobbyPlayer : NetworkBehaviour
{
[SerializeField] private Button rdyBtn;
[SerializeField] private TextMeshProUGUI usernameText;
[SerializeField] private TextMeshProUGUI rdyText;
// UI Objects
[SerializeField] Button rdyBtn; // Button Object for listeners
[SerializeField] TextMeshProUGUI rdyButtonText; // Seperatly getting the text, because it's safer instead of "rdy.Btn.getChild..."
[SerializeField] private Button team1Btn;
[SerializeField] private Button team2Btn;
[SerializeField] TextMeshProUGUI usernameText; // Username Text Object -> Where the Username of every LobbyPlayer will get displayed
[SerializeField] TextMeshProUGUI rdyText; // Ready Text Object -> Where the Ready-State of every LobbyPlayer will get displayed
[SerializeField] Button team1Btn; // Join Team 1 Button
[SerializeField] Button team2Btn; // Join Team 2 Button
// Sync vars
[SyncVar(hook = "DisplayPlayerName")]
[SerializeField] public string username;
[SyncVar(hook = "ChangeReadyState")]
[SerializeField] bool ready = false;
public bool ready = false; // is the LobbyPlayer ready?
[SyncVar(hook = "ChangeDisplayTeam")]
[SerializeField] int teamId = 0;
[SerializeField] int teamId = 0; // which team did the player choose?
// Vars
Lobby lobby;
public override void OnStartClient()
{
lobby = GameObject.Find("LobbyManager").GetComponent<Lobby>();
lobby.RegisterPlayer(this);
lobby = GameObject.Find("LobbyManager").GetComponent<Lobby>(); // Get the Lobby Object in Scene
lobby.RegisterPlayer(this); // Register the LobbyPlayer, so the lobby can store him in a list for future use
}
public void Start()
{
if (isLocalPlayer)
{
VariableSaver vs = GameObject.FindGameObjectWithTag("VariableSaver").GetComponent<VariableSaver>();
/*
* The Varaible Saver is used to store vars across different scenes and Player-Objects(LobbyPlayer/GamePlayer).
* In this case it's used to get the values of the players BEFORE they joined the server.
* -> username input field
*/
VariableSaver vs = GameObject.FindGameObjectWithTag("VariableSaver").GetComponent<VariableSaver>();
// Find GameObjects in Scene BY NAME
rdyBtn = GameObject.Find("ReadyButton").GetComponent<Button>();
rdyBtn.onClick.AddListener(CmdChangeReady);
CmdSendName(vs.username);
lobby.SetTitle(this, "Game Of\n" + username);
rdyButtonText = GameObject.Find("RdyBtnText").GetComponent<TextMeshProUGUI>();
team1Btn = GameObject.Find("Team1Btn").GetComponent<Button>();
team2Btn = GameObject.Find("Team2Btn").GetComponent<Button>();
// Set Button Listeners
rdyBtn.onClick.AddListener(CmdChangeReady);
team1Btn.onClick.AddListener(delegate { SelectTeam(1); });
team2Btn.onClick.AddListener(delegate { SelectTeam(2); });
// Send the username from the local variable saver to the synced var
CmdSendName(vs.username);
// Set the lobby title -> only works if you're the host
lobby.SetTitle(this, "Game Of\n" + username);
}
}
[Command]
public void CmdSendName(string playerName)
void Update()
{
username = playerName;
if (isLocalPlayer)
{
/*
* Update the "Ready-Button":
* Change the text based on host or client / ready or not ready.
* Adds and removes Start Listener to the hosts button
*/
rdyBtn.onClick.RemoveListener(CmdStartGame); // Clear listener
if (lobby.AuthHost(this) && lobby.allReady) // If all players are ready and your the host
{
rdyBtn.onClick.AddListener(CmdStartGame);
rdyButtonText.SetText("Start");
}
else // You are not the host OR not all Players are ready
{
if (ready) // You are already ready
{
rdyButtonText.SetText("Un-Ready");
}
else // You are not ready
{
rdyButtonText.SetText("Ready");
}
}
}
}
[Command]
public void CmdChangeReady()
#region hooks
/* HOOKS */
public void DisplayPlayerName(string oldName, string newName) // Changes the text value of the Player-Username-GameObject
{
ready = !ready;
Debug.Log("Player changed name from " + oldName + " to " + newName); // Just for debug -> No future use
usernameText.text = newName; // sets the new text in the gameobject
}
[Command]
public void SelectTeam(int _teamId)
{
teamId = _teamId;
}
public void DisplayPlayerName(string oldName, string newName)
{
Debug.Log("Player changed name from " + oldName + " to " + newName);
usernameText.text = newName;
}
public void ChangeReadyState(bool oldState, bool newState)
public void ChangeReadyState(bool oldState, bool newState) // Changes the Ready-Text-Object of the player
{
// sets the new text based on the ready state of the player
if (newState)
{
rdyText.text = "Ready";
@@ -77,17 +119,49 @@ public class LobbyPlayer : NetworkBehaviour
{
rdyText.text = "Not Ready";
}
}
public void ChangeDisplayTeam(int oldTeamId, int newTeamId)
public void ChangeDisplayTeam(int oldTeamId, int newTeamId) // moves the player into the correct team-list
{
if(newTeamId == 1)
// moves the player based on which team he has choosen
if (newTeamId == 1)
{
gameObject.transform.parent = GameObject.FindGameObjectWithTag("Team1List").transform;
}else if(newTeamId == 2)
}
else if (newTeamId == 2)
{
gameObject.transform.parent = GameObject.FindGameObjectWithTag("Team2List").transform;
}
}
#endregion
#region commands
/* COMMANDS */
[Command]
void CmdStartGame()
{
if (lobby.AuthHost(this))
{
lobby.StartGame();
}
}
[Command]
void CmdSendName(string playerName) //Send/Set the username from local to the synced var
{
username = playerName;
}
[Command]
void CmdChangeReady() // Updates the Ready-State of the Player (Synced Var)
{
ready = !ready;
}
[Command]
void SelectTeam(int _teamId) // Updates the team of the Player (Synced Var)
{
teamId = _teamId;
}
#endregion
}

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8200a488f55a39a46852e1266a825379
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,18 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReadyUp : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 435efa23db9058e4ba98afefb0f5f72e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: