-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeakAction.cs
140 lines (116 loc) · 4.27 KB
/
SpeakAction.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System.Collections.Generic;
using Unity.LEGO.Behaviours.Actions;
using Unity.LEGO.UI;
using UnityEngine;
namespace Unity.LEGO.Behaviours
{
public class SpeakAction : RepeatableAction
{
public const int MaxCharactersPerSpeechBubble = 60;
[SerializeField]
List<SpeechBubblePrompt.BubbleInfo> m_SpeechBubbleInfos = new List<SpeechBubblePrompt.BubbleInfo>
{ new SpeechBubblePrompt.BubbleInfo { Text = "Hello!", Type = SpeechBubblePrompt.Type.Talk } };
[SerializeField]
GameObject m_SpeechBubblePromptPrefab = default;
SpeechBubblePrompt m_SpeechBubblePrompt;
bool m_PromptActive = true;
int m_Id;
protected override void Reset()
{
base.Reset();
m_IconPath = "Assets/LEGO/Gizmos/LEGO Behaviour Icons/Speak Action.png";
}
protected override void Start()
{
base.Start();
if (IsPlacedOnBrick())
{
foreach (var speechBubleInfo in m_SpeechBubbleInfos)
{
if (speechBubleInfo.Text.Length > MaxCharactersPerSpeechBubble)
{
speechBubleInfo.Text = speechBubleInfo.Text.Substring(0, MaxCharactersPerSpeechBubble);
}
}
}
}
protected void Update()
{
if (m_Active)
{
if (m_SpeechBubbleInfos.Count > 0 && !m_SpeechBubblePrompt)
{
SetupPrompt();
}
UpdatePrompt(IsVisible());
}
}
void SetupPrompt()
{
PromptPlacementHandler promptHandler = null;
foreach (var brick in m_ScopedBricks)
{
if (brick.GetComponent<PromptPlacementHandler>())
{
promptHandler = brick.GetComponent<PromptPlacementHandler>();
}
var speakActions = brick.GetComponents<SpeakAction>();
foreach (var speakAction in speakActions)
{
if (speakAction.m_SpeechBubblePrompt)
{
m_SpeechBubblePrompt = speakAction.m_SpeechBubblePrompt;
break;
}
}
}
var activeFromStart = IsVisible();
// Create a new speech bubble prompt if none was found.
if (!m_SpeechBubblePrompt)
{
if (!promptHandler)
{
promptHandler = gameObject.AddComponent<PromptPlacementHandler>();
}
var go = Instantiate(m_SpeechBubblePromptPrefab, promptHandler.transform);
m_SpeechBubblePrompt = go.GetComponent<SpeechBubblePrompt>();
// Get the current scoped bounds - might be different than the initial scoped bounds.
var scopedBounds = GetScopedBounds(m_ScopedBricks, out _, out _);
promptHandler.AddInstance(go, scopedBounds, PromptPlacementHandler.PromptType.SpeechBubble, activeFromStart);
}
// Add this Speak Action to the speech bubble prompt.
m_Id = m_SpeechBubblePrompt.AddSpeech(m_SpeechBubbleInfos, m_Pause, m_Repeat, SpeechFinished, activeFromStart, promptHandler);
}
void UpdatePrompt(bool active)
{
if (m_PromptActive != active)
{
m_PromptActive = active;
if (active)
{
m_SpeechBubblePrompt.Activate(m_Id);
}
else
{
m_SpeechBubblePrompt.Deactivate(m_Id);
}
}
}
void SpeechFinished(int id)
{
if (m_Id == id)
{
UpdatePrompt(false);
m_Active = false;
}
}
protected override void OnDestroy()
{
base.OnDestroy();
if (m_SpeechBubblePrompt)
{
UpdatePrompt(false);
}
}
}
}