frigame3 0.0.0-stage → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE +373 -0
  3. package/README.md +80 -2
  4. package/biome.json +93 -0
  5. package/demo/package-lock.json +515 -0
  6. package/demo/package.json +19 -0
  7. package/demo/snake/index.html +16 -0
  8. package/demo/snake/index.ts +110 -0
  9. package/demo/tetris/index.html +16 -0
  10. package/demo/tetris/index.ts +536 -0
  11. package/demo/tsconfig.json +53 -0
  12. package/frigame.css +12 -0
  13. package/package.json +50 -4
  14. package/src/Animation.ts +266 -0
  15. package/src/BaseSprite.ts +682 -0
  16. package/src/Gradient.ts +120 -0
  17. package/src/Playground.ts +247 -0
  18. package/src/Rect.ts +237 -0
  19. package/src/Rectangle.ts +108 -0
  20. package/src/Renderer.ts +28 -0
  21. package/src/ResourceManager.ts +145 -0
  22. package/src/Sprite.ts +484 -0
  23. package/src/SpriteGroup.ts +258 -0
  24. package/src/canvasRenderer/renderGroup.ts +194 -0
  25. package/src/canvasRenderer/renderPlayground.ts +103 -0
  26. package/src/canvasRenderer/renderRectangle.ts +421 -0
  27. package/src/canvasRenderer/renderSprite.ts +148 -0
  28. package/src/canvasRenderer.ts +38 -0
  29. package/src/dummyRenderer.ts +62 -0
  30. package/src/plugins/GamepadTracker.ts +97 -0
  31. package/src/plugins/Tilemap.ts +123 -0
  32. package/src/plugins/Vec2.ts +420 -0
  33. package/src/plugins/fx/Shaker.ts +293 -0
  34. package/src/plugins/fx/Tweener.ts +252 -0
  35. package/src/plugins/fx/easing.ts +276 -0
  36. package/src/plugins/isometric/ISOMixin.ts +444 -0
  37. package/src/plugins/isometric/ISORectangle.ts +183 -0
  38. package/src/plugins/isometric/ISOSprite.ts +228 -0
  39. package/src/plugins/isometric/ISOSpriteGroup.ts +155 -0
  40. package/src/plugins/isometric/ISOTilemap.ts +122 -0
  41. package/src/plugins/isometric/utils.ts +19 -0
  42. package/src/plugins/keyTracker.ts +31 -0
  43. package/src/plugins/mixer.ts +523 -0
  44. package/src/plugins/sorted/SortedAnimation.ts +53 -0
  45. package/src/plugins/sorted/SortedGroup.ts +94 -0
  46. package/src/plugins/sorted/SortedMixin.ts +203 -0
  47. package/src/plugins/sorted/SortedRectangle.ts +35 -0
  48. package/src/plugins/sorted/SortedSprite.ts +56 -0
  49. package/src/utils.ts +53 -0
  50. package/test/animation/index.html +20 -0
  51. package/test/animation/index.ts +253 -0
  52. package/test/animation/mh.png +0 -0
  53. package/test/animation/moh.png +0 -0
  54. package/test/animation/mov.png +0 -0
  55. package/test/animation/mv.png +0 -0
  56. package/test/animation/rebound.png +0 -0
  57. package/test/animation/reboundm.png +0 -0
  58. package/test/animation/sh.png +0 -0
  59. package/test/animation/soh.png +0 -0
  60. package/test/animation/sov.png +0 -0
  61. package/test/animation/sv.png +0 -0
  62. package/test/isometric1/beach.png +0 -0
  63. package/test/isometric1/foglie.png +0 -0
  64. package/test/isometric1/grass.png +0 -0
  65. package/test/isometric1/index.html +17 -0
  66. package/test/isometric1/index.ts +68 -0
  67. package/test/isometric1/tronco.png +0 -0
  68. package/test/isometric1/untitled.tmx +25 -0
  69. package/test/isometric1/water.png +0 -0
  70. package/test/isometric2/banner.png +0 -0
  71. package/test/isometric2/books.png +0 -0
  72. package/test/isometric2/candlestick.png +0 -0
  73. package/test/isometric2/ice.png +0 -0
  74. package/test/isometric2/index.html +16 -0
  75. package/test/isometric2/index.ts +129 -0
  76. package/test/isometric2/knight_se.png +0 -0
  77. package/test/isometric2/tile.png +0 -0
  78. package/test/isometric2/untitled.tmx +36 -0
  79. package/test/isometric2/wall.png +0 -0
  80. package/test/layers/boy.png +0 -0
  81. package/test/layers/catgirl.png +0 -0
  82. package/test/layers/fri1.svg +8 -0
  83. package/test/layers/grassblock.png +0 -0
  84. package/test/layers/horngirl.png +0 -0
  85. package/test/layers/index.html +17 -0
  86. package/test/layers/index.ts +191 -0
  87. package/test/layers/pinkgirl.png +0 -0
  88. package/test/layers/render_ruler.py +57 -0
  89. package/test/layers/ruler.png +0 -0
  90. package/test/layers/sh.png +0 -0
  91. package/test/mixer/dieci.opus +0 -0
  92. package/test/mixer/dieci.wav +0 -0
  93. package/test/mixer/index.html +33 -0
  94. package/test/mixer/index.ts +206 -0
  95. package/test/mixer/news_intro.mp3 +0 -0
  96. package/test/mixer/news_intro.ogg +0 -0
  97. package/test/mixer/prova.mp3 +0 -0
  98. package/test/mixer/prova.ogg +0 -0
  99. package/test/package-lock.json +515 -0
  100. package/test/package.json +19 -0
  101. package/test/sorted/Brown Block.png +0 -0
  102. package/test/sorted/index.html +16 -0
  103. package/test/sorted/index.ts +38 -0
  104. package/test/transformation1/index.html +17 -0
  105. package/test/transformation1/index.ts +51 -0
  106. package/test/transformation1/sh.png +0 -0
  107. package/test/transformation2/index.html +20 -0
  108. package/test/transformation2/index.ts +71 -0
  109. package/test/transformation2/sh.png +0 -0
  110. package/test/tsconfig.json +53 -0
  111. package/tsconfig.json +51 -0
