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,31 @@
1
+ let started = false;
2
+
3
+ export const keyTracker: Record<string, boolean> = {};
4
+
5
+ function onKeydown(e: KeyboardEvent): void {
6
+ keyTracker[e.code] = true;
7
+ }
8
+
9
+ function onKeyup(e: KeyboardEvent): void {
10
+ keyTracker[e.code] = false;
11
+ }
12
+
13
+ export function startKeyTracker(): void {
14
+ if (!started) {
15
+ document.addEventListener("keydown", onKeydown);
16
+ document.addEventListener("keyup", onKeyup);
17
+
18
+ started = true;
19
+ }
20
+ }
21
+
22
+ export function stopKeyTracker(): void {
23
+ if (started) {
24
+ document.removeEventListener("keydown", onKeydown);
25
+ document.removeEventListener("keyup", onKeyup);
26
+
27
+ started = false;
28
+ }
29
+ }
30
+
31
+ startKeyTracker();
@@ -0,0 +1,523 @@
1
+ import type { Resource } from "../ResourceManager.js";
2
+ import { clamp, noop, pick } from "../utils.js";
3
+
4
+ export interface ChannelOptions {
5
+ muted: boolean;
6
+ volume: number;
7
+ panning: number;
8
+ }
9
+
10
+ export interface SingleChannelPlayOptions {
11
+ loop: boolean;
12
+ callback: () => void;
13
+ playbackRate: number;
14
+ }
15
+
16
+ export interface MultiChannelPlayOptions {
17
+ callback: () => void;
18
+ playbackRate: number;
19
+ }
20
+
21
+ export const canPlay = {
22
+ opus: false,
23
+ ogg: false,
24
+ mp3: false,
25
+ m4a: false,
26
+ wav: false,
27
+ };
28
+
29
+ // Setup Web Audio API
30
+ export const _audioCtx: AudioContext = new AudioContext();
31
+
32
+ // Setup HTML5 Audio
33
+ ((): void => {
34
+ const a = new Audio();
35
+
36
+ if (a.canPlayType('audio/ogg; codecs="opus"') === "probably") {
37
+ canPlay.opus = true;
38
+ }
39
+
40
+ if (a.canPlayType('audio/ogg; codecs="vorbis"') === "probably") {
41
+ canPlay.ogg = true;
42
+ }
43
+
44
+ // When testing for mp3 support, some implementations always return 'maybe' to canPlayType
45
+ if (
46
+ a.canPlayType('audio/mpeg; codecs="mp3"') === "probably" ||
47
+ a.canPlayType('audio/mpeg; codecs="mp3"') === "maybe"
48
+ ) {
49
+ canPlay.mp3 = true;
50
+ }
51
+
52
+ if (
53
+ a.canPlayType('audio/mp4; codecs="mp4a.40.5"') === "probably" ||
54
+ a.canPlayType('audio/mp4; codecs="mp4a.40.2"') === "probably"
55
+ ) {
56
+ canPlay.m4a = true;
57
+ }
58
+
59
+ if (a.canPlayType('audio/wav; codecs="1"') === "probably") {
60
+ canPlay.wav = true;
61
+ }
62
+ })();
63
+
64
+ export class Sound implements Resource {
65
+ soundURL = "";
66
+ streaming = false;
67
+
68
+ _initialized = false;
69
+ _audio: HTMLAudioElement | null = null;
70
+ _source: MediaElementAudioSourceNode | null = null;
71
+ _audioBuffer: AudioBuffer | null = null;
72
+ _waitAudioBuffer = false;
73
+
74
+ constructor(
75
+ soundURLs:
76
+ | string
77
+ | string[]
78
+ | Partial<Record<keyof typeof canPlay, string>>,
79
+ options?: { streaming: boolean },
80
+ ) {
81
+ if (options?.streaming === true) {
82
+ this.streaming = true;
83
+ }
84
+
85
+ // Determine the sound URL
86
+ if (typeof soundURLs === "string") {
87
+ this.soundURL = soundURLs;
88
+ } else if (Array.isArray(soundURLs)) {
89
+ // Check which sound can be played
90
+ for (const sound_url of soundURLs) {
91
+ // Determine the file type by the extension
92
+ const format = sound_url.split(".").at(-1)?.toLowerCase();
93
+ if (canPlay[format as keyof typeof canPlay]) {
94
+ this.soundURL = sound_url;
95
+ break;
96
+ }
97
+ }
98
+ } else {
99
+ // soundURLs is an object literal
100
+ for (const [format, sound_url] of Object.entries(soundURLs)) {
101
+ if (canPlay[format as keyof typeof canPlay]) {
102
+ this.soundURL = sound_url;
103
+ }
104
+ }
105
+ }
106
+ }
107
+
108
+ complete(): boolean {
109
+ let completed = true;
110
+
111
+ if (!this._initialized) {
112
+ this._initialized = true;
113
+
114
+ if (_audioCtx.state !== "running") {
115
+ _audioCtx.resume().catch(noop);
116
+ }
117
+
118
+ // Create the sound or the Audio element
119
+ const sound_url = this.soundURL;
120
+
121
+ if (sound_url !== "") {
122
+ if (!this.streaming) {
123
+ // Sound supported through Web Audio API
124
+ this._waitAudioBuffer = true;
125
+
126
+ fetch(sound_url)
127
+ .then(
128
+ (response: Response): Promise<ArrayBuffer> =>
129
+ response.arrayBuffer(),
130
+ )
131
+ .then(
132
+ (arrayBuffer: ArrayBuffer): Promise<AudioBuffer> =>
133
+ _audioCtx.decodeAudioData(arrayBuffer),
134
+ )
135
+ .then((audioBuffer: AudioBuffer): void => {
136
+ this._audioBuffer = audioBuffer;
137
+ this._waitAudioBuffer = false;
138
+ })
139
+ .catch(noop);
140
+ } else {
141
+ // Sound supported through HTML5 Audio
142
+ const audio = new Audio(sound_url);
143
+ audio.load();
144
+ this._audio = audio;
145
+ }
146
+ }
147
+ }
148
+
149
+ if (this._waitAudioBuffer) {
150
+ completed = false;
151
+ }
152
+
153
+ const audio = this._audio;
154
+
155
+ if (audio !== null && audio.readyState < audio.HAVE_ENOUGH_DATA) {
156
+ completed = false;
157
+ }
158
+
159
+ return completed;
160
+ }
161
+
162
+ onLoad(): void {
163
+ if (this._audio !== null) {
164
+ this._source = new MediaElementAudioSourceNode(_audioCtx, {
165
+ mediaElement: this._audio,
166
+ });
167
+ }
168
+ }
169
+ }
170
+
171
+ class Channel {
172
+ _muted = false;
173
+ _volume = 1;
174
+ _panning = 0;
175
+
176
+ _panner: StereoPannerNode;
177
+ _gainNode: GainNode;
178
+
179
+ get muted(): boolean {
180
+ return this._muted;
181
+ }
182
+
183
+ set muted(value: boolean) {
184
+ if (value !== this._muted) {
185
+ this._muted = value;
186
+
187
+ if (value) {
188
+ this._gainNode.gain.value = 0;
189
+ } else {
190
+ this._gainNode.gain.value = this._volume;
191
+ }
192
+ }
193
+ }
194
+
195
+ get volume(): number {
196
+ return this._volume;
197
+ }
198
+
199
+ set volume(value: number) {
200
+ if (value !== this._volume) {
201
+ this._volume = clamp(value, 0, 1);
202
+
203
+ if (!this._muted) {
204
+ this._gainNode.gain.value = this._volume;
205
+ }
206
+ }
207
+ }
208
+
209
+ get panning(): number {
210
+ return this._panning;
211
+ }
212
+
213
+ set panning(value: number) {
214
+ if (value !== this._panning) {
215
+ this._panning = clamp(value, -1, 1);
216
+
217
+ this._panner.pan.value = this._panning;
218
+ }
219
+ }
220
+
221
+ constructor(options?: Partial<ChannelOptions>) {
222
+ this._panner = new StereoPannerNode(_audioCtx);
223
+ this._panner.connect(_audioCtx.destination);
224
+
225
+ this._gainNode = new GainNode(_audioCtx);
226
+ this._gainNode.connect(this._panner);
227
+
228
+ if (options !== undefined) {
229
+ Object.assign(this, pick(options, ["muted", "volume", "panning"]));
230
+ }
231
+ }
232
+ }
233
+
234
+ export class SingleChannel extends Channel {
235
+ _sound: Sound | null = null;
236
+ _source: AudioNode | null = null;
237
+ _playbackRate = 1;
238
+ _startTime = 0;
239
+ _pauseTime = 0;
240
+ _old_loop = false;
241
+ _old_onended: (() => void) | null = null;
242
+
243
+ // Public functions
244
+
245
+ get sound(): Sound | null {
246
+ return this._sound;
247
+ }
248
+
249
+ get playbackRate(): number {
250
+ return this._playbackRate;
251
+ }
252
+
253
+ set playbackRate(value: number) {
254
+ const oldPlaybackRate = this._playbackRate;
255
+ const playbackRate = clamp(value, 0.25, 4) || 1;
256
+
257
+ if (playbackRate !== oldPlaybackRate) {
258
+ this._playbackRate = playbackRate;
259
+
260
+ const audio = this._sound?._audio ?? null;
261
+ if (audio !== null) {
262
+ audio.playbackRate = playbackRate;
263
+ } else {
264
+ const currentTime = this._pauseTime || _audioCtx.currentTime;
265
+ const offset = (currentTime - this._startTime) * oldPlaybackRate;
266
+
267
+ this._startTime = currentTime - offset / playbackRate;
268
+
269
+ if (this._source instanceof AudioBufferSourceNode) {
270
+ this._source.playbackRate.value = playbackRate;
271
+ }
272
+ }
273
+ }
274
+ }
275
+
276
+ play(sound: Sound, options?: Partial<SingleChannelPlayOptions>): this {
277
+ // Make sure the audio is stopped before changing its options
278
+ this.stop();
279
+
280
+ this._sound = sound;
281
+
282
+ const playbackRate = options?.playbackRate || 0;
283
+ if (playbackRate !== 0) {
284
+ this._playbackRate = clamp(playbackRate, 0.25, 4) || 1;
285
+ } else {
286
+ this._playbackRate = 1;
287
+ }
288
+
289
+ if (sound._audio !== null) {
290
+ const audio = sound._audio;
291
+
292
+ audio.pause();
293
+ audio.currentTime = 0;
294
+
295
+ if (options?.loop === true) {
296
+ audio.loop = true;
297
+ audio.onended = null;
298
+ } else if (options?.callback !== undefined) {
299
+ const callback = options.callback;
300
+
301
+ audio.loop = false;
302
+ audio.onended = (): void => {
303
+ this._disconnect();
304
+
305
+ callback();
306
+ };
307
+ } else {
308
+ audio.loop = false;
309
+ audio.onended = this._disconnect;
310
+ }
311
+
312
+ audio.playbackRate = this._playbackRate;
313
+
314
+ // TODO: Should sound._source be disconnected first?
315
+ this._source = sound._source;
316
+ sound._source?.connect(this._gainNode);
317
+
318
+ audio.play().catch(noop);
319
+ } else if (sound._audioBuffer !== null) {
320
+ const audioBuffer = sound._audioBuffer;
321
+
322
+ const source = new AudioBufferSourceNode(_audioCtx, {
323
+ buffer: audioBuffer,
324
+ });
325
+ this._source = source;
326
+ source.connect(this._gainNode);
327
+
328
+ if (options?.loop === true) {
329
+ source.loop = true;
330
+ source.onended = null;
331
+ } else if (options?.callback !== undefined) {
332
+ const callback = options.callback;
333
+
334
+ source.loop = false;
335
+ source.onended = (): void => {
336
+ this._disconnect();
337
+
338
+ callback();
339
+ };
340
+ } else {
341
+ source.loop = false;
342
+ source.onended = this._disconnect;
343
+ }
344
+
345
+ source.playbackRate.value = this._playbackRate;
346
+
347
+ this._startTime = _audioCtx.currentTime;
348
+ source.start();
349
+ } else {
350
+ // Make sure the callback gets called even if the sound cannot be played
351
+ if (options?.loop !== true && options?.callback !== undefined) {
352
+ const callback = options.callback;
353
+
354
+ callback();
355
+ }
356
+ }
357
+
358
+ return this;
359
+ }
360
+
361
+ stop(): this {
362
+ const audio = this._sound?._audio ?? null;
363
+ if (audio !== null) {
364
+ audio.pause();
365
+ audio.currentTime = 0;
366
+ }
367
+
368
+ this._playbackRate = 1;
369
+ this._pauseTime = 0;
370
+ this._old_loop = false;
371
+ this._old_onended = null;
372
+
373
+ if (this._source instanceof AudioBufferSourceNode) {
374
+ this._source.onended = null;
375
+ this._source.stop();
376
+ }
377
+
378
+ this._disconnect();
379
+
380
+ return this;
381
+ }
382
+
383
+ pause(): this {
384
+ const audio = this._sound?._audio ?? null;
385
+ if (audio !== null) {
386
+ audio.pause();
387
+ }
388
+
389
+ if (
390
+ this._source instanceof AudioBufferSourceNode &&
391
+ this._pauseTime === 0
392
+ ) {
393
+ const source = this._source;
394
+
395
+ // Since pause / resume is not supported in the Web Audio API, here the currentTime is saved,
396
+ // in order to create a new source object when the sound is resumed
397
+ this._old_loop = source.loop;
398
+ this._old_onended = source.onended as () => void;
399
+ source.loop = false;
400
+ source.onended = null;
401
+
402
+ this._pauseTime = _audioCtx.currentTime;
403
+ source.stop();
404
+
405
+ this._source = null;
406
+ }
407
+
408
+ return this;
409
+ }
410
+
411
+ resume(): this {
412
+ const audio = this._sound?._audio ?? null;
413
+ if (audio !== null) {
414
+ audio.play().catch(noop);
415
+ }
416
+
417
+ if (this._pauseTime !== 0) {
418
+ // Since pause / resume is not supported in the Web Audio API, a new source object is created
419
+ // containing all the values of the old source object
420
+ const audioBuffer = this._sound!._audioBuffer!;
421
+
422
+ const source = new AudioBufferSourceNode(_audioCtx, {
423
+ buffer: audioBuffer,
424
+ });
425
+ this._source = source;
426
+ source.connect(this._gainNode);
427
+
428
+ source.loop = this._old_loop;
429
+ source.onended = this._old_onended;
430
+ this._old_loop = false;
431
+ this._old_onended = null;
432
+
433
+ const playbackRate = this._playbackRate;
434
+
435
+ source.playbackRate.value = playbackRate;
436
+
437
+ const offset =
438
+ ((this._pauseTime - this._startTime) * playbackRate) %
439
+ audioBuffer.duration;
440
+
441
+ this._pauseTime = 0;
442
+ this._startTime = _audioCtx.currentTime - offset / playbackRate;
443
+ source.start(0, offset);
444
+ }
445
+
446
+ return this;
447
+ }
448
+
449
+ // Implementation details
450
+
451
+ _disconnect = (): void => {
452
+ const audio = this._sound?._audio ?? null;
453
+ if (audio !== null) {
454
+ audio.loop = false;
455
+ }
456
+
457
+ if (this._source instanceof AudioBufferSourceNode) {
458
+ this._source.loop = false;
459
+ }
460
+
461
+ this._source = null;
462
+
463
+ this._sound = null;
464
+ };
465
+ }
466
+
467
+ export class MultiChannel extends Channel {
468
+ // Public functions
469
+
470
+ play(sound: Sound, options?: Partial<MultiChannelPlayOptions>): this {
471
+ const playbackRate = clamp(options?.playbackRate || 1, 0.25, 4) || 1;
472
+
473
+ if (sound._audio !== null) {
474
+ const audio = sound._audio;
475
+
476
+ audio.pause();
477
+ audio.currentTime = 0;
478
+
479
+ audio.loop = false;
480
+
481
+ if (options?.callback !== undefined) {
482
+ audio.onended = options.callback;
483
+ } else {
484
+ audio.onended = null;
485
+ }
486
+
487
+ audio.playbackRate = playbackRate;
488
+
489
+ // TODO: Should sound._source be disconnected first?
490
+ sound._source?.connect(this._gainNode);
491
+
492
+ audio.play().catch(noop);
493
+ } else if (sound._audioBuffer !== null) {
494
+ const audioBuffer = sound._audioBuffer;
495
+
496
+ const source = new AudioBufferSourceNode(_audioCtx, {
497
+ buffer: audioBuffer,
498
+ });
499
+ source.connect(this._gainNode);
500
+
501
+ source.loop = false;
502
+
503
+ if (options?.callback !== undefined) {
504
+ source.onended = options.callback;
505
+ } else {
506
+ source.onended = null;
507
+ }
508
+
509
+ source.playbackRate.value = playbackRate;
510
+
511
+ source.start();
512
+ } else {
513
+ // Make sure the callback gets called even if the sound cannot be played
514
+ if (options?.callback !== undefined) {
515
+ const callback = options.callback;
516
+
517
+ callback();
518
+ }
519
+ }
520
+
521
+ return this;
522
+ }
523
+ }
@@ -0,0 +1,53 @@
1
+ import type {
2
+ AnimationOptions,
3
+ FrameOptions,
4
+ SpriteSheet,
5
+ } from "../../Animation.js";
6
+ import { Animation } from "../../Animation.js";
7
+ import type { RectSizeX, RectSizeY } from "../../Rect.js";
8
+
9
+ export interface SortedAnimationOptions extends AnimationOptions {
10
+ originx: keyof RectSizeX | number;
11
+ originy: keyof RectSizeY | number;
12
+ }
13
+
14
+ export class SortedAnimation
15
+ extends Animation
16
+ implements SortedAnimationOptions
17
+ {
18
+ originx: keyof RectSizeX | number = "halfWidth";
19
+ originy: keyof RectSizeY | number = "height";
20
+
21
+ constructor(
22
+ options:
23
+ | Partial<SpriteSheet & SortedAnimationOptions & FrameOptions>
24
+ | string,
25
+ ) {
26
+ super(options);
27
+
28
+ if (options !== undefined && typeof options !== "string") {
29
+ if (options.originx !== undefined) {
30
+ this.originx = options.originx;
31
+ }
32
+ if (options.originy !== undefined) {
33
+ this.originy = options.originy;
34
+ }
35
+ }
36
+ }
37
+
38
+ override onLoad(): void {
39
+ super.onLoad();
40
+
41
+ const originx = this.originx;
42
+
43
+ if (typeof originx === "string") {
44
+ this.originx = this[originx];
45
+ }
46
+
47
+ const originy = this.originy;
48
+
49
+ if (typeof originy === "string") {
50
+ this.originy = this[originy];
51
+ }
52
+ }
53
+ }
@@ -0,0 +1,94 @@
1
+ import type { BaseSprite } from "../../BaseSprite.js";
2
+ import type { SpriteGroupOptions } from "../../SpriteGroup.js";
3
+ import { SpriteGroup } from "../../SpriteGroup.js";
4
+ import type { SpriteRef } from "../../utils.js";
5
+ import type { SortedOptions } from "./SortedMixin.js";
6
+ import { Sorted } from "./SortedMixin.js";
7
+
8
+ export interface SortedGroupOptions extends SpriteGroupOptions, SortedOptions {}
9
+
10
+ const SortedBaseGroup = Sorted(SpriteGroup);
11
+
12
+ export class SortedGroup extends SortedBaseGroup {
13
+ _needsSorting = false;
14
+
15
+ constructor(
16
+ options?: Partial<SortedGroupOptions> & { ref?: SpriteRef<SortedGroup> },
17
+ ) {
18
+ super(options);
19
+
20
+ if (options !== undefined) {
21
+ if (options.originx !== undefined) {
22
+ this.originx = options.originx;
23
+ }
24
+ if (options.originy !== undefined) {
25
+ this.originy = options.originy;
26
+ }
27
+ }
28
+
29
+ this._sort_y = this._calcSortY();
30
+ }
31
+
32
+ override addChild<T extends BaseSprite>(
33
+ child: T,
34
+ options?: { suppressWarning?: boolean },
35
+ ): T {
36
+ super.addChild(child, options);
37
+
38
+ this._needsSorting = true;
39
+
40
+ return child;
41
+ }
42
+
43
+ override insertChild<T extends BaseSprite>(
44
+ child: T,
45
+ options?: { suppressWarning?: boolean },
46
+ ): T {
47
+ super.insertChild(child, options);
48
+
49
+ this._needsSorting = true;
50
+
51
+ return child;
52
+ }
53
+
54
+ _sortLayers(): void {
55
+ const layers = this._layers;
56
+ const len_layers = layers.length;
57
+
58
+ if (len_layers > 1) {
59
+ let prev = layers[0] as SortedGroup;
60
+ let prev_y = prev._sort_y;
61
+ for (let i_cur = 1; i_cur < len_layers; i_cur += 1) {
62
+ let cur = layers[i_cur] as SortedGroup;
63
+ let cur_y = cur._sort_y;
64
+
65
+ while (cur_y < prev_y) {
66
+ layers.splice(i_cur, 1);
67
+
68
+ let i_sorted = i_cur - 2;
69
+ for (; i_sorted >= 0; i_sorted -= 1) {
70
+ if (cur_y >= (layers[i_sorted] as SortedGroup)._sort_y) {
71
+ break;
72
+ }
73
+ }
74
+
75
+ layers.splice(i_sorted + 1, 0, cur);
76
+ cur = layers[i_cur] as SortedGroup;
77
+ cur_y = cur._sort_y;
78
+ }
79
+
80
+ prev = cur;
81
+ prev_y = cur_y;
82
+ }
83
+ }
84
+ }
85
+
86
+ override _draw(interp: number): void {
87
+ if (this._needsSorting) {
88
+ this._needsSorting = false;
89
+ this._sortLayers();
90
+ }
91
+
92
+ super._draw(interp);
93
+ }
94
+ }