mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 12:52:07 +01:00
added Ready, NameChange, LobbyTitleChange
Added the function "AuthHost" (lobby.cs) )which can be used to check if the request comes from a host
This commit is contained in:
@@ -1,9 +1,40 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Lobby : MonoBehaviour
|
||||
using Mirror;
|
||||
using TMPro;
|
||||
public class Lobby : NetworkBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
public List<LobbyPlayer> LobbyPlayers = new List<LobbyPlayer>();
|
||||
|
||||
[SyncVar(hook = "ChangeTitle")]
|
||||
[SerializeField] private string lobbyTitle;
|
||||
|
||||
public bool AuthHost(LobbyPlayer player)
|
||||
{
|
||||
if(LobbyPlayers.IndexOf(player) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SetTitle(LobbyPlayer player, string text)
|
||||
{
|
||||
if (AuthHost(player))
|
||||
{
|
||||
lobbyTitle = text;
|
||||
}
|
||||
}
|
||||
|
||||
public void RegisterPlayer(LobbyPlayer player)
|
||||
{
|
||||
LobbyPlayers.Add(player);
|
||||
}
|
||||
|
||||
public void ChangeTitle(string oldTitle, string newTitle)
|
||||
{
|
||||
GameObject.Find("title").GetComponent<TextMeshProUGUI>().text = newTitle;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,69 @@
|
||||
using Mirror;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class LobbyPlayer : NetworkBehaviour
|
||||
{
|
||||
private Lobby lobby = this.gameobject.getComponent<Lobby>();
|
||||
[SerializeField] private Button rdyBtn;
|
||||
[SerializeField] private TextMeshProUGUI usernameText;
|
||||
[SerializeField] private TextMeshProUGUI rdyText;
|
||||
|
||||
[SyncVar(hook = "DisplayPlayerName")]
|
||||
[SerializeField] public string username;
|
||||
|
||||
[SyncVar(hook = "ChangeReadyState")]
|
||||
[SerializeField] bool ready = false;
|
||||
|
||||
Lobby lobby;
|
||||
public override void OnStartClient()
|
||||
{
|
||||
lobby = GameObject.Find("LobbyManager").GetComponent<Lobby>();
|
||||
lobby.RegisterPlayer(this);
|
||||
|
||||
gameObject.transform.parent = GameObject.FindGameObjectWithTag("Team1").transform;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
if (isLocalPlayer)
|
||||
{
|
||||
VariableSaver vs = GameObject.FindGameObjectWithTag("VariableSaver").GetComponent<VariableSaver>();
|
||||
rdyBtn = GameObject.Find("ReadyButton").GetComponent<Button>();
|
||||
rdyBtn.onClick.AddListener(CmdChangeReady);
|
||||
CmdSendName(vs.username);
|
||||
lobby.SetTitle(this, "Game Of\n" + username);
|
||||
}
|
||||
}
|
||||
|
||||
[Command]
|
||||
public void CmdSendName(string playerName)
|
||||
{
|
||||
username = playerName;
|
||||
}
|
||||
|
||||
[Command]
|
||||
public void CmdChangeReady()
|
||||
{
|
||||
ready = !ready;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (newState)
|
||||
{
|
||||
rdyText.text = "Ready";
|
||||
}
|
||||
else
|
||||
{
|
||||
rdyText.text = "Not Ready";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,17 @@ using TMPro;
|
||||
public class MenuStartClient : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private TMP_InputField IpInput;
|
||||
[SerializeField] private TMP_InputField UsernameInput;
|
||||
public void StartClient()
|
||||
{
|
||||
Debug.Log("[MENU] Starting client...");
|
||||
|
||||
NetworkManager.singleton.networkAddress = IpInput.text;
|
||||
NetworkManager.singleton.StartClient();
|
||||
if(UsernameInput.text != null)
|
||||
{
|
||||
Debug.Log("[MENU] Starting client...");
|
||||
|
||||
GameObject.FindGameObjectWithTag("VariableSaver").GetComponent<VariableSaver>().username = UsernameInput.text;
|
||||
|
||||
NetworkManager.singleton.networkAddress = IpInput.text;
|
||||
NetworkManager.singleton.StartClient();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
using Mirror;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
|
||||
public class MenuStartHost : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private TMP_InputField UsernameInput;
|
||||
public void StartHost()
|
||||
{
|
||||
Debug.Log("[MENU] Starting host...");
|
||||
NetworkManager.singleton.StartHost();
|
||||
if (UsernameInput.text != null)
|
||||
{
|
||||
Debug.Log("[MENU] Starting host...");
|
||||
GameObject.FindGameObjectWithTag("VariableSaver").GetComponent<VariableSaver>().username = UsernameInput.text;
|
||||
NetworkManager.singleton.StartHost();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
11
Assets/Scripts/VariableSaver.cs
Normal file
11
Assets/Scripts/VariableSaver.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class VariableSaver : MonoBehaviour
|
||||
{
|
||||
public string username;
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/VariableSaver.cs.meta
Normal file
11
Assets/Scripts/VariableSaver.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57fc6cd6aec47d54a8d9dc70e91de8d1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user