mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-30 04:57:10 +01:00
Ready Up and Clean Up
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user