-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TutorialStep.cs
118 lines (94 loc) · 3.35 KB
/
TutorialStep.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
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Events;
using SweatyChair.UI;
namespace SweatyChair
{
/// <summary>
/// A step in a tutorial, a step should contain at least ONE tutorial task.
/// </summary>
public class TutorialStep : MonoBehaviour
{
// Step count on current tutorial instance, for game analytics
private static int _stepNumber = 1;
public event UnityAction completed;
[HideInInspector] public TutorialTask[] tasks;
[SerializeField] private TutorialStep _nextStep;
[SerializeField] private string _sceneName;
private int _assistantCount = 0;
private int _completedCount = 0;
public TutorialStep nextStep => _nextStep;
public static void ResetStepNumber()
{
_stepNumber = 1;
}
private void Awake()
{
tasks = GetComponents<TutorialTask>();
_assistantCount = tasks.Length;
foreach (TutorialTask tt in tasks) {
tt.completed += OnStepComplete;
tt.failed += OnStepFail;
}
}
private void Start()
{
TutorialManager.SetCurrentTutorialStep(this);
if (!string.IsNullOrEmpty(_sceneName) && SceneManager.GetActiveScene().name != _sceneName)
SceneManager.LoadScene(_sceneName);
}
private void Update()
{
if (Input.GetMouseButtonUp(0))
TutorialManager.FireCompleteTrigger(TutoriaCompletelTrigger.OnClick);
// TODO: on drag
}
public void Activate()
{
//Debug.LogFormat("{0}/{1}:TutorialStep - tutorial step started", transform.parent.name, transform.name);
gameObject.SetActive(true);
}
public void OnStepComplete()
{
_completedCount++;
if (_assistantCount <= _completedCount)
Complete();
}
public void OnStepFail()
{
Complete();
}
public void StartTask(int index)
{
index = Mathf.Clamp(index, 0, index);
if (tasks.Length > index && tasks[index] != null)
tasks[index].DoStart();
else
Debug.LogErrorFormat("TutorialStep:StartTask - Task {0} does not exist", index);
}
public void CompleteTask(int index)
{
index = Mathf.Max(0, index);
if (tasks.Length > index && tasks[index] != null)
tasks[index].DoComplete();
else
Debug.LogErrorFormat("TutorialStep:CompleteTask - Task {0} does not exist", index);
}
public void Complete()
{
//Debug.LogFormat("{0}/{1}:TutorialStep - tutorial step completed", transform.parent.name, transform.name);
foreach (TutorialTask tt in tasks)
tt.Reset();
if (TutorialManager.currentTutorial != null)
TutorialManager.AnalyticsTutorialStep(TutorialManager.currentTutorial.id, _stepNumber++);
if (_nextStep != null) {
_nextStep.Activate();
} else { // Whole tutorial done
TutorialManager.CompleteCurrentTutorial();
TutorialPanel.DestroyInstance();
}
completed?.Invoke();
Destroy(gameObject);
}
}
}