하트 시스템, 2d 버튼 에셋 추가
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Author: ariel oliveira [o.arielg@gmail.com]
|
||||
*/
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class HealthBarController : MonoBehaviour
|
||||
{
|
||||
private GameObject[] heartContainers;
|
||||
private Image[] heartFills;
|
||||
|
||||
public Transform heartsParent;
|
||||
public GameObject heartContainerPrefab;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Should I use lists? Maybe :)
|
||||
heartContainers = new GameObject[(int)PlayerStats.Instance.MaxTotalHealth];
|
||||
heartFills = new Image[(int)PlayerStats.Instance.MaxTotalHealth];
|
||||
|
||||
PlayerStats.Instance.onHealthChangedCallback += UpdateHeartsHUD;
|
||||
InstantiateHeartContainers();
|
||||
UpdateHeartsHUD();
|
||||
}
|
||||
|
||||
public void UpdateHeartsHUD()
|
||||
{
|
||||
SetHeartContainers();
|
||||
SetFilledHearts();
|
||||
}
|
||||
|
||||
void SetHeartContainers()
|
||||
{
|
||||
for (int i = 0; i < heartContainers.Length; i++)
|
||||
{
|
||||
if (i < PlayerStats.Instance.MaxHealth)
|
||||
{
|
||||
heartContainers[i].SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
heartContainers[i].SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SetFilledHearts()
|
||||
{
|
||||
for (int i = 0; i < heartFills.Length; i++)
|
||||
{
|
||||
if (i < PlayerStats.Instance.Health)
|
||||
{
|
||||
heartFills[i].fillAmount = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
heartFills[i].fillAmount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (PlayerStats.Instance.Health % 1 != 0)
|
||||
{
|
||||
int lastPos = Mathf.FloorToInt(PlayerStats.Instance.Health);
|
||||
heartFills[lastPos].fillAmount = PlayerStats.Instance.Health % 1;
|
||||
}
|
||||
}
|
||||
|
||||
void InstantiateHeartContainers()
|
||||
{
|
||||
for (int i = 0; i < PlayerStats.Instance.MaxTotalHealth; i++)
|
||||
{
|
||||
GameObject temp = Instantiate(heartContainerPrefab);
|
||||
temp.transform.SetParent(heartsParent, false);
|
||||
heartContainers[i] = temp;
|
||||
heartFills[i] = temp.transform.Find("HeartFill").GetComponent<Image>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5c0f78e4acfe21a4594760304164796e
|
||||
timeCreated: 1528773206
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Author: ariel oliveira [o.arielg@gmail.com]
|
||||
*/
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
public class HealthBarHUDTester : MonoBehaviour
|
||||
{
|
||||
public void AddHealth()
|
||||
{
|
||||
PlayerStats.Instance.AddHealth();
|
||||
}
|
||||
|
||||
public void Heal(float health)
|
||||
{
|
||||
PlayerStats.Instance.Heal(health);
|
||||
}
|
||||
|
||||
public void Hurt(float dmg)
|
||||
{
|
||||
PlayerStats.Instance.TakeDamage(dmg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dcf2586f5b209a045af9dff869aa39f4
|
||||
timeCreated: 1528773206
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ec0b64c0b1dccc64d91eee52d85267bd
|
||||
timeCreated: 1528773206
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user