-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClickableCharacter.cs
394 lines (353 loc) · 15.3 KB
/
ClickableCharacter.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// 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.Events;
using System;
using System.Linq;
namespace Fungus
{
public enum clicChar2
{
Enable,
Disable
}
public enum InvokeTypeClick
{
/// <summary> Call a method with an optional constant value parameter. </summary>
Static,
/// <summary> Call a method with an optional boolean constant / variable parameter. </summary>
DynamicBoolean,
/// <summary> Call a method with an optional integer constant / variable parameter. </summary>
DynamicInteger,
/// <summary> Call a method with an optional float constant / variable parameter. </summary>
DynamicFloat,
/// <summary> Call a method with an optional string constant / variable parameter. </summary>
DynamicString
}
public enum InvokeTypeClick2
{
/// <summary> Call a method with an optional constant value parameter. </summary>
Static,
/// <summary> Call a method with an optional boolean constant / variable parameter. </summary>
DynamicBoolean,
/// <summary> Call a method with an optional integer constant / variable parameter. </summary>
DynamicInteger,
/// <summary> Call a method with an optional float constant / variable parameter. </summary>
DynamicFloat,
/// <summary> Call a method with an optional string constant / variable parameter. </summary>
DynamicString
}
/// <summary>
/// Tween sequence
/// </summary>
[CommandInfo("Sprite",
"Clickable Character",
"Adds ability for character to be clickable with the use of BoxCollider and Raycast. This won'r follow character's movements nor animations. To update it's position you must create new one. Note: Offset: 0 : 0 means center. Make sure these requirements met: Camera has a Physics Raycaster, EventSystem, Verify no collider (possibly without a mesh) is obscuring the selectable game object")]
[AddComponentMenu("")]
[ExecuteInEditMode]
public class ClickableCharacter : Command
{
[Tooltip("Enable")]
[SerializeField] public clicChar2 status;
[SerializeField] protected Character character;
[SerializeField] protected Camera mainCam;
[SerializeField] protected Vector2 hitBoxSize = new Vector2(13f, 28f);
[SerializeField] protected Vector2 offsets = new Vector2(0, 0);
[SerializeField] protected bool enableDebugLog = false;
[Tooltip("A description of what this command does. Appears in the command summary.")]
[SerializeField] protected string description = "";
[Tooltip("Selects type of method parameter to pass")]
[SerializeField] protected InvokeTypeClick invokeType;
[Tooltip("Delay (in seconds) before the methods will be called")]
[SerializeField] protected float delay;
[Tooltip("List of methods to call. Supports methods with no parameters or exactly one string, int, float or object parameter.")]
[SerializeField] protected UnityEvent staticEvent = new UnityEvent();
[Tooltip("Boolean parameter to pass to the invoked methods.")]
[SerializeField] protected BooleanData booleanParameter;
[Tooltip("List of methods to call. Supports methods with one boolean parameter.")]
[SerializeField] protected BooleanEvent booleanEvent = new BooleanEvent();
[Tooltip("Integer parameter to pass to the invoked methods.")]
[SerializeField] protected IntegerData integerParameter;
[Tooltip("List of methods to call. Supports methods with one integer parameter.")]
[SerializeField] protected IntegerEvent integerEvent = new IntegerEvent();
[Tooltip("Float parameter to pass to the invoked methods.")]
[SerializeField] protected FloatData floatParameter;
[Tooltip("List of methods to call. Supports methods with one float parameter.")]
[SerializeField] protected FloatEvent floatEvent = new FloatEvent();
[Tooltip("String parameter to pass to the invoked methods.")]
[SerializeField] protected StringDataMulti stringParameter;
[Tooltip("List of methods to call. Supports methods with one string parameter.")]
[SerializeField] protected StringEvent stringEvent = new StringEvent();
[Tooltip("Selects type of method parameter to pass")]
[SerializeField] protected InvokeTypeClick2 invokeType2;
[Tooltip("Delay (in seconds) before the methods will be called")]
[SerializeField] protected float delay2;
[Tooltip("List of methods to call. Supports methods with no parameters or exactly one string, int, float or object parameter.")]
[SerializeField] protected UnityEvent staticEvent2 = new UnityEvent();
[Tooltip("Boolean parameter to pass to the invoked methods.")]
[SerializeField] protected BooleanData booleanParameter2;
[Tooltip("List of methods to call. Supports methods with one boolean parameter.")]
[SerializeField] protected BooleanEvent booleanEvent2 = new BooleanEvent();
[Tooltip("Integer parameter to pass to the invoked methods.")]
[SerializeField] protected IntegerData integerParameter2;
[Tooltip("List of methods to call. Supports methods with one integer parameter.")]
[SerializeField] protected IntegerEvent integerEvent2 = new IntegerEvent();
[Tooltip("Float parameter to pass to the invoked methods.")]
[SerializeField] protected FloatData floatParameter2;
[Tooltip("List of methods to call. Supports methods with one float parameter.")]
[SerializeField] protected FloatEvent floatEvent2 = new FloatEvent();
[Tooltip("String parameter to pass to the invoked methods.")]
[SerializeField] protected StringDataMulti stringParameter2;
[Tooltip("List of methods to call. Supports methods with one string parameter.")]
[SerializeField] protected StringEvent stringEvent2 = new StringEvent();
protected string myGoGo = "";
protected Stage stage;
protected virtual void DoInvoke()
{
switch (invokeType)
{
default:
case InvokeTypeClick.Static:
staticEvent.Invoke();
break;
case InvokeTypeClick.DynamicBoolean:
booleanEvent.Invoke(booleanParameter.Value);
break;
case InvokeTypeClick.DynamicInteger:
integerEvent.Invoke(integerParameter.Value);
break;
case InvokeTypeClick.DynamicFloat:
floatEvent.Invoke(floatParameter.Value);
break;
case InvokeTypeClick.DynamicString:
stringEvent.Invoke(stringParameter.Value);
break;
}
}
protected string cacheChar;
protected bool actives = false;
#region Public members
[Serializable] public class BooleanEvent : UnityEvent<bool> {}
[Serializable] public class IntegerEvent : UnityEvent<int> {}
[Serializable] public class FloatEvent : UnityEvent<float> {}
[Serializable] public class StringEvent : UnityEvent<string> {}
void Update()
{
if(actives)
{
if(character.State.portraitImage != null)
{
if(character != null && mainCam != null)
{
if(character.State.portraitImage.name != cacheChar)
{
cacheChar = character.State.portraitImage.name;
}
if ( Input.GetMouseButtonDown (0))
{
CastRay();
}
}
}
else
{
Debug.Log("Character's portrait has not been spawned yet! Make sure it exist before this command");
}
}
}
protected void CastRay()
{
Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);
if (hit)
{
if(hit.collider.gameObject.name == myGoGo)
{
if(enableDebugLog)
{
Debug.Log ("This character was clicked : " + hit.collider.gameObject.name);
}
//Invoke starts here!
if (Mathf.Approximately(delay, 0f))
{
this.DoInvoke();
}
else
{
this.Invoke("DoInvoke", delay);
}
}
}
else
{
if(enableDebugLog)
{
Debug.Log("Clicked outside character : " + character.name);
}
}
}
protected void DisableClickableCharacter()
{
if (character != null)
{
if(!actives)
{
for (int i = character.transform.childCount - 1; i >= 0; i--)
{
GameObject.Destroy(character.transform.GetChild(i).gameObject);
}
actives = false;
}
}
else
{
return;
}
}
//This kinda problematic if user spammed clicks! Disabled for now
/*
protected void UpdateColliderPosition()
{
//Get the actuall postion on screen
//Ray ray = mainCam.ScreenPointToRay(new Vector3(character.State.portraitImage.transform.position.x, character.State.portraitImage.transform.position.y));
//RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);
//Vector2 vv = ray.origin;
//var b = character.gameObject.GetComponentInChildren<BoxCollider2D>();
//b.offset = vv;
}
*/
private static int indexNum = 0;
protected void AddCollider()
{
if(character != null && mainCam != null)
{
BoxCollider2D boxy;
float rectX = character.State.portraitImage.rectTransform.sizeDelta.x;
float rectY = character.State.portraitImage.rectTransform.sizeDelta.y;
GameObject myGO = new GameObject(character.name + "_" + indexNum++);
myGoGo = myGO.name;
myGO.AddComponent<BoxCollider2D>();
boxy = myGO.GetComponent<BoxCollider2D>();
boxy.transform.SetParent(character.transform, false);
boxy.size = hitBoxSize;
boxy.offset = offsets;
}
}
protected void UpdateStage()
{
if (stage == null)
{
// If no default specified, try to get any portrait stage in the scene
stage = FindObjectOfType<Stage>();
// If portrait stage does not exist, do nothing
if (stage == null)
{
Continue();
return;
}
}
}
public override string GetSummary()
{
string noCol = "";
string noCam = "";
if(status == clicChar2.Enable)
{
if (character == null)
{
noCol = "Error: No Character is selected";
}
if (mainCam == null)
{
noCam = "Error: No active camera is selected";
}
}
if(status == clicChar2.Disable)
{
if (character == null)
{
noCol = "Error: No Character selected to be disabled";
}
}
return noCol + " : " + noCam;
#pragma warning disable CS0162
if (!string.IsNullOrEmpty(description))
{
return description;
}
string summary = invokeType.ToString() + " ";
string summary2 = invokeType2.ToString() + " ";
switch (invokeType)
{
default:
case InvokeTypeClick.Static:
summary += staticEvent.GetPersistentEventCount();
break;
case InvokeTypeClick.DynamicBoolean:
summary += booleanEvent.GetPersistentEventCount();
break;
case InvokeTypeClick.DynamicInteger:
summary += integerEvent.GetPersistentEventCount();
break;
case InvokeTypeClick.DynamicFloat:
summary += floatEvent.GetPersistentEventCount();
break;
case InvokeTypeClick.DynamicString:
summary += stringEvent.GetPersistentEventCount();
break;
}
switch (invokeType2)
{
default:
case InvokeTypeClick2.Static:
summary2 += staticEvent2.GetPersistentEventCount();
break;
case InvokeTypeClick2.DynamicBoolean:
summary2 += booleanEvent2.GetPersistentEventCount();
break;
case InvokeTypeClick2.DynamicInteger:
summary2 += integerEvent2.GetPersistentEventCount();
break;
case InvokeTypeClick2.DynamicFloat:
summary2 += floatEvent2.GetPersistentEventCount();
break;
case InvokeTypeClick2.DynamicString:
summary2 += stringEvent2.GetPersistentEventCount();
break;
}
return summary + summary2 + " methods";
}
public override void OnEnter()
{
//Force 1st frame update
Canvas.ForceUpdateCanvases();
UpdateStage();
switch (status)
{
case (clicChar2.Disable):
DisableClickableCharacter();
break;
case (clicChar2.Enable):
actives = true;
AddCollider();
break;
}
Continue();
}
public override Color GetButtonColor()
{
return new Color32(221, 184, 169, 255);
}
public override bool HasReference(Variable variable)
{
return booleanParameter.booleanRef == variable || integerParameter.integerRef == variable ||
floatParameter.floatRef == variable || stringParameter.stringRef == variable ||
base.HasReference(variable);
}
public override void OnCommandAdded(Block parentBlock)
{
status = clicChar2.Disable;
}
#endregion
}
}