-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaloonDialog.cs
354 lines (317 loc) · 13.5 KB
/
BaloonDialog.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// This code is part of the Fungus library (https://github.com/snozbot/fungus)
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Linq;
namespace Fungus
{
public enum transiType2
{
/// <summary> Fade In effect sequence. </summary>
FadeIn,
/// <summary> Punch effect sequence. </summary>
Punch,
/// <summary> Stop All </summary>
None
}
/// <summary>
/// Pauses the game/by setting timescale to 0.
/// </summary>
[CommandInfo("Animation",
"Baloon Dialog",
"Sequence of baloon dialogs. Text & Image components MUST be placed in the same parent canvas. Preferably a freshly created canvas. To stop the animation, you must copy the exact settings and it's effect type set to NONE")]
[AddComponentMenu("")]
[ExecuteInEditMode]
public class BaloonDialog : Command
{
[Tooltip("Wait until the tween has finished before executing the next command")]
public transiType2 effectType;
[Tooltip("GameObject/Bitmaps")]
[SerializeField] public GameObject balloonSprite;
[SerializeField] public float blnPositionX = 0f;
[SerializeField] public float blnPositionY = 0f;
[Space(4)]
[Tooltip("Parent Canvas")]
[SerializeField] public GameObject canvas;
[Space(4)]
[Tooltip("Text")]
[SerializeField] public GameObject textComponent;
[SerializeField] protected bool rotateAround = false;
[SerializeField] public float txtX = 0f;
[SerializeField] public float txtY = 0f;
[Space(4)]
[Tooltip("Small bubble dialog Scale")]
[SerializeField] public float sDialogWidth = 0.3f;
[Tooltip("Small bubble dialog Scale")]
[SerializeField] public float sDialogHeight = 0.3f;
[Tooltip("Medium/middle bubble dialog X position")]
[SerializeField] public float mDialogX = 35f;
[Tooltip("Medium/middle bubble dialog Y position")]
[SerializeField] public float mDialogY = 35f;
[Tooltip("Medium/middle bubble dialog Scale")]
[SerializeField] public float mDialogWidth = 0.5f;
[Tooltip("Medium/middle bubble dialog Scale")]
[SerializeField] public float mDialogHeight = 0.5f;
[Tooltip("Big/Main bubble dialog X position")]
[SerializeField] public float bDialogX = 120f;
[Tooltip("Big/Main bubble dialog Y position")]
[SerializeField] public float bDialogY = 120f;
[Tooltip("Big/Main bubble dialog Scale")]
[SerializeField] public float bDialogWidth = 2.0f;
[Tooltip("Big/Main bubble dialog Scale")]
[SerializeField] public float bDialogHeight = 2.0f;
[Space(4)]
[Tooltip("Stop at index, based on how many times it gets triggered e.g button presses/clicks etc")]
[SerializeField] public int stopAtIndex = 0;
[Tooltip("if you have a custom button to trigger the progression of the story add it here so the stop at index could work")]
[SerializeField] protected KeyCode optionalButtonTrigger;
[SerializeField] protected bool waitUntilFinished = false;
private static int indexNum = 0;
private bool isRunning = false;
private Vector3 posxy;
private IEnumerator coroutine;
protected virtual void OnTweenComplete()
{
Continue();
}
protected void FadeIns()
{
coroutine = SlowBaloon(0.3f);
StartCoroutine(coroutine);
}
protected void Punchy()
{
coroutine = PunchBaloon(0.3f);
StartCoroutine(coroutine);
}
protected void saveWait()
{
coroutine = StopAnim(0.2f);
StartCoroutine(coroutine);
}
void Update()
{
if(isRunning)
{
if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(optionalButtonTrigger))
{
indexNum++;
//Debug.Log("Left click." + indexNum);
if(indexNum == stopAtIndex)
{
saveWait();
}
}
}
}
//Slow appearing baloon
protected IEnumerator SlowBaloon(float waitSlowBaloon)
{
if (canvas != null && canvas.activeInHierarchy == false)
{
canvas.SetActive(true);
}
if (balloonSprite != null)
{
posxy = new Vector3(blnPositionX, blnPositionY, 1);
var spwn = posxy;
GameObject blob0 = Instantiate(balloonSprite, spwn, Quaternion.identity) as GameObject;
blob0.SetActive(false);
blob0.name = balloonSprite.name + "_baloonpop";
blob0.transform.SetParent(canvas.transform, false);
blob0.transform.SetAsFirstSibling();
blob0.transform.localScale = new Vector3(sDialogWidth, sDialogHeight, 0f);
LeanTween.value(blob0.gameObject, 0f, 1f, 0.3f).setOnUpdate((float val) =>
{
Image sr = blob0.GetComponent<Image>();
Color newColor = sr.color;
newColor.a = val;
sr.color = newColor;
blob0.SetActive(true);
});
yield return new WaitForSeconds(0.3f);
GameObject blob1 = Instantiate(balloonSprite, spwn + new Vector3(mDialogX, mDialogY, 0f), Quaternion.identity) as GameObject;
blob1.SetActive(false);
blob1.name = balloonSprite.name + "_baloonpop";
blob1.transform.SetParent(canvas.transform, false);
blob1.transform.SetAsFirstSibling();
blob1.transform.localScale = new Vector3(mDialogWidth, mDialogHeight, 0f);
LeanTween.value(blob1.gameObject, 0f, 1f, 1.3f).setOnUpdate((float val) =>
{
Image sr = blob1.GetComponent<Image>();
Color newColor = sr.color;
newColor.a = val;
sr.color = newColor;
blob1.SetActive(true);
});
yield return new WaitForSeconds(0.3f);
GameObject blob2 = Instantiate(balloonSprite, spwn + new Vector3(bDialogX, bDialogY, 0), Quaternion.identity) as GameObject;
blob2.SetActive(false);
blob2.name = balloonSprite.name + "_baloonpop";
blob2.transform.SetParent(canvas.transform, false);
blob2.transform.SetAsFirstSibling();
blob2.transform.localScale = new Vector3(bDialogWidth, bDialogHeight, 0f);
LeanTween.value(blob2.gameObject, 0f, 1f, 1.3f).setOnUpdate((float val) =>
{
Image sr = blob2.GetComponent<Image>();
Color newColor = sr.color;
newColor.a = val;
sr.color = newColor;
blob2.SetActive(true);
});
textComponent.SetActive(false);
textComponent.transform.localPosition = blob2.transform.localPosition + new Vector3(txtX, txtY, 0);
textComponent.transform.SetAsLastSibling();
LeanTween.value(textComponent.gameObject, 0f, 1f, 1f).setOnUpdate((float val) =>
{
Text sr = textComponent.gameObject.GetComponent<Text>();
Color newColor = sr.color;
newColor.a = val;
sr.color = newColor;
textComponent.SetActive(true);
});
if(rotateAround)
{
LeanTween.rotateAroundLocal(textComponent.gameObject, Vector3.forward, 30f, 0.3f).setLoopPingPong(-1);
}
}
isRunning = true;
if (waitUntilFinished)
{
Continue();
}
}
//Punch baloon dialog sequence
protected IEnumerator PunchBaloon(float waitPunch)
{
if (balloonSprite != null)
{
posxy = new Vector3(blnPositionX, blnPositionY, 1);
var spwn = posxy;
GameObject blob0 = Instantiate(balloonSprite, spwn, Quaternion.identity) as GameObject;
blob0.name = balloonSprite.name + "_baloonpop";
blob0.transform.SetParent(canvas.transform, false);
blob0.transform.SetAsFirstSibling();
blob0.transform.localScale = new Vector3(sDialogWidth, sDialogHeight, 0f);
blob0.SetActive(true);
LeanTween.scale(blob0.gameObject, Vector3.zero, 0.5f).setEase(LeanTweenType.punch);
yield return new WaitForSeconds(0.3f);
GameObject blob1 = Instantiate(balloonSprite, spwn + new Vector3(mDialogX, mDialogY, 0f), Quaternion.identity) as GameObject;
blob1.name = balloonSprite.name + "_baloonpop";
blob1.transform.SetParent(canvas.transform, false);
blob1.transform.localScale = new Vector3(0.5f, 0.5f, 0f);
blob1.SetActive(true);
LeanTween.scale(blob1.gameObject, Vector3.zero, 0.5f).setEase(LeanTweenType.punch);
yield return new WaitForSeconds(0.3f);
GameObject blob2 = Instantiate(balloonSprite, spwn + new Vector3(bDialogX, bDialogY, 0), Quaternion.identity) as GameObject;
blob2.name = balloonSprite.name + "_baloonpop";
blob2.transform.SetParent(canvas.transform, false);
blob2.transform.localScale = new Vector3(2f, 2f, 0f);
blob2.SetActive(true);
LeanTween.scale(blob2.gameObject, Vector3.zero, 0.5f).setEase(LeanTweenType.punch);
textComponent.transform.localPosition = blob2.transform.localPosition + new Vector3(txtX, txtY, 0);
textComponent.SetActive(true);
textComponent.transform.SetAsLastSibling();
if(rotateAround)
{
LeanTween.rotateAroundLocal(textComponent.transform.gameObject, Vector3.forward, 30f, 0.3f).setLoopPingPong(-1);
}
else
{
LeanTween.scale(textComponent.gameObject, Vector3.zero, 0.8f).setEase(LeanTweenType.punch).setLoopClamp();
}
}
isRunning = true;
if (waitUntilFinished)
{
Continue();
}
}
//Destroy all instantiated objects
protected IEnumerator StopAnim(float searchNdestroy)
{
var dObjects = Resources.FindObjectsOfTypeAll<Image>().Where(obj => obj.name.Contains("_baloonpop"));
foreach (Image clone in dObjects)
{
if (clone != null)
{
LeanTween.scale(clone.gameObject, new Vector3(-1f, 0f, 0f), 0.2f).setDestroyOnComplete(true);
}
}
if (balloonSprite != null)
{
LeanTween.cancel(balloonSprite.transform.gameObject, true);
balloonSprite.SetActive(false);
}
yield return new WaitForSeconds(searchNdestroy);
if (textComponent != null)
{
LeanTween.cancel(textComponent.transform.gameObject, true);
textComponent.transform.position = textComponent.transform.localPosition;
textComponent.SetActive(false);
}
isRunning = false;
indexNum = 0;
StopAllCoroutines();
if (waitUntilFinished)
{
Continue();
}
}
protected virtual void OnComplete()
{
Continue();
}
#region Public members
public override string GetSummary()
{
string spawnSummary = "";
string textSummary = "";
if (effectType != transiType2.None)
{
if (balloonSprite == null)
{
return "Error: Image can't be empty";
}
if (canvas == null)
{
return "Error: Canvas can't be empty";
}
}
return spawnSummary + textSummary;
}
public override Color GetButtonColor()
{
return new Color32(221, 184, 169, 255);
}
public override void OnEnter()
{
Canvas.ForceUpdateCanvases();
if (effectType == transiType2.None)
{
saveWait();
}
switch (effectType)
{
case (transiType2.FadeIn):
FadeIns();
break;
case (transiType2.Punch):
Punchy();
break;
case (transiType2.None):
break;
}
if (!waitUntilFinished)
{
Continue();
}
}
public override void OnCommandAdded(Block parentBlock)
{
effectType = transiType2.None;
}
#endregion
}
}