-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerController
More file actions
421 lines (338 loc) · 11.2 KB
/
Copy pathPlayerController
File metadata and controls
421 lines (338 loc) · 11.2 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class PlayerController : MonoBehaviour
{
//private movement variables
private float horizontalInput;
public float speed = 10;
private float facing;
private Vector3 yFlip = new Vector3(0, 180, 0);
private Rigidbody2D rb2D;
public float jumpForce = 10;
//attack variables
public GameObject attackPrefab;
private Vector3 attackOffset = new Vector3(1.0f, -0.5f, 0);
public bool isAttacking = false;
//health variables
public int curHealth = 0;
public int maxHealth = 5;
public HealthBar healthBar;
public GameObject healthCanvas;
//player respawn variables
public float spawnX = -40;
public float spawnY = 40;
//animation variables
private Animator anim;
private AnimatorClipInfo[] currentClipInfo;
private string clipName;
//door variables
private bool hasKey = false;
public TextMeshProUGUI doorText;
public GameObject doorTextBox;
//player boss fight variables
public bool inGuardFight = false;
public bool inFrayFight = false;
public bool beatFray = false;
public GameObject fray;
public GameObject guard;
public GameObject dialogue;
public GameObject healthSpawner;
public TextMeshProUGUI frayText;
public GameObject frayTextBox;
private Vector3 guardSpawn = new Vector3(256, 92.2f, 0);
//credits variables
private GameObject credits;
//sound effects variables
private AudioSource audios;
public AudioClip healthSound;
public AudioClip jumpSound;
public AudioClip hurtSound;
public AudioClip landingSound;
private bool onGround = false;
public AudioClip attackSound;
public AudioClip deathSound;
public AudioClip hitEnemySound;
public AudioClip bounceSound;
public AudioClip hitArmourSound;
// Start is called before the first frame update
void Start()
{
//get components and objects
rb2D = GetComponent<Rigidbody2D>();
dialogue = GameObject.Find("Fray Text");
frayText = dialogue.GetComponent<TextMeshProUGUI>();
frayTextBox = GameObject.Find("Fray Text Box");
healthSpawner = GameObject.FindGameObjectWithTag("Spawn Manager");
credits = GameObject.FindGameObjectWithTag("Credits");
healthCanvas = GameObject.FindGameObjectWithTag("Health Canvas");
doorText = GameObject.Find("Door Text").GetComponent<TextMeshProUGUI>();
doorTextBox = GameObject.Find("Door Text Box");
anim = GetComponent<Animator>();
audios = GetComponent<AudioSource>();
//initialize variables
facing = 1;
transform.rotation = Quaternion.Euler(0, 0, 0);
curHealth = maxHealth;
//deactivate credits
credits.SetActive(false);
}
// Update is called once per frame
void Update()
{
//character movement
//orient the character in the correct direction
horizontalInput = Input.GetAxis("Horizontal");
if (horizontalInput > 0)
{
if (facing != 1)
{
facing = 1;
transform.Rotate(yFlip);
}
}
else if (horizontalInput < 0)
{
if (facing != -1)
{
facing = -1;
transform.Rotate(yFlip);
}
}
//go forward according to local rotation
transform.Translate(Vector3.right * Time.deltaTime * Mathf.Abs(horizontalInput) * speed);
//don't play walking animation when standing still
currentClipInfo = anim.GetCurrentAnimatorClipInfo(0);
//make sure the clip is not out of bounds
if (currentClipInfo.Length != 0)
{
clipName = currentClipInfo[0].clip.name;
}
if (clipName == "Player Walk Cycle")
{
//player is walking
anim.speed = Mathf.Abs(horizontalInput);
}
else
{
//another animation is playing
anim.speed = 1;
}
//jump if spacebar is pressed
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
//launch an attack
if (Input.GetKeyDown(KeyCode.X) && !isAttacking)
{
anim.Play("Base Layer.Player Attack");
audios.PlayOneShot(attackSound);
isAttacking = true;
GameObject attack = Instantiate(attackPrefab, transform.position, transform.rotation);
attack.transform.Translate(attackOffset);
}
//check if the player has died
if (curHealth <= 0)
{
audios.PlayOneShot(deathSound);
//respawn
curHealth = maxHealth;
healthBar.SetHealth(curHealth);
transform.position = new Vector3(spawnX, spawnY, 0);
if (inGuardFight)
{
StartGuardFight();
}
else if(inFrayFight)
{
StartFrayFight();
}
}
}
//allow to the player to take damage from non-triggers like enemies
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Enemy")
{
//take damage
DamagePlayer(1);
//make the enemy freeze
if (other.gameObject.GetComponent<MoveBetweenBounds>() != null)
{
other.gameObject.GetComponent<MoveBetweenBounds>().freeze();
}
}
//allow the player to open the door
else if (other.gameObject.name == "Key Door" && hasKey)
{
Destroy(other.gameObject);
doorTextBox.SetActive(false);
doorText.text = "";
}
else if (other.gameObject.tag == "Bounce")
{
audios.PlayOneShot(bounceSound);
}
//play a sound if the player is landing
if (!onGround)
{
audios.PlayOneShot(landingSound);
}
}
//allow to the player to take damage from triggers like acid
private void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.name == "Acid")
{
//take damage
DamagePlayer(1);
}
}
//allow the player to interact with triggers
private void OnTriggerEnter2D(Collider2D other)
{
//allow the player to pick up the key
if(other.name == "Key")
{
Destroy(other.gameObject);
hasKey = true;
doorText.text = "Well done, you found the key. Touch the door with it so we can keep moving.";
}
else if(other.gameObject.tag == "Health")
{
audios.PlayOneShot(healthSound);
}
}
//allow the player to jump
private void Jump()
{
if (rb2D.velocity.y < 0.4 && rb2D.velocity.y > -0.4)
{
anim.Play("Base Layer.Player Jump");
audios.PlayOneShot(jumpSound);
onGround = false;
rb2D.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
}
//allow the player to jump
public void ExitGame()
{
Debug.Log("quit game");
Application.Quit();
}
public void SetHealth(int newHealth)
{
curHealth = newHealth;
}
//method to cause the player to take damage
public void DamagePlayer(int damage)
{
audios.PlayOneShot(hurtSound);
curHealth -= damage;
healthBar.SetHealth(curHealth);
}
public void PlayHitEnemy()
{
audios.PlayOneShot(hitEnemySound);
}
public void StartGuardFight()
{
frayText.text = "";
//activate spawner
healthSpawner.SetActive(true);
//get enemies
fray = GameObject.FindGameObjectWithTag("Fray");
guard = GameObject.FindGameObjectWithTag("Guard");
//reset the guard enemy
guard.transform.position = guardSpawn;
guard.GetComponent<GuardScript>().ResetStats();
//reset fray's position
fray.transform.position = new Vector3(250, 108, 0);
//play through the guard cutscene
frayTextBox.SetActive(false);
StartCoroutine(DisplayFirstFrayText(1));
StartCoroutine(WaitThenClearFrayText(5));
}
//Guard cutscene: display the first line said by fray
IEnumerator DisplayFirstFrayText(int timeToWait)
{
yield return new WaitForSeconds(timeToWait);
frayTextBox.SetActive(true);
frayText.text = "Here it is! They hid the Artifact inside the Beacon! Let's kill the guard and finish our mission, attack his head!";
}
//Guard cutscene: clear fray's dialogue
IEnumerator WaitThenClearFrayText(int timeToWait)
{
yield return new WaitForSeconds(timeToWait);
frayText.text = "";
frayTextBox.SetActive(false);
}
public void StartFrayFight()
{
inGuardFight = false;
inFrayFight = true;
fray.GetComponent<FrayScript>().inFight = false;
fray.GetComponent<FrayScript>().ResetStats();
//play cutscene
StartCoroutine(FrayFightCutscene1(1));
}
IEnumerator FrayFightCutscene1(int timeToWait)
{
yield return new WaitForSeconds(timeToWait);
frayTextBox.SetActive(true);
frayText.text = "You killed him. I'll give you a moment to celebrate.";
StartCoroutine(FrayFightCutscene2(3));
}
IEnumerator FrayFightCutscene2(int timeToWait)
{
yield return new WaitForSeconds(timeToWait);
frayText.text = "Why aren't I so happy? Well you see, my mission is not yet complete.";
StartCoroutine(FrayFightCutscene3(5));
//play animation
}
IEnumerator FrayFightCutscene3(int timeToWait)
{
yield return new WaitForSeconds(timeToWait);
frayText.text = "Your mission was to bring the Artifact home. Mine... to take it from you.";
StartCoroutine(FrayFightCutscene4(4));
}
IEnumerator FrayFightCutscene4(int timeToWait)
{
yield return new WaitForSeconds(timeToWait);
frayText.text = "";
frayTextBox.SetActive(false);
fray.GetComponent<FrayScript>().inFight = true;
}
//called by FrayScript once she has been defeated (inFight set to false beforehand)
public void FrayDied()
{
beatFray = true;
inFrayFight = false;
StartCoroutine(FrayDefeated1(0));
}
IEnumerator FrayDefeated1(int timeToWait)
{
yield return new WaitForSeconds(timeToWait);
frayTextBox.SetActive(true);
frayText.text = "Agh... You were much stronger than I thought.";
StartCoroutine(FrayDefeated2(3));
}
IEnumerator FrayDefeated2(int timeToWait)
{
yield return new WaitForSeconds(timeToWait);
frayText.text = "The Artifact will belong to Elacsc someday... there will never be peace between us.";
StartCoroutine(Credits(5));
}
IEnumerator Credits(int timeToWait)
{
yield return new WaitForSeconds(timeToWait);
//hide other ui objects from view
frayTextBox.SetActive(false);
frayText.text = "";
healthCanvas.SetActive(false);
credits.SetActive(true);
}
}