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.
- package/CHANGELOG.md +13 -0
- package/LICENSE +373 -0
- package/README.md +80 -2
- package/biome.json +93 -0
- package/demo/package-lock.json +515 -0
- package/demo/package.json +19 -0
- package/demo/snake/index.html +16 -0
- package/demo/snake/index.ts +110 -0
- package/demo/tetris/index.html +16 -0
- package/demo/tetris/index.ts +536 -0
- package/demo/tsconfig.json +53 -0
- package/frigame.css +12 -0
- package/package.json +50 -4
- package/src/Animation.ts +266 -0
- package/src/BaseSprite.ts +682 -0
- package/src/Gradient.ts +120 -0
- package/src/Playground.ts +247 -0
- package/src/Rect.ts +237 -0
- package/src/Rectangle.ts +108 -0
- package/src/Renderer.ts +28 -0
- package/src/ResourceManager.ts +145 -0
- package/src/Sprite.ts +484 -0
- package/src/SpriteGroup.ts +258 -0
- package/src/canvasRenderer/renderGroup.ts +194 -0
- package/src/canvasRenderer/renderPlayground.ts +103 -0
- package/src/canvasRenderer/renderRectangle.ts +421 -0
- package/src/canvasRenderer/renderSprite.ts +148 -0
- package/src/canvasRenderer.ts +38 -0
- package/src/dummyRenderer.ts +62 -0
- package/src/plugins/GamepadTracker.ts +97 -0
- package/src/plugins/Tilemap.ts +123 -0
- package/src/plugins/Vec2.ts +420 -0
- package/src/plugins/fx/Shaker.ts +293 -0
- package/src/plugins/fx/Tweener.ts +252 -0
- package/src/plugins/fx/easing.ts +276 -0
- package/src/plugins/isometric/ISOMixin.ts +444 -0
- package/src/plugins/isometric/ISORectangle.ts +183 -0
- package/src/plugins/isometric/ISOSprite.ts +228 -0
- package/src/plugins/isometric/ISOSpriteGroup.ts +155 -0
- package/src/plugins/isometric/ISOTilemap.ts +122 -0
- package/src/plugins/isometric/utils.ts +19 -0
- package/src/plugins/keyTracker.ts +31 -0
- package/src/plugins/mixer.ts +523 -0
- package/src/plugins/sorted/SortedAnimation.ts +53 -0
- package/src/plugins/sorted/SortedGroup.ts +94 -0
- package/src/plugins/sorted/SortedMixin.ts +203 -0
- package/src/plugins/sorted/SortedRectangle.ts +35 -0
- package/src/plugins/sorted/SortedSprite.ts +56 -0
- package/src/utils.ts +53 -0
- package/test/animation/index.html +20 -0
- package/test/animation/index.ts +253 -0
- 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 +17 -0
- package/test/isometric1/index.ts +68 -0
- package/test/isometric1/tronco.png +0 -0
- package/test/isometric1/untitled.tmx +25 -0
- 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 +16 -0
- package/test/isometric2/index.ts +129 -0
- package/test/isometric2/knight_se.png +0 -0
- package/test/isometric2/tile.png +0 -0
- package/test/isometric2/untitled.tmx +36 -0
- 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 +8 -0
- package/test/layers/grassblock.png +0 -0
- package/test/layers/horngirl.png +0 -0
- package/test/layers/index.html +17 -0
- package/test/layers/index.ts +191 -0
- package/test/layers/pinkgirl.png +0 -0
- package/test/layers/render_ruler.py +57 -0
- 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 +33 -0
- package/test/mixer/index.ts +206 -0
- 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 +515 -0
- package/test/package.json +19 -0
- package/test/sorted/Brown Block.png +0 -0
- package/test/sorted/index.html +16 -0
- package/test/sorted/index.ts +38 -0
- package/test/transformation1/index.html +17 -0
- package/test/transformation1/index.ts +51 -0
- package/test/transformation1/sh.png +0 -0
- package/test/transformation2/index.html +20 -0
- package/test/transformation2/index.ts +71 -0
- package/test/transformation2/sh.png +0 -0
- package/test/tsconfig.json +53 -0
- package/tsconfig.json +51 -0
|
@@ -0,0 +1,682 @@
|
|
|
1
|
+
import type { Playground } from "./Playground.js";
|
|
2
|
+
import type {
|
|
3
|
+
RectOptions,
|
|
4
|
+
RectPos,
|
|
5
|
+
RectSize,
|
|
6
|
+
RectSizeX,
|
|
7
|
+
RectSizeY,
|
|
8
|
+
} from "./Rect.js";
|
|
9
|
+
import { Rect } from "./Rect.js";
|
|
10
|
+
import type { SpriteGroup } from "./SpriteGroup.js";
|
|
11
|
+
import { clamp, lerp, pick } from "./utils.js";
|
|
12
|
+
|
|
13
|
+
// source-over, lighter, multiply, screen
|
|
14
|
+
export type BlendMode = "normal" | "add" | "multiply" | "screen";
|
|
15
|
+
|
|
16
|
+
export interface TransformOptions {
|
|
17
|
+
transformOrigin: number;
|
|
18
|
+
transformOriginx: keyof RectSizeX | number;
|
|
19
|
+
transformOriginy: keyof RectSizeY | number;
|
|
20
|
+
|
|
21
|
+
angle: number;
|
|
22
|
+
|
|
23
|
+
scale: number;
|
|
24
|
+
scalex: number;
|
|
25
|
+
scaley: number;
|
|
26
|
+
|
|
27
|
+
flip: boolean;
|
|
28
|
+
fliph: boolean;
|
|
29
|
+
flipv: boolean;
|
|
30
|
+
|
|
31
|
+
opacity: number;
|
|
32
|
+
hidden: boolean;
|
|
33
|
+
|
|
34
|
+
blendMode: BlendMode;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface BaseSpriteOptions extends RectOptions, TransformOptions {}
|
|
38
|
+
|
|
39
|
+
export class BaseSprite extends Rect implements BaseSpriteOptions {
|
|
40
|
+
_playground?: WeakRef<Playground>;
|
|
41
|
+
_parent?: WeakRef<SpriteGroup>;
|
|
42
|
+
|
|
43
|
+
_transformOriginx: keyof RectSizeX | number = "halfWidth";
|
|
44
|
+
_transformOriginy: keyof RectSizeY | number = "halfHeight";
|
|
45
|
+
|
|
46
|
+
_angle = 0;
|
|
47
|
+
|
|
48
|
+
_scalex = 1;
|
|
49
|
+
_scaley = 1;
|
|
50
|
+
_fliph = 1;
|
|
51
|
+
_flipv = 1;
|
|
52
|
+
|
|
53
|
+
_opacity = 1;
|
|
54
|
+
_hidden = false;
|
|
55
|
+
|
|
56
|
+
_scaleh = 1;
|
|
57
|
+
_scalev = 1;
|
|
58
|
+
|
|
59
|
+
_blendMode: BlendMode = "normal";
|
|
60
|
+
|
|
61
|
+
// Implementation details
|
|
62
|
+
|
|
63
|
+
_needsUpdate = false;
|
|
64
|
+
_prevLeft = 0;
|
|
65
|
+
_prevTop = 0;
|
|
66
|
+
_frameCounterLastMove = 0;
|
|
67
|
+
_drawLeft = 0;
|
|
68
|
+
_drawTop = 0;
|
|
69
|
+
_drawWidth = 0;
|
|
70
|
+
_drawHeight = 0;
|
|
71
|
+
|
|
72
|
+
get playground(): Playground | undefined {
|
|
73
|
+
return this._playground?.deref();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get parent(): SpriteGroup | undefined {
|
|
77
|
+
return this._parent?.deref();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
get transformOrigin(): number {
|
|
81
|
+
const origin = this._transformOriginx;
|
|
82
|
+
|
|
83
|
+
if (typeof origin === "string") {
|
|
84
|
+
return this[origin];
|
|
85
|
+
} else {
|
|
86
|
+
return origin;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
set transformOrigin(value: number) {
|
|
91
|
+
this._transformOriginx = value;
|
|
92
|
+
this._transformOriginy = value;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
get transformOriginx(): keyof RectSizeX | number {
|
|
96
|
+
return this._transformOriginx;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
set transformOriginx(value: keyof RectSizeX | number) {
|
|
100
|
+
this._transformOriginx = value;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get transformOriginy(): keyof RectSizeY | number {
|
|
104
|
+
return this._transformOriginy;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
set transformOriginy(value: keyof RectSizeY | number) {
|
|
108
|
+
this._transformOriginy = value;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
get angle(): number {
|
|
112
|
+
return this._angle;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
set angle(value: number) {
|
|
116
|
+
this._angle = value;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
get scalex(): number {
|
|
120
|
+
return this._scalex;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
set scalex(value: number) {
|
|
124
|
+
this._scalex = value;
|
|
125
|
+
this._scaleh = value * this._fliph;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
get scaley(): number {
|
|
129
|
+
return this._scaley;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
set scaley(value: number) {
|
|
133
|
+
this._scaley = value;
|
|
134
|
+
this._scalev = value * this._flipv;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
get scale(): number {
|
|
138
|
+
return this._scalex;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
set scale(value: number) {
|
|
142
|
+
this._scalex = value;
|
|
143
|
+
this._scaley = value;
|
|
144
|
+
|
|
145
|
+
this._scaleh = value * this._fliph;
|
|
146
|
+
this._scalev = value * this._flipv;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
get fliph(): boolean {
|
|
150
|
+
return this._fliph < 0;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
set fliph(value: boolean) {
|
|
154
|
+
if (value) {
|
|
155
|
+
this._fliph = -1;
|
|
156
|
+
this._scaleh = -this._scalex;
|
|
157
|
+
} else {
|
|
158
|
+
this._fliph = 1;
|
|
159
|
+
this._scaleh = this._scalex;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
get flipv(): boolean {
|
|
164
|
+
return this._flipv < 0;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
set flipv(value: boolean) {
|
|
168
|
+
if (value) {
|
|
169
|
+
this._flipv = -1;
|
|
170
|
+
this._scalev = -this._scaley;
|
|
171
|
+
} else {
|
|
172
|
+
this._flipv = 1;
|
|
173
|
+
this._scalev = this._scaley;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
get flip(): boolean {
|
|
178
|
+
return this._fliph < 0;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
set flip(value: boolean) {
|
|
182
|
+
if (value) {
|
|
183
|
+
this._fliph = -1;
|
|
184
|
+
this._flipv = -1;
|
|
185
|
+
|
|
186
|
+
this._scaleh = -this._scalex;
|
|
187
|
+
this._scalev = -this._scaley;
|
|
188
|
+
} else {
|
|
189
|
+
this._fliph = 1;
|
|
190
|
+
this._flipv = 1;
|
|
191
|
+
|
|
192
|
+
this._scaleh = this._scalex;
|
|
193
|
+
this._scalev = this._scaley;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
get opacity(): number {
|
|
198
|
+
return this._opacity;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
set opacity(value: number) {
|
|
202
|
+
this._opacity = clamp(value, 0, 1) || 0;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
get hidden(): boolean {
|
|
206
|
+
return this._hidden;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
set hidden(value: boolean) {
|
|
210
|
+
this._hidden = value;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
get blendMode(): BlendMode {
|
|
214
|
+
return this._blendMode;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
set blendMode(value: BlendMode) {
|
|
218
|
+
this._blendMode = value;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
override get left(): number {
|
|
222
|
+
return super.left;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
override set left(value: number) {
|
|
226
|
+
this._move("left", value);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
override get right(): number {
|
|
230
|
+
return super.right;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
override set right(value: number) {
|
|
234
|
+
this._move("right", value);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
override get centerx(): number {
|
|
238
|
+
return super.centerx;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
override set centerx(value: number) {
|
|
242
|
+
this._move("centerx", value);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
override get top(): number {
|
|
246
|
+
return super.top;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
override set top(value: number) {
|
|
250
|
+
this._move("top", value);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
override get bottom(): number {
|
|
254
|
+
return super.bottom;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
override set bottom(value: number) {
|
|
258
|
+
this._move("bottom", value);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
override get centery(): number {
|
|
262
|
+
return super.centery;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
override set centery(value: number) {
|
|
266
|
+
this._move("centery", value);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
override get width(): number {
|
|
270
|
+
return super.width;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
override set width(value: number) {
|
|
274
|
+
this._resize("width", value);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
override get halfWidth(): number {
|
|
278
|
+
return super.halfWidth;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
override set halfWidth(value: number) {
|
|
282
|
+
this._resize("halfWidth", value);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
override get height(): number {
|
|
286
|
+
return super.height;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
override set height(value: number) {
|
|
290
|
+
this._resize("height", value);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
override get halfHeight(): number {
|
|
294
|
+
return super.halfHeight;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
override set halfHeight(value: number) {
|
|
298
|
+
this._resize("halfHeight", value);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
override get radius(): number {
|
|
302
|
+
return super.radius;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
override set radius(value: number) {
|
|
306
|
+
this._resize("radius", value);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
constructor(options?: Partial<BaseSpriteOptions>) {
|
|
310
|
+
super();
|
|
311
|
+
|
|
312
|
+
if (options !== undefined) {
|
|
313
|
+
Object.assign(
|
|
314
|
+
this,
|
|
315
|
+
pick(options, [
|
|
316
|
+
"left",
|
|
317
|
+
"right",
|
|
318
|
+
"centerx",
|
|
319
|
+
"top",
|
|
320
|
+
"bottom",
|
|
321
|
+
"centery",
|
|
322
|
+
"width",
|
|
323
|
+
"halfWidth",
|
|
324
|
+
"height",
|
|
325
|
+
"halfHeight",
|
|
326
|
+
"radius",
|
|
327
|
+
"transformOrigin",
|
|
328
|
+
"transformOriginx",
|
|
329
|
+
"transformOriginy",
|
|
330
|
+
"angle",
|
|
331
|
+
"scale",
|
|
332
|
+
"scalex",
|
|
333
|
+
"scaley",
|
|
334
|
+
"flip",
|
|
335
|
+
"fliph",
|
|
336
|
+
"flipv",
|
|
337
|
+
"opacity",
|
|
338
|
+
"hidden",
|
|
339
|
+
"blendMode",
|
|
340
|
+
]),
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
this.teleport();
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Public functions
|
|
348
|
+
|
|
349
|
+
drawFirst(): this {
|
|
350
|
+
const parent = this.parent;
|
|
351
|
+
|
|
352
|
+
if (parent !== undefined) {
|
|
353
|
+
const parent_layers = parent._layers;
|
|
354
|
+
|
|
355
|
+
const index = parent_layers.indexOf(this);
|
|
356
|
+
if (index >= 0) {
|
|
357
|
+
// Step 1: Remove myself from the parent layers
|
|
358
|
+
parent_layers.splice(index, 1);
|
|
359
|
+
|
|
360
|
+
// Step 2: Insert myself
|
|
361
|
+
parent_layers.unshift(this);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return this;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
drawLast(): this {
|
|
369
|
+
const parent = this.parent;
|
|
370
|
+
|
|
371
|
+
if (parent !== undefined) {
|
|
372
|
+
const parent_layers = parent._layers;
|
|
373
|
+
|
|
374
|
+
const index = parent_layers.indexOf(this);
|
|
375
|
+
if (index >= 0) {
|
|
376
|
+
// Step 1: Remove myself from the parent layers
|
|
377
|
+
parent_layers.splice(index, 1);
|
|
378
|
+
|
|
379
|
+
// Step 2: Insert myself
|
|
380
|
+
parent_layers.push(this);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return this;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
getDrawIndex(): number {
|
|
388
|
+
const parent = this.parent;
|
|
389
|
+
|
|
390
|
+
if (parent !== undefined) {
|
|
391
|
+
return parent._layers.indexOf(this);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return -1;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
drawTo(index: number): this {
|
|
398
|
+
const parent = this.parent;
|
|
399
|
+
|
|
400
|
+
if (parent !== undefined) {
|
|
401
|
+
const parent_layers = parent._layers;
|
|
402
|
+
|
|
403
|
+
const i = parent_layers.indexOf(this);
|
|
404
|
+
if (i >= 0) {
|
|
405
|
+
// Step 1: Remove myself from the parent layers
|
|
406
|
+
parent_layers.splice(i, 1);
|
|
407
|
+
|
|
408
|
+
// Step 2: Insert myself
|
|
409
|
+
parent_layers.splice(
|
|
410
|
+
clamp(Math.round(index) || 0, 0, parent_layers.length),
|
|
411
|
+
0,
|
|
412
|
+
this,
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return this;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
drawBefore(sprite: BaseSprite): this {
|
|
421
|
+
const parent = this.parent;
|
|
422
|
+
|
|
423
|
+
if (parent !== undefined) {
|
|
424
|
+
const parent_layers = parent._layers;
|
|
425
|
+
const sprite_index = parent_layers.indexOf(sprite);
|
|
426
|
+
|
|
427
|
+
if (sprite_index >= 0) {
|
|
428
|
+
const index = parent_layers.indexOf(this);
|
|
429
|
+
if (index >= 0) {
|
|
430
|
+
// Step 1: Remove myself from the parent layers
|
|
431
|
+
parent_layers.splice(index, 1);
|
|
432
|
+
|
|
433
|
+
// Step 2: Insert myself
|
|
434
|
+
parent_layers.splice(sprite_index, 0, this);
|
|
435
|
+
}
|
|
436
|
+
} else if (typeof console !== "undefined") {
|
|
437
|
+
console.error("Sprite not found in the same sprite group");
|
|
438
|
+
console.trace();
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
return this;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
drawAfter(sprite: BaseSprite): this {
|
|
446
|
+
const parent = this.parent;
|
|
447
|
+
|
|
448
|
+
if (parent !== undefined) {
|
|
449
|
+
const parent_layers = parent._layers;
|
|
450
|
+
const sprite_index = parent_layers.indexOf(sprite);
|
|
451
|
+
|
|
452
|
+
if (sprite_index >= 0) {
|
|
453
|
+
const index = parent_layers.indexOf(this);
|
|
454
|
+
if (index >= 0) {
|
|
455
|
+
// Step 1: Remove myself from the parent layers
|
|
456
|
+
parent_layers.splice(index, 1);
|
|
457
|
+
|
|
458
|
+
// Step 2: Insert myself
|
|
459
|
+
parent_layers.splice(sprite_index + 1, 0, this);
|
|
460
|
+
}
|
|
461
|
+
} else if (typeof console !== "undefined") {
|
|
462
|
+
console.error("Sprite not found in the same sprite group");
|
|
463
|
+
console.trace();
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
return this;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
getAbsRect(absRect?: Rect): Rect {
|
|
471
|
+
let left = this.left;
|
|
472
|
+
let top = this.top;
|
|
473
|
+
let parent = this.parent;
|
|
474
|
+
while (parent !== undefined) {
|
|
475
|
+
left += parent.left;
|
|
476
|
+
top += parent.top;
|
|
477
|
+
parent = parent.parent;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if (absRect !== undefined) {
|
|
481
|
+
absRect.left = left;
|
|
482
|
+
absRect.top = top;
|
|
483
|
+
absRect.width = this.width;
|
|
484
|
+
absRect.height = this.height;
|
|
485
|
+
|
|
486
|
+
return absRect;
|
|
487
|
+
} else {
|
|
488
|
+
return new Rect({
|
|
489
|
+
left: left,
|
|
490
|
+
top: top,
|
|
491
|
+
width: this.width,
|
|
492
|
+
height: this.height,
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
teleport(): this {
|
|
498
|
+
const playground = this.playground;
|
|
499
|
+
|
|
500
|
+
this._prevLeft = this._left;
|
|
501
|
+
this._prevTop = this._top;
|
|
502
|
+
|
|
503
|
+
if (playground !== undefined) {
|
|
504
|
+
this._frameCounterLastMove = playground.frameCounter;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
return this;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
wrapLeft(increment: number, modulo: number): this {
|
|
511
|
+
this.left += increment;
|
|
512
|
+
|
|
513
|
+
if (modulo > 0) {
|
|
514
|
+
while (this._left > 0) {
|
|
515
|
+
this._left -= modulo;
|
|
516
|
+
this._prevLeft -= modulo;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
while (this._left <= -modulo) {
|
|
520
|
+
this._left += modulo;
|
|
521
|
+
this._prevLeft += modulo;
|
|
522
|
+
}
|
|
523
|
+
} else if (modulo < 0) {
|
|
524
|
+
while (this._left > 0) {
|
|
525
|
+
this._left += modulo;
|
|
526
|
+
this._prevLeft += modulo;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
while (this._left <= modulo) {
|
|
530
|
+
this._left -= modulo;
|
|
531
|
+
this._prevLeft -= modulo;
|
|
532
|
+
}
|
|
533
|
+
} else {
|
|
534
|
+
this._left = 0;
|
|
535
|
+
this._prevLeft = 0;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
return this;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
wrapTop(increment: number, modulo: number): this {
|
|
542
|
+
this.top += increment;
|
|
543
|
+
|
|
544
|
+
if (modulo > 0) {
|
|
545
|
+
while (this._top > 0) {
|
|
546
|
+
this._top -= modulo;
|
|
547
|
+
this._prevTop -= modulo;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
while (this._top <= -modulo) {
|
|
551
|
+
this._top += modulo;
|
|
552
|
+
this._prevTop += modulo;
|
|
553
|
+
}
|
|
554
|
+
} else if (modulo < 0) {
|
|
555
|
+
while (this._top > 0) {
|
|
556
|
+
this._top += modulo;
|
|
557
|
+
this._prevTop += modulo;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
while (this._top <= modulo) {
|
|
561
|
+
this._top -= modulo;
|
|
562
|
+
this._prevTop -= modulo;
|
|
563
|
+
}
|
|
564
|
+
} else {
|
|
565
|
+
this._top = 0;
|
|
566
|
+
this._prevTop = 0;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
return this;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// Implementation details
|
|
573
|
+
|
|
574
|
+
_resize(prop: keyof RectSize, value: number): void {
|
|
575
|
+
const left = this._left;
|
|
576
|
+
const top = this._top;
|
|
577
|
+
const prevLeft = this._prevLeft;
|
|
578
|
+
const prevTop = this._prevTop;
|
|
579
|
+
const frameCounterLastMove = this._frameCounterLastMove;
|
|
580
|
+
|
|
581
|
+
super[prop] = value;
|
|
582
|
+
|
|
583
|
+
// Resizing the sprite does not mean that it should change its position.
|
|
584
|
+
// However, the new dimensions must be taken into consideration for the interpolation
|
|
585
|
+
this._prevLeft = prevLeft + (this._left - left);
|
|
586
|
+
this._prevTop = prevTop + (this._top - top);
|
|
587
|
+
this._frameCounterLastMove = frameCounterLastMove;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
_move(prop: keyof RectPos, value: number): void {
|
|
591
|
+
const playground = this.playground;
|
|
592
|
+
|
|
593
|
+
if (playground !== undefined) {
|
|
594
|
+
const frameCounter = playground.frameCounter;
|
|
595
|
+
|
|
596
|
+
if (frameCounter !== this._frameCounterLastMove) {
|
|
597
|
+
this._prevLeft = this._left;
|
|
598
|
+
this._prevTop = this._top;
|
|
599
|
+
this._frameCounterLastMove = frameCounter;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
super[prop] = value;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
_checkUpdate(): void {
|
|
607
|
+
const oldNeedsUpdate = this._needsUpdate;
|
|
608
|
+
|
|
609
|
+
this._needsUpdate = false;
|
|
610
|
+
|
|
611
|
+
this._updateNeedsUpdate(oldNeedsUpdate);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
_updateNeedsUpdate(oldNeedsUpdate: boolean): void {
|
|
615
|
+
const parent = this.parent;
|
|
616
|
+
|
|
617
|
+
if (parent !== undefined) {
|
|
618
|
+
const parent_update_list = parent._updateList;
|
|
619
|
+
|
|
620
|
+
if (this._needsUpdate && !oldNeedsUpdate) {
|
|
621
|
+
const index = parent_update_list.indexOf(this);
|
|
622
|
+
if (index < 0) {
|
|
623
|
+
parent_update_list.push(this);
|
|
624
|
+
}
|
|
625
|
+
} else if (!this._needsUpdate && oldNeedsUpdate) {
|
|
626
|
+
const index = parent_update_list.indexOf(this);
|
|
627
|
+
if (index >= 0) {
|
|
628
|
+
parent_update_list.splice(index, 1);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
_onReparent(): void {
|
|
635
|
+
// no-op
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
_initRenderer(): void {
|
|
639
|
+
// no-op
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
_update(): void {
|
|
643
|
+
// no-op
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
_draw(interp: number): void {
|
|
647
|
+
const playground = this.playground;
|
|
648
|
+
|
|
649
|
+
if (playground !== undefined) {
|
|
650
|
+
const trunc = Math.trunc;
|
|
651
|
+
const myLeft = this._left;
|
|
652
|
+
const myTop = this._top;
|
|
653
|
+
const prevLeft = this._prevLeft;
|
|
654
|
+
const prevTop = this._prevTop;
|
|
655
|
+
const truncPrevLeft = trunc(prevLeft);
|
|
656
|
+
const truncPrevTop = trunc(prevTop);
|
|
657
|
+
let left = trunc(myLeft);
|
|
658
|
+
let top = trunc(myTop);
|
|
659
|
+
|
|
660
|
+
if (left !== truncPrevLeft || top !== truncPrevTop) {
|
|
661
|
+
if (this._frameCounterLastMove === playground.frameCounter - 1) {
|
|
662
|
+
const round = Math.round;
|
|
663
|
+
|
|
664
|
+
left = round(lerp(truncPrevLeft, left, interp));
|
|
665
|
+
top = round(lerp(truncPrevTop, top, interp));
|
|
666
|
+
} else {
|
|
667
|
+
this._prevLeft = myLeft;
|
|
668
|
+
this._prevTop = myTop;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
this._drawLeft = left;
|
|
673
|
+
this._drawTop = top;
|
|
674
|
+
this._drawWidth = trunc(this._width);
|
|
675
|
+
this._drawHeight = trunc(this._height);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
_remove(): void {
|
|
680
|
+
// no-op
|
|
681
|
+
}
|
|
682
|
+
}
|