You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.3 KiB
55 lines
1.3 KiB
using UnityEngine;
|
|
|
|
public class HealthSystem : MonoBehaviour {
|
|
|
|
[Header("Health Settings")]
|
|
public int MaxHp = 20;
|
|
public int CurrentHp = 20;
|
|
public bool invulnerable;
|
|
|
|
#if UNITY_EDITOR
|
|
[HelpAttribute("Change these settings if you want to change the player portrait or player name in the healthbar located in the upperleft corner of the screen.", UnityEditor.MessageType.Info)]
|
|
#endif
|
|
|
|
[Header("Healthbar Settings")]
|
|
public Sprite HUDPortrait;
|
|
public string PlayerName;
|
|
|
|
public delegate void OnHealthChange(float percentage, GameObject GO);
|
|
public static event OnHealthChange onHealthChange;
|
|
|
|
void Start(){
|
|
SendHealthUpdateEvent();
|
|
}
|
|
|
|
//substract health
|
|
public void SubstractHealth(int damage){
|
|
if(!invulnerable){
|
|
|
|
//reduce hp
|
|
CurrentHp = Mathf.Clamp(CurrentHp -= damage, 0, MaxHp);
|
|
|
|
//Health reaches 0
|
|
if (isDead()) gameObject.SendMessage("Death", SendMessageOptions.DontRequireReceiver);
|
|
}
|
|
|
|
SendHealthUpdateEvent();
|
|
}
|
|
|
|
//add health
|
|
public void AddHealth(int amount){
|
|
CurrentHp = Mathf.Clamp(CurrentHp += amount, 0, MaxHp);
|
|
SendHealthUpdateEvent();
|
|
}
|
|
|
|
//health update event
|
|
void SendHealthUpdateEvent(){
|
|
float CurrentHealthPercentage = 1f/MaxHp * CurrentHp;
|
|
if(onHealthChange != null) onHealthChange(CurrentHealthPercentage, gameObject);
|
|
}
|
|
|
|
//death
|
|
bool isDead(){
|
|
return CurrentHp == 0;
|
|
}
|
|
}
|
|
|