-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnemyHealth.cs
90 lines (76 loc) · 2.44 KB
/
EnemyHealth.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyHealth : MonoBehaviour {
private float enemyMaxHealth = 100f;
public float enemyCurrentHealth;
private SpriteRenderer enemySR;
private float enemySRred = 1f;
[Header("Unity UI")]
public Image enemyHealthBar;
public Image enemyHealthUI;
// Use this for initialization
private void OnEnable()
{
enemyCurrentHealth = enemyMaxHealth;
enemySR = GetComponent<SpriteRenderer>();
enemySRred = enemySR.color.r;
}
public void EnemyTakeDamage(float amount)
{
StartCoroutine(ShowWorldspaceUI());
StartCoroutine(Flashing());
enemyCurrentHealth -= amount;
enemyHealthBar.fillAmount = enemyCurrentHealth / enemyMaxHealth;
if(enemyCurrentHealth <= 0)
{
gameObject.SetActive(false);
}
}
public void EnemyDamageFlash()
{
enemySR.color = new Color(enemySR.color.r, enemySR.color.g, enemySR.color.b, 0.1f);
}
public void EnemyDamageShow()
{
enemySR.color = new Color(enemySR.color.r, enemySR.color.g, enemySR.color.b);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Projectile")
{
EnemyTakeDamage(25f);
enemyHealthBar.fillAmount = enemyCurrentHealth / enemyMaxHealth;
}
if (collision.gameObject.tag == "Player")
{
var player = collision.gameObject.GetComponent<TouchControls>();
player.knockbackCount = player.knockbackLength;
if (collision.transform.position.x < transform.position.x)
{
player.knockFromRight = true;
}
else
{
player.knockFromRight = false;
}
}
}
private IEnumerator ShowWorldspaceUI()
{
enemyHealthUI.gameObject.SetActive(true);
yield return new WaitForSeconds(3f);
enemyHealthUI.gameObject.SetActive(false);
}
private IEnumerator Flashing()
{
EnemyDamageFlash();
yield return new WaitForSeconds(0.3f);
EnemyDamageShow();
yield return new WaitForSeconds(0.3f);
EnemyDamageFlash();
yield return new WaitForSeconds(0.3f);
EnemyDamageShow();
}
}