ltrout

EnemyHealth.cs

Dec 24th, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using PlayerInfo;
  5. using Shell;
  6. using Management;
  7.  
  8. namespace EnemyInfo
  9. {
  10.     public class EnemyHealth : MonoBehaviour
  11.     {
  12.         public float StartHealth = 100f;
  13.         public Slider Slider;
  14.         public Image FillImage;
  15.         public Color FullHealth = Color.green;
  16.         public Color ZeroHealth = Color.red;
  17.         public GameObject ExplosionPrefab;
  18.         public int PlayerWinRound = 1;
  19.  
  20.         AudioSource ExplosionAudio;
  21.         ParticleSystem ExplosionParticles;
  22.         [HideInInspector] public float CurrentHealth;
  23.         [HideInInspector] public bool Dead;
  24.  
  25.         void Awake ()
  26.         {
  27.             ExplosionParticles = Instantiate (ExplosionPrefab).GetComponent<ParticleSystem> ();
  28.  
  29.             ExplosionAudio = ExplosionParticles.GetComponent<AudioSource> ();
  30.  
  31.             ExplosionParticles.gameObject.SetActive (false);
  32.         }
  33.  
  34.         void OnEnable ()
  35.         {
  36.             CurrentHealth = StartHealth;
  37.             Dead = false;
  38.  
  39.             SetHealthUI ();
  40.         }
  41.  
  42.         public void TakeDamage (float amount)
  43.         {
  44.             CurrentHealth -= amount;
  45.  
  46.             SetHealthUI ();
  47.  
  48.             if (CurrentHealth <= 0f && !Dead)
  49.             {
  50.                 OnDeath ();
  51.             }
  52.         }
  53.  
  54.         void SetHealthUI ()
  55.         {
  56.             Slider.value = CurrentHealth;
  57.  
  58.             FillImage.color = Color.Lerp (ZeroHealth, FullHealth, CurrentHealth / StartHealth);
  59.         }
  60.  
  61.         void OnDeath ()
  62.         {
  63.             Dead = true;
  64.  
  65.             ExplosionParticles.transform.position = transform.position;
  66.             ExplosionParticles.gameObject.SetActive (true);
  67.  
  68.             ExplosionParticles.Play ();
  69.  
  70.             ExplosionAudio.Play ();
  71.             gameObject.SetActive (false);
  72.  
  73.             ScoreManager.PlayerScore += PlayerWinRound;
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment