Advertisement
SvOzMaS

PlayerHealth

Dec 13th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.32 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class PlayerHealth : MonoBehaviour {
  6.  
  7.     public Texture2D[] screenBloodTextures;                     //container array of the blood screen textures
  8.     public Image hitImage;
  9.     public float flashSpeed = 5f;                               //The speed the damageImage will fade at.
  10.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);     //The colour the damageImage is set to, to flash.
  11.     public float health;                                        //player's current health
  12.     public float maxHealth;                                     //player's max health  
  13.     public float freezeHealingTime;                             //Time to wait for healing again after the player is damaged    
  14.     public float repeatDamagePeriod = 1f;                       //How frequently the player can be damaged.
  15.     [HideInInspector]
  16.     public bool isHurting;
  17.     [HideInInspector]
  18.     public bool isDead;
  19.     [Range(0.1f, 100)]
  20.     public float healingPointsPerSecond;                         //Amount of health points the player will recover by second  
  21.    
  22.     private float lastHitTime;                                   //The time at which the player was last hit.
  23.  
  24.     public float alpha;                                          //Transparency to apply to screen textures  
  25.     public int drawDepth;
  26.  
  27.     // Use this for initialization
  28.     void Start () {
  29.  
  30.         drawDepth = -1000;
  31.     }
  32.    
  33.     // Update is called once per frame
  34.     void Update () {
  35.         regenerateHealth();
  36.         hurtFlashing();
  37.     }
  38.  
  39.     private void regenerateHealth() {
  40.         if (Time.time > lastHitTime + repeatDamagePeriod + freezeHealingTime && !isHurting) {
  41.             if (health < maxHealth) {
  42.                 //regenerating health over time
  43.                 health += Time.deltaTime * healingPointsPerSecond;
  44.  
  45.                 //Avoid the health to has a value over the maxHealth value
  46.                 if (health > maxHealth)
  47.                     health = maxHealth;
  48.             }
  49.         }
  50.     }
  51.  
  52.     private void hurtFlashing() {
  53.         // If the player has just been damaged...
  54.         if (isHurting) {
  55.             // ... set the colour of the damageImage to the flash colour.
  56.             hitImage.color = flashColour;
  57.         }
  58.         // Otherwise...
  59.         else {
  60.             // ... transition the colour back to clear.
  61.             hitImage.color = Color.Lerp(hitImage.color, Color.clear, flashSpeed * Time.deltaTime);
  62.         }
  63.     }
  64.  
  65.     public void hurt(int damage) {
  66.         if (Time.time > lastHitTime + repeatDamagePeriod) {
  67.                 // ... take damage and reset the lastHitTime.
  68.                 StartCoroutine(takeDamage(damage));
  69.                 lastHitTime = Time.time;
  70.         }
  71.     }
  72.  
  73.  
  74.     private IEnumerator takeDamage(int damage) {
  75.  
  76.         // Player is getting damage
  77.         isHurting = true;
  78.         // Reduce the player's health by enemy damage.
  79.         health -= damage;
  80.  
  81.         // If the player has lost all it's health and the death flag hasn't been set yet...
  82.         if (health <= 0 && !isDead) {
  83.             health = 0;
  84.             // ... it should die.
  85.             death();
  86.         }
  87.  
  88.         // Wait the repeatDamagePeriod until the player can receive damage again
  89.         yield return new WaitForSeconds(repeatDamagePeriod);
  90.         // Player is no longer taking damage
  91.         isHurting = false;
  92.     }
  93.  
  94.     private void death() {
  95.         isDead = true;
  96.  
  97.         //TODO Play death transition/animation/sounds/effects and disable the scripts
  98.  
  99.     }
  100.  
  101.     void OnGUI() {
  102.  
  103.         //get the corresponding texture array index
  104.         float percentagePerImage = 100 / screenBloodTextures.Length;
  105.         float takenHealthPercentage = 100 - ((health * 100) / maxHealth);
  106.         int textureIndex = (int)(takenHealthPercentage / percentagePerImage);
  107.  
  108.         if (textureIndex == screenBloodTextures.Length)
  109.             textureIndex--;
  110.  
  111.         //adjusting alpha based on player's left health percetage
  112.         alpha = (1 - (health * 100 / maxHealth) / 100);
  113.  
  114.         //drawing the texture
  115.         GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha);
  116.         GUI.depth = drawDepth;
  117.         GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), screenBloodTextures[textureIndex]); ;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement