Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4.  
  5. [System.Serializable]
  6. public struct tHealthMAnager
  7. {
  8.     public ActorBase tParent;
  9.    
  10.     public float fMaxHealth;
  11.     public float fCurrentHealth;
  12.     public bool bIsDead;
  13.    
  14.     public void InflictDamagePercentage( float fPercentage )
  15.     {
  16.         InflictDamage (fMaxHealth / 100 * fPercentage);
  17.     }
  18.    
  19.     public void InflictDamage( float fDamage )
  20.     {
  21.         fCurrentHealth -= fDamage;
  22.         if (CheckDeath ())
  23.             OnDeath ();
  24.         ClampHealth ();
  25.        
  26.         OnHit();
  27.     }
  28.    
  29.     public void OnDeath()
  30.     {
  31.         tParent.OnDeath ();
  32.     }
  33.    
  34.     public void HealDamage( float fHealAmount, bool bCanRevive = false )
  35.     {
  36.         fCurrentHealth += fHealAmount;
  37.         if (bCanRevive && fCurrentHealth > 0)
  38.         {
  39.             bIsDead = false;
  40.         }
  41.         ClampHealth ();
  42.     }
  43.  
  44.     public void OnHit()
  45.     {
  46.         tParent.OnHit ();
  47.     }
  48.    
  49.     bool CheckDeath()
  50.     {
  51.         bool bIsDeadBeforeChange = bIsDead;
  52.         bIsDead = ( fCurrentHealth <= 0 );
  53.         if ((bIsDeadBeforeChange == false) && (bIsDead == true))
  54.             return true;
  55.         else
  56.             return false;
  57.     }
  58.    
  59.     void ClampHealth()
  60.     {
  61.         fCurrentHealth = Mathf.Clamp (fCurrentHealth, 0, fMaxHealth);
  62.     }
  63. }
  64.  
  65. [CustomEditor(typeof(ActorBase))]
  66. public class ActorBase : Editor
  67. {
  68.     public tHealthMAnager HealthManager;
  69.  
  70.     public override void OnInspectorGUI()
  71.     {
  72.         DrawDefaultInspector();
  73.        
  74.         ActorBase myScript = (ActorBase)target;
  75.         if(GUILayout.Button("Kill this"))
  76.         {
  77.             myScript.HealthManager.InflictDamagePercentage(150);
  78.         }
  79.     }
  80.    
  81.     virtual public void Start()
  82.     {
  83.         HealthManager.tParent = this;
  84.     }
  85.  
  86.     virtual public void Update ()
  87.     {
  88.        
  89.     }
  90.  
  91.     public virtual void OnDeath()
  92.     {
  93.         Debug.Log("A base actor has died.");
  94.     }
  95.  
  96.     public virtual void OnHit()
  97.     {
  98.         Debug.Log("A base actor has been hit.");
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement