using System; using System.Collections; using System.Threading.Tasks; using Drawing; using NodeCanvas.BehaviourTrees; using NodeCanvas.Framework; using NodeCanvas.StateMachines; using Reset; using Reset.Units; using Unity.Collections; using Unity.Netcode; using UnityEngine; namespace Reset.Units{ public class Unit : NetworkBehaviour, INetworkSerializeByMemcpy{ public string state; public NetworkVariable testSTate; private FSMOwner fsm; private UnitMovementHandler _movement; internal UnitMovementHandler Movement{ get{ if (!_movement) { _movement = GetComponent(); } return _movement; } } private GraphOwner _graph; internal GraphOwner Graph{ get{ if (!_graph) { _graph = GetComponent(); } return _graph; } } private void Awake(){ fsm = GetComponent(); } public virtual void Start(){ UnitStart(); } public virtual void UnitStart(){ } public bool UnitIsNetworked(){ return NetworkManager.Singleton.IsConnectedClient || NetworkManager.Singleton.IsHost; } public bool UnitIsLocal(){ if (UnitIsNetworked()) { return IsOwner; } return true; } protected virtual void Update(){ UpdateGizmos(); if (GetComponent() != null) { GetComponent().DrawHealthDebug(); } UnitUpdate(); } public virtual void UnitUpdate(){ } void UpdateGizmos(){ DrawOnlineStatusGizmo(); DrawStateGizmo(); } private void DrawOnlineStatusGizmo(){ string onlineStatus = "Not Online"; Color onlineColor = Color.gray; if (UnitIsNetworked() && UnitIsLocal()) { onlineStatus = "Online, Owned"; onlineColor = Color.mediumSeaGreen; } else if (UnitIsNetworked() && !IsSpawned) { onlineStatus = "Not Spawned"; } else if (UnitIsNetworked()) { onlineStatus = "Online, Not Owned"; onlineColor = Color.gold; } Draw.ingame.Label2D(transform.position + Vector3.up * 2.5f, onlineStatus, onlineColor); } [Rpc(SendTo.Owner)] public void SetNewPositionRpc(Vector3 position){ SetNewPosition(position); } public void SetNewPosition(Vector3 position){ var contr = GetComponent(); if (contr) contr.enabled = false; transform.position = position; if (contr) contr.enabled = true; } private void DrawStateGizmo(){ if (fsm && UnitIsLocal()) { testSTate.Value = fsm.currentRootStateName; // if (UnitIsNetworked()) { // // state = fsm.currentRootStateName; // SendStateInformationRpc(fsm.currentRootStateName); // } else { // state = fsm.currentRootStateName; // } } else { fsm = GetComponent(); } // state = state.ToString().ToUpper(); try { Draw.ingame.Label2D(transform.position + Vector3.up * 2.7f, testSTate.Value.ToString(), Color.red); } catch (Exception e) { Debug.LogError(e.Message); } } [Rpc(SendTo.Everyone)] public void SendStateInformationRpc(string newState){ Debug.Log($"newstate! {newState}"); state = newState; } [Rpc(SendTo.Owner)] public void TakeOwnershipRpc(ulong clientID){ GetComponent().ChangeOwnership(clientID); } [Rpc(SendTo.Owner)] public void DoGraphEventRpc(string eventToSend){ Debug.Log(eventToSend); GetComponent().SendEvent(eventToSend); } } }