Files
project-reset/Assets/Scripts/Units/Unit.cs

143 lines
4.0 KiB
C#

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<FixedString64Bytes> testSTate;
private FSMOwner fsm;
private UnitMovementHandler _movement;
internal UnitMovementHandler Movement{
get{
if (!_movement) {
_movement = GetComponent<UnitMovementHandler>();
}
return _movement;
}
}
private GraphOwner _graph;
internal GraphOwner Graph{
get{
if (!_graph) {
_graph = GetComponent<GraphOwner>();
}
return _graph;
}
}
private void Awake(){
fsm = GetComponent<FSMOwner>();
}
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<IKillable>() != null) {
GetComponent<IKillable>().DrawHealthDebug();
}
UnitUpdate();
}
public virtual void SetNewPosition(Vector3 position){ }
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);
}
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<FSMOwner>();
}
// 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<NetworkObject>().ChangeOwnership(clientID);
}
[Rpc(SendTo.Owner)]
public void DoGraphEventRpc(string eventToSend){
Debug.Log(eventToSend);
GetComponent<FSMOwner>().SendEvent(eventToSend); }
}
}