forked from Angus-Fan/TurnBasedStrategyGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitScript.cs
355 lines (287 loc) · 9.17 KB
/
UnitScript.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class UnitScript : MonoBehaviour
{
public int teamNum;
public int x;
public int y;
//This is a low tier idea, don't use it
public bool coroutineRunning;
//Meta defining play here
public Queue<int> movementQueue;
public Queue<int> combatQueue;
//This global variable is used to increase the units movementSpeed when travelling on the board
public float visualMovementSpeed = .15f;
//Animator
public Animator animator;
public GameObject tileBeingOccupied;
public GameObject damagedParticle;
//UnitStats
public string unitName;
public int moveSpeed = 2;
public int attackRange = 1;
public int attackDamage = 1;
public int maxHealthPoints = 5;
public int currentHealthPoints;
public Sprite unitSprite;
[Header("UI Elements")]
//Unity UI References
public Canvas healthBarCanvas;
public TMP_Text hitPointsText;
public Image healthBar;
public Canvas damagePopupCanvas;
public TMP_Text damagePopupText;
public Image damageBackdrop;
//This may change in the future if 2d sprites are used instead
//public Material unitMaterial;
//public Material unitWaitMaterial;
public tileMapScript map;
//Location for positional update
public Transform startPoint;
public Transform endPoint;
public float moveSpeedTime = 1f;
//3D Model or 2D Sprite variables to check which version to use
//Make sure only one of them are enabled in the inspector
//public GameObject holder3D;
public GameObject holder2D;
// Total distance between the markers.
private float journeyLength;
//Boolean to startTravelling
public bool unitInMovement;
//Enum for unit states
public enum movementStates
{
Unselected,
Selected,
Moved,
Wait
}
public movementStates unitMoveState;
//Pathfinding
public List<Node> path = null;
//Path for moving unit's transform
public List<Node> pathForMovement = null;
public bool completedMovement = false;
private void Awake()
{
animator = holder2D.GetComponent<Animator>();
movementQueue = new Queue<int>();
combatQueue = new Queue<int>();
x = (int)transform.position.x;
y = (int)transform.position.z;
unitMoveState = movementStates.Unselected;
currentHealthPoints = maxHealthPoints;
hitPointsText.SetText(currentHealthPoints.ToString());
}
public void LateUpdate()
{
healthBarCanvas.transform.forward = Camera.main.transform.forward;
//damagePopupCanvas.transform.forward = Camera.main.transform.forward;
holder2D.transform.forward = Camera.main.transform.forward;
}
public void MoveNextTile()
{
if (path.Count == 0)
{
return;
}
else
{
StartCoroutine(moveOverSeconds(transform.gameObject, path[path.Count - 1]));
}
}
public void moveAgain()
{
path = null;
setMovementState(0);
completedMovement = false;
gameObject.GetComponentInChildren<SpriteRenderer>().color = Color.white;
setIdleAnimation();
//gameObject.GetComponentInChildren<Renderer>().material = unitMaterial;
}
public movementStates getMovementStateEnum(int i)
{
if (i == 0)
{
return movementStates.Unselected;
}
else if (i == 1)
{
return movementStates.Selected;
}
else if (i == 2)
{
return movementStates.Moved;
}
else if (i == 3)
{
return movementStates.Wait;
}
return movementStates.Unselected;
}
public void setMovementState(int i)
{
if (i == 0)
{
unitMoveState = movementStates.Unselected;
}
else if (i == 1)
{
unitMoveState = movementStates.Selected;
}
else if (i == 2)
{
unitMoveState = movementStates.Moved;
}
else if (i == 3)
{
unitMoveState = movementStates.Wait;
}
}
public void updateHealthUI()
{
healthBar.fillAmount = (float)currentHealthPoints / maxHealthPoints;
hitPointsText.SetText(currentHealthPoints.ToString());
}
public void dealDamage(int x)
{
currentHealthPoints = currentHealthPoints - x;
updateHealthUI();
}
public void wait()
{
gameObject.GetComponentInChildren<SpriteRenderer>().color = Color.gray;
//gameObject.GetComponentInChildren<Renderer>().material = unitWaitMaterial;
}
public void changeHealthBarColour(int i)
{
if (i == 0)
{
healthBar.color = Color.blue;
}
else if (i == 1)
{
healthBar.color = Color.red;
}
}
public void unitDie()
{
if (holder2D.activeSelf)
{
StartCoroutine(fadeOut());
StartCoroutine(checkIfRoutinesRunning());
}
//Destroy(gameObject,2f);
/*
Renderer rend = GetComponentInChildren<SpriteRenderer>();
Color c = rend.material.color;
c.a = 0f;
rend.material.color = c;
StartCoroutine(fadeOut(rend));*/
}
public IEnumerator checkIfRoutinesRunning()
{
while (combatQueue.Count>0)
{
yield return new WaitForEndOfFrame();
}
Destroy(gameObject);
}
public IEnumerator fadeOut()
{
combatQueue.Enqueue(1);
//setDieAnimation();
//yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length);
Renderer rend = GetComponentInChildren<SpriteRenderer>();
for (float f = 1f; f >= .05; f -= 0.01f)
{
Color c = rend.material.color;
c.a = f;
rend.material.color = c;
yield return new WaitForEndOfFrame();
}
combatQueue.Dequeue();
}
public IEnumerator moveOverSeconds(GameObject objectToMove,Node endNode)
{
movementQueue.Enqueue(1);
//remove first thing on path because, its the tile we are standing on
path.RemoveAt(0);
while (path.Count != 0)
{
Vector3 endPos = map.tileCoordToWorldCoord(path[0].x, path[0].y);
objectToMove.transform.position = Vector3.Lerp(transform.position, endPos, visualMovementSpeed);
if ((transform.position - endPos).sqrMagnitude < 0.001)
{
path.RemoveAt(0);
}
yield return new WaitForEndOfFrame();
}
visualMovementSpeed = 0.15f;
transform.position = map.tileCoordToWorldCoord(endNode.x, endNode.y);
x = endNode.x;
y = endNode.y;
tileBeingOccupied.GetComponent<ClickableTileScript>().unitOnTile = null;
tileBeingOccupied = map.tilesOnMap[x, y];
movementQueue.Dequeue();
}
public IEnumerator displayDamageEnum(int damageTaken)
{
combatQueue.Enqueue(1);
damagePopupText.SetText(damageTaken.ToString());
damagePopupCanvas.enabled = true;
for (float f = 1f; f >=-0.01f; f -= 0.01f)
{
Color backDrop = damageBackdrop.GetComponent<Image>().color;
Color damageValue = damagePopupText.color;
backDrop.a = f;
damageValue.a = f;
damageBackdrop.GetComponent<Image>().color = backDrop;
damagePopupText.color = damageValue;
yield return new WaitForEndOfFrame();
}
//damagePopup.enabled = false;
combatQueue.Dequeue();
}
public void resetPath()
{
path = null;
completedMovement = false;
}
public void displayDamage(int damageTaken)
{
damagePopupCanvas.enabled = true;
damagePopupText.SetText(damageTaken.ToString());
}
public void disableDisplayDamage()
{
damagePopupCanvas.enabled = false;
}
public void setSelectedAnimation()
{
animator.SetTrigger("toSelected");
}
public void setIdleAnimation()
{
animator.SetTrigger("toIdle");
}
public void setWalkingAnimation()
{
animator.SetTrigger("toWalking");
}
public void setAttackAnimation()
{
animator.SetTrigger("toAttacking");
}
public void setWaitIdleAnimation()
{
animator.SetTrigger("toIdleWait");
}
public void setDieAnimation()
{
animator.SetTrigger("dieTrigger");
}
}