Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using _Scripts.Enemy;
- namespace _Scripts.Player
- {
- public class PlayerDamageBehaviour : MonoBehaviour
- {
- public PlayerHealth playerHealth;
- // public GUITexture bloodGUI1, bloodGUI2, bloodGUI3;
- public CanvasGroup bloodGUI1, bloodGUI2, bloodGUI3;
- public GameObject screen;
- // Use this for initialization
- void Start()
- {
- playerHealth.AddOnHealthChangedListener(SetBloodGUI);
- playerHealth.AddOnDeathListener(Died);
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetButton("Jump"))
- {
- playerHealth.TakeDamage(2);
- }
- }
- void SetBloodGUI()
- {
- float maxHealth = playerHealth.GetMaxHealth();
- bloodGUI1.alpha = BloodAlpha(maxHealth); // start appearing when health < 100%
- bloodGUI2.alpha = BloodAlpha(0.6f * maxHealth); // start appearing when health < 60%
- bloodGUI3.alpha = BloodAlpha(0.3f * maxHealth); // start appearing when health < 30%
- }
- private float BloodAlpha(float start)
- {
- float alpha = 1 - (100 * playerHealth.GetHealth()) / (start * playerHealth.GetMaxHealth());
- Debug.Log(alpha);
- return Mathf.Clamp(alpha, 0f, 0.8f);
- }
- private void Died()
- {
- screen.SetActive(true);
- }
- private void OnCollisionEnter(Collision collision)
- {
- if (collision.collider.CompareTag("Enemy"))
- {
- Debug.Log("Enemy touching");
- EnemyBehaviour enemy = collision.collider.GetComponent<EnemyBehaviour>();
- if (enemy.IsAttacking())
- {
- playerHealth.TakeDamage(enemy.data.attackDamage);
- }
- // Check if enemy is in attacking state
- // Get amount of damage from EnemyDamage ScriptableObject
- // Deduct damage from PlayerHealth
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement