frigame3 3.0.0 → 3.0.1
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.
- package/dist/Animation.d.ts +42 -0
- package/dist/Animation.js +189 -0
- package/dist/BaseSprite.d.ts +116 -0
- package/dist/BaseSprite.js +518 -0
- package/dist/Gradient.d.ts +31 -0
- package/dist/Gradient.js +89 -0
- package/dist/Playground.d.ts +48 -0
- package/dist/Playground.js +167 -0
- package/dist/Rect.d.ts +63 -0
- package/dist/Rect.js +155 -0
- package/dist/Rectangle.d.ts +31 -0
- package/dist/Rectangle.js +69 -0
- package/dist/Renderer.d.ts +23 -0
- package/dist/Renderer.js +1 -0
- package/dist/ResourceManager.d.ts +21 -0
- package/dist/ResourceManager.js +100 -0
- package/dist/Sprite.d.ts +56 -0
- package/dist/Sprite.js +437 -0
- package/dist/SpriteGroup.d.ts +39 -0
- package/dist/SpriteGroup.js +176 -0
- package/dist/canvasRenderer/renderGroup.d.ts +15 -0
- package/dist/canvasRenderer/renderGroup.js +134 -0
- package/dist/canvasRenderer/renderPlayground.d.ts +9 -0
- package/dist/canvasRenderer/renderPlayground.js +67 -0
- package/dist/canvasRenderer/renderRectangle.d.ts +15 -0
- package/dist/canvasRenderer/renderRectangle.js +287 -0
- package/dist/canvasRenderer/renderSprite.d.ts +4 -0
- package/dist/canvasRenderer/renderSprite.js +93 -0
- package/dist/canvasRenderer.d.ts +2 -0
- package/dist/canvasRenderer.js +19 -0
- package/dist/dummyRenderer.d.ts +2 -0
- package/dist/dummyRenderer.js +41 -0
- package/dist/plugins/GamepadTracker.d.ts +15 -0
- package/dist/plugins/GamepadTracker.js +74 -0
- package/dist/plugins/Tilemap.d.ts +26 -0
- package/dist/plugins/Tilemap.js +85 -0
- package/dist/plugins/Vec2.d.ts +69 -0
- package/dist/plugins/Vec2.js +296 -0
- package/dist/plugins/fx/Shaker.d.ts +46 -0
- package/dist/plugins/fx/Shaker.js +190 -0
- package/dist/plugins/fx/Tweener.d.ts +42 -0
- package/dist/plugins/fx/Tweener.js +166 -0
- package/dist/plugins/fx/easing.d.ts +32 -0
- package/dist/plugins/fx/easing.js +192 -0
- package/dist/plugins/isometric/ISOMixin.d.ts +138 -0
- package/dist/plugins/isometric/ISOMixin.js +322 -0
- package/dist/plugins/isometric/ISORectangle.d.ts +151 -0
- package/dist/plugins/isometric/ISORectangle.js +127 -0
- package/dist/plugins/isometric/ISOSprite.d.ts +157 -0
- package/dist/plugins/isometric/ISOSprite.js +176 -0
- package/dist/plugins/isometric/ISOSpriteGroup.d.ts +151 -0
- package/dist/plugins/isometric/ISOSpriteGroup.js +116 -0
- package/dist/plugins/isometric/ISOTilemap.d.ts +24 -0
- package/dist/plugins/isometric/ISOTilemap.js +85 -0
- package/dist/plugins/isometric/utils.d.ts +2 -0
- package/dist/plugins/isometric/utils.js +10 -0
- package/dist/plugins/keyTracker.d.ts +3 -0
- package/dist/plugins/keyTracker.js +23 -0
- package/dist/plugins/mixer.d.ts +72 -0
- package/dist/plugins/mixer.js +396 -0
- package/dist/plugins/sorted/SortedAnimation.d.ts +13 -0
- package/dist/plugins/sorted/SortedAnimation.js +27 -0
- package/dist/plugins/sorted/SortedGroup.d.ts +137 -0
- package/dist/plugins/sorted/SortedGroup.js +61 -0
- package/dist/plugins/sorted/SortedMixin.d.ts +122 -0
- package/dist/plugins/sorted/SortedMixin.js +153 -0
- package/dist/plugins/sorted/SortedRectangle.d.ts +127 -0
- package/dist/plugins/sorted/SortedRectangle.js +17 -0
- package/dist/plugins/sorted/SortedSprite.d.ts +130 -0
- package/dist/plugins/sorted/SortedSprite.js +35 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.js +33 -0
- package/package.json +6 -2
- package/CHANGELOG.md +0 -13
- package/biome.json +0 -93
- package/demo/package-lock.json +0 -515
- package/demo/package.json +0 -19
- package/demo/snake/index.html +0 -16
- package/demo/snake/index.ts +0 -110
- package/demo/tetris/index.html +0 -16
- package/demo/tetris/index.ts +0 -536
- package/demo/tsconfig.json +0 -53
- package/src/Animation.ts +0 -266
- package/src/BaseSprite.ts +0 -682
- package/src/Gradient.ts +0 -120
- package/src/Playground.ts +0 -247
- package/src/Rect.ts +0 -237
- package/src/Rectangle.ts +0 -108
- package/src/Renderer.ts +0 -28
- package/src/ResourceManager.ts +0 -145
- package/src/Sprite.ts +0 -484
- package/src/SpriteGroup.ts +0 -258
- package/src/canvasRenderer/renderGroup.ts +0 -194
- package/src/canvasRenderer/renderPlayground.ts +0 -103
- package/src/canvasRenderer/renderRectangle.ts +0 -421
- package/src/canvasRenderer/renderSprite.ts +0 -148
- package/src/canvasRenderer.ts +0 -38
- package/src/dummyRenderer.ts +0 -62
- package/src/plugins/GamepadTracker.ts +0 -97
- package/src/plugins/Tilemap.ts +0 -123
- package/src/plugins/Vec2.ts +0 -420
- package/src/plugins/fx/Shaker.ts +0 -293
- package/src/plugins/fx/Tweener.ts +0 -252
- package/src/plugins/fx/easing.ts +0 -276
- package/src/plugins/isometric/ISOMixin.ts +0 -444
- package/src/plugins/isometric/ISORectangle.ts +0 -183
- package/src/plugins/isometric/ISOSprite.ts +0 -228
- package/src/plugins/isometric/ISOSpriteGroup.ts +0 -155
- package/src/plugins/isometric/ISOTilemap.ts +0 -122
- package/src/plugins/isometric/utils.ts +0 -19
- package/src/plugins/keyTracker.ts +0 -31
- package/src/plugins/mixer.ts +0 -523
- package/src/plugins/sorted/SortedAnimation.ts +0 -53
- package/src/plugins/sorted/SortedGroup.ts +0 -94
- package/src/plugins/sorted/SortedMixin.ts +0 -203
- package/src/plugins/sorted/SortedRectangle.ts +0 -35
- package/src/plugins/sorted/SortedSprite.ts +0 -56
- package/src/utils.ts +0 -53
- package/test/animation/index.html +0 -20
- package/test/animation/index.ts +0 -253
- package/test/animation/mh.png +0 -0
- package/test/animation/moh.png +0 -0
- package/test/animation/mov.png +0 -0
- package/test/animation/mv.png +0 -0
- package/test/animation/rebound.png +0 -0
- package/test/animation/reboundm.png +0 -0
- package/test/animation/sh.png +0 -0
- package/test/animation/soh.png +0 -0
- package/test/animation/sov.png +0 -0
- package/test/animation/sv.png +0 -0
- package/test/isometric1/beach.png +0 -0
- package/test/isometric1/foglie.png +0 -0
- package/test/isometric1/grass.png +0 -0
- package/test/isometric1/index.html +0 -17
- package/test/isometric1/index.ts +0 -68
- package/test/isometric1/tronco.png +0 -0
- package/test/isometric1/untitled.tmx +0 -25
- package/test/isometric1/water.png +0 -0
- package/test/isometric2/banner.png +0 -0
- package/test/isometric2/books.png +0 -0
- package/test/isometric2/candlestick.png +0 -0
- package/test/isometric2/ice.png +0 -0
- package/test/isometric2/index.html +0 -16
- package/test/isometric2/index.ts +0 -129
- package/test/isometric2/knight_se.png +0 -0
- package/test/isometric2/tile.png +0 -0
- package/test/isometric2/untitled.tmx +0 -36
- package/test/isometric2/wall.png +0 -0
- package/test/layers/boy.png +0 -0
- package/test/layers/catgirl.png +0 -0
- package/test/layers/fri1.svg +0 -8
- package/test/layers/grassblock.png +0 -0
- package/test/layers/horngirl.png +0 -0
- package/test/layers/index.html +0 -17
- package/test/layers/index.ts +0 -191
- package/test/layers/pinkgirl.png +0 -0
- package/test/layers/render_ruler.py +0 -57
- package/test/layers/ruler.png +0 -0
- package/test/layers/sh.png +0 -0
- package/test/mixer/dieci.opus +0 -0
- package/test/mixer/dieci.wav +0 -0
- package/test/mixer/index.html +0 -33
- package/test/mixer/index.ts +0 -206
- package/test/mixer/news_intro.mp3 +0 -0
- package/test/mixer/news_intro.ogg +0 -0
- package/test/mixer/prova.mp3 +0 -0
- package/test/mixer/prova.ogg +0 -0
- package/test/package-lock.json +0 -515
- package/test/package.json +0 -19
- package/test/sorted/Brown Block.png +0 -0
- package/test/sorted/index.html +0 -16
- package/test/sorted/index.ts +0 -38
- package/test/transformation1/index.html +0 -17
- package/test/transformation1/index.ts +0 -51
- package/test/transformation1/sh.png +0 -0
- package/test/transformation2/index.html +0 -20
- package/test/transformation2/index.ts +0 -71
- package/test/transformation2/sh.png +0 -0
- package/test/tsconfig.json +0 -53
- package/tsconfig.json +0 -51
package/dist/Sprite.js
ADDED
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import { BaseSprite } from "./BaseSprite.js";
|
|
2
|
+
import { pick } from "./utils.js";
|
|
3
|
+
export class Sprite extends BaseSprite {
|
|
4
|
+
_animation = null;
|
|
5
|
+
_animationIndex = 0;
|
|
6
|
+
_callback = null;
|
|
7
|
+
_paused = false;
|
|
8
|
+
_rate = 1;
|
|
9
|
+
_reportedRate = 0;
|
|
10
|
+
_once = false;
|
|
11
|
+
_pingpong = false;
|
|
12
|
+
_backwards = false;
|
|
13
|
+
// Implementation details
|
|
14
|
+
_idleCounter = 0;
|
|
15
|
+
_lastSpriteSheet = 0;
|
|
16
|
+
_currentSpriteSheet = 0;
|
|
17
|
+
_numberOfFrame = 0;
|
|
18
|
+
_currentFrame = 0;
|
|
19
|
+
_frameIncrement = 1;
|
|
20
|
+
_multix = 0;
|
|
21
|
+
_multiy = 0;
|
|
22
|
+
_endAnimation = false;
|
|
23
|
+
get animation() {
|
|
24
|
+
return this._animation;
|
|
25
|
+
}
|
|
26
|
+
set animation(value) {
|
|
27
|
+
if (value !== this._animation) {
|
|
28
|
+
this._animation = value;
|
|
29
|
+
if (value !== null) {
|
|
30
|
+
const playground = this.playground;
|
|
31
|
+
if (playground !== undefined) {
|
|
32
|
+
this._rate = playground.framesFromMs(value.rate);
|
|
33
|
+
}
|
|
34
|
+
this._reportedRate = value.rate;
|
|
35
|
+
this._once = value.once;
|
|
36
|
+
this._pingpong = value.pingpong;
|
|
37
|
+
this._backwards = value.backwards;
|
|
38
|
+
// Force new width and height based on the animation frame size
|
|
39
|
+
super.width = value.frameWidth;
|
|
40
|
+
super.height = value.frameHeight;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
this._rate = 1;
|
|
44
|
+
this._reportedRate = 0;
|
|
45
|
+
this._once = false;
|
|
46
|
+
this._pingpong = false;
|
|
47
|
+
this._backwards = false;
|
|
48
|
+
super.width = 0;
|
|
49
|
+
super.height = 0;
|
|
50
|
+
}
|
|
51
|
+
this._animationIndex = 0;
|
|
52
|
+
this.callback = null;
|
|
53
|
+
this.paused = false;
|
|
54
|
+
this.restartAnimation();
|
|
55
|
+
this._checkUpdate();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
get animationIndex() {
|
|
59
|
+
return this._animationIndex;
|
|
60
|
+
}
|
|
61
|
+
set animationIndex(value) {
|
|
62
|
+
if (value !== this._animationIndex) {
|
|
63
|
+
this._animationIndex = value;
|
|
64
|
+
this.callback = null;
|
|
65
|
+
this.paused = false;
|
|
66
|
+
this.restartAnimation();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
get callback() {
|
|
70
|
+
return this._callback;
|
|
71
|
+
}
|
|
72
|
+
set callback(value) {
|
|
73
|
+
this._callback = value;
|
|
74
|
+
this._checkUpdate();
|
|
75
|
+
}
|
|
76
|
+
get paused() {
|
|
77
|
+
return this._paused;
|
|
78
|
+
}
|
|
79
|
+
set paused(value) {
|
|
80
|
+
this._paused = value;
|
|
81
|
+
this._checkUpdate();
|
|
82
|
+
}
|
|
83
|
+
get rate() {
|
|
84
|
+
return this._reportedRate;
|
|
85
|
+
}
|
|
86
|
+
set rate(value) {
|
|
87
|
+
const playground = this.playground;
|
|
88
|
+
if (playground !== undefined) {
|
|
89
|
+
this._rate = playground.framesFromMs(value);
|
|
90
|
+
}
|
|
91
|
+
this._reportedRate = value;
|
|
92
|
+
}
|
|
93
|
+
get once() {
|
|
94
|
+
return this._once;
|
|
95
|
+
}
|
|
96
|
+
set once(value) {
|
|
97
|
+
this._once = value;
|
|
98
|
+
}
|
|
99
|
+
get pingpong() {
|
|
100
|
+
return this._pingpong;
|
|
101
|
+
}
|
|
102
|
+
set pingpong(value) {
|
|
103
|
+
this._pingpong = value;
|
|
104
|
+
}
|
|
105
|
+
get backwards() {
|
|
106
|
+
return this._backwards;
|
|
107
|
+
}
|
|
108
|
+
set backwards(value) {
|
|
109
|
+
if (value !== this._backwards) {
|
|
110
|
+
this._backwards = value;
|
|
111
|
+
this._frameIncrement *= -1;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
constructor(options) {
|
|
115
|
+
super(options);
|
|
116
|
+
if (options !== undefined) {
|
|
117
|
+
Object.assign(this, pick(options, [
|
|
118
|
+
"animation",
|
|
119
|
+
"animationIndex",
|
|
120
|
+
"callback",
|
|
121
|
+
"paused",
|
|
122
|
+
"rate",
|
|
123
|
+
"once",
|
|
124
|
+
"pingpong",
|
|
125
|
+
"backwards",
|
|
126
|
+
]));
|
|
127
|
+
if (options.ref !== undefined) {
|
|
128
|
+
options.ref.current = this;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
this.restartAnimation();
|
|
132
|
+
this.teleport();
|
|
133
|
+
}
|
|
134
|
+
// Public functions
|
|
135
|
+
restartAnimation() {
|
|
136
|
+
const animation = this._animation;
|
|
137
|
+
if (animation !== null && this._backwards) {
|
|
138
|
+
this._lastSpriteSheet = animation.frameset.length - 1;
|
|
139
|
+
this._currentSpriteSheet = this._lastSpriteSheet;
|
|
140
|
+
this._numberOfFrame =
|
|
141
|
+
animation.frameset[this._currentSpriteSheet].numberOfFrame;
|
|
142
|
+
this._currentFrame = this._numberOfFrame - 1;
|
|
143
|
+
this._frameIncrement = -1;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
this._currentSpriteSheet = 0;
|
|
147
|
+
if (animation !== null) {
|
|
148
|
+
this._lastSpriteSheet = animation.frameset.length - 1;
|
|
149
|
+
this._numberOfFrame = animation.frameset[0].numberOfFrame;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
this._lastSpriteSheet = 0;
|
|
153
|
+
this._numberOfFrame = 0;
|
|
154
|
+
}
|
|
155
|
+
this._currentFrame = 0;
|
|
156
|
+
this._frameIncrement = 1;
|
|
157
|
+
}
|
|
158
|
+
this._idleCounter = 0;
|
|
159
|
+
this._endAnimation = false;
|
|
160
|
+
const index = this._animationIndex;
|
|
161
|
+
if (animation !== null && index !== 0) {
|
|
162
|
+
const sprite_sheet = animation.frameset[this._currentSpriteSheet];
|
|
163
|
+
this._multix = index * sprite_sheet._multix;
|
|
164
|
+
this._multiy = index * sprite_sheet._multiy;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
this._multix = 0;
|
|
168
|
+
this._multiy = 0;
|
|
169
|
+
}
|
|
170
|
+
return this;
|
|
171
|
+
}
|
|
172
|
+
// Implementation details
|
|
173
|
+
_checkUpdate() {
|
|
174
|
+
const oldNeedsUpdate = this._needsUpdate;
|
|
175
|
+
if (this._endAnimation ||
|
|
176
|
+
this._paused ||
|
|
177
|
+
(this._callback === null &&
|
|
178
|
+
(this._animation === null ||
|
|
179
|
+
(this._lastSpriteSheet <= 0 && this._numberOfFrame <= 1)))) {
|
|
180
|
+
this._needsUpdate = false;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
this._needsUpdate = true;
|
|
184
|
+
}
|
|
185
|
+
this._updateNeedsUpdate(oldNeedsUpdate);
|
|
186
|
+
}
|
|
187
|
+
_onReparent() {
|
|
188
|
+
const playground = this.playground;
|
|
189
|
+
if (playground !== undefined) {
|
|
190
|
+
this._rate = playground.framesFromMs(this._reportedRate);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
_initRenderer() {
|
|
194
|
+
this.playground?._renderer.initSprite(this);
|
|
195
|
+
}
|
|
196
|
+
_update() {
|
|
197
|
+
const callback = this._callback;
|
|
198
|
+
const animation = this._animation;
|
|
199
|
+
let currentSpriteSheet = this._currentSpriteSheet;
|
|
200
|
+
let currentFrame = this._currentFrame;
|
|
201
|
+
if (!(this._endAnimation || this._paused)) {
|
|
202
|
+
if (animation !== null) {
|
|
203
|
+
this._idleCounter += 1;
|
|
204
|
+
if (this._idleCounter >= this._rate) {
|
|
205
|
+
this._idleCounter = 0;
|
|
206
|
+
currentFrame += this._frameIncrement;
|
|
207
|
+
if (this._backwards) {
|
|
208
|
+
// Backwards animations
|
|
209
|
+
if (this._pingpong) {
|
|
210
|
+
// In pingpong animations the end is when the frame returns to the last frame
|
|
211
|
+
if (currentFrame >= this._numberOfFrame) {
|
|
212
|
+
if (currentSpriteSheet < this._lastSpriteSheet) {
|
|
213
|
+
currentSpriteSheet += 1;
|
|
214
|
+
this._currentSpriteSheet = currentSpriteSheet;
|
|
215
|
+
this._numberOfFrame =
|
|
216
|
+
animation.frameset[currentSpriteSheet].numberOfFrame;
|
|
217
|
+
this._currentFrame = 0;
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
this._frameIncrement = -1;
|
|
221
|
+
if (this._once) {
|
|
222
|
+
currentFrame -= 1;
|
|
223
|
+
this._idleCounter = 1;
|
|
224
|
+
this._endAnimation = true;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
// The first frame has already been displayed, start from the second
|
|
228
|
+
if (this._numberOfFrame > 1) {
|
|
229
|
+
currentFrame -= 2;
|
|
230
|
+
}
|
|
231
|
+
else if (this._lastSpriteSheet > 0) {
|
|
232
|
+
currentSpriteSheet -= 1;
|
|
233
|
+
this._currentSpriteSheet = currentSpriteSheet;
|
|
234
|
+
this._numberOfFrame =
|
|
235
|
+
animation.frameset[currentSpriteSheet].numberOfFrame;
|
|
236
|
+
currentFrame = this._numberOfFrame - 1;
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
currentFrame -= 1;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
// Update the details before the callback
|
|
243
|
+
this._currentFrame = currentFrame;
|
|
244
|
+
callback?.();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
else if (currentFrame < 0) {
|
|
248
|
+
if (currentSpriteSheet > 0) {
|
|
249
|
+
currentSpriteSheet -= 1;
|
|
250
|
+
this._currentSpriteSheet = currentSpriteSheet;
|
|
251
|
+
this._numberOfFrame =
|
|
252
|
+
animation.frameset[currentSpriteSheet].numberOfFrame;
|
|
253
|
+
this._currentFrame = this._numberOfFrame - 1;
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
// Last frame reached, change animation direction
|
|
257
|
+
this._frameIncrement = 1;
|
|
258
|
+
// The first frame has already been displayed, start from the second
|
|
259
|
+
if (this._numberOfFrame > 1) {
|
|
260
|
+
currentFrame = 1;
|
|
261
|
+
}
|
|
262
|
+
else if (this._lastSpriteSheet > 0) {
|
|
263
|
+
currentSpriteSheet += 1;
|
|
264
|
+
this._currentSpriteSheet = currentSpriteSheet;
|
|
265
|
+
this._numberOfFrame =
|
|
266
|
+
animation.frameset[currentSpriteSheet].numberOfFrame;
|
|
267
|
+
currentFrame = 0;
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
currentFrame = 0;
|
|
271
|
+
}
|
|
272
|
+
this._currentFrame = currentFrame;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
// This is no particular frame, simply update the details
|
|
277
|
+
this._currentFrame = currentFrame;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
// Normal animation
|
|
282
|
+
if (currentFrame < 0) {
|
|
283
|
+
if (currentSpriteSheet > 0) {
|
|
284
|
+
currentSpriteSheet -= 1;
|
|
285
|
+
this._currentSpriteSheet = currentSpriteSheet;
|
|
286
|
+
this._numberOfFrame =
|
|
287
|
+
animation.frameset[currentSpriteSheet].numberOfFrame;
|
|
288
|
+
this._currentFrame = this._numberOfFrame - 1;
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
// Last frame reached
|
|
292
|
+
if (this._once) {
|
|
293
|
+
currentFrame = 0;
|
|
294
|
+
this._idleCounter = 1;
|
|
295
|
+
this._endAnimation = true;
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
currentSpriteSheet = this._lastSpriteSheet;
|
|
299
|
+
this._currentSpriteSheet = currentSpriteSheet;
|
|
300
|
+
this._numberOfFrame =
|
|
301
|
+
animation.frameset[currentSpriteSheet].numberOfFrame;
|
|
302
|
+
currentFrame = this._numberOfFrame - 1;
|
|
303
|
+
}
|
|
304
|
+
// Update the details before the callback
|
|
305
|
+
this._currentFrame = currentFrame;
|
|
306
|
+
callback?.();
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
// This is no particular frame, simply update the details
|
|
311
|
+
this._currentFrame = currentFrame;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
// Forwards animations
|
|
317
|
+
if (this._pingpong) {
|
|
318
|
+
// In pingpong animations the end is when the frame goes below 0
|
|
319
|
+
if (currentFrame < 0) {
|
|
320
|
+
if (currentSpriteSheet > 0) {
|
|
321
|
+
currentSpriteSheet -= 1;
|
|
322
|
+
this._currentSpriteSheet = currentSpriteSheet;
|
|
323
|
+
this._numberOfFrame =
|
|
324
|
+
animation.frameset[currentSpriteSheet].numberOfFrame;
|
|
325
|
+
this._currentFrame = this._numberOfFrame - 1;
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
this._frameIncrement = 1;
|
|
329
|
+
if (this._once) {
|
|
330
|
+
currentFrame = 0;
|
|
331
|
+
this._idleCounter = 1;
|
|
332
|
+
this._endAnimation = true;
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
// The first frame has already been displayed, start from the second
|
|
336
|
+
if (this._numberOfFrame > 1) {
|
|
337
|
+
currentFrame = 1;
|
|
338
|
+
}
|
|
339
|
+
else if (this._lastSpriteSheet > 0) {
|
|
340
|
+
currentSpriteSheet += 1;
|
|
341
|
+
this._currentSpriteSheet = currentSpriteSheet;
|
|
342
|
+
this._numberOfFrame =
|
|
343
|
+
animation.frameset[currentSpriteSheet].numberOfFrame;
|
|
344
|
+
currentFrame = 0;
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
currentFrame = 0;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
// Update the details before the callback
|
|
351
|
+
this._currentFrame = currentFrame;
|
|
352
|
+
callback?.();
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
else if (currentFrame >= this._numberOfFrame) {
|
|
356
|
+
if (currentSpriteSheet < this._lastSpriteSheet) {
|
|
357
|
+
currentSpriteSheet += 1;
|
|
358
|
+
this._currentSpriteSheet = currentSpriteSheet;
|
|
359
|
+
this._numberOfFrame =
|
|
360
|
+
animation.frameset[currentSpriteSheet].numberOfFrame;
|
|
361
|
+
this._currentFrame = 0;
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
// Last frame reached, change animation direction
|
|
365
|
+
this._frameIncrement = -1;
|
|
366
|
+
if (this._numberOfFrame > 1) {
|
|
367
|
+
currentFrame -= 2;
|
|
368
|
+
}
|
|
369
|
+
else if (this._lastSpriteSheet > 0) {
|
|
370
|
+
currentSpriteSheet -= 1;
|
|
371
|
+
this._currentSpriteSheet = currentSpriteSheet;
|
|
372
|
+
this._numberOfFrame =
|
|
373
|
+
animation.frameset[currentSpriteSheet].numberOfFrame;
|
|
374
|
+
currentFrame = this._numberOfFrame - 1;
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
currentFrame -= 1;
|
|
378
|
+
}
|
|
379
|
+
this._currentFrame = currentFrame;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
// This is no particular frame, simply update the details
|
|
384
|
+
this._currentFrame = currentFrame;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
else {
|
|
388
|
+
// Normal animation
|
|
389
|
+
if (currentFrame >= this._numberOfFrame) {
|
|
390
|
+
if (currentSpriteSheet < this._lastSpriteSheet) {
|
|
391
|
+
currentSpriteSheet += 1;
|
|
392
|
+
this._currentSpriteSheet = currentSpriteSheet;
|
|
393
|
+
this._numberOfFrame =
|
|
394
|
+
animation.frameset[currentSpriteSheet].numberOfFrame;
|
|
395
|
+
this._currentFrame = 0;
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
// Last frame reached
|
|
399
|
+
if (this._once) {
|
|
400
|
+
currentFrame -= 1;
|
|
401
|
+
this._idleCounter = 1;
|
|
402
|
+
this._endAnimation = true;
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
currentSpriteSheet = 0;
|
|
406
|
+
this._currentSpriteSheet = currentSpriteSheet;
|
|
407
|
+
this._numberOfFrame =
|
|
408
|
+
animation.frameset[currentSpriteSheet].numberOfFrame;
|
|
409
|
+
currentFrame = 0;
|
|
410
|
+
}
|
|
411
|
+
// Update the details before the callback
|
|
412
|
+
this._currentFrame = currentFrame;
|
|
413
|
+
callback?.();
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
// This is no particular frame, simply update the details
|
|
418
|
+
this._currentFrame = currentFrame;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
else {
|
|
425
|
+
// Make sure that the callback is called even if there is no animation
|
|
426
|
+
callback?.();
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
_draw(interp) {
|
|
431
|
+
super._draw(interp);
|
|
432
|
+
this.playground?._renderer.drawSprite(this, interp);
|
|
433
|
+
}
|
|
434
|
+
_remove() {
|
|
435
|
+
this.playground?._renderer.removeSprite(this);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { BaseSpriteOptions } from "./BaseSprite.js";
|
|
2
|
+
import { BaseSprite } from "./BaseSprite.js";
|
|
3
|
+
import type { SpriteRef } from "./utils.js";
|
|
4
|
+
export interface SpriteGroupOptions extends BaseSpriteOptions {
|
|
5
|
+
crop: boolean;
|
|
6
|
+
borderRadius: number;
|
|
7
|
+
children: BaseSprite[];
|
|
8
|
+
}
|
|
9
|
+
export declare class SpriteGroup extends BaseSprite {
|
|
10
|
+
_crop: boolean;
|
|
11
|
+
_borderRadius: number;
|
|
12
|
+
get crop(): boolean;
|
|
13
|
+
set crop(value: boolean);
|
|
14
|
+
get borderRadius(): number;
|
|
15
|
+
set borderRadius(value: number);
|
|
16
|
+
_layers: BaseSprite[];
|
|
17
|
+
_updateList: BaseSprite[];
|
|
18
|
+
constructor(options?: Partial<SpriteGroupOptions> & {
|
|
19
|
+
ref?: SpriteRef<SpriteGroup>;
|
|
20
|
+
});
|
|
21
|
+
clear(): this;
|
|
22
|
+
addChild<T extends BaseSprite>(child: T, options?: {
|
|
23
|
+
suppressWarning?: boolean;
|
|
24
|
+
}): T;
|
|
25
|
+
insertChild<T extends BaseSprite>(child: T, options?: {
|
|
26
|
+
suppressWarning?: boolean;
|
|
27
|
+
}): T;
|
|
28
|
+
removeChild(child: BaseSprite | null, options?: {
|
|
29
|
+
suppressWarning?: boolean;
|
|
30
|
+
}): this;
|
|
31
|
+
addChildren(children: BaseSprite[]): this;
|
|
32
|
+
_reparentChild(child: BaseSprite): void;
|
|
33
|
+
_addPlaygroundToChild(child: BaseSprite): void;
|
|
34
|
+
_checkUpdate(): void;
|
|
35
|
+
_initRenderer(): void;
|
|
36
|
+
_update(): void;
|
|
37
|
+
_draw(interp: number): void;
|
|
38
|
+
_remove(): void;
|
|
39
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { BaseSprite } from "./BaseSprite.js";
|
|
2
|
+
import { pick } from "./utils.js";
|
|
3
|
+
export class SpriteGroup extends BaseSprite {
|
|
4
|
+
_crop = false;
|
|
5
|
+
_borderRadius = 0;
|
|
6
|
+
get crop() {
|
|
7
|
+
return this._crop;
|
|
8
|
+
}
|
|
9
|
+
set crop(value) {
|
|
10
|
+
this._crop = value;
|
|
11
|
+
}
|
|
12
|
+
get borderRadius() {
|
|
13
|
+
return this._borderRadius;
|
|
14
|
+
}
|
|
15
|
+
set borderRadius(value) {
|
|
16
|
+
this._borderRadius = value;
|
|
17
|
+
}
|
|
18
|
+
// Implementation details
|
|
19
|
+
_layers = [];
|
|
20
|
+
_updateList = [];
|
|
21
|
+
constructor(options) {
|
|
22
|
+
super(options);
|
|
23
|
+
if (options !== undefined) {
|
|
24
|
+
Object.assign(this, pick(options, ["crop", "borderRadius"]));
|
|
25
|
+
if (options.children !== undefined) {
|
|
26
|
+
this.addChildren(options.children);
|
|
27
|
+
}
|
|
28
|
+
if (options.ref !== undefined) {
|
|
29
|
+
options.ref.current = this;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
this.teleport();
|
|
33
|
+
}
|
|
34
|
+
// Public functions
|
|
35
|
+
clear() {
|
|
36
|
+
for (const sprite of this._layers) {
|
|
37
|
+
sprite._remove();
|
|
38
|
+
}
|
|
39
|
+
this._layers.splice(0, this._layers.length);
|
|
40
|
+
this._updateList.splice(0, this._updateList.length);
|
|
41
|
+
this._checkUpdate();
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
addChild(child, options) {
|
|
45
|
+
if (child.parent === undefined) {
|
|
46
|
+
this._layers.push(child);
|
|
47
|
+
this._reparentChild(child);
|
|
48
|
+
this._checkUpdate();
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
if (typeof console !== "undefined" &&
|
|
52
|
+
options?.suppressWarning === false) {
|
|
53
|
+
console.warn("child already has a parent");
|
|
54
|
+
console.trace();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return child;
|
|
58
|
+
}
|
|
59
|
+
insertChild(child, options) {
|
|
60
|
+
if (child.parent === undefined) {
|
|
61
|
+
this._layers.unshift(child);
|
|
62
|
+
this._reparentChild(child);
|
|
63
|
+
this._checkUpdate();
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
if (typeof console !== "undefined" &&
|
|
67
|
+
options?.suppressWarning === false) {
|
|
68
|
+
console.warn("child already has a parent");
|
|
69
|
+
console.trace();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return child;
|
|
73
|
+
}
|
|
74
|
+
removeChild(child, options) {
|
|
75
|
+
if (child === null) {
|
|
76
|
+
if (typeof console !== "undefined" &&
|
|
77
|
+
options?.suppressWarning === false) {
|
|
78
|
+
console.warn("child is null");
|
|
79
|
+
console.trace();
|
|
80
|
+
}
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
const layers = this._layers;
|
|
84
|
+
const index = layers.indexOf(child);
|
|
85
|
+
if (index >= 0) {
|
|
86
|
+
layers.splice(index, 1);
|
|
87
|
+
const update_list = this._updateList;
|
|
88
|
+
const i = update_list.indexOf(child);
|
|
89
|
+
if (i >= 0) {
|
|
90
|
+
update_list.splice(i, 1);
|
|
91
|
+
}
|
|
92
|
+
child._remove();
|
|
93
|
+
this._checkUpdate();
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
if (typeof console !== "undefined" &&
|
|
97
|
+
options?.suppressWarning === false) {
|
|
98
|
+
console.warn("No children removed");
|
|
99
|
+
console.trace();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
addChildren(children) {
|
|
105
|
+
for (const child of children) {
|
|
106
|
+
this.addChild(child);
|
|
107
|
+
}
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
// Implementation details
|
|
111
|
+
_reparentChild(child) {
|
|
112
|
+
child._parent = new WeakRef(this);
|
|
113
|
+
child._needsUpdate = false;
|
|
114
|
+
child._checkUpdate();
|
|
115
|
+
this._addPlaygroundToChild(child);
|
|
116
|
+
}
|
|
117
|
+
_addPlaygroundToChild(child) {
|
|
118
|
+
const playground = this.playground;
|
|
119
|
+
if (playground !== undefined) {
|
|
120
|
+
child._playground = new WeakRef(playground);
|
|
121
|
+
child._frameCounterLastMove = playground.frameCounter;
|
|
122
|
+
child._initRenderer();
|
|
123
|
+
child._onReparent();
|
|
124
|
+
if ("_layers" in child) {
|
|
125
|
+
for (const layer of child._layers) {
|
|
126
|
+
this._addPlaygroundToChild(layer);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
_checkUpdate() {
|
|
132
|
+
const oldNeedsUpdate = this._needsUpdate;
|
|
133
|
+
if (this._layers.length === 0) {
|
|
134
|
+
this._needsUpdate = false;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
this._needsUpdate = true;
|
|
138
|
+
}
|
|
139
|
+
this._updateNeedsUpdate(oldNeedsUpdate);
|
|
140
|
+
}
|
|
141
|
+
_initRenderer() {
|
|
142
|
+
this.playground?._renderer.initGroup(this);
|
|
143
|
+
}
|
|
144
|
+
_update() {
|
|
145
|
+
for (const sprite of this._updateList) {
|
|
146
|
+
sprite._update();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
_draw(interp) {
|
|
150
|
+
super._draw(interp);
|
|
151
|
+
const playground = this.playground;
|
|
152
|
+
if (playground !== undefined) {
|
|
153
|
+
const absLeft = playground._absLeft;
|
|
154
|
+
const absTop = playground._absTop;
|
|
155
|
+
playground._absLeft += this._drawLeft;
|
|
156
|
+
playground._absTop += this._drawTop;
|
|
157
|
+
const renderer = playground._renderer;
|
|
158
|
+
renderer.drawGroupBeforeChildren(this, interp);
|
|
159
|
+
if (this._opacity !== 0 &&
|
|
160
|
+
this._scaleh !== 0 &&
|
|
161
|
+
this._scalev !== 0 &&
|
|
162
|
+
!this._hidden) {
|
|
163
|
+
for (const sprite of this._layers) {
|
|
164
|
+
sprite._draw(interp);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
renderer.drawGroupAfterChildren(this, interp);
|
|
168
|
+
playground._absLeft = absLeft;
|
|
169
|
+
playground._absTop = absTop;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
_remove() {
|
|
173
|
+
this.clear();
|
|
174
|
+
this.playground?._renderer.removeGroup(this);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { SpriteGroup } from "../SpriteGroup.js";
|
|
2
|
+
export interface GroupObj {
|
|
3
|
+
oldAlpha: number;
|
|
4
|
+
alphaChanged: boolean;
|
|
5
|
+
oldBlendMode: GlobalCompositeOperation;
|
|
6
|
+
blendModeChanged: boolean;
|
|
7
|
+
left: number;
|
|
8
|
+
top: number;
|
|
9
|
+
contextSaved: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare const spriteGroupMap: WeakMap<SpriteGroup, GroupObj>;
|
|
12
|
+
export declare function initGroup(group: SpriteGroup): void;
|
|
13
|
+
export declare function removeGroup(_group: SpriteGroup): void;
|
|
14
|
+
export declare function drawGroupBeforeChildren(group: SpriteGroup, _interp: number): void;
|
|
15
|
+
export declare function drawGroupAfterChildren(group: SpriteGroup, _interp: number): void;
|