AbstractUnit for cross team compatibility

+ New folder in Scripts
This commit is contained in:
Crizomb 2025-01-09 11:44:24 +01:00
parent 385d8669b1
commit f8e85d7134
18 changed files with 112 additions and 80 deletions

View file

@ -0,0 +1,62 @@
using UnityEngine;
[RequireComponent(typeof(MinecraftUnit))]
public class HealthHandler : MonoBehaviour
{
[SerializeField] private float maxHealth;
[SerializeField] private float currentHealth;
[SerializeField] private float armor;
private MinecraftUnit _minecraftUnit;
public void Awake()
{
_minecraftUnit = GetComponent<MinecraftUnit>();
}
public void TakeDamage(float damage)
{
Debug.Assert(damage >= 0, "Damage cannot be negative, use Heal if you want to heal");
currentHealth -= Mathf.Max(0, damage-armor);
if (currentHealth <= 0) Death();
}
public void Heal(float value)
{
Debug.Assert(value >= 0, "value can't be less than zero");
currentHealth = Mathf.Min(currentHealth + value, maxHealth);
}
public float GetArmor()
{
return armor;
}
public void EquipArmor(float armorBoost)
{
Debug.Assert(armorBoost >= 0, "armorBoost can't be less than zero, use UnEquipArmor instead");
armor += armorBoost;
}
public void UnEquipArmor(float armorBoost)
{
Debug.Assert(armorBoost >= 0, "armorBoost can't be less than zero, use EquipArmor instead");
armor -= armorBoost;
}
public void Death()
{
print("you dead");
if (_minecraftUnit.IsTeamA)
{
GlobalsVariable.AliveUnitsTeamA.Remove(_minecraftUnit);
}
else
{
GlobalsVariable.AliveUnitsTeamB.Remove(_minecraftUnit);
}
Destroy(gameObject);
}
}