Update README.md
[BattleCats.git] / Assets / Scripts / BossScripts / BossHealth.cs
blobc1fc39b2bd33b4d7e25bec0fc5f3a1af4bd434bc
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.UI;
6 public class BossHealth : MonoBehaviour {
8 [SerializeField]
9 private GameObject m_ExplosionPrefab;
10 [SerializeField]
11 private GameObject m_FireworksPrefab;
12 [SerializeField]
13 private GameObject lightningGroup;
14 private GameObject lightningClone;
15 private GameObject lightningClone2;
16 [SerializeField]
17 private Slider m_HealthSlider;
18 private Color m_NormalColor;
20 [SerializeField]
21 private float m_TotalHealth;
22 private float m_CurrentHealth;
24 [SerializeField]
25 private float m_InvulnerabilityDuration;
26 private float m_InvulnerabilityTimer;
27 [SerializeField]
28 private Color m_InvulnerabilityColor;
29 [SerializeField]
30 private Sprite m_InvulnerabilitySprite;
31 private Sprite m_NormalSprite;
32 private SpriteRenderer m_SpriteRenderer;
34 public bool IsDefeated { get; private set; }
36 // Use in BossMovement script if boss phases depend on its current health
37 public float HealthRatio
39 get { return CalculateHealth(); }
41 public bool IsInvulnerable { get; set; }
42 private bool isInvulnerableCustom;
43 private bool endInvulnerableCustom;
45 private void Awake()
47 m_CurrentHealth = m_TotalHealth;
48 IsInvulnerable = false;
49 isInvulnerableCustom = false;
50 endInvulnerableCustom = false;
51 IsDefeated = false;
52 m_SpriteRenderer = GetComponent<SpriteRenderer>();
53 m_NormalSprite = m_SpriteRenderer.sprite;
56 private void Start()
58 m_HealthSlider.value = CalculateHealth();
59 m_NormalColor = m_HealthSlider.fillRect.GetComponentInChildren<Image>().color;
62 void Update()
64 if (IsInvulnerable)
66 m_InvulnerabilityTimer += Time.deltaTime;
67 m_HealthSlider.fillRect.GetComponentInChildren<Image>().color = m_InvulnerabilityColor;
68 m_SpriteRenderer.sprite = m_InvulnerabilitySprite;
69 lightningClone2.transform.position = (transform.position + new Vector3 (-0.5f, -3.5f, 0f));
70 if (m_InvulnerabilityTimer > m_InvulnerabilityDuration)
72 IsInvulnerable = false;
73 m_InvulnerabilityTimer = 0.0f;
74 m_HealthSlider.fillRect.GetComponentInChildren<Image>().color = m_NormalColor;
75 m_SpriteRenderer.sprite = m_NormalSprite;
76 Destroy (lightningClone2);
79 if (isInvulnerableCustom)
81 m_HealthSlider.fillRect.GetComponentInChildren<Image>().color = m_InvulnerabilityColor;
82 m_SpriteRenderer.sprite = m_InvulnerabilitySprite;
83 lightningClone.transform.position = transform.position + new Vector3(-0.5f, -3.5f, 0f);
85 if (endInvulnerableCustom)
87 isInvulnerableCustom = false;
88 m_HealthSlider.fillRect.GetComponentInChildren<Image>().color = m_NormalColor;
89 m_SpriteRenderer.sprite = m_NormalSprite;
90 endInvulnerableCustom = false;
91 Destroy (lightningClone);
96 private float CalculateHealth()
98 return m_CurrentHealth / m_TotalHealth;
101 private void OnTriggerEnter2D(Collider2D col)
103 if (col.CompareTag("YarnABullet"))
105 DealDamage(25, col.transform.position, col.gameObject);
107 else if (col.CompareTag("YarnBBullet"))
109 DealDamage(50, col.transform.position, col.gameObject);
111 else if (col.CompareTag("YarnCBullet"))
113 DealDamage(75, col.transform.position, col.gameObject);
117 void DealDamage(float damageValue, Vector3 ColPosition, GameObject bulletOject)
119 Destroy(bulletOject);
121 if (IsInvulnerable || isInvulnerableCustom)
123 return;
126 m_CurrentHealth -= damageValue;
128 if (m_CurrentHealth <= 0.0f)
130 m_CurrentHealth = 0.0f;
131 IsDefeated = true;
134 // For testing... might remove depending on boss invulnerability conditions
135 if ((HealthRatio < 0.78f && HealthRatio > 0.75f) || (HealthRatio < 0.53f && HealthRatio > 0.50f) || (HealthRatio < 0.30f && HealthRatio > 0.25f))
137 IsInvulnerable = true;
138 lightningClone2 = new GameObject ();
139 lightningClone2 = Instantiate (lightningGroup, transform.position + new Vector3(-0.5f, -3.5f, 0f), transform.rotation);
142 m_HealthSlider.value = CalculateHealth();
144 Instantiate(m_ExplosionPrefab, ColPosition, Quaternion.identity);
146 if (IsDefeated)
148 Destroy(gameObject);
149 GameObject.FindWithTag("YarnPhysics").GetComponent<HealthSlider>().BossEnded = true;
150 Instantiate(m_FireworksPrefab, ColPosition, Quaternion.identity);
154 public void StartInvulnerabilty ()
156 isInvulnerableCustom = true;
157 lightningClone = new GameObject ();
158 lightningClone = Instantiate (lightningGroup, transform.position + new Vector3(-0.5f, -3.5f, 0f), transform.rotation);
162 public void EndInvulnerability()
164 endInvulnerableCustom = true;