Files
project-reset/Assets/Scripts/Units/Unit.cs
Chris 96c43c6369 feat: better state handling for combat
melee weapon now spawns in hand
melee substate cleaned up to work with above
added new playerinventory script on player
added new playeranimation script on player
changed inventory to IInventory
animations tweaked to avoid a lockout
2026-01-08 01:22:45 -05:00

141 lines
4.3 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; }
}
private UnitCombat _combat;
internal UnitCombat Combat{
get {
if (_combat == null) { _combat = GetComponent<UnitCombat>(); }
return _combat;
}
}
private UnitAnimation _animation;
internal UnitAnimation Animation{
get {
if (_animation == null) { _animation = GetComponent<UnitAnimation>(); }
return _animation;
}
}
// 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
string stateString = "";
if (UnitIsNetworked() && UnitIsLocal()) {
graphStateAsString.Value = FSM.currentRootStateName;
stateString = graphStateAsString.Value.ToString();
} else {
stateString = FSM.currentRootStateName;
}
// Draw state gizmo, regardless of if local or not
try {
Draw.ingame.Label2D(transform.position + Vector3.up * 2.7f, stateString,
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){
GetComponent<FSMOwner>().SendEvent(eventToSend); }
}
}