@@ -0,0 +1,28 @@
1
+ import type { Playground, PlaygroundOptions } from "./Playground.js";
2
+ import type { Rectangle } from "./Rectangle.js";
3
+ import type { Sprite } from "./Sprite.js";
4
+ import type { SpriteGroup } from "./SpriteGroup.js";
5
+
6
+ export interface RendererElement {
7
+ offsetWidth: number;
8
+ offsetHeight: number;
9
+ }
10
+
11
+ export interface Renderer {
12
+ initPlayground(
13
+ playground: Playground,
14
+ options?: Partial<PlaygroundOptions>,
15
+ ): [number, number];
16
+ drawPlaygroundBeforeChildren(playground: Playground, interp: number): void;
17
+ drawPlaygroundAfterChildren(playground: Playground, interp: number): void;
18
+ initGroup(group: SpriteGroup): void;
19
+ removeGroup(group: SpriteGroup): void;
20
+ drawGroupBeforeChildren(group: SpriteGroup, interp: number): void;
21
+ drawGroupAfterChildren(group: SpriteGroup, interp: number): void;
22
+ initSprite(sprite: Sprite): void;
23
+ removeSprite(sprite: Sprite): void;
24
+ drawSprite(sprite: Sprite, interp: number): void;
25
+ initRectangle(rectangle: Rectangle): void;
26
+ removeRectangle(rectangle: Rectangle): void;
27
+ drawRectangle(rectangle: Rectangle, interp: number): void;
28
+ }
@@ -0,0 +1,145 @@
1
+ export interface Resource {
2
+ complete: () => boolean;
3
+ onLoad: () => void;
4
+ }
5
+
6
+ export class ResourceManager {
7
+ loadCallback: ((percent: number) => void) | null = null;
8
+
9
+ // Implementation details
10
+
11
+ _idPreload: number | null = null;
12
+ _preloadList: Resource[] = [];
13
+ _resolveCallback: ((value?: undefined) => void) | null = null;
14
+ _rejectCallback: (() => void) | null = null;
15
+ _complete = true;
16
+
17
+ complete(): boolean {
18
+ return this._complete;
19
+ }
20
+
21
+ percent(): number {
22
+ const len_preload_list = this._preloadList.length;
23
+
24
+ if (len_preload_list === 0) {
25
+ return 1;
26
+ }
27
+
28
+ const completed = this._preloadList.reduce(
29
+ (accumulator: number, resource: Resource): number =>
30
+ accumulator + Number(resource.complete()),
31
+ 0,
32
+ );
33
+
34
+ return completed / len_preload_list;
35
+ }
36
+
37
+ addResource<T extends Resource>(resource: T): T {
38
+ this._preloadList.push(resource);
39
+
40
+ return resource;
41
+ }
42
+
43
+ removeResource(
44
+ resource: Resource | null,
45
+ options?: { suppressWarning?: boolean },
46
+ ): void {
47
+ if (resource === null) {
48
+ if (
49
+ typeof console !== "undefined" &&
50
+ options?.suppressWarning === false
51
+ ) {
52
+ console.warn("resource is null");
53
+ console.trace();
54
+ }
55
+ return;
56
+ }
57
+
58
+ const index = this._preloadList.indexOf(resource);
59
+ if (index >= 0) {
60
+ this._preloadList.splice(index, 1);
61
+ } else {
62
+ if (
63
+ typeof console !== "undefined" &&
64
+ options?.suppressWarning === false
65
+ ) {
66
+ console.warn("No resources removed");
67
+ console.trace();
68
+ }
69
+ }
70
+ }
71
+
72
+ clear(): void {
73
+ this._preloadList.splice(0, this._preloadList.length);
74
+ }
75
+
76
+ preload(): Promise<void> {
77
+ const reject_callback = this._rejectCallback;
78
+
79
+ this._resolveCallback = null;
80
+ this._rejectCallback = null;
81
+
82
+ reject_callback?.();
83
+
84
+ this._complete = false;
85
+
86
+ // Call this._doPreload() now, in order to have the resources initialize
87
+ // inside the function that called resourceManager.preload().
88
+ // This is useful for preloading sounds in mobile environments, for
89
+ // example, where the sounds will not load if audio.load() is not called in
90
+ // an user event handler such as mousedown.
91
+ this._doPreload();
92
+
93
+ return new Promise((resolve: () => void, reject: () => void): void => {
94
+ if (this._complete) {
95
+ resolve();
96
+ } else {
97
+ this._resolveCallback = resolve;
98
+ this._rejectCallback = reject;
99
+
100
+ if (this._idPreload === null) {
101
+ this._idPreload = setInterval(this._doPreload, 100);
102
+ }
103
+ }
104
+ });
105
+ }
106
+
107
+ // Implementation details
108
+
109
+ _doPreload = (): void => {
110
+ const completed = this._preloadList.reduce(
111
+ (accumulator: number, resource: Resource): number =>
112
+ accumulator + Number(resource.complete()),
113
+ 0,
114
+ );
115
+
116
+ const len_preload_list = this._preloadList.length;
117
+ const loadCallback = this.loadCallback;
118
+
119
+ if (completed === len_preload_list) {
120
+ this._complete = true;
121
+
122
+ if (this._idPreload !== null) {
123
+ clearInterval(this._idPreload);
124
+ this._idPreload = null;
125
+ }
126
+
127
+ for (const resource of this._preloadList) {
128
+ resource.onLoad();
129
+ }
130
+ this._preloadList.splice(0, len_preload_list);
131
+
132
+ const resolve_callback = this._resolveCallback;
133
+
134
+ // Set to null this._resolveCallback before calling resolve_callback
135
+ // in order to enable recursion
136
+ this._resolveCallback = null;
137
+ this._rejectCallback = null;
138
+
139
+ loadCallback?.(1);
140
+ resolve_callback?.();
141
+ } else if (len_preload_list !== 0) {
142
+ loadCallback?.(completed / len_preload_list);
143
+ }
144
+ };
145
+ }
package/src/Sprite.ts ADDED
@@ -0,0 +1,484 @@
1
+ import type { Animation, AnimationOptions } from "./Animation.js";
2
+ import type { BaseSpriteOptions } from "./BaseSprite.js";
3
+ import { BaseSprite } from "./BaseSprite.js";
4
+ import type { SpriteRef } from "./utils.js";
5
+ import { pick } from "./utils.js";
6
+
7
+ export interface SpriteOptions extends BaseSpriteOptions, AnimationOptions {
8
+ animation: Animation | null;
9
+ animationIndex: number;
10
+ callback: (() => void) | null;
11
+ paused: boolean;
12
+ }
13
+
14
+ export class Sprite extends BaseSprite implements SpriteOptions {
15
+ _animation: Animation | null = null;
16
+ _animationIndex = 0;
17
+ _callback: (() => void) | null = null;
18
+ _paused = false;
19
+ _rate = 1;
20
+ _reportedRate = 0;
21
+ _once = false;
22
+ _pingpong = false;
23
+ _backwards = false;
24
+
25
+ // Implementation details
26
+
27
+ _idleCounter = 0;
28
+ _lastSpriteSheet = 0;
29
+ _currentSpriteSheet = 0;
30
+ _numberOfFrame = 0;
31
+ _currentFrame = 0;
32
+ _frameIncrement = 1;
33
+ _multix = 0;
34
+ _multiy = 0;
35
+ _endAnimation = false;
36
+
37
+ get animation(): Animation | null {
38
+ return this._animation;
39
+ }
40
+
41
+ set animation(value: Animation | null) {
42
+ if (value !== this._animation) {
43
+ this._animation = value;
44
+
45
+ if (value !== null) {
46
+ const playground = this.playground;
47
+
48
+ if (playground !== undefined) {
49
+ this._rate = playground.framesFromMs(value.rate);
50
+ }
51
+
52
+ this._reportedRate = value.rate;
53
+ this._once = value.once;
54
+ this._pingpong = value.pingpong;
55
+ this._backwards = value.backwards;
56
+
57
+ // Force new width and height based on the animation frame size
58
+ super.width = value.frameWidth;
59
+ super.height = value.frameHeight;
60
+ } else {
61
+ this._rate = 1;
62
+ this._reportedRate = 0;
63
+ this._once = false;
64
+ this._pingpong = false;
65
+ this._backwards = false;
66
+
67
+ super.width = 0;
68
+ super.height = 0;
69
+ }
70
+
71
+ this._animationIndex = 0;
72
+ this.callback = null;
73
+ this.paused = false;
74
+
75
+ this.restartAnimation();
76
+
77
+ this._checkUpdate();
78
+ }
79
+ }
80
+
81
+ get animationIndex(): number {
82
+ return this._animationIndex;
83
+ }
84
+
85
+ set animationIndex(value: number) {
86
+ if (value !== this._animationIndex) {
87
+ this._animationIndex = value;
88
+ this.callback = null;
89
+ this.paused = false;
90
+
91
+ this.restartAnimation();
92
+ }
93
+ }
94
+
95
+ get callback(): (() => void) | null {
96
+ return this._callback;
97
+ }
98
+
99
+ set callback(value: (() => void) | null) {
100
+ this._callback = value;
101
+
102
+ this._checkUpdate();
103
+ }
104
+
105
+ get paused(): boolean {
106
+ return this._paused;
107
+ }
108
+
109
+ set paused(value: boolean) {
110
+ this._paused = value;
111
+
112
+ this._checkUpdate();
113
+ }
114
+
115
+ get rate(): number {
116
+ return this._reportedRate;
117
+ }
118
+
119
+ set rate(value: number) {
120
+ const playground = this.playground;
121
+
122
+ if (playground !== undefined) {
123
+ this._rate = playground.framesFromMs(value);
124
+ }
125
+
126
+ this._reportedRate = value;
127
+ }
128
+
129
+ get once(): boolean {
130
+ return this._once;
131
+ }
132
+
133
+ set once(value: boolean) {
134
+ this._once = value;
135
+ }
136
+
137
+ get pingpong(): boolean {
138
+ return this._pingpong;
139
+ }
140
+
141
+ set pingpong(value: boolean) {
142
+ this._pingpong = value;
143
+ }
144
+
145
+ get backwards(): boolean {
146
+ return this._backwards;
147
+ }
148
+
149
+ set backwards(value: boolean) {
150
+ if (value !== this._backwards) {
151
+ this._backwards = value;
152
+ this._frameIncrement *= -1;
153
+ }
154
+ }
155
+
156
+ constructor(options?: Partial<SpriteOptions> & { ref?: SpriteRef<Sprite> }) {
157
+ super(options);
158
+
159
+ if (options !== undefined) {
160
+ Object.assign(
161
+ this,
162
+ pick(options, [
163
+ "animation",
164
+ "animationIndex",
165
+ "callback",
166
+ "paused",
167
+ "rate",
168
+ "once",
169
+ "pingpong",
170
+ "backwards",
171
+ ]),
172
+ );
173
+
174
+ if (options.ref !== undefined) {
175
+ options.ref.current = this;
176
+ }
177
+ }
178
+
179
+ this.restartAnimation();
180
+
181
+ this.teleport();
182
+ }
183
+
184
+ // Public functions
185
+
186
+ restartAnimation(): this {
187
+ const animation = this._animation;
188
+
189
+ if (animation !== null && this._backwards) {
190
+ this._lastSpriteSheet = animation.frameset.length - 1;
191
+ this._currentSpriteSheet = this._lastSpriteSheet;
192
+ this._numberOfFrame =
193
+ animation.frameset[this._currentSpriteSheet]!.numberOfFrame;
194
+ this._currentFrame = this._numberOfFrame - 1;
195
+ this._frameIncrement = -1;
196
+ } else {
197
+ this._currentSpriteSheet = 0;
198
+ if (animation !== null) {
199
+ this._lastSpriteSheet = animation.frameset.length - 1;
200
+ this._numberOfFrame = animation.frameset[0]!.numberOfFrame;
201
+ } else {
202
+ this._lastSpriteSheet = 0;
203
+ this._numberOfFrame = 0;
204
+ }
205
+ this._currentFrame = 0;
206
+ this._frameIncrement = 1;
207
+ }
208
+
209
+ this._idleCounter = 0;
210
+ this._endAnimation = false;
211
+
212
+ const index = this._animationIndex;
213
+
214
+ if (animation !== null && index !== 0) {
215
+ const sprite_sheet = animation.frameset[this._currentSpriteSheet]!;
216
+ this._multix = index * sprite_sheet._multix;
217
+ this._multiy = index * sprite_sheet._multiy;
218
+ } else {
219
+ this._multix = 0;
220
+ this._multiy = 0;
221
+ }
222
+
223
+ return this;
224
+ }
225
+
226
+ // Implementation details
227
+
228
+ override _checkUpdate(): void {
229
+ const oldNeedsUpdate = this._needsUpdate;
230
+
231
+ if (
232
+ this._endAnimation ||
233
+ this._paused ||
234
+ (this._callback === null &&
235
+ (this._animation === null ||
236
+ (this._lastSpriteSheet <= 0 && this._numberOfFrame <= 1)))
237
+ ) {
238
+ this._needsUpdate = false;
239
+ } else {
240
+ this._needsUpdate = true;
241
+ }
242
+
243
+ this._updateNeedsUpdate(oldNeedsUpdate);
244
+ }
245
+
246
+ override _onReparent(): void {
247
+ const playground = this.playground;
248
+
249
+ if (playground !== undefined) {
250
+ this._rate = playground.framesFromMs(this._reportedRate);
251
+ }
252
+ }
253
+
254
+ override _initRenderer(): void {
255
+ this.playground?._renderer.initSprite(this);
256
+ }
257
+
258
+ override _update(): void {
259
+ const callback = this._callback;
260
+ const animation = this._animation;
261
+
262
+ let currentSpriteSheet = this._currentSpriteSheet;
263
+ let currentFrame = this._currentFrame;
264
+
265
+ if (!(this._endAnimation || this._paused)) {
266
+ if (animation !== null) {
267
+ this._idleCounter += 1;
268
+ if (this._idleCounter >= this._rate) {
269
+ this._idleCounter = 0;
270
+ currentFrame += this._frameIncrement;
271
+ if (this._backwards) {
272
+ // Backwards animations
273
+ if (this._pingpong) {
274
+ // In pingpong animations the end is when the frame returns to the last frame
275
+ if (currentFrame >= this._numberOfFrame) {
276
+ if (currentSpriteSheet < this._lastSpriteSheet) {
277
+ currentSpriteSheet += 1;
278
+ this._currentSpriteSheet = currentSpriteSheet;
279
+ this._numberOfFrame =
280
+ animation.frameset[currentSpriteSheet]!.numberOfFrame;
281
+ this._currentFrame = 0;
282
+ } else {
283
+ this._frameIncrement = -1;
284
+ if (this._once) {
285
+ currentFrame -= 1;
286
+ this._idleCounter = 1;
287
+ this._endAnimation = true;
288
+ } else {
289
+ // The first frame has already been displayed, start from the second
290
+ if (this._numberOfFrame > 1) {
291
+ currentFrame -= 2;
292
+ } else if (this._lastSpriteSheet > 0) {
293
+ currentSpriteSheet -= 1;
294
+ this._currentSpriteSheet = currentSpriteSheet;
295
+ this._numberOfFrame =
296
+ animation.frameset[currentSpriteSheet]!.numberOfFrame;
297
+ currentFrame = this._numberOfFrame - 1;
298
+ } else {
299
+ currentFrame -= 1;
300
+ }
301
+ }
302
+
303
+ // Update the details before the callback
304
+ this._currentFrame = currentFrame;
305
+
306
+ callback?.();
307
+ }
308
+ } else if (currentFrame < 0) {
309
+ if (currentSpriteSheet > 0) {
310
+ currentSpriteSheet -= 1;
311
+ this._currentSpriteSheet = currentSpriteSheet;
312
+ this._numberOfFrame =
313
+ animation.frameset[currentSpriteSheet]!.numberOfFrame;
314
+ this._currentFrame = this._numberOfFrame - 1;
315
+ } else {
316
+ // Last frame reached, change animation direction
317
+ this._frameIncrement = 1;
318
+ // The first frame has already been displayed, start from the second
319
+ if (this._numberOfFrame > 1) {
320
+ currentFrame = 1;
321
+ } else if (this._lastSpriteSheet > 0) {
322
+ currentSpriteSheet += 1;
323
+ this._currentSpriteSheet = currentSpriteSheet;
324
+ this._numberOfFrame =
325
+ animation.frameset[currentSpriteSheet]!.numberOfFrame;
326
+ currentFrame = 0;
327
+ } else {
328
+ currentFrame = 0;
329
+ }
330
+ this._currentFrame = currentFrame;
331
+ }
332
+ } else {
333
+ // This is no particular frame, simply update the details
334
+ this._currentFrame = currentFrame;
335
+ }
336
+ } else {
337
+ // Normal animation
338
+ if (currentFrame < 0) {
339
+ if (currentSpriteSheet > 0) {
340
+ currentSpriteSheet -= 1;
341
+ this._currentSpriteSheet = currentSpriteSheet;
342
+ this._numberOfFrame =
343
+ animation.frameset[currentSpriteSheet]!.numberOfFrame;
344
+ this._currentFrame = this._numberOfFrame - 1;
345
+ } else {
346
+ // Last frame reached
347
+ if (this._once) {
348
+ currentFrame = 0;
349
+ this._idleCounter = 1;
350
+ this._endAnimation = true;
351
+ } else {
352
+ currentSpriteSheet = this._lastSpriteSheet;
353
+ this._currentSpriteSheet = currentSpriteSheet;
354
+ this._numberOfFrame =
355
+ animation.frameset[currentSpriteSheet]!.numberOfFrame;
356
+ currentFrame = this._numberOfFrame - 1;
357
+ }
358
+
359
+ // Update the details before the callback
360
+ this._currentFrame = currentFrame;
361
+
362
+ callback?.();
363
+ }
364
+ } else {
365
+ // This is no particular frame, simply update the details
366
+ this._currentFrame = currentFrame;
367
+ }
368
+ }
369
+ } else {
370
+ // Forwards animations
371
+ if (this._pingpong) {
372
+ // In pingpong animations the end is when the frame goes below 0
373
+ if (currentFrame < 0) {
374
+ if (currentSpriteSheet > 0) {
375
+ currentSpriteSheet -= 1;
376
+ this._currentSpriteSheet = currentSpriteSheet;
377
+ this._numberOfFrame =
378
+ animation.frameset[currentSpriteSheet]!.numberOfFrame;
379
+ this._currentFrame = this._numberOfFrame - 1;
380
+ } else {
381
+ this._frameIncrement = 1;
382
+ if (this._once) {
383
+ currentFrame = 0;
384
+ this._idleCounter = 1;
385
+ this._endAnimation = true;
386
+ } else {
387
+ // The first frame has already been displayed, start from the second
388
+ if (this._numberOfFrame > 1) {
389
+ currentFrame = 1;
390
+ } else if (this._lastSpriteSheet > 0) {
391
+ currentSpriteSheet += 1;
392
+ this._currentSpriteSheet = currentSpriteSheet;
393
+ this._numberOfFrame =
394
+ animation.frameset[currentSpriteSheet]!.numberOfFrame;
395
+ currentFrame = 0;
396
+ } else {
397
+ currentFrame = 0;
398
+ }
399
+ }
400
+
401
+ // Update the details before the callback
402
+ this._currentFrame = currentFrame;
403
+
404
+ callback?.();
405
+ }
406
+ } else if (currentFrame >= this._numberOfFrame) {
407
+ if (currentSpriteSheet < this._lastSpriteSheet) {
408
+ currentSpriteSheet += 1;
409
+ this._currentSpriteSheet = currentSpriteSheet;
410
+ this._numberOfFrame =
411
+ animation.frameset[currentSpriteSheet]!.numberOfFrame;
412
+ this._currentFrame = 0;
413
+ } else {
414
+ // Last frame reached, change animation direction
415
+ this._frameIncrement = -1;
416
+ if (this._numberOfFrame > 1) {
417
+ currentFrame -= 2;
418
+ } else if (this._lastSpriteSheet > 0) {
419
+ currentSpriteSheet -= 1;
420
+ this._currentSpriteSheet = currentSpriteSheet;
421
+ this._numberOfFrame =
422
+ animation.frameset[currentSpriteSheet]!.numberOfFrame;
423
+ currentFrame = this._numberOfFrame - 1;
424
+ } else {
425
+ currentFrame -= 1;
426
+ }
427
+ this._currentFrame = currentFrame;
428
+ }
429
+ } else {
430
+ // This is no particular frame, simply update the details
431
+ this._currentFrame = currentFrame;
432
+ }
433
+ } else {
434
+ // Normal animation
435
+ if (currentFrame >= this._numberOfFrame) {
436
+ if (currentSpriteSheet < this._lastSpriteSheet) {
437
+ currentSpriteSheet += 1;
438
+ this._currentSpriteSheet = currentSpriteSheet;
439
+ this._numberOfFrame =
440
+ animation.frameset[currentSpriteSheet]!.numberOfFrame;
441
+ this._currentFrame = 0;
442
+ } else {
443
+ // Last frame reached
444
+ if (this._once) {
445
+ currentFrame -= 1;
446
+ this._idleCounter = 1;
447
+ this._endAnimation = true;
448
+ } else {
449
+ currentSpriteSheet = 0;
450
+ this._currentSpriteSheet = currentSpriteSheet;
451
+ this._numberOfFrame =
452
+ animation.frameset[currentSpriteSheet]!.numberOfFrame;
453
+ currentFrame = 0;
454
+ }
455
+
456
+ // Update the details before the callback
457
+ this._currentFrame = currentFrame;
458
+
459
+ callback?.();
460
+ }
461
+ } else {
462
+ // This is no particular frame, simply update the details
463
+ this._currentFrame = currentFrame;
464
+ }
465
+ }
466
+ }
467
+ }
468
+ } else {
469
+ // Make sure that the callback is called even if there is no animation
470
+ callback?.();
471
+ }
472
+ }
473
+ }
474
+
475
+ override _draw(interp: number): void {
476
+ super._draw(interp);
477
+
478
+ this.playground?._renderer.drawSprite(this, interp);
479
+ }
480
+
481
+ override _remove(): void {
482
+ this.playground?._renderer.removeSprite(this);
483
+ }
484
+ }