Addind Units to a List on the player owning them
This commit is contained in:
parent
77db237e9e
commit
369edeb584
|
@ -10,7 +10,7 @@ public class RtsNetworkManager : NetworkManager
|
|||
{
|
||||
base.OnServerAddPlayer(conn);
|
||||
|
||||
NetworkPlayer player = conn.identity.GetComponent<NetworkPlayer>();
|
||||
RTSPlayer player = conn.identity.GetComponent<RTSPlayer>();
|
||||
player.SetDisplayName($"Player {numPlayers.ToString()}");
|
||||
player.SetPlayerColor(ColorManipulation.GetRandomColor());
|
||||
|
||||
|
|
|
@ -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<Unit> 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)
|
|
@ -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<Unit> ServerOnUnitSpawned;
|
||||
public static event Action<Unit> ServerOnUnitDeSpawned;
|
||||
public static event Action<Unit> AuthorityOnUnitSpawned;
|
||||
public static event Action<Unit> 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()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue