added: full logic and ability for downing and picking up allies

This commit is contained in:
Chris
2025-10-20 16:36:23 -04:00
parent 021e664320
commit 0bb10a8f46
10 changed files with 489 additions and 64 deletions

View File

@@ -1,24 +1,28 @@
using System;
using System.Collections;
using Drawing;
using Reset;
using Reset.Core;
using Reset.Core.Tools;
using Reset.Units;
using UnityEngine;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using UnityEngine;
using Unity.Netcode;
namespace Reset.Units{
public class Player : Unit, IKillable{
public class Player : Unit, IKillable, IInteractable{
[HideInInspector] public PlayerControls controls;
public float maxHealth{ get; set; }
public float currentHealth{ get; set; }
float IKillable.maxHealth{ get; set; }
float IKillable.currentHealth{ get; set; }
public NetworkVariable<bool> _isDowned;
public bool IsDowned => _isDowned.Value;
private float timeDowned;
public GameObject pickupTarget;
void Awake(){
controls = GetComponent<PlayerControls>();
maxHealth = 20f;
}
public void Attach(){
@@ -47,6 +51,14 @@ namespace Reset.Units{
public override void UnitUpdate(){
GetComponent<IKillable>().DrawHealthDebug();
if (IsDowned) {
timeDowned += 1f * Time.deltaTime;
}
if (timeDowned > 5f) {
Kill();
}
}
public void TakeDamage(DamageSource[] sources){
@@ -58,14 +70,107 @@ namespace Reset.Units{
public void TakeDamage(DamageSource source){
((IKillable)this).currentHealth -= source.damageDealt;
if (UnitIsNetworked()) {
SetNewHealthRpc(currentHealth);
}
}
// TODO: Move somewhere not stupid so there's not an identical method in Enemy.cs
[Rpc(SendTo.Everyone)]
public void SetNewHealthRpc(float health){
currentHealth = health;
// NOTE: only here for testing. Move out of here into some "post damage" check method
if (((IKillable)this).currentHealth <= 0) {
Kill();
// if (UnitIsNetworked()) {
Down();
// } else {
// Kill();
// }
}
}
public void Kill(){
throw new NotImplementedException();
public override void SetNewPosition(Vector3 position){
var contr = GetComponent<CharacterController>();
contr.enabled = false;
transform.position = position;
contr.enabled = true;
}
public void Down(){
Graph.SendEvent("Downed");
}
public void Kill(){
Graph.SendEvent("Killed");
}
public void Interact(){
// 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
Graph.SendEvent("Picking Up Ally");
// Tell the target player to start getting picked up.
pickupTarget.GetComponent<Player>().StartPickupRpc();
// Wait for the pickup timer to finish
StartCoroutine(PickupTimer());
}
}
[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");
}
IEnumerator PickupTimer(){
// Start a timer and wait for it to complete
float elapsed = 0f;
while (elapsed < 7f) {
elapsed += 1f * Time.deltaTime;
yield return null;
}
Graph.SendEvent("Pick Up Success");
}
public bool CanInteract(){
return IsDowned;
}
public void CancelInteract(){
Graph.SendEvent("Pick Up Failed");
}
public void OnObserverDetected(EnvironmentObserver observer){
// Try and get a Player component from the current hit object
// The rest of the logic will continue as expected so long as an
pickupTarget = observer.hit.collider.gameObject;
DrawInteractStatus();
}
void DrawInteractStatus(){
using (Draw.WithColor(Color.blue)) {
Draw.ingame.Label2D(transform.position + Vector3.up * 2.5f, "Interactable",
Color.orchid);
}
}
void LateUpdate(){
// Clear the pickupTarget every frame
// NOTE: Will this work online?
pickupTarget = null;
}
}
}