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

@@ -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;
}