-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.html
More file actions
1196 lines (1021 loc) · 31.7 KB
/
code.html
File metadata and controls
1196 lines (1021 loc) · 31.7 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<nav class="navbar">
<div class="logo">Andres Wallace</div>
<div class="links">
<a href="index.html">About Me</a>
<a href="projects.html">Projects</a>
<a href="resume.html">Resume</a>
<a href="code.html">Code Samples</a>
</div>
</nav>
<head>
<meta charset="UTF-8">
<title>Code Samples</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<section>
<h2>Code Samples</h2>
<div class="code-grid">
<div class="code-card" onclick="toggleCard(this)">
<h3>Character Controller</h3>
<p class="code-subtitle">Neon Breakline</p>
<p>
Full code for the Character Controller in Neon Breakline (Careful its around 700 lines long)
</p>
<span class="expand-indicator">Click to expand</span>
<div class="code-content">
<pre><code>
using System.Collections;
using Unity.Cinemachine;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
//RULE: the player ALWAYS moves forward
//There is NO input that stops or reverses movement
public static PlayerController Instance;
[Header("Player Stats")]
public bool advance = true;
public bool levelFinished = false;
public int maxHealth = 3;
public int currentHealth;
[HideInInspector] public enum SpeedTier { Base, Lv1, Lv2, Lv3 }
public SpeedTier speedTier = SpeedTier.Base;
public bool playerDied = false;
[Header("Forward Movement Settings")]
public float baseSpeed = 5f;
public float lv1Speed = 7f;
public float lv2Speed = 9f;
public float lv3Speed = 12f;
public float baseAnimSpeed = .8f;
public float lv1AnimSpeed = 1f;
public float lv2AnimSpeed = 1.1f;
public float lv3AnimSpeed = 1.2f;
[Header("Jump Settings")]
public float jumpForce = 8f;
public Transform groundCheck;
[SerializeField] private float groundCheckY = 0.2f;
[SerializeField] private float groundCheckX = 0.5f;
public LayerMask groundLayer;
public bool isGrounded;
[Header("Jump Assist")]
[SerializeField] private float coyoteTime = 0.1f;
[SerializeField] private float jumpBufferTime = 0.1f;
private float coyoteTimeCounter;
private float jumpBufferCounter;
[Header("Slide Settings")]
public float slideDuration = 1f;
[Header("Attack Settings")]
public GameObject attackHitbox;
public float attackCooldown = 0.25f;
private float lastAttackTime;
public GameObject slashFX;
[SerializeField] private float attackBufferTime = .1f;
private float attackBufferCounter;
[Header("Parry Settings")]
public GameObject parryHitbox;
public float parryAnimDuration = 0.12f;
public float parrySpeedMultiplier = 0.5f;
[Header("Pushback Settings")]
public float pushbackForce = 6f;
public float pushbackDuration = 0.2f;
private Coroutine pushbackCoroutine;
[Header("Hitstop Settings")]
public float hitstopDuration = 0.07f;
public float parryHitstopDuration = 0.1f;
public bool isInHitstop = false;
[Header("Screen Shake Settings")]
public CinemachineImpulseSource wallHitImpulseSource;
public CinemachineImpulseSource parryImpulseSource;
public CinemachineImpulseSource attackImpulseSource;
[Header("Camera Settings")]
public float baseFOV = 40f;
public float lv1FOV = 41f;
public float lv2FOV = 43f;
public float lv3FOV = 45f;
[Header("Particles")]
public ParticleSystem parryParticles;
public ParticleSystem jumpParticles;
public ParticleSystem bloodParticles;
#region Input Actions
private PlayerInput playerInput;
private InputAction jumpAction;
private InputAction attackAction;
private InputAction parryAction;
private InputAction crouchAction;
#endregion
#region Input Bools
public bool JumpJustPressed { get; private set; }
public bool JumpBeingHeld { get; private set; }
public bool JumpReleased { get; private set; }
public bool AttackInput { get; private set; }
public bool ParryInput { get; private set; }
public bool CrouchInput { get; private set; }
#endregion
[HideInInspector] public Rigidbody2D rb;
[HideInInspector] public Animator anim;
[HideInInspector] public AudioSource audsrc;
[HideInInspector] public PlayerStates pState;
[HideInInspector] public AudioManager audioManager;
void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
Instance = this;
}
playerInput = GetComponent();
SetupInputActions();
rb = GetComponent();
anim = GetComponent();
audsrc = GetComponent();
pState = GetComponent();
}
void Start()
{
ToggleCursorOff();
audioManager = AudioManager.Instance;
currentHealth = maxHealth;
PlayerHUDManager.Instance.UpdateHealthUI();
PlayerHUDManager.Instance.UpdateSpeedTierUI();
}
private void SetupInputActions()
{
jumpAction = playerInput.actions["Jump"];
attackAction = playerInput.actions["Attack"];
parryAction = playerInput.actions["Parry"];
crouchAction = playerInput.actions["Crouch"];
}
public void GetInputs()
{
AttackInput = attackAction.WasPressedThisFrame();
ParryInput = parryAction.WasPressedThisFrame();
CrouchInput = crouchAction.IsPressed();
JumpJustPressed = jumpAction.WasPressedThisFrame();
JumpBeingHeld = jumpAction.IsPressed();
JumpReleased = jumpAction.WasReleasedThisFrame();
}
void Update()
{
if (GameManager.Instance.gameIsPaused || levelFinished || playerDied) return;
CheckGround();
GetInputs();
HandleJump();
Slide();
HandleAttack();
Parry();
}
void FixedUpdate()
{
if (GameManager.Instance.gameIsPaused || levelFinished) return;
if (advance && !playerDied)
{
rb.linearVelocity = new Vector2(GetSpeedFromTier(), rb.linearVelocityY);
}
}
private void Jump()
{
if (!pState.jumping && !pState.sliding && !pState.parrying)
{
pState.jumping = true;
anim.SetBool("Jumping", true);
jumpParticles.Play();
audioManager.PlayRandomJumpSound();
rb.linearVelocity = new Vector2(rb.linearVelocityX, jumpForce);
}
}
private void HandleJump()
{
UpdateCoyoteTime();
UpdateJumpBuffer();
TryJump();
}
private void UpdateCoyoteTime()
{
if (isGrounded)
{
coyoteTimeCounter = coyoteTime;
}
else
{
coyoteTimeCounter -= Time.deltaTime;
}
}
private void UpdateJumpBuffer()
{
if (JumpJustPressed)
jumpBufferCounter = jumpBufferTime;
else
jumpBufferCounter -= Time.deltaTime;
}
private void TryJump()
{
if (jumpBufferCounter > 0f && coyoteTimeCounter > 0f)
{
Jump();
jumpBufferCounter = 0f;
coyoteTimeCounter = 0f;
}
}
private void Slide()
{
if (CrouchInput && isGrounded && !pState.sliding && !pState.attacking && !pState.parrying)
{
StartCoroutine(PerformSlide());
}
}
private IEnumerator PerformSlide()
{
pState.sliding = true;
anim.SetTrigger("Slide");
audioManager.PlaySFX(audioManager.slideSound);
yield return new WaitForSeconds(slideDuration);
pState.sliding = false;
}
private void HandleAttack()
{
UpdateAttackBuffer();
TryAttack();
}
private bool Attack()
{
if (Time.time >= lastAttackTime + attackCooldown && !pState.attacking && !pState.sliding && !pState.parrying)
{
pState.attacking = true;
anim.SetTrigger("Attack");
audioManager.PlayRandomAttackSound();
lastAttackTime = Time.time;
return true;
}
return false;
}
private void UpdateAttackBuffer()
{
if (AttackInput)
attackBufferCounter = attackBufferTime;
else
attackBufferCounter -= Time.deltaTime;
}
private void TryAttack()
{
if (attackBufferCounter > 0f)
{
if (Attack())
{
attackBufferCounter = 0f;
}
}
}
private void Parry()
{
if (ParryInput && !pState.parrying && !pState.attacking && !pState.sliding && !pState.beingPushedBack)
{
StartCoroutine(ParryRoutine());
}
}
private IEnumerator ParryRoutine()
{
if (pState.beingPushedBack)
yield break;
pState.parrying = true;
anim.SetTrigger("Parry");
audioManager.PlaySFX(audioManager.parrySound);
float originalSpeed = GetSpeedFromTier();
advance = false;
rb.linearVelocity = new Vector2(originalSpeed * parrySpeedMultiplier, rb.linearVelocityY);
yield return new WaitForSeconds(parryAnimDuration);
if (CanAdvance())
{
advance = true;
}
pState.parrying = false;
}
public void OnSuccessfulParry()
{
pState.parrying = false;
anim.SetTrigger("ParrySuccess");
parryParticles.Play();
audioManager.PlayRandomParriedSound();
StartCoroutine(TemporaryInvincibility(0.05f)); // Set Based on animation
GainSpeedTier();
StartCoroutine(Hitstop(parryHitstopDuration));
StartCoroutine(SmallSpeedBoost());
parryImpulseSource.GenerateImpulse();
}
public IEnumerator TemporaryInvincibility(float duration)
{
pState.invincible = true;
yield return new WaitForSeconds(duration);
pState.invincible = false;
}
public IEnumerator SmallSpeedBoost()
{
float originalSpeed = GetSpeedFromTier();
advance = false;
rb.linearVelocity = new Vector2(originalSpeed * 1.5f, rb.linearVelocityY);
yield return new WaitForSeconds(0.2f);
if (CanAdvance())
{
advance = true;
}
}
public float GetSpeedFromTier()
{
switch (speedTier)
{
case SpeedTier.Lv1:
return lv1Speed;
case SpeedTier.Lv2:
return lv2Speed;
case SpeedTier.Lv3:
return lv3Speed;
default:
return baseSpeed;
}
}
private void CheckGround()
{
isGrounded = Physics2D.Raycast(groundCheck.position, Vector2.down, groundCheckY, groundLayer)
|| Physics2D.Raycast(groundCheck.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, groundLayer)
|| Physics2D.Raycast(groundCheck.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, groundLayer);
if (isGrounded && rb.linearVelocityY <= 0)
{
pState.jumping = false;
anim.SetBool("Jumping", false);
anim.SetBool("Falling", false);
}
else if (!isGrounded)
{
anim.SetBool("Falling", true);
}
}
private void OnDrawGizmos()
{
if (groundCheck == null) return;
// Draw the three raycast lines in the Scene view
Gizmos.color = Color.yellow;
Vector3 center = groundCheck.position;
Vector3 right = center + new Vector3(groundCheckX, 0, 0);
Vector3 left = center + new Vector3(-groundCheckX, 0, 0);
Gizmos.DrawLine(center, center + Vector3.down * groundCheckY);
Gizmos.DrawLine(right, right + Vector3.down * groundCheckY);
Gizmos.DrawLine(left, left + Vector3.down * groundCheckY);
}
public void GainSpeedTier()
{
if (playerDied) return;
if (speedTier < SpeedTier.Lv3)
{
speedTier++;
PlayerHUDManager.Instance.UpdateSpeedTierUI();
audioManager.PlayTierUpPitched(speedTier);
}
}
public void LoseSpeedTier()
{
if (playerDied) return;
if (speedTier > SpeedTier.Base)
{
if (pState.invincible) return;
audioManager.PlayTierDownPitched(speedTier);
speedTier--;
PlayerHUDManager.Instance.UpdateSpeedTierUI();
}
}
public void TakeDamage(int damage)
{
if (pState.invincible || playerDied) return;
currentHealth -= damage;
PlayerHUDManager.Instance.UpdateHealthUI();
if (currentHealth <= 0)
{
Die();
}
}
private void Die()
{
if (playerDied) return;
StartCoroutine(DeathRoutine());
}
private IEnumerator DeathRoutine()
{
audioManager.PlayRandomDeathSound();
advance = false;
playerDied = true;
rb.linearVelocity = new Vector2(0,0);
anim.Play("PlayerDeath");
yield return new WaitForSeconds(2f);
GameManager.Instance.RestartLevelAfterFade();
}
public void ApplyPushback()
{
if (pState.invincible) return;
audioManager.PlaySFX(audioManager.hitWallSound);
pState.beingPushedBack = true;
anim.SetTrigger("Hit");
rb.linearVelocity = Vector2.zero;
rb.AddForce(new Vector2(-pushbackForce, pushbackForce * 0.5f), ForceMode2D.Impulse);
LoseSpeedTier();
wallHitImpulseSource.GenerateImpulse();
StartCoroutine(Hitstop(hitstopDuration));
if (pushbackCoroutine != null)
{
StopCoroutine(pushbackCoroutine);
}
pushbackCoroutine = StartCoroutine(ContinueAfterPushback(pushbackDuration));
}
public IEnumerator ContinueAfterPushback(float delay)
{
advance = false;
yield return new WaitForSeconds(delay);
advance = true;
pState.beingPushedBack = false;
}
IEnumerator Hitstop(float time)
{
if (isInHitstop)
yield break;
isInHitstop = true;
//float originalTimeScale = Time.timeScale;
Time.timeScale = 0f;
yield return new WaitForSecondsRealtime(time);
if (!GameManager.Instance.gameIsPaused)
{
Time.timeScale = /*originalTimeScale*/ 1f;
}
isInHitstop = false;
}
public void DoHitStop(float time)
{
StartCoroutine(Hitstop(time));
}
public void DoAttackScreenShake()
{
attackImpulseSource.GenerateImpulse();
}
public void SpawnSlashFX()
{
float randomDir = Random.Range(-4,4);
Quaternion rotation = Quaternion.Euler(0, 0, randomDir);
Vector3 spawnPos = transform.position;
spawnPos.y += Random.Range(-.15f, .15f);
GameObject slash = Instantiate(slashFX, spawnPos, rotation);
float randomScale = Random.Range(.9f, 1.1f);
slash.transform.localScale *= randomScale;
}
public void SpawnBloodFX(Vector3 position)
{
ParticleSystem particles = Instantiate(bloodParticles, position, Quaternion.identity);
Destroy(particles.gameObject, particles.main.duration + particles.main.startLifetime.constantMax);
}
public void ChangeCameraFov()
{
baseFOV = CameraManager.Instance.normalCameraFov;
switch (speedTier)
{
case SpeedTier.Lv1:
CameraManager.Instance.currentCamera.Lens.FieldOfView = lv1FOV;
anim.SetFloat("RunSpeed", lv1AnimSpeed);
break;
case SpeedTier.Lv2:
CameraManager.Instance.currentCamera.Lens.FieldOfView = lv2FOV;
anim.SetFloat("RunSpeed", lv2AnimSpeed);
break;
case SpeedTier.Lv3:
CameraManager.Instance.currentCamera.Lens.FieldOfView = lv3FOV;
anim.SetFloat("RunSpeed", lv3AnimSpeed);
break;
default:
CameraManager.Instance.currentCamera.Lens.FieldOfView = baseFOV;
anim.SetFloat("RunSpeed", baseAnimSpeed);
break;
}
}
public void EnableAttackHitbox()
{
attackHitbox.SetActive(true);
}
public void DisableAttackHitbox()
{
pState.attacking = false;
attackHitbox.SetActive(false);
}
public void EnableParryHitbox()
{
parryHitbox.SetActive(true);
}
public void DisableParryHitbox()
{
pState.parrying = false;
parryHitbox.SetActive(false);
}
public bool CanAdvance()
{
return !pState.beingPushedBack; // Add anythyng needed
}
public void PlayFootStepSound()
{
audioManager.PlayRandomFootStepSound();
}
/// UTILITIES
public void ToggleCursorOn()
{
if (playerInput.currentControlScheme == "Controller") return;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
public void ToggleCursorOff()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
</code></pre>
</div>
</div>
<div class="code-card" onclick="toggleCard(this)">
<h3>Game Manager</h3>
<p class="code-subtitle">Neon Breakline</p>
<p>
Handles game states and scene changes.
</p>
<span class="expand-indicator">Click to expand</span>
<div class="code-content">
<pre><code>
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public static GameManager Instance { get; private set; }
[SerializeField] private GameObject pauseMenu;
[SerializeField] private Button pauseMenuButton;
public bool gameIsPaused;
private PlayerInput playerInput;
private InputAction pauseAction;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
Instance = this;
}
Time.timeScale = 1f;
playerInput = FindFirstObjectByType<PlayerInput>();
pauseAction = playerInput.actions["Pause"];
}
private void Update()
{
if (pauseAction.WasPressedThisFrame())
{
TogglePause();
}
}
public void TogglePause()
{
if (PlayerController.Instance.pState.inTutorial || PlayerController.Instance.levelFinished) return;
if (gameIsPaused)
ResumeGame();
else
PauseGame();
}
public void PauseGame()
{
Time.timeScale = 0f;
gameIsPaused = true;
pauseMenu.SetActive(true);
pauseMenuButton.Select();
}
public void ResumeGame()
{
Time.timeScale = 1f;
gameIsPaused = false;
pauseMenu.SetActive(false);
}
public void ExitToMainMenu()
{
Time.timeScale = 1f;
SceneManager.LoadScene("MainMenu");
}
public void RestartLevel()
{
Time.timeScale = 1f;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
</code></pre>
</div>
</div>
<div class="code-card" onclick="toggleCard(this)">
<h3>Save Encryption</h3>
<p class="code-subtitle">Neon Breakline</p>
<p>
Helped encrypt the save data for Neon Breakline so it couldn't be easily editable.
</p>
<span class="expand-indicator">Click to expand</span>
<div class="code-content">
<pre><code>
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
public static class EncryptionUtility
{
private static readonly string key = "ProjectForwardWallaceKey"; //24 chars
private static readonly string iv = "ProjectForwardIV"; //16 chars
public static string Encrypt(string plainText)
{
using (Aes aes = Aes.Create())
{
aes.Key = Encoding.UTF8.GetBytes(key.Substring(0, 24));
aes.IV = Encoding.UTF8.GetBytes(iv.Substring(0, 16));
using (MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(plainText);
sw.Close();
return Convert.ToBase64String(ms.ToArray());
}
}
}
public static string Decrypt(string cipherText)
{
using (Aes aes = Aes.Create())
{
aes.Key = Encoding.UTF8.GetBytes(key.Substring(0, 24));
aes.IV = Encoding.UTF8.GetBytes(iv.Substring(0, 16));
byte[] buffer = Convert.FromBase64String(cipherText);
using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(cipherText)))
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read))
using (StreamReader sr = new StreamReader(cs))
{
return sr.ReadToEnd();
}
}
}
}
</code></pre>
</div>
</div>
<div class="code-card" onclick="toggleCard(this)">
<h3>Dialogue Cutscene System</h3>
<p class="code-subtitle">Thousand Paths (Metroidvania)</p>
<p>
Handled Dialogue Scenes in my Metroidvania Project.
</p>
<span class="expand-indicator">Click to expand</span>
<div class="code-content">
<pre><code>
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEditor.Rendering;
using UnityEngine;
using UnityEngine.UI;
public class CutsceneSystem : MonoBehaviour
{
[Header("Core")]
public static CutsceneSystem instance;
public AudioSource audioSource;
[Header("Dialogue")]
public bool isInDialogue;
public DialogueLines[] lines;
public int currentLine;
public float ogTextSpeed;
public float textSpeed;
[SerializeField][TextArea] private string[] textInfo;
[Header("UI")]
public GameObject dialogueBox;
public TMP_Text currentText;
public TMP_Text currentNameText;
public Image characterImage;
public Image indicator;
[HideInInspector] public Animator indicatorAnim;
[Header("CharacterCheck")]
public bool solveigTalking;
public bool asaTalking;
public bool kurohaTalking;
public bool ichikaTalking;
public bool kingTalking;
public bool mayukaTalking;
public bool mikaelTalking;
private Queue<DialogueLines> dialogueQueue;
private bool isScrolling;
private void Awake()
{
if (instance != null && instance != this)
{
Destroy(gameObject);
}
else
{
instance = this;
}
dialogueQueue = new Queue<DialogueLines>();
ogTextSpeed = textSpeed;
indicatorAnim = indicator.GetComponent<Animator>();
this.gameObject.SetActive(false);
}
public void Update()
{
if (!isInDialogue) return;
if (textSpeed > ogTextSpeed)
{
textSpeed = ogTextSpeed;
}
if (Input.GetButtonDown("Submit") || Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.Mouse0))
{
DisplayNextLine();
if (isScrolling)
{
textSpeed /= 7;
}
}
else if (Input.GetButtonUp("Submit") || Input.GetKeyUp(KeyCode.Space) || Input.GetKeyUp(KeyCode.Mouse0))
{
textSpeed *= 7;
}
}
public void StartDialogue(DialogueLines[] lines)
{
if (isInDialogue) return;
textSpeed = ogTextSpeed;
dialogueQueue.Clear();
foreach (var line in lines)
{
if (!PlayerController.Instance.playedDialogueIds.Contains(line.id))
{
dialogueQueue.Enqueue(line);
if (line.deleteAfterPlay)
{
PlayerController.Instance.playedDialogueIds.Add(line.id);
}
}
}
isInDialogue = true;
PlayerController.Instance.pState.cantControl = true;
PlayerController.Instance.rb.velocity = new Vector3(0, 0, 0);
PlayerController.Instance.ReturnToIdle();
dialogueBox.SetActive(true);
DisplayNextLine();
}
public void DisplayNextLine()
{
if (isScrolling) return;
indicatorAnim.SetBool("End", false);
if (dialogueQueue.Count == 0)
{
EndDialogue();
return;
}
if (dialogueQueue.Count == 1)
{
indicatorAnim.SetBool("End",true);
}
var currentLine = dialogueQueue.Dequeue();
UpdateUI(currentLine);
PlayDialogueSound(currentLine);
StartCoroutine(ScrollText(currentLine.dialogueText));
if (currentLine.action.GetPersistentEventCount() > 0)
{
currentLine.action.Invoke();
}
}
private void UpdateUI(DialogueLines line)
{
if (line.character.ToString() == "NONE")
{
currentNameText.text = "";
characterImage.sprite = line.GetCharacterPortrait();
}
else
{
currentNameText.text = line.character.ToString();
characterImage.sprite = line.GetCharacterPortrait();
}
}
private IEnumerator ScrollText(string text)
{
indicatorAnim.SetTrigger("Scrolling");
isScrolling = true;
currentText.text = "";
foreach (char c in text)
{
currentText.text += c;
yield return new WaitForSeconds(textSpeed);
}
isScrolling = false;
if (indicatorAnim.GetBool("End") == false)
{
indicatorAnim.SetTrigger("Next");
}
}
private void PlayDialogueSound(DialogueLines line)
{
if (audioSource == null) return;
AudioClip clip = line.GetSoundEffect();
if (clip != null)
{
audioSource.PlayOneShot(clip);
}
}
public void EndDialogue()
{
dialogueBox.SetActive(false);
isInDialogue = false;
PlayerController.Instance.pState.cantControl = false;
this.gameObject.SetActive(false);
}
}
</code></pre>
</div>
</div>
<div class="code-card" onclick="toggleCard(this)">
<h3>Health Controller</h3>
<p class="code-subtitle">Thousand Paths (Metroidvania)</p>