하트 시스템, 2d 버튼 에셋 추가
This commit is contained in:
67
Gameton-06/Assets/HealthHeartSystem/Scripts/PlayerStats.cs
Normal file
67
Gameton-06/Assets/HealthHeartSystem/Scripts/PlayerStats.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Author: ariel oliveira [o.arielg@gmail.com]
|
||||
*/
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerStats : MonoBehaviour
|
||||
{
|
||||
public delegate void OnHealthChangedDelegate();
|
||||
public OnHealthChangedDelegate onHealthChangedCallback;
|
||||
|
||||
#region Sigleton
|
||||
private static PlayerStats instance;
|
||||
public static PlayerStats Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (instance == null)
|
||||
instance = FindObjectOfType<PlayerStats>();
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
[SerializeField]
|
||||
private float health;
|
||||
[SerializeField]
|
||||
private float maxHealth;
|
||||
[SerializeField]
|
||||
private float maxTotalHealth;
|
||||
|
||||
public float Health { get { return health; } }
|
||||
public float MaxHealth { get { return maxHealth; } }
|
||||
public float MaxTotalHealth { get { return maxTotalHealth; } }
|
||||
|
||||
public void Heal(float health)
|
||||
{
|
||||
this.health += health;
|
||||
ClampHealth();
|
||||
}
|
||||
|
||||
public void TakeDamage(float dmg)
|
||||
{
|
||||
health -= dmg;
|
||||
ClampHealth();
|
||||
}
|
||||
|
||||
public void AddHealth()
|
||||
{
|
||||
if (maxHealth < maxTotalHealth)
|
||||
{
|
||||
maxHealth += 1;
|
||||
health = maxHealth;
|
||||
|
||||
if (onHealthChangedCallback != null)
|
||||
onHealthChangedCallback.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
void ClampHealth()
|
||||
{
|
||||
health = Mathf.Clamp(health, 0, maxHealth);
|
||||
|
||||
if (onHealthChangedCallback != null)
|
||||
onHealthChangedCallback.Invoke();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user