120 lines
3.7 KiB
C#
120 lines
3.7 KiB
C#
using System;
|
|
using Drawing;
|
|
using NodeCanvas.Framework;
|
|
using NodeCanvas.StateMachines;
|
|
using Unity.Collections;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
|
|
namespace Reset.Units{
|
|
public class Unit : NetworkBehaviour, INetworkSerializeByMemcpy{
|
|
// References
|
|
private FSMOwner _fsm;
|
|
internal FSMOwner FSM{
|
|
get{ if (!_fsm) { _fsm = GetComponent<FSMOwner>(); } return _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; }
|
|
}
|
|
|
|
// Debug and Gizmos
|
|
public NetworkVariable<FixedString64Bytes> graphStateAsString;
|
|
|
|
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();
|
|
}
|
|
|
|
// Draw Gizmos
|
|
void UpdateGizmos(){
|
|
if (GetComponent<IKillable>() != null) {
|
|
GetComponent<IKillable>().DrawHealthDebug();
|
|
}
|
|
|
|
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){
|
|
// Set position, disabling the character controller if one is available
|
|
var contr = GetComponent<CharacterController>();
|
|
if (contr) contr.enabled = false;
|
|
|
|
transform.position = position;
|
|
|
|
if (contr) contr.enabled = true;
|
|
}
|
|
|
|
private void DrawStateGizmo(){
|
|
// Get state from FSM
|
|
if (UnitIsLocal()) {
|
|
graphStateAsString.Value = FSM.currentRootStateName;
|
|
}
|
|
|
|
// Draw state gizmo, regardless of if local or not
|
|
try {
|
|
Draw.ingame.Label2D(transform.position + Vector3.up * 2.7f, graphStateAsString.Value.ToString(),
|
|
Color.red);
|
|
} catch (Exception e) {
|
|
Debug.LogError(e.Message);
|
|
}
|
|
}
|
|
|
|
[Rpc(SendTo.Owner)]
|
|
public void TakeOwnershipRpc(ulong newOwnerID){
|
|
GetComponent<NetworkObject>().ChangeOwnership(newOwnerID);
|
|
}
|
|
|
|
[Rpc(SendTo.Owner)]
|
|
public void DoGraphEventRpc(string eventToSend){
|
|
Debug.Log(eventToSend);
|
|
GetComponent<FSMOwner>().SendEvent(eventToSend); }
|
|
}
|
|
} |