maint: clean-up of some combat related classes and methods for clarity

This commit is contained in:
Chris
2025-10-23 13:04:56 -04:00
parent eb1622d4ed
commit a83ab1156a
13 changed files with 135 additions and 527 deletions

View File

@@ -1,9 +1,6 @@
using UnityEngine;
namespace Reset.Core{
public interface IDamageable{
public void TakeDamage(DamageSource source);
public void TakeDamage(DamageSource[] sources);
}
}

View File

@@ -6,14 +6,17 @@ using UnityEngine;
namespace Reset.Units{
public class Enemy : Unit, ILockOnTarget, IKillable {
// Lock-On
public float lockonTargetRadius{ get; set; } = 10f;
[ShowInInspector]
public bool lockonDebug{ get; set; } = true;
public float lockonRaycastVerticalOffset{ get; set; } = 1f;
// IKillable
public float maxHealth{ get; set; }
public float currentHealth{ get; set; }
// Object References
public Animator testModelAnimator;
public override void UnitStart(){
@@ -54,7 +57,7 @@ namespace Reset.Units{
}
[Rpc(SendTo.Everyone)]
void SetHealthRpc(float newHealth){
public void SetHealthRpc(float newHealth){
currentHealth = newHealth;
}

View File

@@ -1,5 +1,6 @@
using Drawing;
using Reset.Core;
using Unity.Netcode;
using UnityEngine;
namespace Reset.Units{
@@ -21,6 +22,9 @@ namespace Reset.Units{
currentHealth = maxHealth;
}
}
[Rpc(SendTo.Everyone)]
void SetHealthRpc(float health);
internal void DrawHealthDebug(){
using (Draw.WithColor(Color.blue)) {

View File

@@ -7,25 +7,35 @@ using Unity.Netcode;
namespace Reset.Units{
public class Player : Unit, IKillable, IInteractable{
[HideInInspector] public PlayerControls controls;
// IKillable
public float maxHealth{ get; set; }
public float currentHealth{ get; set; }
// State management (don't like this being used like this)
public NetworkVariable<bool> _isDowned;
public bool IsDowned{
get{
if (UnitIsLocal()){
_isDowned.Value = FSM.currentRootStateName == "Downed";
}
return _isDowned.Value;
}
}
public bool IsDowned => _isDowned.Value;
// References
public GameObject pickupTarget; // NOTE: Might be removed in a refactor (https://thunderstar.codecks.io/card/15v-refactor-interaction-handler)
private float timeDowned;
public GameObject pickupTarget;
// (TEMP) Revive UI
// NOTE: When I make the actual UI it's a good idea to have them somehow inherit from a class or something that
// will make them persist for more than a frame since Rpc calls will make it flash
public float persistDrawingRevive;
public float lastKnownReviveTime;
void Awake(){
controls = GetComponent<PlayerControls>();
maxHealth = 20f;
}
public void Attach(){
void AttachToGame(){
if (IsLocalPlayer || !UnitIsNetworked()) { //
PlayerManager.Player = gameObject;
@@ -38,7 +48,8 @@ namespace Reset.Units{
base.UnitStart();
SetPlayerName();
Attach();
AttachToGame();
((IKillable)this).IKillableInitialize();
}
@@ -49,8 +60,14 @@ namespace Reset.Units{
}
}
public override void UnitUpdate(){
GetComponent<IKillable>().DrawHealthDebug();
protected override void Update(){
base.Update();
// Draw Revive UI for at least .5 seconds to prevent flashing
if (persistDrawingRevive > 0) {
persistDrawingRevive -= 1f * Time.deltaTime;
DrawReviveBarRpc(lastKnownReviveTime);
}
}
public void TakeDamage(DamageSource[] sources){
@@ -59,32 +76,36 @@ namespace Reset.Units{
}
}
public void TakeDamage(DamageSource source){
((IKillable)this).currentHealth -= source.damageDealt;
public void TakeDamage(DamageSource source){
// Calculate health after damage, locally
float newHealth = ((IKillable)this).currentHealth - source.damageDealt;
// Tell every unit to set the new health value
if (UnitIsNetworked()) {
SetNewHealthRpc(currentHealth);
SetHealthRpc(newHealth);
}
}
// TODO: Move somewhere not stupid so there's not an identical method in Enemy.cs
[Rpc(SendTo.Everyone)]
public void SetNewHealthRpc(float health){
public void SetHealthRpc(float health){
// Set health to new value, clamped to 0
health = Mathf.Max(health, 0f);
currentHealth = health;
// NOTE: only here for testing. Move out of here into some "post damage" check method
if (((IKillable)this).currentHealth <= 0) {
// if (UnitIsNetworked()) {
Down();
// } else {
// Kill();
// }
// For local players, run things based on health value.
// This Rpc is global but only the owner checks health
CheckHealth();
}
void CheckHealth(){
if (UnitIsLocal()){
if (currentHealth <= 0f) {
Down();
}
}
}
public void Down(){
void Down(){
Graph.SendEvent("Downed");
}
@@ -93,6 +114,7 @@ namespace Reset.Units{
}
public void Interact(){
// Interaction for picking up allies
// Check if the other player can be interacted with at all
if (pickupTarget&& pickupTarget.GetComponent<Player>().CanInteract()) {
// Tell the local player to start picking up the ally and switch states
@@ -109,7 +131,6 @@ namespace Reset.Units{
[Rpc(SendTo.Owner)]
public void StartPickupRpc(){
// When picked up by another player, move into the pick up state
// TODO: Turn all these send events into a goddamn task maybe? State stuff should happen in the state machine. Concurrent stuff should happen in code
Graph.SendEvent("Pick Up Start");
}
@@ -120,8 +141,6 @@ namespace Reset.Units{
while (elapsed < 7f) {
elapsed += 1f * Time.deltaTime;
yield return null;
}
@@ -152,24 +171,35 @@ namespace Reset.Units{
[Rpc(SendTo.Everyone)]
public void DrawReviveBarRpc(float elapsedTime){
Debug.Log("test is drawn");
// Draw a (shoddy) UI bar that shows the revie progress
using (Draw.ingame.WithLineWidth(5f)) {
using (Draw.InLocalSpace(transform)){
// Set width of the bar
float width = .9f;
float widthDone = width * elapsedTime / 5f;
float widthDone = width * elapsedTime / 5f; // 5f assumes a 5 second revive time
// Clamp the red bar showing how much is progresseed to the max of width of the purple bar background (to prevent it going outside)
widthDone = Mathf.Clamp(widthDone, 0, width);
Vector3 pos = Vector3.up * 1.8f + Vector3.left * width + Vector3.forward * .01f;
Draw.ingame.Line(Camera.main.transform.rotation * pos, Camera.main.transform.rotation *
(pos + Vector3.right * width * 2f), Color.rebeccaPurple);
// Draw background bar
Vector3 pos = Vector3.up * 1.8f + Vector3.left * width + Vector3.forward * +.01f;
Draw.ingame.Line(
transform.position + Camera.main.transform.rotation * pos,
transform.position + Camera.main.transform.rotation * (pos + Vector3.right * width * 2f),
Color.rebeccaPurple
);
// Draw foreground bar
Vector3 donePos = Vector3.up * 1.8f + Vector3.left * widthDone;
Draw.ingame.Line(Camera.main.transform.rotation * donePos, Camera.main.transform.rotation *
(donePos + Vector3.right * widthDone * 2f), Color.red);
Draw.ingame.Line(
transform.position + Camera.main.transform.rotation * donePos,
transform.position + Camera.main.transform.rotation * (donePos + Vector3.right * widthDone * 2f),
Color.red
);
}
}
// Set last known time so that online players don't have flashing UIs
lastKnownReviveTime = elapsedTime;
}

View File

@@ -1,51 +1,32 @@
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;
// 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;
}
get{ if (!_movement) { _movement = GetComponent<UnitMovementHandler>(); } return _movement; }
}
private GraphOwner _graph;
internal GraphOwner Graph{
get{
if (!_graph) {
_graph = GetComponent<GraphOwner>();
}
return _graph;
}
get{ if (!_graph) { _graph = GetComponent<GraphOwner>(); } return _graph; }
}
private void Awake(){
fsm = GetComponent<FSMOwner>();
}
// Debug and Gizmos
public NetworkVariable<FixedString64Bytes> graphStateAsString;
public virtual void Start(){
UnitStart();
}
@@ -67,17 +48,14 @@ namespace Reset.Units{
protected virtual void Update(){
UpdateGizmos();
}
// Draw Gizmos
void UpdateGizmos(){
if (GetComponent<IKillable>() != null) {
GetComponent<IKillable>().DrawHealthDebug();
}
UnitUpdate();
}
public virtual void UnitUpdate(){ }
void UpdateGizmos(){
DrawOnlineStatusGizmo();
DrawStateGizmo();
}
@@ -104,50 +82,34 @@ namespace Reset.Units{
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;
if (contr) contr.enabled = false;
transform.position = position;
if (contr)
contr.enabled = true;
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<FSMOwner>();
// Get state from FSM
if (UnitIsLocal()) {
graphStateAsString.Value = FSM.currentRootStateName;
}
// state = state.ToString().ToUpper();
// Draw state gizmo, regardless of if local or not
try {
Draw.ingame.Label2D(transform.position + Vector3.up * 2.7f, testSTate.Value.ToString(), Color.red);
Draw.ingame.Label2D(transform.position + Vector3.up * 2.7f, graphStateAsString.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);
public void TakeOwnershipRpc(ulong newOwnerID){
GetComponent<NetworkObject>().ChangeOwnership(newOwnerID);
}
[Rpc(SendTo.Owner)]

View File

@@ -8,13 +8,9 @@ namespace Reset.Units{
// Temporary
private float inputMagnitude;
void Start(){
}
void Update(){
// Temporary
try {
// Temporary
inputMagnitude = Mathf.MoveTowards(inputMagnitude, GetComponent<PlayerControls>().rawMoveInput.magnitude * 2f, 6f * Time.deltaTime);
modelAnimator.SetFloat("Move Direction X", Unit.Movement.GetResolvedDirectionLocal().x * inputMagnitude);
@@ -23,21 +19,6 @@ namespace Reset.Units{
Debug.LogError($"Failed in setting X and Y move direction floats: {e.Message}");
}
}
public void SendTriggerToAnimator(string trigger){
if (!modelAnimator) {
Debug.LogError($"There is no animator set to recieve the trigger '{trigger}' on unit {name}.", gameObject);
return;
}
try {
modelAnimator.SetTrigger(trigger);
}
catch (Exception e)
{
Debug.LogError($"Failed to send trigger to Animator: {e.Message}");
}
}
}