-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpriteLogic.cs
387 lines (340 loc) · 11.4 KB
/
SpriteLogic.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
using FmodAudio;
using SpriteEditor.Models;
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace SpriteEditor
{
/// <summary>
/// A class that encapsulates sprite updating logic.
/// </summary>
public class SpriteLogic : IDisposable
{
/// <summary>
/// Current frame index.
/// </summary>
private int frameIndex;
/// <summary>
/// Used to check if enough time passed since last frame.
/// </summary>
private long oldTime;
/// <summary>
/// The duration of the current frame
/// </summary>
private int? currentFrameTime;
/// <summary>
/// Number of times animation repeated so far.
/// </summary>
private int repeatNumber;
/// <summary>
/// Total number of frames.
/// </summary>
private int frameCount;
/// <summary>
/// Is the animation in a tween frame?
/// </summary>
private bool tweening;
/// <summary>
/// Last frame where sound was played
/// </summary>
private int lastSoundFrame;
/// <summary>
/// FMOD system
/// </summary>
private readonly FmodSystem? fmodSystem;
/// <summary>
/// Resource disposal status
/// </summary>
private bool isDisposed;
public SpriteLogic(bool soundPlayback)
{
SrcRect = new Rectangle(-1, -1, -1, -1);
lastSoundFrame = -1;
if (!soundPlayback) return;
try
{
fmodSystem = Fmod.CreateSystem();
fmodSystem.Value.Init(10);
}
catch (Exception ex)
{
fmodSystem = null;
if (OperatingSystem.IsWindowsVersionAtLeast(6, 1))
{
MessageBox.Show("Failed to load FMOD sound system - make sure the correct DLL is in the executable folder - " + ex.Message);
}
}
}
public SpriteLogic(SpriteData spriteData, string path, bool soundPlayback) : this(soundPlayback)
{
SpriteData = spriteData;
OpenedFileName = path;
}
~SpriteLogic()
{
Dispose(false);
}
/// <summary>
/// Is the pose finished?
/// </summary>
public bool IsFinished { get; set; }
/// <summary>
/// Data to act on.
/// </summary>
public SpriteData SpriteData { get; set; }
/// <summary>
/// Name of currently opened sprite file
/// </summary>
public string OpenedFileName { get; set; }
/// <summary>
/// Currently active pose.
/// </summary>
public Pose CurrentPose { get; private set; }
/// <summary>
/// Source rectangle of the current frame.
/// </summary>
public Rectangle SrcRect { get; set; }
/// <summary>
/// Get currently active image.
/// </summary>
public string Image
{
get
{
if (!string.IsNullOrEmpty(CurrentFrame?.Image))
{
return CurrentFrame.Image;
}
if (!string.IsNullOrEmpty(CurrentPose?.Image))
{
return CurrentPose.Image;
}
if (!string.IsNullOrEmpty(SpriteData.Image))
{
return SpriteData.Image;
}
return null;
}
}
public string TransparentColorHex => CurrentFrame?.TransparentColor
?? CurrentPose?.TransparentColor
?? SpriteData.TransparentColor;
/// <summary>
/// Current frame in the current pose.
/// </summary>
public Frame CurrentFrame => frameCount == 0 ? null : CurrentPose.Frames[frameIndex];
/// <summary>
/// Reset values to their defaults.
/// </summary>
public void Reset(int currentTime)
{
frameIndex = 0;
oldTime = currentTime;
currentFrameTime = null;
repeatNumber = 0;
frameCount = CurrentPose?.Frames.Count ?? 0;
IsFinished = false;
tweening = false;
lastSoundFrame = -1;
}
/// <summary>
/// Set the current pose.
/// </summary>
/// <param name="poseName">
/// Name of the new pose.
/// </param>
/// <param name="currentTime">
/// Update time.
/// </param>
public void SetPose(string poseName, int currentTime)
{
var poses = SpriteData.Poses;
if (CurrentPose == null ||
!poseName.Equals(CurrentPose.NameWithTags(), StringComparison.InvariantCultureIgnoreCase))
{
var pose =
poses.FirstOrDefault(p => p.NameWithTags()
.Equals(poseName, StringComparison.InvariantCultureIgnoreCase));
if (pose != null)
{
CurrentPose = pose;
}
}
Reset(currentTime);
}
private int GetFrameTime()
{
int frameDuration = CurrentFrame.Duration;
var frameTime = frameDuration == -1 ?
CurrentPose.DefaultDuration : frameDuration;
if (CurrentFrame.MaxDuration.HasValue)
{
frameTime = new Random()
.Next(frameTime, CurrentFrame.MaxDuration.Value);
}
return frameTime;
}
/// <summary>
/// Update the current pose.
/// </summary>
public void Update(int currentTime)
{
fmodSystem?.Update();
if (CurrentPose == null || frameCount == 0)
{
return;
}
// If we number of repeats is reached then animation is finished
if (FinishedRepeating())
{
IsFinished = true;
}
if (IsFinished)
{
return;
}
// If animation is still not finished...
var soundData = CurrentFrame.Sound;
if (fmodSystem != null && !string.IsNullOrEmpty(soundData?.Filename) && lastSoundFrame != frameIndex)
{
try
{
PlaySound(soundData);
}
catch (Exception)
{
// Ignore playback errors (e.g. ogg files aren't supported)
}
lastSoundFrame = frameIndex;
}
if (!currentFrameTime.HasValue)
{
currentFrameTime = GetFrameTime();
}
if (currentTime - oldTime >= currentFrameTime)
{
oldTime = currentTime;
tweening = false;
frameIndex++;
if (frameIndex >= frameCount)
{
repeatNumber++;
lastSoundFrame = -1;
if (FinishedRepeating())
{
frameIndex--;
return;
}
}
frameIndex %= frameCount;
currentFrameTime = null;
}
if (!tweening && CurrentFrame.IsTweenFrame)
{
var prevFrame = CurrentPose.Frames[frameIndex - 1];
CurrentFrame.Rectangle = prevFrame.Rectangle;
oldTime = currentTime;
currentFrameTime = GetFrameTime();
tweening = true;
}
if (tweening)
{
var prevFrame = CurrentPose.Frames[frameIndex - 1];
var nextFrame = CurrentPose.Frames[frameIndex + 1];
var frameTime = currentFrameTime.Value;
float alpha = (float)(currentTime - oldTime) / (frameTime == 0 ? 1 : frameTime);
alpha = Math.Min(Math.Max(alpha, 0.0f), 1.0f);
CurrentFrame.Magnification.X = Lerp(prevFrame.Magnification.X, nextFrame.Magnification.X, alpha);
CurrentFrame.Magnification.Y = Lerp(prevFrame.Magnification.Y, nextFrame.Magnification.Y, alpha);
CurrentFrame.Angle = (int)Lerp(prevFrame.Angle, nextFrame.Angle, alpha);
CurrentFrame.Opacity = Lerp(prevFrame.Opacity, nextFrame.Opacity, alpha);
}
}
public void PlaySound(Models.Sound soundData)
{
if (string.IsNullOrEmpty(soundData.Filename)) return;
var sound = soundData.LoadFmodSound(fmodSystem.Value, ResolvePath(soundData.Filename));
Channel channel = fmodSystem.Value.PlaySound(sound.Value, paused: true);
if (soundData.Pitch.HasValue)
{
channel.Pitch = soundData.Pitch.Value;
}
if (soundData.Volume.HasValue)
{
channel.Volume = soundData.Volume.Value;
}
channel.Paused = false;
}
/// <summary>
/// Set current frame
/// </summary>
/// <param name="index">Frame index</param>
public void SetFrame(int index)
{
frameIndex = index;
}
/// <summary>
/// Resolve a relative base directory to a full path
/// </summary>
public string ResolveBaseDir()
{
var baseDir = SpriteData.BaseDirectory;
if (OpenedFileName != null && !Path.IsPathRooted(baseDir))
{
baseDir = Utilities.ResolveRelativePath(
Path.GetDirectoryName(OpenedFileName), baseDir);
}
return baseDir;
}
/// <summary>
/// Resolve a relative path into a full one
/// </summary>
public string ResolvePath(string filename)
{
var baseDir = SpriteData.BaseDirectory;
if (baseDir == "." || Path.IsPathRooted(filename)) return filename;
try
{
baseDir = ResolveBaseDir();
return Utilities.ResolveRelativePath(baseDir, filename);
}
catch (Exception)
{
return filename;
}
}
/// <summary>
/// Linear interpolation between two floats.
/// @param start Starting value.
/// @param end Ending value.
/// @param alpha Current time.
/// @return Linearly interpolated value at time alpha.
/// </summary>
private static float Lerp(float start, float end, float alpha)
{
return (1.0f - alpha) * start + alpha * end;
}
private bool FinishedRepeating()
{
return CurrentPose.Repeats != -1 && repeatNumber >= CurrentPose.Repeats;
}
protected virtual void Dispose(bool disposing)
{
if (isDisposed) return;
if (disposing)
{
SpriteData?.Dispose();
}
fmodSystem?.Dispose();
isDisposed = true;
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}