-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorial.cs
More file actions
119 lines (105 loc) · 4.17 KB
/
Copy pathTutorial.cs
File metadata and controls
119 lines (105 loc) · 4.17 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
using System;
using DG.Tweening;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class Tutorial: MonoBehaviour {
public event Action OnTutorialCompleted;
public static Tutorial I {
get {
if (_instance == null) {
_instance = FindFirstObjectByType<Tutorial>();
}
return _instance;
}
}
private static Tutorial _instance;
const string tutorialSaveString = "tutorial_completed";
public bool TutorialCompleted => PlayerPrefs.GetInt(tutorialSaveString, 0) == 1;
[SerializeField] bool resetProgress = false;
[SerializeField] TextMeshProUGUI tutorialText;
[SerializeField] Image inputImage;
[SerializeField] Sprite arrowKeys;
[SerializeField] Sprite leftStick;
[SerializeField] Sprite westBtn;
[SerializeField] Sprite southBtn;
[SerializeField] Sprite spaceBar;
[SerializeField] Sprite leftShift;
CanvasGroup canvasGroup;
void Awake() {
#if !UNITY_EDITOR
resetProgress = false;
#endif
canvasGroup = tutorialText.GetComponentInParent<CanvasGroup>();
if (canvasGroup == null) {
canvasGroup = tutorialText.gameObject.transform.parent.AddComponent<CanvasGroup>();
}
canvasGroup.interactable = false;
canvasGroup.blocksRaycasts = false;
canvasGroup.alpha = 0f;
inputImage.preserveAspect = true;
if (resetProgress) {
PlayerPrefs.SetInt(tutorialSaveString, 0);
}
if (TutorialCompleted) return;
DoTutorial();
}
async void DoTutorial() {
// Learn Movement
await Awaitable.WaitForSecondsAsync(1.5f);
tutorialText.text = $"{GetText("Press the <i>arrows keys</i>", "Use the <i>left stick</i>")} to move...";
inputImage.sprite = GetSprite(arrowKeys, leftStick);
await AwaitFade(true);
var playerMovement = InputSystem.actions.FindAction("Move", throwIfNotFound:true);
while (playerMovement.ReadValue<Vector2>() == Vector2.zero) {
await Awaitable.NextFrameAsync();
}
await Awaitable.WaitForSecondsAsync(1f);
await AwaitFade(false, 1f);
// Learn Attack
tutorialText.text = $"Press {GetText("<i>SpaceBar</i>", "<i>West Button</i>")} to attack...";
inputImage.sprite = GetSprite(spaceBar, westBtn);
await AwaitFade(true);
bool attacked = false;
void OnAttack() => attacked = true;
Player.I.OnAttack += OnAttack;
while (!attacked) {
await Awaitable.NextFrameAsync();
}
Player.I.OnAttack -= OnAttack;
await Awaitable.WaitForSecondsAsync(1f);
await AwaitFade(false, 1f);
// Learn Dodge
tutorialText.text = $"Press {GetText("<i>Shift</i>", "<i>South Button</i>")} to dodge...";
inputImage.sprite = GetSprite(leftShift, southBtn);
await AwaitFade(true);
bool dodged = false;
void OnDodge() => dodged = true;
Player.I.OnDodge += OnDodge;
while (!dodged) {
await Awaitable.NextFrameAsync();
}
Player.I.OnDodge -= OnDodge;
await Awaitable.WaitForSecondsAsync(1f);
await AwaitFade(false, 1f);
// Final Step!
tutorialText.text = "<b>Good Luck!</b>";
tutorialText.fontSize = 128;
Destroy(inputImage);
await AwaitFade(true, 2f);
await AwaitFade(false);
//Set tutorial as completed
PlayerPrefs.SetInt(tutorialSaveString, 1);
OnTutorialCompleted?.Invoke();
}
async Awaitable AwaitFade(bool fadeIn, float additionalWait = 0f) {
const float fadeTime = 1f;
canvasGroup.DOFade(fadeIn ? 1f : 0f, fadeTime).SetEase(Ease.InOutSine);
await Awaitable.WaitForSecondsAsync(fadeTime + additionalWait);
}
string GetText(string mouseKeyboardText, string controllerText) => ControllerConnected ? controllerText : mouseKeyboardText;
Sprite GetSprite(Sprite mouseKeyboardSprite, Sprite controllerSprite) => ControllerConnected ? controllerSprite : mouseKeyboardSprite;
bool ControllerConnected => Gamepad.all.Count > 0;
}