fgm24/Assets/Scripts/Player/HealthComponent.cs

82 lines
2.0 KiB
C#
Raw Normal View History

2024-02-03 01:14:34 +01:00
using UnityEngine;
using UnityEngine.Events;
using TMPro;
2024-02-04 01:25:59 +01:00
using System;
using UnityUtils;
2024-02-03 01:14:34 +01:00
public class HealthComponent : MonoBehaviour, ISquezeDamageReceiver
{
2024-02-04 01:25:59 +01:00
[SerializeField] float maxHealth = 100;
[SerializeField] float damageTickDelay = 0.25f;
private float currentDamageTick = 0f;
private float accumulatedDamageInTick = 0f;
public float currentHealth { get; private set; }
public static event Action<Vector3, float> OnHealthChangeAtPos;
2024-02-03 01:14:34 +01:00
public UnityEvent OnHealthZero;
2024-02-04 01:25:59 +01:00
public UnityEvent<float, float> OnHealthChange;
2024-02-03 01:14:34 +01:00
[Header("Squeze Damage")]
[SerializeField]
float minThreshold = 1f;
[SerializeField]
float squezeDamageScalor = 1f;
void Awake()
{
currentHealth = maxHealth;
}
2024-02-04 01:25:59 +01:00
void Update()
{
if (currentDamageTick < Time.time)
{
if (accumulatedDamageInTick < 1f) return;
OnHealthChangeAtPos?.Invoke(transform.position.Add(y: 2f), accumulatedDamageInTick);
currentDamageTick = Time.time + damageTickDelay;
accumulatedDamageInTick = 0f;
}
}
public float getMaxHealth() {
2024-02-03 18:07:52 +01:00
return maxHealth;
}
2024-02-04 01:25:59 +01:00
public void setMaxHealth(float amount, bool heal = false) {
2024-02-03 18:07:52 +01:00
maxHealth = amount;
if (heal)
currentHealth = amount;
}
2024-02-04 01:25:59 +01:00
public void TakeDamage(float damage)
2024-02-03 01:14:34 +01:00
{
currentHealth -= damage;
OnHealthChange?.Invoke(currentHealth + damage, currentHealth);
2024-02-04 01:25:59 +01:00
accumulatedDamageInTick += damage;
if (currentHealth <= 0) {
2024-02-03 01:14:34 +01:00
OnHealthZero?.Invoke();
2024-02-04 00:19:46 +01:00
BloodComputeShader.Instance.createBlood(transform.position, (int)(maxHealth *maxHealth * BloodComputeShader.Instance.scoreMult), maxHealth / 25.0f);
}
2024-02-03 01:14:34 +01:00
}
public void TakeSquezeDamage(float squezeDamage)
{
if (squezeDamage < minThreshold) return;
TakeDamage((int) Mathf.Round(squezeDamage * squezeDamageScalor));
}
public void SimpleKill()
{
Destroy(gameObject);
}
}