-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEnemy.cs
More file actions
207 lines (175 loc) · 4.95 KB
/
Enemy.cs
File metadata and controls
207 lines (175 loc) · 4.95 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using System.Linq;
public class Enemy : Entity {
[HideInInspector] public Rigidbody2D rb2d;
public int hp = 5;
[HideInInspector]
public int totalHP;
public int moveForce;
public int maxSpeed;
public float diStrength = 0.5f;
public float diScaleMagnitude = 0.1f;
private int selfJuggleChain;
public GameObject playerObject;
[HideInInspector] public Animator anim;
[HideInInspector] public bool hasAnimator;
[HideInInspector] public EnemyBehavior[] behaviors;
Material whiteMaterial;
Material defaultMaterial;
bool dead = false;
bool hasSprites = false;
Renderer mainChildRenderer;
bool fakeDamage = false;
[HideInInspector] public SpriteRenderer spr;
List<SpriteRenderer> spriteRenderers;
public AudioClip hitSound;
public bool burstOnDeath = false;
public Transform burstEffect;
public UnityEvent deathEvent;
GameObject bossResources;
public bool IsStunned() {
return stunned;
}
override protected void OnEnable() {
base.OnEnable();
fakeDamage = hp < 0;
totalHP = hp;
rb2d = this.GetComponent<Rigidbody2D>();
playerObject = GlobalController.pc.gameObject;
if ((anim = this.GetComponent<Animator>()) != null) {
this.hasAnimator = true;
anim.logWarnings = false;
}
behaviors = this.GetComponents<EnemyBehavior>();
spr = this.GetComponent<SpriteRenderer>();
whiteMaterial = Resources.Load<Material>("Shaders/WhiteFlash");
spriteRenderers = new List<SpriteRenderer>(GetComponentsInChildren<SpriteRenderer>(includeInactive:true))
.Where(x => x.GetComponent<IgnoreWhiteFlash>() == null).ToList();
if (spr != null) {
defaultMaterial = spr.material;
} else if (spriteRenderers.Count > 0) {
defaultMaterial = spriteRenderers[0].material;
}
mainChildRenderer = GetComponentInChildren<Renderer>();
hasSprites = (spr != null || spriteRenderers.Count > 0 || mainChildRenderer != null);
// if (dropBossResources) {
// bossResources = Resources.Load<GameObject>("Effects/BossResources");
// }
}
override public void KnockBack(Vector2 kv) {
selfJuggleChain++;
kv += Random.insideUnitCircle * diStrength * (selfJuggleChain*diScaleMagnitude);
base.KnockBack(kv);
}
virtual public void DamageFor(int dmg) {
CombatMusic.EnterCombat();
if (fakeDamage) return;
OnDamage();
this.hp -= dmg;
if (this.hp <= 0 && !dead) {
Die();
} else {
WhiteSprite();
}
}
public override void OnHit(Attack attack) {
if (attack.GetComponent<PlayerAttack>() != null) {
PlayerAttack a = attack.GetComponent<PlayerAttack>();
if (a.hitstopLength > 0 && this.hp > attack.GetDamage()) {
Hitstop.Run(a.hitstopLength);
}
}
DamageFor(attack.GetDamage());
if (this.hitSound != null && mainChildRenderer.isVisible) {
AlerterText.Alert("Playing hit sound: "+hitSound.name);
SoundManager.PlayIfClose(this.hitSound, this.gameObject);
}
StunFor(attack.GetStunLength());
if (attack.knockBack) {
KnockBack(attack.GetKnockback());
}
}
protected virtual void Die(){
CloseHurtboxes();
this.frozen = true;
this.dead = true;
Hitstop.Run(.1f);
if (!burstOnDeath && anim != null) {
anim.SetTrigger("Die");
} else {
if (burstEffect != null) {
Burst();
}
Destroy(this.gameObject);
return;
}
deathEvent.Invoke();
}
// for each added behavior, call it
virtual protected void Update() {
if (!stunned) {
foreach (EnemyBehavior eb in this.behaviors) {
eb.Move();
}
}
}
override protected void UnStun() {
selfJuggleChain = 0;
base.UnStun();
}
//on death, remove damage dealing even though it'll live a little bit while the dying animation finishes
public void CloseHurtboxes() {
foreach (Transform child in transform) {
if (child.gameObject.tag.Equals(Tags.EnemyHurtbox)) {
if (child.GetComponent<Collider2D>() != null) child.GetComponent<Collider2D>().enabled = false;
}
}
}
public void WhiteSprite() {
if (!hasSprites) {
return;
}
foreach (SpriteRenderer x in spriteRenderers) {
x.material = whiteMaterial;
}
if (spr != null) {
spr.material = whiteMaterial;
}
StartCoroutine(normalSprite());
}
IEnumerator normalSprite() {
yield return new WaitForSecondsRealtime(.1f);
spriteRenderers.ForEach(x => {
x.material = defaultMaterial;
});
if (spr != null) {
spr.material = defaultMaterial;
}
if (anim != null) {
anim.SetBool("WhiteSprite", false);
}
}
public virtual void OnDamage() {
if (anim != null) anim.SetTrigger("Hurt");
}
public void Burst() {
Instantiate(burstEffect, this.transform.position, Quaternion.identity);
Destroy();
}
public override void OnGroundHit(float impactSpeed) {
base.OnGroundHit(impactSpeed);
anim.SetBool("Grounded", true);
foreach (EnemyBehavior eb in this.behaviors) {
eb.OnGroundHit();
}
}
public override void OnGroundLeave() {
anim.SetBool("Grounded", false);
foreach (EnemyBehavior eb in this.behaviors) {
eb.OnGroundLeave();
}
}
}