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
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { SpriteGroup } from "./SpriteGroup.js";
|
|
2
|
+
export class Playground extends EventTarget {
|
|
3
|
+
_width = 0;
|
|
4
|
+
_height = 0;
|
|
5
|
+
_refreshRate = 1000 / 60;
|
|
6
|
+
running = false;
|
|
7
|
+
frameCounter = 0;
|
|
8
|
+
scenegraph;
|
|
9
|
+
// Implementation details
|
|
10
|
+
_renderer;
|
|
11
|
+
_nextId = 1; // Start from 1 to guarantee that callbackId is always truthy
|
|
12
|
+
_callbacks = new Map();
|
|
13
|
+
_idDraw = null;
|
|
14
|
+
_accumulator = 0;
|
|
15
|
+
_currentTime = 0;
|
|
16
|
+
_absLeft = 0;
|
|
17
|
+
_absTop = 0;
|
|
18
|
+
get width() {
|
|
19
|
+
return this._width;
|
|
20
|
+
}
|
|
21
|
+
get height() {
|
|
22
|
+
return this._height;
|
|
23
|
+
}
|
|
24
|
+
get halfWidth() {
|
|
25
|
+
return this._width / 2;
|
|
26
|
+
}
|
|
27
|
+
get halfHeight() {
|
|
28
|
+
return this._height / 2;
|
|
29
|
+
}
|
|
30
|
+
get refreshRate() {
|
|
31
|
+
return this._refreshRate;
|
|
32
|
+
}
|
|
33
|
+
constructor(renderer, options) {
|
|
34
|
+
super();
|
|
35
|
+
this._renderer = renderer;
|
|
36
|
+
const [width, height] = renderer.initPlayground(this, options);
|
|
37
|
+
this._width = width;
|
|
38
|
+
this._height = height;
|
|
39
|
+
if (options?.refreshRate !== undefined) {
|
|
40
|
+
this._refreshRate = Number(options.refreshRate) || 1;
|
|
41
|
+
}
|
|
42
|
+
this.scenegraph = new SpriteGroup({ width, height });
|
|
43
|
+
this.scenegraph._playground = new WeakRef(this);
|
|
44
|
+
this.scenegraph._initRenderer();
|
|
45
|
+
this.startGame();
|
|
46
|
+
}
|
|
47
|
+
// Public functions
|
|
48
|
+
registerCallback(callback, rate) {
|
|
49
|
+
const callbackId = this._nextId;
|
|
50
|
+
this._nextId += 1;
|
|
51
|
+
this._callbacks.set(callbackId, {
|
|
52
|
+
callback: callback,
|
|
53
|
+
rate: this.framesFromMs(rate || 0),
|
|
54
|
+
idleCounter: 0,
|
|
55
|
+
});
|
|
56
|
+
return callbackId;
|
|
57
|
+
}
|
|
58
|
+
removeCallback(callbackId, options) {
|
|
59
|
+
if (callbackId === null) {
|
|
60
|
+
if (typeof console !== "undefined" &&
|
|
61
|
+
options?.suppressWarning === false) {
|
|
62
|
+
console.warn("callbackId is null");
|
|
63
|
+
console.trace();
|
|
64
|
+
}
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
const deleted = this._callbacks.delete(callbackId);
|
|
68
|
+
if (!deleted &&
|
|
69
|
+
typeof console !== "undefined" &&
|
|
70
|
+
options?.suppressWarning === false) {
|
|
71
|
+
console.warn("No callbacks removed");
|
|
72
|
+
console.trace();
|
|
73
|
+
}
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
clearCallbacks() {
|
|
77
|
+
this._callbacks.clear();
|
|
78
|
+
this.dispatchEvent(new Event("clearCallbacks"));
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
startGame() {
|
|
82
|
+
this.running = true;
|
|
83
|
+
if (this._idDraw === null) {
|
|
84
|
+
this._accumulator = 0;
|
|
85
|
+
this._currentTime = performance.now();
|
|
86
|
+
this._idDraw = requestAnimationFrame(this._draw);
|
|
87
|
+
}
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
stopGame() {
|
|
91
|
+
this.running = false;
|
|
92
|
+
// The cancelAnimationFrame is optional, since this._draw will not call
|
|
93
|
+
// this._update and requestAnimationFrame if this.running is false
|
|
94
|
+
if (this._idDraw !== null) {
|
|
95
|
+
cancelAnimationFrame(this._idDraw);
|
|
96
|
+
this._idDraw = null;
|
|
97
|
+
}
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
forceRedraw() {
|
|
101
|
+
if (this._idDraw === null) {
|
|
102
|
+
this._idDraw = requestAnimationFrame(this._draw);
|
|
103
|
+
}
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
framesFromMs(ms) {
|
|
107
|
+
return Math.max(Math.round(ms / this._refreshRate), 1) || 1;
|
|
108
|
+
}
|
|
109
|
+
msFromFrames(frames) {
|
|
110
|
+
return frames * this._refreshRate || 0;
|
|
111
|
+
}
|
|
112
|
+
// Implementation details
|
|
113
|
+
_update() {
|
|
114
|
+
this.scenegraph._update();
|
|
115
|
+
for (const callback_obj of this._callbacks.values()) {
|
|
116
|
+
callback_obj.idleCounter += 1;
|
|
117
|
+
if (callback_obj.idleCounter >= callback_obj.rate) {
|
|
118
|
+
callback_obj.idleCounter = 0;
|
|
119
|
+
const callback = callback_obj.callback;
|
|
120
|
+
callback();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
_draw = (timestamp) => {
|
|
125
|
+
const dt = this._refreshRate;
|
|
126
|
+
let accumulator = this._accumulator;
|
|
127
|
+
if (this.running) {
|
|
128
|
+
this._idDraw = requestAnimationFrame(this._draw);
|
|
129
|
+
accumulator += timestamp - this._currentTime;
|
|
130
|
+
this._currentTime = timestamp;
|
|
131
|
+
if (accumulator >= dt) {
|
|
132
|
+
let numUpdateSteps = 0;
|
|
133
|
+
while (accumulator >= dt) {
|
|
134
|
+
this._update();
|
|
135
|
+
accumulator -= dt;
|
|
136
|
+
this.frameCounter += 1;
|
|
137
|
+
// Avoid the spiral of death
|
|
138
|
+
numUpdateSteps += 1;
|
|
139
|
+
if (numUpdateSteps >= 240) {
|
|
140
|
+
accumulator = 0;
|
|
141
|
+
// TODO: Maybe a callback should be called here
|
|
142
|
+
if (typeof console !== "undefined") {
|
|
143
|
+
console.warn("Spiral of death");
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
this._accumulator = accumulator;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
this._idDraw = null;
|
|
153
|
+
}
|
|
154
|
+
const interp = accumulator / dt;
|
|
155
|
+
this._renderer.drawPlaygroundBeforeChildren(this, interp);
|
|
156
|
+
this.scenegraph._draw(interp);
|
|
157
|
+
this._renderer.drawPlaygroundAfterChildren(this, interp);
|
|
158
|
+
};
|
|
159
|
+
_insidePlayground(left, top, width, height) {
|
|
160
|
+
const sprite_left = this._absLeft + left;
|
|
161
|
+
const sprite_top = this._absTop + top;
|
|
162
|
+
return !(sprite_top + height <= 0 ||
|
|
163
|
+
sprite_top >= this._height ||
|
|
164
|
+
sprite_left >= this._width ||
|
|
165
|
+
sprite_left + width <= 0);
|
|
166
|
+
}
|
|
167
|
+
}
|
package/dist/Rect.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export interface RectPosX {
|
|
2
|
+
left: number;
|
|
3
|
+
right: number;
|
|
4
|
+
centerx: number;
|
|
5
|
+
}
|
|
6
|
+
export interface RectPosY {
|
|
7
|
+
top: number;
|
|
8
|
+
bottom: number;
|
|
9
|
+
centery: number;
|
|
10
|
+
}
|
|
11
|
+
export interface RectPos extends RectPosX, RectPosY {
|
|
12
|
+
}
|
|
13
|
+
export interface RectSizeX {
|
|
14
|
+
width: number;
|
|
15
|
+
halfWidth: number;
|
|
16
|
+
radius: number;
|
|
17
|
+
}
|
|
18
|
+
export interface RectSizeY {
|
|
19
|
+
height: number;
|
|
20
|
+
halfHeight: number;
|
|
21
|
+
radius: number;
|
|
22
|
+
}
|
|
23
|
+
export interface RectSize extends RectSizeX, RectSizeY {
|
|
24
|
+
}
|
|
25
|
+
export interface RectOptions extends RectPos, RectSize {
|
|
26
|
+
}
|
|
27
|
+
export declare class Rect implements RectOptions {
|
|
28
|
+
_left: number;
|
|
29
|
+
_top: number;
|
|
30
|
+
_width: number;
|
|
31
|
+
_height: number;
|
|
32
|
+
_last_x: keyof RectPosX;
|
|
33
|
+
_last_y: keyof RectPosY;
|
|
34
|
+
get left(): number;
|
|
35
|
+
set left(value: number);
|
|
36
|
+
get right(): number;
|
|
37
|
+
set right(value: number);
|
|
38
|
+
get centerx(): number;
|
|
39
|
+
set centerx(value: number);
|
|
40
|
+
get top(): number;
|
|
41
|
+
set top(value: number);
|
|
42
|
+
get bottom(): number;
|
|
43
|
+
set bottom(value: number);
|
|
44
|
+
get centery(): number;
|
|
45
|
+
set centery(value: number);
|
|
46
|
+
get width(): number;
|
|
47
|
+
set width(value: number);
|
|
48
|
+
get halfWidth(): number;
|
|
49
|
+
set halfWidth(value: number);
|
|
50
|
+
get height(): number;
|
|
51
|
+
set height(value: number);
|
|
52
|
+
get halfHeight(): number;
|
|
53
|
+
set halfHeight(value: number);
|
|
54
|
+
get radius(): number;
|
|
55
|
+
set radius(value: number);
|
|
56
|
+
constructor(options?: Partial<RectOptions>);
|
|
57
|
+
collideRect(otherRect: Rect): boolean;
|
|
58
|
+
collideRectPoint(x: number, y: number): boolean;
|
|
59
|
+
collideRectCircle(otherRect: Rect): boolean;
|
|
60
|
+
collideCircle(otherRect: Rect): boolean;
|
|
61
|
+
collideCirclePoint(x: number, y: number): boolean;
|
|
62
|
+
collideCircleRect(otherRect: Rect): boolean;
|
|
63
|
+
}
|
package/dist/Rect.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { clamp, pick } from "./utils.js";
|
|
2
|
+
export class Rect {
|
|
3
|
+
// Implementation details
|
|
4
|
+
_left = 0;
|
|
5
|
+
_top = 0;
|
|
6
|
+
_width = 0;
|
|
7
|
+
_height = 0;
|
|
8
|
+
_last_x = "left";
|
|
9
|
+
_last_y = "top";
|
|
10
|
+
get left() {
|
|
11
|
+
return this._left;
|
|
12
|
+
}
|
|
13
|
+
set left(value) {
|
|
14
|
+
this._left = value;
|
|
15
|
+
this._last_x = "left";
|
|
16
|
+
}
|
|
17
|
+
get right() {
|
|
18
|
+
return this._left + this._width;
|
|
19
|
+
}
|
|
20
|
+
set right(value) {
|
|
21
|
+
this._left = value - this._width;
|
|
22
|
+
this._last_x = "right";
|
|
23
|
+
}
|
|
24
|
+
get centerx() {
|
|
25
|
+
return this._left + this._width / 2;
|
|
26
|
+
}
|
|
27
|
+
set centerx(value) {
|
|
28
|
+
this._left = value - this._width / 2;
|
|
29
|
+
this._last_x = "centerx";
|
|
30
|
+
}
|
|
31
|
+
get top() {
|
|
32
|
+
return this._top;
|
|
33
|
+
}
|
|
34
|
+
set top(value) {
|
|
35
|
+
this._top = value;
|
|
36
|
+
this._last_y = "top";
|
|
37
|
+
}
|
|
38
|
+
get bottom() {
|
|
39
|
+
return this._top + this._height;
|
|
40
|
+
}
|
|
41
|
+
set bottom(value) {
|
|
42
|
+
this._top = value - this._height;
|
|
43
|
+
this._last_y = "bottom";
|
|
44
|
+
}
|
|
45
|
+
get centery() {
|
|
46
|
+
return this._top + this._height / 2;
|
|
47
|
+
}
|
|
48
|
+
set centery(value) {
|
|
49
|
+
this._top = value - this._height / 2;
|
|
50
|
+
this._last_y = "centery";
|
|
51
|
+
}
|
|
52
|
+
get width() {
|
|
53
|
+
return this._width;
|
|
54
|
+
}
|
|
55
|
+
set width(value) {
|
|
56
|
+
const old_x = this[this._last_x];
|
|
57
|
+
this._width = value;
|
|
58
|
+
this[this._last_x] = old_x;
|
|
59
|
+
}
|
|
60
|
+
get halfWidth() {
|
|
61
|
+
return this._width / 2;
|
|
62
|
+
}
|
|
63
|
+
set halfWidth(value) {
|
|
64
|
+
const old_x = this[this._last_x];
|
|
65
|
+
this._width = value * 2;
|
|
66
|
+
this[this._last_x] = old_x;
|
|
67
|
+
}
|
|
68
|
+
get height() {
|
|
69
|
+
return this._height;
|
|
70
|
+
}
|
|
71
|
+
set height(value) {
|
|
72
|
+
const old_y = this[this._last_y];
|
|
73
|
+
this._height = value;
|
|
74
|
+
this[this._last_y] = old_y;
|
|
75
|
+
}
|
|
76
|
+
get halfHeight() {
|
|
77
|
+
return this._height / 2;
|
|
78
|
+
}
|
|
79
|
+
set halfHeight(value) {
|
|
80
|
+
const old_y = this[this._last_y];
|
|
81
|
+
this._height = value * 2;
|
|
82
|
+
this[this._last_y] = old_y;
|
|
83
|
+
}
|
|
84
|
+
get radius() {
|
|
85
|
+
return this._width / 2;
|
|
86
|
+
}
|
|
87
|
+
set radius(value) {
|
|
88
|
+
const old_x = this[this._last_x];
|
|
89
|
+
const old_y = this[this._last_y];
|
|
90
|
+
const new_value = value * 2;
|
|
91
|
+
this._width = new_value;
|
|
92
|
+
this._height = new_value;
|
|
93
|
+
this[this._last_x] = old_x;
|
|
94
|
+
this[this._last_y] = old_y;
|
|
95
|
+
}
|
|
96
|
+
constructor(options) {
|
|
97
|
+
if (options !== undefined) {
|
|
98
|
+
Object.assign(this, pick(options, [
|
|
99
|
+
"left",
|
|
100
|
+
"right",
|
|
101
|
+
"centerx",
|
|
102
|
+
"top",
|
|
103
|
+
"bottom",
|
|
104
|
+
"centery",
|
|
105
|
+
"width",
|
|
106
|
+
"halfWidth",
|
|
107
|
+
"height",
|
|
108
|
+
"halfHeight",
|
|
109
|
+
"radius",
|
|
110
|
+
]));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// Collision detection
|
|
114
|
+
collideRect(otherRect) {
|
|
115
|
+
return !(this.bottom <= otherRect.top ||
|
|
116
|
+
this.top >= otherRect.bottom ||
|
|
117
|
+
this.left >= otherRect.right ||
|
|
118
|
+
this.right <= otherRect.left);
|
|
119
|
+
}
|
|
120
|
+
collideRectPoint(x, y) {
|
|
121
|
+
return x >= this.left && x < this.right && y >= this.top && y < this.bottom;
|
|
122
|
+
}
|
|
123
|
+
collideRectCircle(otherRect) {
|
|
124
|
+
const centerx = otherRect.centerx;
|
|
125
|
+
const centery = otherRect.centery;
|
|
126
|
+
const radius = otherRect.radius;
|
|
127
|
+
const nearest_x = clamp(centerx, this.left, this.right);
|
|
128
|
+
const nearest_y = clamp(centery, this.top, this.bottom);
|
|
129
|
+
const dx = centerx - nearest_x;
|
|
130
|
+
const dy = centery - nearest_y;
|
|
131
|
+
return dx * dx + dy * dy < radius * radius;
|
|
132
|
+
}
|
|
133
|
+
collideCircle(otherRect) {
|
|
134
|
+
const dx = otherRect.centerx - this.centerx;
|
|
135
|
+
const dy = otherRect.centery - this.centery;
|
|
136
|
+
const radii = this.radius + otherRect.radius;
|
|
137
|
+
return dx * dx + dy * dy < radii * radii;
|
|
138
|
+
}
|
|
139
|
+
collideCirclePoint(x, y) {
|
|
140
|
+
const dx = x - this.centerx;
|
|
141
|
+
const dy = y - this.centery;
|
|
142
|
+
const radius = this.radius;
|
|
143
|
+
return dx * dx + dy * dy < radius * radius;
|
|
144
|
+
}
|
|
145
|
+
collideCircleRect(otherRect) {
|
|
146
|
+
const centerx = this.centerx;
|
|
147
|
+
const centery = this.centery;
|
|
148
|
+
const radius = this.radius;
|
|
149
|
+
const nearest_x = clamp(centerx, otherRect.left, otherRect.right);
|
|
150
|
+
const nearest_y = clamp(centery, otherRect.top, otherRect.bottom);
|
|
151
|
+
const dx = centerx - nearest_x;
|
|
152
|
+
const dy = centery - nearest_y;
|
|
153
|
+
return dx * dx + dy * dy < radius * radius;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { BaseSpriteOptions } from "./BaseSprite.js";
|
|
2
|
+
import { BaseSprite } from "./BaseSprite.js";
|
|
3
|
+
import type { ColorArr, ColorObj } from "./Gradient.js";
|
|
4
|
+
import { Gradient } from "./Gradient.js";
|
|
5
|
+
import type { SpriteRef } from "./utils.js";
|
|
6
|
+
export interface RectangleOptions extends BaseSpriteOptions {
|
|
7
|
+
background: Gradient | Partial<ColorObj> | Partial<ColorArr> | null;
|
|
8
|
+
borderRadius: number;
|
|
9
|
+
borderWidth: number;
|
|
10
|
+
borderColor: Gradient | Partial<ColorObj> | Partial<ColorArr> | null;
|
|
11
|
+
}
|
|
12
|
+
export declare class Rectangle extends BaseSprite implements RectangleOptions {
|
|
13
|
+
_background: Gradient | null;
|
|
14
|
+
_borderRadius: number;
|
|
15
|
+
_borderWidth: number;
|
|
16
|
+
_borderColor: Gradient | null;
|
|
17
|
+
get background(): Gradient | null;
|
|
18
|
+
set background(value: Gradient | Partial<ColorObj> | Partial<ColorArr> | null);
|
|
19
|
+
get borderRadius(): number;
|
|
20
|
+
set borderRadius(value: number);
|
|
21
|
+
get borderWidth(): number;
|
|
22
|
+
set borderWidth(value: number);
|
|
23
|
+
get borderColor(): Gradient | null;
|
|
24
|
+
set borderColor(value: Gradient | Partial<ColorObj> | Partial<ColorArr> | null);
|
|
25
|
+
constructor(options?: Partial<RectangleOptions & {
|
|
26
|
+
ref?: SpriteRef<Rectangle>;
|
|
27
|
+
}>);
|
|
28
|
+
_initRenderer(): void;
|
|
29
|
+
_draw(interp: number): void;
|
|
30
|
+
_remove(): void;
|
|
31
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { BaseSprite } from "./BaseSprite.js";
|
|
2
|
+
import { Gradient } from "./Gradient.js";
|
|
3
|
+
import { pick } from "./utils.js";
|
|
4
|
+
export class Rectangle extends BaseSprite {
|
|
5
|
+
_background = null;
|
|
6
|
+
_borderRadius = 0;
|
|
7
|
+
_borderWidth = 1;
|
|
8
|
+
_borderColor = null;
|
|
9
|
+
get background() {
|
|
10
|
+
return this._background;
|
|
11
|
+
}
|
|
12
|
+
set background(value) {
|
|
13
|
+
if (value === null || value instanceof Gradient) {
|
|
14
|
+
this._background = value;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
this._background = new Gradient(value);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
get borderRadius() {
|
|
21
|
+
return this._borderRadius;
|
|
22
|
+
}
|
|
23
|
+
set borderRadius(value) {
|
|
24
|
+
this._borderRadius = value;
|
|
25
|
+
}
|
|
26
|
+
get borderWidth() {
|
|
27
|
+
return this._borderWidth;
|
|
28
|
+
}
|
|
29
|
+
set borderWidth(value) {
|
|
30
|
+
this._borderWidth = value;
|
|
31
|
+
}
|
|
32
|
+
get borderColor() {
|
|
33
|
+
return this._borderColor;
|
|
34
|
+
}
|
|
35
|
+
set borderColor(value) {
|
|
36
|
+
if (value === null || value instanceof Gradient) {
|
|
37
|
+
this._borderColor = value;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
this._borderColor = new Gradient(value);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
constructor(options) {
|
|
44
|
+
super(options);
|
|
45
|
+
if (options !== undefined) {
|
|
46
|
+
Object.assign(this, pick(options, [
|
|
47
|
+
"background",
|
|
48
|
+
"borderRadius",
|
|
49
|
+
"borderWidth",
|
|
50
|
+
"borderColor",
|
|
51
|
+
]));
|
|
52
|
+
if (options.ref !== undefined) {
|
|
53
|
+
options.ref.current = this;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
this.teleport();
|
|
57
|
+
}
|
|
58
|
+
// Implementation details
|
|
59
|
+
_initRenderer() {
|
|
60
|
+
this.playground?._renderer.initRectangle(this);
|
|
61
|
+
}
|
|
62
|
+
_draw(interp) {
|
|
63
|
+
super._draw(interp);
|
|
64
|
+
this.playground?._renderer.drawRectangle(this, interp);
|
|
65
|
+
}
|
|
66
|
+
_remove() {
|
|
67
|
+
this.playground?._renderer.removeRectangle(this);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
export interface RendererElement {
|
|
6
|
+
offsetWidth: number;
|
|
7
|
+
offsetHeight: number;
|
|
8
|
+
}
|
|
9
|
+
export interface Renderer {
|
|
10
|
+
initPlayground(playground: Playground, options?: Partial<PlaygroundOptions>): [number, number];
|
|
11
|
+
drawPlaygroundBeforeChildren(playground: Playground, interp: number): void;
|
|
12
|
+
drawPlaygroundAfterChildren(playground: Playground, interp: number): void;
|
|
13
|
+
initGroup(group: SpriteGroup): void;
|
|
14
|
+
removeGroup(group: SpriteGroup): void;
|
|
15
|
+
drawGroupBeforeChildren(group: SpriteGroup, interp: number): void;
|
|
16
|
+
drawGroupAfterChildren(group: SpriteGroup, interp: number): void;
|
|
17
|
+
initSprite(sprite: Sprite): void;
|
|
18
|
+
removeSprite(sprite: Sprite): void;
|
|
19
|
+
drawSprite(sprite: Sprite, interp: number): void;
|
|
20
|
+
initRectangle(rectangle: Rectangle): void;
|
|
21
|
+
removeRectangle(rectangle: Rectangle): void;
|
|
22
|
+
drawRectangle(rectangle: Rectangle, interp: number): void;
|
|
23
|
+
}
|
package/dist/Renderer.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface Resource {
|
|
2
|
+
complete: () => boolean;
|
|
3
|
+
onLoad: () => void;
|
|
4
|
+
}
|
|
5
|
+
export declare class ResourceManager {
|
|
6
|
+
loadCallback: ((percent: number) => void) | null;
|
|
7
|
+
_idPreload: number | null;
|
|
8
|
+
_preloadList: Resource[];
|
|
9
|
+
_resolveCallback: ((value?: undefined) => void) | null;
|
|
10
|
+
_rejectCallback: (() => void) | null;
|
|
11
|
+
_complete: boolean;
|
|
12
|
+
complete(): boolean;
|
|
13
|
+
percent(): number;
|
|
14
|
+
addResource<T extends Resource>(resource: T): T;
|
|
15
|
+
removeResource(resource: Resource | null, options?: {
|
|
16
|
+
suppressWarning?: boolean;
|
|
17
|
+
}): void;
|
|
18
|
+
clear(): void;
|
|
19
|
+
preload(): Promise<void>;
|
|
20
|
+
_doPreload: () => void;
|
|
21
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
export class ResourceManager {
|
|
2
|
+
loadCallback = null;
|
|
3
|
+
// Implementation details
|
|
4
|
+
_idPreload = null;
|
|
5
|
+
_preloadList = [];
|
|
6
|
+
_resolveCallback = null;
|
|
7
|
+
_rejectCallback = null;
|
|
8
|
+
_complete = true;
|
|
9
|
+
complete() {
|
|
10
|
+
return this._complete;
|
|
11
|
+
}
|
|
12
|
+
percent() {
|
|
13
|
+
const len_preload_list = this._preloadList.length;
|
|
14
|
+
if (len_preload_list === 0) {
|
|
15
|
+
return 1;
|
|
16
|
+
}
|
|
17
|
+
const completed = this._preloadList.reduce((accumulator, resource) => accumulator + Number(resource.complete()), 0);
|
|
18
|
+
return completed / len_preload_list;
|
|
19
|
+
}
|
|
20
|
+
addResource(resource) {
|
|
21
|
+
this._preloadList.push(resource);
|
|
22
|
+
return resource;
|
|
23
|
+
}
|
|
24
|
+
removeResource(resource, options) {
|
|
25
|
+
if (resource === null) {
|
|
26
|
+
if (typeof console !== "undefined" &&
|
|
27
|
+
options?.suppressWarning === false) {
|
|
28
|
+
console.warn("resource is null");
|
|
29
|
+
console.trace();
|
|
30
|
+
}
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const index = this._preloadList.indexOf(resource);
|
|
34
|
+
if (index >= 0) {
|
|
35
|
+
this._preloadList.splice(index, 1);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
if (typeof console !== "undefined" &&
|
|
39
|
+
options?.suppressWarning === false) {
|
|
40
|
+
console.warn("No resources removed");
|
|
41
|
+
console.trace();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
clear() {
|
|
46
|
+
this._preloadList.splice(0, this._preloadList.length);
|
|
47
|
+
}
|
|
48
|
+
preload() {
|
|
49
|
+
const reject_callback = this._rejectCallback;
|
|
50
|
+
this._resolveCallback = null;
|
|
51
|
+
this._rejectCallback = null;
|
|
52
|
+
reject_callback?.();
|
|
53
|
+
this._complete = false;
|
|
54
|
+
// Call this._doPreload() now, in order to have the resources initialize
|
|
55
|
+
// inside the function that called resourceManager.preload().
|
|
56
|
+
// This is useful for preloading sounds in mobile environments, for
|
|
57
|
+
// example, where the sounds will not load if audio.load() is not called in
|
|
58
|
+
// an user event handler such as mousedown.
|
|
59
|
+
this._doPreload();
|
|
60
|
+
return new Promise((resolve, reject) => {
|
|
61
|
+
if (this._complete) {
|
|
62
|
+
resolve();
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
this._resolveCallback = resolve;
|
|
66
|
+
this._rejectCallback = reject;
|
|
67
|
+
if (this._idPreload === null) {
|
|
68
|
+
this._idPreload = setInterval(this._doPreload, 100);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
// Implementation details
|
|
74
|
+
_doPreload = () => {
|
|
75
|
+
const completed = this._preloadList.reduce((accumulator, resource) => accumulator + Number(resource.complete()), 0);
|
|
76
|
+
const len_preload_list = this._preloadList.length;
|
|
77
|
+
const loadCallback = this.loadCallback;
|
|
78
|
+
if (completed === len_preload_list) {
|
|
79
|
+
this._complete = true;
|
|
80
|
+
if (this._idPreload !== null) {
|
|
81
|
+
clearInterval(this._idPreload);
|
|
82
|
+
this._idPreload = null;
|
|
83
|
+
}
|
|
84
|
+
for (const resource of this._preloadList) {
|
|
85
|
+
resource.onLoad();
|
|
86
|
+
}
|
|
87
|
+
this._preloadList.splice(0, len_preload_list);
|
|
88
|
+
const resolve_callback = this._resolveCallback;
|
|
89
|
+
// Set to null this._resolveCallback before calling resolve_callback
|
|
90
|
+
// in order to enable recursion
|
|
91
|
+
this._resolveCallback = null;
|
|
92
|
+
this._rejectCallback = null;
|
|
93
|
+
loadCallback?.(1);
|
|
94
|
+
resolve_callback?.();
|
|
95
|
+
}
|
|
96
|
+
else if (len_preload_list !== 0) {
|
|
97
|
+
loadCallback?.(completed / len_preload_list);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
package/dist/Sprite.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
export interface SpriteOptions extends BaseSpriteOptions, AnimationOptions {
|
|
6
|
+
animation: Animation | null;
|
|
7
|
+
animationIndex: number;
|
|
8
|
+
callback: (() => void) | null;
|
|
9
|
+
paused: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare class Sprite extends BaseSprite implements SpriteOptions {
|
|
12
|
+
_animation: Animation | null;
|
|
13
|
+
_animationIndex: number;
|
|
14
|
+
_callback: (() => void) | null;
|
|
15
|
+
_paused: boolean;
|
|
16
|
+
_rate: number;
|
|
17
|
+
_reportedRate: number;
|
|
18
|
+
_once: boolean;
|
|
19
|
+
_pingpong: boolean;
|
|
20
|
+
_backwards: boolean;
|
|
21
|
+
_idleCounter: number;
|
|
22
|
+
_lastSpriteSheet: number;
|
|
23
|
+
_currentSpriteSheet: number;
|
|
24
|
+
_numberOfFrame: number;
|
|
25
|
+
_currentFrame: number;
|
|
26
|
+
_frameIncrement: number;
|
|
27
|
+
_multix: number;
|
|
28
|
+
_multiy: number;
|
|
29
|
+
_endAnimation: boolean;
|
|
30
|
+
get animation(): Animation | null;
|
|
31
|
+
set animation(value: Animation | null);
|
|
32
|
+
get animationIndex(): number;
|
|
33
|
+
set animationIndex(value: number);
|
|
34
|
+
get callback(): (() => void) | null;
|
|
35
|
+
set callback(value: (() => void) | null);
|
|
36
|
+
get paused(): boolean;
|
|
37
|
+
set paused(value: boolean);
|
|
38
|
+
get rate(): number;
|
|
39
|
+
set rate(value: number);
|
|
40
|
+
get once(): boolean;
|
|
41
|
+
set once(value: boolean);
|
|
42
|
+
get pingpong(): boolean;
|
|
43
|
+
set pingpong(value: boolean);
|
|
44
|
+
get backwards(): boolean;
|
|
45
|
+
set backwards(value: boolean);
|
|
46
|
+
constructor(options?: Partial<SpriteOptions> & {
|
|
47
|
+
ref?: SpriteRef<Sprite>;
|
|
48
|
+
});
|
|
49
|
+
restartAnimation(): this;
|
|
50
|
+
_checkUpdate(): void;
|
|
51
|
+
_onReparent(): void;
|
|
52
|
+
_initRenderer(): void;
|
|
53
|
+
_update(): void;
|
|
54
|
+
_draw(interp: number): void;
|
|
55
|
+
_remove(): void;
|
|
56
|
+
}
|