-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogueInteractable.cs
More file actions
201 lines (169 loc) · 6.63 KB
/
Copy pathDialogueInteractable.cs
File metadata and controls
201 lines (169 loc) · 6.63 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
#nullable enable
namespace Yarn.Unity.Samples
{
using UnityEngine;
using UnityEngine.Events;
public abstract class Interactable : MonoBehaviour
{
[SerializeField] protected UnityEvent<bool>? onActiveChanged;
[SerializeField] protected UnityEvent? onInteractionStarted;
[SerializeField] protected UnityEvent? onInteractionEnded;
private bool _isCurrent;
public virtual bool IsCurrent
{
get => _isCurrent; set
{
_isCurrent = value;
onActiveChanged?.Invoke(value);
}
}
public abstract YarnTask Interact(GameObject interactor);
public virtual bool InteractorShouldTurnToFaceWhenInteracted => false;
}
public class DialogueInteractable : Interactable
{
[SerializeField] DialogueReference dialogue = new();
[SerializeField] DialogueRunner? dialogueRunner;
[SerializeField] bool turnsToInteractor = true;
public override bool InteractorShouldTurnToFaceWhenInteracted => turnsToInteractor;
public void OnValidate()
{
#if UNITY_EDITOR
if (UnityEditor.PrefabUtility.IsPartOfPrefabAsset(this))
{
return;
}
#endif
if (dialogueRunner == null)
{
dialogueRunner = FindAnyObjectByType<DialogueRunner>();
}
if (dialogueRunner != null && dialogueRunner.YarnProject != null && dialogue.project == null)
{
dialogue.project = dialogueRunner.YarnProject;
}
}
public override bool IsCurrent
{
set
{
if (value == true)
{
// We've been told we're active. Double check that we
// actually CAN be active based on the additional
// information we have about what would happen if we were
// interacted with.
if (dialogue == null || dialogue.IsValid == false || dialogue.nodeName == null)
{
// We have no dialogue reference, so we can't be interacted with.
return;
}
if (dialogueRunner == null)
{
// We have no dialogue runner, so we can't be interacted with.
onActiveChanged?.Invoke(false);
return;
}
if (dialogueRunner.YarnProject == null)
{
// The dialogue runner has no Yarn Project. We can't ask
// it for saliency info.
onActiveChanged?.Invoke(false);
return;
}
// TODO: remove this once YS core is updated
if (dialogueRunner.Dialogue.ContentSaliencyStrategy == null)
{
dialogueRunner.Dialogue.ContentSaliencyStrategy = new Yarn.Saliency.FirstSaliencyStrategy();
}
var runnableContent = dialogueRunner.Dialogue.GetSaliencyOptionsForNodeGroup(dialogue.nodeName);
var content = dialogueRunner.Dialogue.ContentSaliencyStrategy.QueryBestContent(runnableContent);
if (content == null)
{
// We have no content we can run. Don't show the indicator.
onActiveChanged?.Invoke(false);
return;
}
}
base.IsCurrent = value;
}
}
protected void Awake()
{
IsCurrent = false;
PrewarmJIT(dialogueRunner);
}
static bool hasPrewarmed;
static void PrewarmJIT(DialogueRunner? dialogueRunner)
{
if (hasPrewarmed)
{
return;
}
// If we're not using IL2CPP, we can get a framerate hitch the first
// time we ask the dialogue system if there's any content, due to
// JITing. Pre-warm the JIT by manually exercising a hotspot.
if (dialogueRunner != null && dialogueRunner.YarnProject != null && dialogueRunner.YarnProject.Program != null)
{
// An invalid variable name, but this will cause all necessary
// methods to JIT. Yes, this is a hack. If you know of a better
// way to pre-warm the JIT in Mono, please contact me at
// jon@yarnspinner.dev.
dialogueRunner.YarnProject.Program.GetVariableKind("");
hasPrewarmed = true;
}
}
public override async YarnTask Interact(GameObject interactor)
{
if (dialogue == null)
{
return;
}
if (dialogueRunner == null)
{
Debug.LogError($"Can't run dialogue {dialogue}: dialogue runner not set");
return;
}
if (!dialogue.IsValid || dialogue.nodeName == null)
{
Debug.LogError($"Can't run dialogue {dialogue}: not a valid dialogue reference");
return;
}
if (dialogueRunner.IsDialogueRunning)
{
Debug.LogError($"Can't run dialogue {dialogue}: dialogue runner is already running");
return;
}
onInteractionStarted?.Invoke();
await dialogueRunner.StartDialogue(dialogue.nodeName);
if (turnsToInteractor)
{
if (TryGetComponent<SimpleCharacter>(out var character))
{
character.lookTarget = interactor.transform;
}
if (TryGetComponent<SimpleCharacter2D>(out var character2D))
{
character2D.lookTarget = interactor.transform;
}
}
var destroyCancellation = destroyCancellationToken;
await dialogueRunner.DialogueTask;
if (destroyCancellation.IsCancellationRequested)
{
return;
}
if (turnsToInteractor)
{
if (TryGetComponent<SimpleCharacter>(out var character))
{
character.lookTarget = null;
}
if (TryGetComponent<SimpleCharacter2D>(out var character2D))
{
character2D.lookTarget = null;
}
}
}
}
}