diff --git a/Assets/Scripts/Networking/RTSNetworkManager.cs b/Assets/Scripts/Networking/RTSNetworkManager.cs index bd5d25e..fafe64e 100644 --- a/Assets/Scripts/Networking/RTSNetworkManager.cs +++ b/Assets/Scripts/Networking/RTSNetworkManager.cs @@ -10,7 +10,7 @@ public class RtsNetworkManager : NetworkManager { base.OnServerAddPlayer(conn); - NetworkPlayer player = conn.identity.GetComponent(); + RTSPlayer player = conn.identity.GetComponent(); player.SetDisplayName($"Player {numPlayers.ToString()}"); player.SetPlayerColor(ColorManipulation.GetRandomColor()); diff --git a/Assets/Scripts/Networking/NetworkPlayer.cs b/Assets/Scripts/Networking/RTSPlayer.cs similarity index 55% rename from Assets/Scripts/Networking/NetworkPlayer.cs rename to Assets/Scripts/Networking/RTSPlayer.cs index caa5739..b7c3bbd 100644 --- a/Assets/Scripts/Networking/NetworkPlayer.cs +++ b/Assets/Scripts/Networking/RTSPlayer.cs @@ -1,8 +1,9 @@ +using System.Collections.Generic; using Mirror; using TMPro; using UnityEngine; -public class NetworkPlayer : NetworkBehaviour +public class RTSPlayer : NetworkBehaviour { [Header("Connections")] [SerializeField] private TMP_Text _playerNameObject; @@ -16,8 +17,40 @@ public class NetworkPlayer : NetworkBehaviour [SerializeField] private Color _playerColor = Color.black; + [SerializeField] private List myUnits; + #region Server + public override void OnStartServer() + { + Unit.ServerOnUnitSpawned += ServerHandleUnitSpawned; + Unit.ServerOnUnitDeSpawned += ServerHandleUnitDeSpawned; + } + + public override void OnStopServer() + { + Unit.ServerOnUnitSpawned -= ServerHandleUnitSpawned; + Unit.ServerOnUnitDeSpawned -= ServerHandleUnitDeSpawned; + } + + private void ServerHandleUnitSpawned(Unit unit) + { + var unitBelongsToClient = unit.connectionToClient.connectionId == connectionToClient.connectionId; + if (unitBelongsToClient) + { + myUnits.Add(unit); + } + } + + private void ServerHandleUnitDeSpawned(Unit unit) + { + var unitBelongsToClient = unit.connectionToClient.connectionId == connectionToClient.connectionId; + if (unitBelongsToClient) + { + myUnits.Remove(unit); + } + } + [Server] public void SetDisplayName(string newDisplayName) { @@ -67,6 +100,32 @@ public class NetworkPlayer : NetworkBehaviour #region Client + public override void OnStartClient() + { + if (!isClientOnly) return; + Unit.AuthorityOnUnitSpawned += AuthorityHandleUnitSpawned; + Unit.AuthorityOnUnitDeSpawned += AuthorityHandleUnitDeSpawned; + } + + public override void OnStopClient() + { + if (!isClientOnly) return; + Unit.AuthorityOnUnitSpawned -= AuthorityHandleUnitSpawned; + Unit.AuthorityOnUnitDeSpawned -= AuthorityHandleUnitDeSpawned; + } + + private void AuthorityHandleUnitSpawned(Unit unit) + { + if (!isOwned) return; + myUnits.Add(unit); + } + + private void AuthorityHandleUnitDeSpawned(Unit unit) + { + if (!isOwned) return; + myUnits.Remove(unit); + } + private void SetPlayerNameTagText(string oldText, string newText) { if (_playerNameObject != null) diff --git a/Assets/Scripts/Networking/NetworkPlayer.cs.meta b/Assets/Scripts/Networking/RTSPlayer.cs.meta similarity index 100% rename from Assets/Scripts/Networking/NetworkPlayer.cs.meta rename to Assets/Scripts/Networking/RTSPlayer.cs.meta diff --git a/Assets/Scripts/Units/Unit.cs b/Assets/Scripts/Units/Unit.cs index 0c1bfe8..ab6eeca 100644 --- a/Assets/Scripts/Units/Unit.cs +++ b/Assets/Scripts/Units/Unit.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using Mirror; @@ -11,10 +12,43 @@ public class Unit : NetworkBehaviour [SerializeField] private UnityEvent _onSelect; [SerializeField] private UnityEvent _onDeSelect; + public static event Action ServerOnUnitSpawned; + public static event Action ServerOnUnitDeSpawned; + public static event Action AuthorityOnUnitSpawned; + public static event Action AuthorityOnUnitDeSpawned; + public UnitMovement UnitMovement => _unitMovement; - + + #region Server + + public override void OnStartServer() + { + ServerOnUnitSpawned?.Invoke(this); + } + + public override void OnStopServer() + { + ServerOnUnitDeSpawned?.Invoke(this); + } + + #endregion + #region Client + public override void OnStartClient() + { + if (!isClientOnly) return; + if (!isOwned) return; + AuthorityOnUnitSpawned?.Invoke(this); + } + + public override void OnStopClient() + { + if (!isClientOnly) return; + if (!isOwned) return; + AuthorityOnUnitDeSpawned?.Invoke(this); + } + [Client] public void Select() {