-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPortraitAnim.cs
238 lines (225 loc) · 9.22 KB
/
PortraitAnim.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
// 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 System.Collections;
using System;
using Random = UnityEngine.Random;
public enum actPorAnim
{
Enable,
Disable
}
namespace Fungus
{
/// <summary>
/// Controls a character portrait.
/// </summary>
[CommandInfo("Narrative",
"PortraitAnim",
"Character frame-by-frame animation using portrait lists. Cycle = Stopping the animation based on how many loops. IMPORTANT! Create separate Stage just for this custom comamand and Do not use Dim or any other fancy settings in Stage")]
public class PortraitAnim : ControlWithDisplay<DisplayType>
{
[SerializeField] public actPorAnim enableAnimation;
public Stage stage;
[Tooltip("Character to display")]
[SerializeField] protected Character character;
[Tooltip("Portrait to display")]
[SerializeField] protected Sprite portrait1;
[Tooltip("Portrait to display")]
[SerializeField] protected Sprite portrait2;
[Tooltip("Portrait to display")]
[SerializeField] protected Sprite portrait3;
[Tooltip("Portrait to display")]
[SerializeField] protected Sprite portrait4;
[Tooltip("Portrait to display")]
[SerializeField] protected Sprite portrait5;
[Tooltip("Delay between sequence")]
[SerializeField] protected float frameDelay = 0.1f;
[Tooltip("Reverse the loop after completion of the 1st cycle")]
[SerializeField] protected bool reverseLoop = false;
[Tooltip("Defines how many times it would be animated before get disabled automatically")]
[SerializeField] protected bool useCyclesRange = false;
[Tooltip("How many cycle animation before gets stopped automatically")]
[SerializeField] protected int cycles = 2;
[Tooltip("Random delay based on predefined values, 4 - 12 seconds")]
[SerializeField] protected bool RandomEndDelay = false;
[SerializeField] protected float endFrameDelay = 3f;
#region Public members
/// <summary>
/// Stage to display portrait on.
/// </summary>
public virtual Stage _Stage { get { return stage; } set { stage = value; } }
/// <summary>
/// Character to display.
/// </summary>
public virtual Character _Character { get { return character; } set { character = value; } }
/// <summary>
/// Portrait to display.
/// </summary>
public virtual Sprite _Portrait1 { get { return portrait1; } set { portrait1 = value; } }
public virtual Sprite _Portrait2 { get { return portrait2; } set { portrait2 = value; } }
public virtual Sprite _Portrait3 { get { return portrait3; } set { portrait3 = value; } }
public virtual Sprite _Portrait4 { get { return portrait4; } set { portrait4 = value; } }
public virtual Sprite _Portrait5 { get { return portrait5; } set { portrait5 = value; } }
public bool isAnimating = false;
private IEnumerator coroutine;
protected void sequenceMove()
{
coroutine = charAnim(0.2f);
StartCoroutine(coroutine);
}
public override void OnEnter()
{
display = DisplayType.Show;
if (stage == null)
{
stage = Stage.GetActiveStage();
if (stage == null)
{
Continue();
return;
}
}
if (enableAnimation == actPorAnim.Enable)
{
Stage disdim = Stage.GetActiveStage();
disdim.GetComponent<Stage>().DimPortraits = false;
disdim.GetComponent<Stage>().FadeDuration = 0;
sequenceMove();
}
if (enableAnimation == actPorAnim.Disable)
{
PortraitAnim portan = GetComponent<PortraitAnim>();
portan.disablePortraitAnim(false);
}
}
public virtual void disablePortraitAnim(bool anstate)
{
this.isAnimating = false;
this.StopAllCoroutines();
Continue();
}
private static int amCycle = 0;
public virtual IEnumerator charAnim(float delay)
{
if (portrait1 && portrait2 && portrait3 && portrait4 && portrait5 != null)
{
if (enableAnimation == actPorAnim.Enable && character != null)
{
isAnimating = true;
PortraitOptions options = new PortraitOptions();
var por1 = new Action(() =>
{
options.character = character;
options.portrait = portrait1;
options.display = display;
stage.RunPortraitCommand(options, Continue);
});
var por2 = new Action(() =>
{
options.character = character;
options.portrait = portrait2;
options.display = display;
stage.RunPortraitCommand(options, null);
});
var por3 = new Action(() =>
{
options.character = character;
options.portrait = portrait3;
options.display = display;
stage.RunPortraitCommand(options, null);
});
var por4 = new Action(() =>
{
options.character = character;
options.portrait = portrait4;
options.display = display;
stage.RunPortraitCommand(options, null);
});
var por5 = new Action(() =>
{
options.character = character;
options.portrait = portrait5;
options.display = display;
stage.RunPortraitCommand(options, null);
});
while (isAnimating)
{
por1();
yield return new WaitForSeconds(frameDelay);
por2();
yield return new WaitForSeconds(frameDelay);
por3();
yield return new WaitForSeconds(frameDelay);
por4();
yield return new WaitForSeconds(frameDelay);
por5();
if(reverseLoop)
{
por5();
yield return new WaitForSeconds(frameDelay);
por4();
yield return new WaitForSeconds(frameDelay);
por3();
yield return new WaitForSeconds(frameDelay);
por2();
yield return new WaitForSeconds(frameDelay);
por1();
}
amCycle++;
//Debug.Log("amcycle:"+ amCycle);
if (RandomEndDelay)
{
yield return new WaitForSeconds(Random.Range(4f, 12f));
}
else
{
yield return new WaitForSeconds(endFrameDelay);
}
if (useCyclesRange)
{
if (amCycle == cycles)
{
this.disablePortraitAnim(false);
}
}
}
}
else
{
Continue();
}
}
else
{
Continue();
}
}
public override string GetSummary()
{
if (character == null)
{
return "Error: No character selected";
}
string characterSummary = "";
string portraitSummary = "";
string stageSummary = "";
characterSummary = character.name;
if (enableAnimation == actPorAnim.Enable && portrait1 && portrait2 && portrait3 && portrait4 && portrait5 == null)
{
return "Error: One of portrait slots cannot be empty";
}
return characterSummary + ":" + portraitSummary + ":" + stageSummary;
}
public override Color GetButtonColor()
{
return new Color32(230, 200, 250, 255);
}
public override void OnCommandAdded(Block parentBlock)
{
display = DisplayType.Show;
enableAnimation = actPorAnim.Disable;
}
#endregion
}
}