-
Notifications
You must be signed in to change notification settings - Fork 0
/
BossController.cs
255 lines (150 loc) · 4.68 KB
/
BossController.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public enum BossState {
GO_TO_PLAYER,
MOVE_AWAY_FROM_PLAYER,
SEARCH_FOR_PLAYER,
ATTACK
}
public class BossController : MonoBehaviour
{
private CharacterAnimation bossAnim;
private NavMeshAgent navAgent;
private BossState boss_State;
public float move_Speed = 3.5f;
private Transform player_Target;
public float attack_Distance = 1f;
public float chase_Player_After_Attack_Distance = 1f;
private float wait_Before_Attack_Time = 2f;
private float attack_Timer;
private bool first_Attack;
public float retreat_Distance_Radius = 8f;
private Vector3 randomPos;
void Awake()
{
bossAnim = GetComponent<CharacterAnimation>();
navAgent = GetComponent<NavMeshAgent>();
}
void Start()
{
player_Target = GameObject.FindGameObjectWithTag(TagManager.PLAYER_TAG).transform;
boss_State = BossState.SEARCH_FOR_PLAYER;
}
void Update()
{
if (boss_State == BossState.SEARCH_FOR_PLAYER)
{
if (Vector3.Distance(transform.position, player_Target.position) > attack_Distance)
{
boss_State = BossState.GO_TO_PLAYER;
}
else
{
boss_State = BossState.ATTACK;
}
}
if (boss_State == BossState.GO_TO_PLAYER)
{
GoTowardsPlayer();
}
if (boss_State == BossState.ATTACK)
{
AttackPlayer();
}
if (boss_State == BossState.MOVE_AWAY_FROM_PLAYER)
{
MoveAwayFromPlayer();
}
}
void GoTowardsPlayer()
{
navAgent.isStopped = false;
navAgent.SetDestination(player_Target.position);
if (navAgent.velocity.sqrMagnitude == 0)
{
bossAnim.Walk(false);
}
else
{
bossAnim.Walk(true);
}
if (Vector3.Distance(transform.position, player_Target.position) <= attack_Distance)
{
navAgent.velocity = Vector3.zero;
navAgent.isStopped = true;
bossAnim.Walk(false);
boss_State = BossState.ATTACK;
attack_Timer = wait_Before_Attack_Time / 2f;
}
}
void AttackPlayer()
{
attack_Timer += Time.deltaTime;
if (attack_Timer > wait_Before_Attack_Time)
{
attack_Timer = 0f;
if (!first_Attack)
{
bossAnim.NormalAttack_1();
first_Attack = true;
}
else
{
if (Random.Range(0, 5) >= 1)
{
if (Random.Range(0, 3) > 1)
{
bossAnim.NormalAttack_1();
}
else
{
bossAnim.SpecialAttack_1();
}
}
else
{
randomPos = transform.position
- (transform.forward * retreat_Distance_Radius);
boss_State = BossState.MOVE_AWAY_FROM_PLAYER;
first_Attack = false;
}
}
}
if (Vector3.Distance(transform.position, player_Target.position) >
attack_Distance + chase_Player_After_Attack_Distance)
{
navAgent.isStopped = false;
boss_State = BossState.GO_TO_PLAYER;
}
}
void MoveAwayFromPlayer()
{
navAgent.SetDestination(randomPos);
navAgent.isStopped = false;
if (navAgent.velocity.sqrMagnitude == 0)
{
bossAnim.Walk(false);
}
else
{
bossAnim.Walk(true);
}
if (navAgent.remainingDistance <= 0.2f)
{
navAgent.velocity = Vector3.zero;
navAgent.isStopped = true;
StartCoroutine(WaitThenSearchForPlayer());
}
}
IEnumerator WaitThenSearchForPlayer() {
yield return new WaitForSeconds(Random.Range(1f, 2f));
boss_State = BossState.SEARCH_FOR_PLAYER;
}
void DeactivateScript() {
EndGameManager.instance.GameOver(true);
navAgent.isStopped = true;
enabled = false;
}
}