Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEditor;
- [System.Serializable]
- public struct tHealthMAnager
- {
- public ActorBase tParent;
- public float fMaxHealth;
- public float fCurrentHealth;
- public bool bIsDead;
- public void InflictDamagePercentage( float fPercentage )
- {
- InflictDamage (fMaxHealth / 100 * fPercentage);
- }
- public void InflictDamage( float fDamage )
- {
- fCurrentHealth -= fDamage;
- if (CheckDeath ())
- OnDeath ();
- ClampHealth ();
- OnHit();
- }
- public void OnDeath()
- {
- tParent.OnDeath ();
- }
- public void HealDamage( float fHealAmount, bool bCanRevive = false )
- {
- fCurrentHealth += fHealAmount;
- if (bCanRevive && fCurrentHealth > 0)
- {
- bIsDead = false;
- }
- ClampHealth ();
- }
- public void OnHit()
- {
- tParent.OnHit ();
- }
- bool CheckDeath()
- {
- bool bIsDeadBeforeChange = bIsDead;
- bIsDead = ( fCurrentHealth <= 0 );
- if ((bIsDeadBeforeChange == false) && (bIsDead == true))
- return true;
- else
- return false;
- }
- void ClampHealth()
- {
- fCurrentHealth = Mathf.Clamp (fCurrentHealth, 0, fMaxHealth);
- }
- }
- [CustomEditor(typeof(ActorBase))]
- public class ActorBase : Editor
- {
- public tHealthMAnager HealthManager;
- public override void OnInspectorGUI()
- {
- DrawDefaultInspector();
- ActorBase myScript = (ActorBase)target;
- if(GUILayout.Button("Kill this"))
- {
- myScript.HealthManager.InflictDamagePercentage(150);
- }
- }
- virtual public void Start()
- {
- HealthManager.tParent = this;
- }
- virtual public void Update ()
- {
- }
- public virtual void OnDeath()
- {
- Debug.Log("A base actor has died.");
- }
- public virtual void OnHit()
- {
- Debug.Log("A base actor has been hit.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement