Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using _Scripts.Enemy;
  5. namespace _Scripts.Player
  6. {
  7.     public class PlayerDamageBehaviour : MonoBehaviour
  8.     {
  9.  
  10.         public PlayerHealth playerHealth;
  11.  
  12.         // public GUITexture bloodGUI1, bloodGUI2, bloodGUI3;
  13.         public CanvasGroup bloodGUI1, bloodGUI2, bloodGUI3;
  14.  
  15.         public GameObject screen;
  16.  
  17.         // Use this for initialization
  18.         void Start()
  19.         {
  20.             playerHealth.AddOnHealthChangedListener(SetBloodGUI);
  21.             playerHealth.AddOnDeathListener(Died);
  22.         }
  23.  
  24.         // Update is called once per frame
  25.         void Update()
  26.         {
  27.             if (Input.GetButton("Jump"))
  28.             {
  29.                 playerHealth.TakeDamage(2);
  30.             }
  31.         }
  32.  
  33.         void SetBloodGUI()
  34.         {
  35.             float maxHealth = playerHealth.GetMaxHealth();
  36.  
  37.             bloodGUI1.alpha = BloodAlpha(maxHealth); // start appearing when health < 100%
  38.             bloodGUI2.alpha = BloodAlpha(0.6f * maxHealth); // start appearing when health < 60%
  39.             bloodGUI3.alpha = BloodAlpha(0.3f * maxHealth); // start appearing when health < 30%
  40.         }
  41.  
  42.         private float BloodAlpha(float start)
  43.         {
  44.             float alpha = 1 - (100 * playerHealth.GetHealth()) / (start * playerHealth.GetMaxHealth());
  45.             Debug.Log(alpha);
  46.             return Mathf.Clamp(alpha, 0f, 0.8f);
  47.         }
  48.  
  49.         private void Died()
  50.         {
  51.             screen.SetActive(true);
  52.         }
  53.  
  54.         private void OnCollisionEnter(Collision collision)
  55.         {
  56.  
  57.             if (collision.collider.CompareTag("Enemy"))
  58.             {
  59.                 Debug.Log("Enemy touching");
  60.                 EnemyBehaviour enemy = collision.collider.GetComponent<EnemyBehaviour>();
  61.  
  62.                 if (enemy.IsAttacking())
  63.                 {
  64.                     playerHealth.TakeDamage(enemy.data.attackDamage);
  65.                 }
  66.                 // Check if enemy is in attacking state
  67.                 // Get amount of damage from EnemyDamage ScriptableObject
  68.                 // Deduct damage from PlayerHealth
  69.             }
  70.         }
  71.  
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement