158 lines
3.7 KiB
C#
158 lines
3.7 KiB
C#
using System.Collections.Generic;
|
|
using Mirror;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class RTSPlayer : NetworkBehaviour
|
|
{
|
|
[Header("Connections")]
|
|
[SerializeField] private TMP_Text _playerNameObject;
|
|
|
|
[Header("Variables")]
|
|
[SyncVar(hook = nameof(SetPlayerNameTagText))]
|
|
[SerializeField]
|
|
private string _displayName = "Missing Name";
|
|
|
|
[SyncVar(hook = nameof(SetPlayerNameTagColor))]
|
|
[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)
|
|
{
|
|
_displayName = newDisplayName;
|
|
}
|
|
|
|
[Server]
|
|
public void SetPlayerColor(Color newPlayerColor)
|
|
{
|
|
_playerColor = newPlayerColor;
|
|
}
|
|
|
|
[Command]
|
|
private void CmdSetDisplayName(string newDisplayName)
|
|
{
|
|
if (NameLengthIsCorrect(newDisplayName))
|
|
{
|
|
RpcDebugLog($"Setting player names{newDisplayName}");
|
|
SetDisplayName(newDisplayName);
|
|
}
|
|
else
|
|
{
|
|
RpcDebugLog($"new name: {newDisplayName} : is too long");
|
|
}
|
|
}
|
|
|
|
[Command]
|
|
private void CmdSetNewColor()
|
|
{
|
|
Color newColor = ColorManipulation.GetRandomColor();
|
|
RpcDebugLog($"Setting player color{newColor.ToString()}");
|
|
SetPlayerColor(newColor);
|
|
}
|
|
|
|
private bool NameLengthIsCorrect(string newDisplayName)
|
|
{
|
|
return newDisplayName.Length < 10;
|
|
}
|
|
|
|
[ClientRpc]
|
|
private void RpcDebugLog(string message)
|
|
{
|
|
Debug.Log(message);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#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)
|
|
{
|
|
_playerNameObject.text = newText;
|
|
}
|
|
}
|
|
|
|
private void SetPlayerNameTagColor(Color oldColor, Color newColor)
|
|
{
|
|
if (_playerNameObject != null)
|
|
{
|
|
_playerNameObject.color = newColor;
|
|
}
|
|
}
|
|
|
|
[ContextMenu("SetName")]
|
|
private void SetMyName()
|
|
{
|
|
CmdSetDisplayName("New Name");
|
|
}
|
|
|
|
[ContextMenu("NewColor")]
|
|
private void SetNewColor()
|
|
{
|
|
CmdSetNewColor();
|
|
}
|
|
|
|
#endregion
|
|
} |