-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ExampleDialoguePlayback.cs
80 lines (62 loc) · 2.36 KB
/
ExampleDialoguePlayback.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
using System.Collections;
using CleverCrow.Fluid.Databases;
using CleverCrow.Fluid.Dialogues.Graphs;
using UnityEngine;
using UnityEngine.UI;
namespace CleverCrow.Fluid.Dialogues.Examples {
public class ExampleDialoguePlayback : MonoBehaviour {
private DialogueController _ctrl;
public DialogueGraph dialogue;
[Header("Graphics")]
public GameObject speakerContainer;
public Image portrait;
public Text lines;
public RectTransform choiceList;
public ChoiceButton choicePrefab;
private void Awake () {
var database = new DatabaseInstance();
_ctrl = new DialogueController(database);
// @NOTE If you don't need audio just call _ctrl.Events.Speak((actor, text) => {}) instead
_ctrl.Events.SpeakWithAudio.AddListener((actor, text, audioClip) => {
if (audioClip) Debug.Log($"Audio Clip Detected ${audioClip.name}");
ClearChoices();
portrait.sprite = actor.Portrait;
lines.text = text;
StartCoroutine(NextDialogue());
});
_ctrl.Events.Choice.AddListener((actor, text, choices) => {
ClearChoices();
portrait.sprite = actor.Portrait;
lines.text = text;
choices.ForEach(c => {
var choice = Instantiate(choicePrefab, choiceList);
choice.title.text = c.Text;
choice.clickEvent.AddListener(_ctrl.SelectChoice);
});
});
_ctrl.Events.End.AddListener(() => {
speakerContainer.SetActive(false);
});
_ctrl.Events.NodeEnter.AddListener((node) => {
Debug.Log($"Node Enter: {node.GetType()} - {node.UniqueId}");
});
_ctrl.Play(dialogue);
}
private void ClearChoices () {
foreach (Transform child in choiceList) {
Destroy(child.gameObject);
}
}
private IEnumerator NextDialogue () {
yield return null;
while (!Input.GetMouseButtonDown(0)) {
yield return null;
}
_ctrl.Next();
}
private void Update () {
// Required to run actions that may span multiple frames
_ctrl.Tick();
}
}
}