Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- public class PlayerHealth : MonoBehaviour {
- public Texture2D[] screenBloodTextures; //container array of the blood screen textures
- public Image hitImage;
- public float flashSpeed = 5f; //The speed the damageImage will fade at.
- public Color flashColour = new Color(1f, 0f, 0f, 0.1f); //The colour the damageImage is set to, to flash.
- public float health; //player's current health
- public float maxHealth; //player's max health
- public float freezeHealingTime; //Time to wait for healing again after the player is damaged
- public float repeatDamagePeriod = 1f; //How frequently the player can be damaged.
- [HideInInspector]
- public bool isHurting;
- [HideInInspector]
- public bool isDead;
- [Range(0.1f, 100)]
- public float healingPointsPerSecond; //Amount of health points the player will recover by second
- private float lastHitTime; //The time at which the player was last hit.
- public float alpha; //Transparency to apply to screen textures
- public int drawDepth;
- // Use this for initialization
- void Start () {
- drawDepth = -1000;
- }
- // Update is called once per frame
- void Update () {
- regenerateHealth();
- hurtFlashing();
- }
- private void regenerateHealth() {
- if (Time.time > lastHitTime + repeatDamagePeriod + freezeHealingTime && !isHurting) {
- if (health < maxHealth) {
- //regenerating health over time
- health += Time.deltaTime * healingPointsPerSecond;
- //Avoid the health to has a value over the maxHealth value
- if (health > maxHealth)
- health = maxHealth;
- }
- }
- }
- private void hurtFlashing() {
- // If the player has just been damaged...
- if (isHurting) {
- // ... set the colour of the damageImage to the flash colour.
- hitImage.color = flashColour;
- }
- // Otherwise...
- else {
- // ... transition the colour back to clear.
- hitImage.color = Color.Lerp(hitImage.color, Color.clear, flashSpeed * Time.deltaTime);
- }
- }
- public void hurt(int damage) {
- if (Time.time > lastHitTime + repeatDamagePeriod) {
- // ... take damage and reset the lastHitTime.
- StartCoroutine(takeDamage(damage));
- lastHitTime = Time.time;
- }
- }
- private IEnumerator takeDamage(int damage) {
- // Player is getting damage
- isHurting = true;
- // Reduce the player's health by enemy damage.
- health -= damage;
- // If the player has lost all it's health and the death flag hasn't been set yet...
- if (health <= 0 && !isDead) {
- health = 0;
- // ... it should die.
- death();
- }
- // Wait the repeatDamagePeriod until the player can receive damage again
- yield return new WaitForSeconds(repeatDamagePeriod);
- // Player is no longer taking damage
- isHurting = false;
- }
- private void death() {
- isDead = true;
- //TODO Play death transition/animation/sounds/effects and disable the scripts
- }
- void OnGUI() {
- //get the corresponding texture array index
- float percentagePerImage = 100 / screenBloodTextures.Length;
- float takenHealthPercentage = 100 - ((health * 100) / maxHealth);
- int textureIndex = (int)(takenHealthPercentage / percentagePerImage);
- if (textureIndex == screenBloodTextures.Length)
- textureIndex--;
- //adjusting alpha based on player's left health percetage
- alpha = (1 - (health * 100 / maxHealth) / 100);
- //drawing the texture
- GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha);
- GUI.depth = drawDepth;
- GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), screenBloodTextures[textureIndex]); ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement