frigame3 0.0.0-stage → 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.
Files changed (76) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +80 -2
  3. package/dist/Animation.d.ts +42 -0
  4. package/dist/Animation.js +189 -0
  5. package/dist/BaseSprite.d.ts +116 -0
  6. package/dist/BaseSprite.js +518 -0
  7. package/dist/Gradient.d.ts +31 -0
  8. package/dist/Gradient.js +89 -0
  9. package/dist/Playground.d.ts +48 -0
  10. package/dist/Playground.js +167 -0
  11. package/dist/Rect.d.ts +63 -0
  12. package/dist/Rect.js +155 -0
  13. package/dist/Rectangle.d.ts +31 -0
  14. package/dist/Rectangle.js +69 -0
  15. package/dist/Renderer.d.ts +23 -0
  16. package/dist/Renderer.js +1 -0
  17. package/dist/ResourceManager.d.ts +21 -0
  18. package/dist/ResourceManager.js +100 -0
  19. package/dist/Sprite.d.ts +56 -0
  20. package/dist/Sprite.js +437 -0
  21. package/dist/SpriteGroup.d.ts +39 -0
  22. package/dist/SpriteGroup.js +176 -0
  23. package/dist/canvasRenderer/renderGroup.d.ts +15 -0
  24. package/dist/canvasRenderer/renderGroup.js +134 -0
  25. package/dist/canvasRenderer/renderPlayground.d.ts +9 -0
  26. package/dist/canvasRenderer/renderPlayground.js +67 -0
  27. package/dist/canvasRenderer/renderRectangle.d.ts +15 -0
  28. package/dist/canvasRenderer/renderRectangle.js +287 -0
  29. package/dist/canvasRenderer/renderSprite.d.ts +4 -0
  30. package/dist/canvasRenderer/renderSprite.js +93 -0
  31. package/dist/canvasRenderer.d.ts +2 -0
  32. package/dist/canvasRenderer.js +19 -0
  33. package/dist/dummyRenderer.d.ts +2 -0
  34. package/dist/dummyRenderer.js +41 -0
  35. package/dist/plugins/GamepadTracker.d.ts +15 -0
  36. package/dist/plugins/GamepadTracker.js +74 -0
  37. package/dist/plugins/Tilemap.d.ts +26 -0
  38. package/dist/plugins/Tilemap.js +85 -0
  39. package/dist/plugins/Vec2.d.ts +69 -0
  40. package/dist/plugins/Vec2.js +296 -0
  41. package/dist/plugins/fx/Shaker.d.ts +46 -0
  42. package/dist/plugins/fx/Shaker.js +190 -0
  43. package/dist/plugins/fx/Tweener.d.ts +42 -0
  44. package/dist/plugins/fx/Tweener.js +166 -0
  45. package/dist/plugins/fx/easing.d.ts +32 -0
  46. package/dist/plugins/fx/easing.js +192 -0
  47. package/dist/plugins/isometric/ISOMixin.d.ts +138 -0
  48. package/dist/plugins/isometric/ISOMixin.js +322 -0
  49. package/dist/plugins/isometric/ISORectangle.d.ts +151 -0
  50. package/dist/plugins/isometric/ISORectangle.js +127 -0
  51. package/dist/plugins/isometric/ISOSprite.d.ts +157 -0
  52. package/dist/plugins/isometric/ISOSprite.js +176 -0
  53. package/dist/plugins/isometric/ISOSpriteGroup.d.ts +151 -0
  54. package/dist/plugins/isometric/ISOSpriteGroup.js +116 -0
  55. package/dist/plugins/isometric/ISOTilemap.d.ts +24 -0
  56. package/dist/plugins/isometric/ISOTilemap.js +85 -0
  57. package/dist/plugins/isometric/utils.d.ts +2 -0
  58. package/dist/plugins/isometric/utils.js +10 -0
  59. package/dist/plugins/keyTracker.d.ts +3 -0
  60. package/dist/plugins/keyTracker.js +23 -0
  61. package/dist/plugins/mixer.d.ts +72 -0
  62. package/dist/plugins/mixer.js +396 -0
  63. package/dist/plugins/sorted/SortedAnimation.d.ts +13 -0
  64. package/dist/plugins/sorted/SortedAnimation.js +27 -0
  65. package/dist/plugins/sorted/SortedGroup.d.ts +137 -0
  66. package/dist/plugins/sorted/SortedGroup.js +61 -0
  67. package/dist/plugins/sorted/SortedMixin.d.ts +122 -0
  68. package/dist/plugins/sorted/SortedMixin.js +153 -0
  69. package/dist/plugins/sorted/SortedRectangle.d.ts +127 -0
  70. package/dist/plugins/sorted/SortedRectangle.js +17 -0
  71. package/dist/plugins/sorted/SortedSprite.d.ts +130 -0
  72. package/dist/plugins/sorted/SortedSprite.js +35 -0
  73. package/dist/utils.d.ts +11 -0
  74. package/dist/utils.js +33 -0
  75. package/frigame.css +12 -0
  76. package/package.json +54 -4
@@ -0,0 +1,69 @@
1
+ export interface IVec2 {
2
+ x: number;
3
+ y: number;
4
+ }
5
+ export declare class Vec2 implements IVec2 {
6
+ x: number;
7
+ y: number;
8
+ constructor(x: number, y: number);
9
+ static clone(a: IVec2): Vec2;
10
+ clone(b: IVec2): this;
11
+ static fromValues(x: number, y: number): Vec2;
12
+ fromValues(x: number, y: number): this;
13
+ static fromPolar(mag: number, angle: number): Vec2;
14
+ fromPolar(mag: number, angle: number): this;
15
+ static randomUnit(): Vec2;
16
+ randomUnit(): this;
17
+ static random(scalar?: number): Vec2;
18
+ random(scalar?: number): this;
19
+ static add(a: IVec2, b: IVec2): Vec2;
20
+ add(b: IVec2): this;
21
+ static sub(a: IVec2, b: IVec2): Vec2;
22
+ sub(b: IVec2): this;
23
+ static scale(a: IVec2, sx: number, sy?: number): Vec2;
24
+ scale(sx: number, sy?: number): this;
25
+ static invert(a: IVec2): Vec2;
26
+ invert(): this;
27
+ static normalize(a: IVec2): Vec2;
28
+ normalize(): this;
29
+ static rotate(a: IVec2, angle: number): Vec2;
30
+ rotate(angle: number): this;
31
+ static rotateAroundPoint(a: IVec2, axisPoint: IVec2, angle: number): Vec2;
32
+ rotateAroundPoint(axisPoint: IVec2, angle: number): this;
33
+ static scaleAndRotate(a: IVec2, axisPoint: IVec2, angle: number, sx: number, sy?: number): Vec2;
34
+ scaleAndRotate(axisPoint: IVec2, angle: number, sx: number, sy?: number): this;
35
+ static lerp(a: IVec2, b: IVec2, t: number): Vec2;
36
+ lerp(b: IVec2, t: number): this;
37
+ static perp(a: IVec2): Vec2;
38
+ perp(): this;
39
+ static magnitude(a: IVec2): number;
40
+ magnitude(): number;
41
+ static squaredMagnitude(a: IVec2): number;
42
+ squaredMagnitude(): number;
43
+ static azimuth(a: IVec2): number;
44
+ azimuth(): number;
45
+ static dot(a: IVec2, b: IVec2): number;
46
+ dot(b: IVec2): number;
47
+ static cross(a: IVec2, b: IVec2): number;
48
+ cross(b: IVec2): number;
49
+ static distance(a: IVec2, b: IVec2): number;
50
+ distance(b: IVec2): number;
51
+ static squaredDistance(a: IVec2, b: IVec2): number;
52
+ squaredDistance(b: IVec2): number;
53
+ static direction(a: IVec2, b: IVec2): Vec2;
54
+ direction(b: IVec2): this;
55
+ static project(a: IVec2, b: IVec2): Vec2;
56
+ project(b: IVec2): this;
57
+ static abs(a: IVec2): Vec2;
58
+ abs(): this;
59
+ static ceil(a: IVec2): Vec2;
60
+ ceil(): this;
61
+ static floor(a: IVec2): Vec2;
62
+ floor(): this;
63
+ static round(a: IVec2): Vec2;
64
+ round(): this;
65
+ static trunc(a: IVec2): Vec2;
66
+ trunc(): this;
67
+ static equals(a: IVec2, b: IVec2): boolean;
68
+ equals(b: IVec2): boolean;
69
+ }
@@ -0,0 +1,296 @@
1
+ export class Vec2 {
2
+ x;
3
+ y;
4
+ constructor(x, y) {
5
+ this.x = x;
6
+ this.y = y;
7
+ }
8
+ static clone(a) {
9
+ return new Vec2(a.x, a.y);
10
+ }
11
+ clone(b) {
12
+ this.x = b.x;
13
+ this.y = b.y;
14
+ return this;
15
+ }
16
+ static fromValues(x, y) {
17
+ return new Vec2(x, y);
18
+ }
19
+ fromValues(x, y) {
20
+ this.x = x;
21
+ this.y = y;
22
+ return this;
23
+ }
24
+ static fromPolar(mag, angle) {
25
+ return new Vec2(Math.cos(angle) * mag, Math.sin(angle) * mag);
26
+ }
27
+ fromPolar(mag, angle) {
28
+ this.x = Math.cos(angle) * mag;
29
+ this.y = Math.sin(angle) * mag;
30
+ return this;
31
+ }
32
+ static randomUnit() {
33
+ const angle = Math.random() * Math.PI * 2;
34
+ return new Vec2(Math.cos(angle), Math.sin(angle));
35
+ }
36
+ randomUnit() {
37
+ const angle = Math.random() * Math.PI * 2;
38
+ this.x = Math.cos(angle);
39
+ this.y = Math.sin(angle);
40
+ return this;
41
+ }
42
+ static random(scalar = 1) {
43
+ const mag = Math.random() * scalar;
44
+ const angle = Math.random() * Math.PI * 2;
45
+ return new Vec2(Math.cos(angle) * mag, Math.sin(angle) * mag);
46
+ }
47
+ random(scalar = 1) {
48
+ const mag = Math.random() * scalar;
49
+ const angle = Math.random() * Math.PI * 2;
50
+ this.x = Math.cos(angle) * mag;
51
+ this.y = Math.sin(angle) * mag;
52
+ return this;
53
+ }
54
+ static add(a, b) {
55
+ return new Vec2(a.x + b.x, a.y + b.y);
56
+ }
57
+ add(b) {
58
+ this.x += b.x;
59
+ this.y += b.y;
60
+ return this;
61
+ }
62
+ static sub(a, b) {
63
+ return new Vec2(a.x - b.x, a.y - b.y);
64
+ }
65
+ sub(b) {
66
+ this.x -= b.x;
67
+ this.y -= b.y;
68
+ return this;
69
+ }
70
+ static scale(a, sx, sy = sx) {
71
+ return new Vec2(a.x * sx, a.y * sy);
72
+ }
73
+ scale(sx, sy = sx) {
74
+ this.x *= sx;
75
+ this.y *= sy;
76
+ return this;
77
+ }
78
+ static invert(a) {
79
+ return new Vec2(-a.x, -a.y);
80
+ }
81
+ invert() {
82
+ this.x *= -1;
83
+ this.y *= -1;
84
+ return this;
85
+ }
86
+ static normalize(a) {
87
+ let mag = Math.hypot(a.x, a.y);
88
+ if (mag !== 0) {
89
+ mag = 1 / mag;
90
+ }
91
+ return new Vec2(a.x * mag, a.y * mag);
92
+ }
93
+ normalize() {
94
+ let mag = Math.hypot(this.x, this.y);
95
+ if (mag !== 0) {
96
+ mag = 1 / mag;
97
+ }
98
+ this.x *= mag;
99
+ this.y *= mag;
100
+ return this;
101
+ }
102
+ static rotate(a, angle) {
103
+ const x = a.x;
104
+ const y = a.y;
105
+ const ct = Math.cos(angle);
106
+ const st = Math.sin(angle);
107
+ return new Vec2(x * ct - y * st, x * st + y * ct);
108
+ }
109
+ rotate(angle) {
110
+ const x = this.x;
111
+ const y = this.y;
112
+ const ct = Math.cos(angle);
113
+ const st = Math.sin(angle);
114
+ this.x = x * ct - y * st;
115
+ this.y = x * st + y * ct;
116
+ return this;
117
+ }
118
+ static rotateAroundPoint(a, axisPoint, angle) {
119
+ return Vec2.clone(a).sub(axisPoint).rotate(angle).add(axisPoint);
120
+ }
121
+ rotateAroundPoint(axisPoint, angle) {
122
+ return this.sub(axisPoint).rotate(angle).add(axisPoint);
123
+ }
124
+ static scaleAndRotate(a, axisPoint, angle, sx, sy = sx) {
125
+ return Vec2.clone(a)
126
+ .sub(axisPoint)
127
+ .scale(sx, sy)
128
+ .rotate(angle)
129
+ .add(axisPoint);
130
+ }
131
+ scaleAndRotate(axisPoint, angle, sx, sy = sx) {
132
+ return this.sub(axisPoint).scale(sx, sy).rotate(angle).add(axisPoint);
133
+ }
134
+ static lerp(a, b, t) {
135
+ const ax = a.x;
136
+ const ay = a.y;
137
+ return new Vec2(ax + t * (b.x - ax), ay + t * (b.y - ay));
138
+ }
139
+ lerp(b, t) {
140
+ const x = this.x;
141
+ const y = this.y;
142
+ this.x = x + t * (b.x - x);
143
+ this.y = y + t * (b.y - y);
144
+ return this;
145
+ }
146
+ static perp(a) {
147
+ return new Vec2(-a.y, a.x);
148
+ }
149
+ perp() {
150
+ const x = this.x;
151
+ const y = this.y;
152
+ this.x = -y;
153
+ this.y = x;
154
+ return this;
155
+ }
156
+ static magnitude(a) {
157
+ return Math.hypot(a.x, a.y);
158
+ }
159
+ magnitude() {
160
+ return Math.hypot(this.x, this.y);
161
+ }
162
+ static squaredMagnitude(a) {
163
+ const x = a.x;
164
+ const y = a.y;
165
+ return x * x + y * y;
166
+ }
167
+ squaredMagnitude() {
168
+ const x = this.x;
169
+ const y = this.y;
170
+ return x * x + y * y;
171
+ }
172
+ static azimuth(a) {
173
+ return Math.atan2(a.y, a.x);
174
+ }
175
+ azimuth() {
176
+ return Math.atan2(this.y, this.x);
177
+ }
178
+ static dot(a, b) {
179
+ return a.x * b.x + a.y * b.y;
180
+ }
181
+ dot(b) {
182
+ return this.x * b.x + this.y * b.y;
183
+ }
184
+ static cross(a, b) {
185
+ return a.x * b.y - a.y * b.x;
186
+ }
187
+ cross(b) {
188
+ return this.x * b.y - this.y * b.x;
189
+ }
190
+ static distance(a, b) {
191
+ return Math.hypot(a.x - b.x, a.y - b.y);
192
+ }
193
+ distance(b) {
194
+ return Math.hypot(this.x - b.x, this.y - b.y);
195
+ }
196
+ static squaredDistance(a, b) {
197
+ const dx = a.x - b.x;
198
+ const dy = a.y - b.y;
199
+ return dx * dx + dy * dy;
200
+ }
201
+ squaredDistance(b) {
202
+ const dx = this.x - b.x;
203
+ const dy = this.y - b.y;
204
+ return dx * dx + dy * dy;
205
+ }
206
+ static direction(a, b) {
207
+ const dx = b.x - a.x;
208
+ const dy = b.y - a.y;
209
+ let mag = Math.hypot(dx, dy);
210
+ if (mag !== 0) {
211
+ mag = 1 / mag;
212
+ }
213
+ return new Vec2(dx * mag, dy * mag);
214
+ }
215
+ direction(b) {
216
+ const dx = b.x - this.x;
217
+ const dy = b.y - this.y;
218
+ let mag = Math.hypot(dx, dy);
219
+ if (mag !== 0) {
220
+ mag = 1 / mag;
221
+ }
222
+ this.x = dx * mag;
223
+ this.y = dy * mag;
224
+ return this;
225
+ }
226
+ static project(a, b) {
227
+ const bx = b.x;
228
+ const by = b.y;
229
+ const dotAB = a.x * bx + a.y * by;
230
+ let squaredMagB = bx * bx + by * by;
231
+ if (squaredMagB !== 0) {
232
+ squaredMagB = 1 / squaredMagB;
233
+ }
234
+ const scalar = dotAB * squaredMagB;
235
+ return new Vec2(bx * scalar, by * scalar);
236
+ }
237
+ project(b) {
238
+ const bx = b.x;
239
+ const by = b.y;
240
+ const dotAB = this.x * bx + this.y * by;
241
+ let squaredMagB = bx * bx + by * by;
242
+ if (squaredMagB !== 0) {
243
+ squaredMagB = 1 / squaredMagB;
244
+ }
245
+ const scalar = dotAB * squaredMagB;
246
+ this.x *= scalar;
247
+ this.y *= scalar;
248
+ return this;
249
+ }
250
+ static abs(a) {
251
+ return new Vec2(Math.abs(a.x), Math.abs(a.y));
252
+ }
253
+ abs() {
254
+ this.x = Math.abs(this.x);
255
+ this.y = Math.abs(this.y);
256
+ return this;
257
+ }
258
+ static ceil(a) {
259
+ return new Vec2(Math.ceil(a.x), Math.ceil(a.y));
260
+ }
261
+ ceil() {
262
+ this.x = Math.ceil(this.x);
263
+ this.y = Math.ceil(this.y);
264
+ return this;
265
+ }
266
+ static floor(a) {
267
+ return new Vec2(Math.floor(a.x), Math.floor(a.y));
268
+ }
269
+ floor() {
270
+ this.x = Math.floor(this.x);
271
+ this.y = Math.floor(this.y);
272
+ return this;
273
+ }
274
+ static round(a) {
275
+ return new Vec2(Math.round(a.x), Math.round(a.y));
276
+ }
277
+ round() {
278
+ this.x = Math.round(this.x);
279
+ this.y = Math.round(this.y);
280
+ return this;
281
+ }
282
+ static trunc(a) {
283
+ return new Vec2(Math.trunc(a.x), Math.trunc(a.y));
284
+ }
285
+ trunc() {
286
+ this.x = Math.trunc(this.x);
287
+ this.y = Math.trunc(this.y);
288
+ return this;
289
+ }
290
+ static equals(a, b) {
291
+ return a.x === b.x && a.y === b.y;
292
+ }
293
+ equals(b) {
294
+ return this.x === b.x && this.y === b.y;
295
+ }
296
+ }
@@ -0,0 +1,46 @@
1
+ import type { Playground } from "../../Playground.js";
2
+ export interface ShakeOptions {
3
+ duration: number;
4
+ frequency: number;
5
+ decay: (t: number) => number;
6
+ callback: () => void;
7
+ suppressWarning: boolean;
8
+ }
9
+ interface ShakeProp {
10
+ amplitude: number;
11
+ start_value: number;
12
+ current_value: number;
13
+ sample_value: number;
14
+ }
15
+ interface ShakeObj<T extends object> {
16
+ target_obj: T;
17
+ current_sample: number;
18
+ num_sample: number;
19
+ current_step: number;
20
+ steps_per_sample: number;
21
+ decay: (t: number) => number;
22
+ callback?: () => void;
23
+ property_list: Map<keyof T, ShakeProp>;
24
+ }
25
+ export declare class Shaker extends EventTarget {
26
+ _nextId: number;
27
+ _shake_queue: Map<number, ShakeObj<Record<string, number>>>;
28
+ _shaking: boolean;
29
+ _playground: WeakRef<Playground>;
30
+ _callbackId: number | null;
31
+ constructor(playground: Playground);
32
+ _shakeStep: () => void;
33
+ registerCallback(options?: {
34
+ suppressWarning?: boolean;
35
+ }): this;
36
+ removeCallback(options?: {
37
+ suppressWarning?: boolean;
38
+ }): this;
39
+ _onClearCallbacks: () => void;
40
+ shake<T extends object>(target_obj: T, properties: Partial<Record<keyof T, number>>, options?: Partial<ShakeOptions>): number;
41
+ removeShake(shakeId: number | null, options?: {
42
+ suppressWarning?: boolean;
43
+ }): this;
44
+ clearShakes(): this;
45
+ }
46
+ export {};
@@ -0,0 +1,190 @@
1
+ import { lerp } from "../../utils.js";
2
+ import { linear } from "./easing.js";
3
+ export class Shaker extends EventTarget {
4
+ _nextId = 1; // Start from 1 to guarantee that shakeId is always truthy
5
+ _shake_queue = new Map();
6
+ _shaking = false;
7
+ _playground;
8
+ _callbackId = null;
9
+ constructor(playground) {
10
+ super();
11
+ this._playground = new WeakRef(playground);
12
+ this.registerCallback();
13
+ playground.addEventListener("clearCallbacks", this._onClearCallbacks);
14
+ }
15
+ _shakeStep = () => {
16
+ if (this._shaking) {
17
+ // Process all the shakes in the queue
18
+ for (const [shakeId, shake_obj] of this._shake_queue) {
19
+ const target_obj = shake_obj.target_obj;
20
+ shake_obj.current_step += 1;
21
+ if (shake_obj.current_step >= shake_obj.steps_per_sample) {
22
+ shake_obj.current_step = 0;
23
+ shake_obj.current_sample += 1;
24
+ if (shake_obj.current_sample >= shake_obj.num_sample) {
25
+ // This object has finished its shaking
26
+ // Restore every property to its initial value
27
+ for (const [property, shake_prop] of shake_obj.property_list) {
28
+ target_obj[property] = shake_prop.start_value;
29
+ }
30
+ // Call the complete callback
31
+ const callback = shake_obj.callback;
32
+ callback?.();
33
+ // Remove this shake
34
+ this._shake_queue.delete(shakeId);
35
+ }
36
+ else {
37
+ for (const [property, shake_prop] of shake_obj.property_list) {
38
+ // Set the properties to the current value
39
+ target_obj[property] = shake_prop.sample_value;
40
+ // Update the property list with a new sample
41
+ shake_prop.current_value = shake_prop.sample_value;
42
+ shake_prop.sample_value =
43
+ shake_prop.start_value +
44
+ lerp((Math.random() * 2 - 1) * shake_prop.amplitude, 0, shake_obj.decay(shake_obj.current_sample / shake_obj.num_sample));
45
+ }
46
+ }
47
+ }
48
+ else {
49
+ // Set the properties to the current value
50
+ for (const [property, shake_prop] of shake_obj.property_list) {
51
+ target_obj[property] = lerp(shake_prop.current_value, shake_prop.sample_value, shake_obj.current_step / shake_obj.steps_per_sample);
52
+ }
53
+ }
54
+ }
55
+ // If there are no more shakes in the queue, this callback can be stopped
56
+ if (this._shake_queue.size === 0) {
57
+ this._shaking = false;
58
+ }
59
+ }
60
+ };
61
+ registerCallback(options) {
62
+ if (this._callbackId === null) {
63
+ const playground = this._playground.deref();
64
+ if (playground !== undefined) {
65
+ this._callbackId = playground.registerCallback(this._shakeStep);
66
+ }
67
+ else {
68
+ if (typeof console !== "undefined" &&
69
+ options?.suppressWarning === false) {
70
+ console.warn("playground has been garbage collected");
71
+ console.trace();
72
+ }
73
+ }
74
+ }
75
+ else {
76
+ if (typeof console !== "undefined" &&
77
+ options?.suppressWarning === false) {
78
+ console.warn("Callback already registered");
79
+ console.trace();
80
+ }
81
+ }
82
+ return this;
83
+ }
84
+ removeCallback(options) {
85
+ const playground = this._playground.deref();
86
+ if (playground !== undefined) {
87
+ playground.removeCallback(this._callbackId, options);
88
+ }
89
+ else {
90
+ if (typeof console !== "undefined" &&
91
+ options?.suppressWarning === false) {
92
+ console.warn("playground has been garbage collected");
93
+ console.trace();
94
+ }
95
+ }
96
+ this._callbackId = null;
97
+ return this;
98
+ }
99
+ _onClearCallbacks = () => {
100
+ if (this._callbackId !== null) {
101
+ this._callbackId = null;
102
+ this.registerCallback();
103
+ }
104
+ };
105
+ shake(target_obj, properties, options) {
106
+ const playground = this._playground.deref();
107
+ if (playground !== undefined) {
108
+ const duration = options?.duration || 1;
109
+ const frequency = options?.frequency || 60;
110
+ const decay = options?.decay ?? linear;
111
+ const steps_per_sample = playground.framesFromMs(1000 / frequency);
112
+ const num_sample = Math.ceil(playground.framesFromMs(duration) / steps_per_sample);
113
+ const shake_obj = {
114
+ target_obj: target_obj,
115
+ current_sample: 0,
116
+ num_sample: num_sample,
117
+ current_step: 0,
118
+ steps_per_sample: steps_per_sample,
119
+ decay: decay,
120
+ property_list: new Map(),
121
+ };
122
+ if (options?.callback !== undefined) {
123
+ shake_obj.callback = options.callback;
124
+ }
125
+ const property_list = shake_obj.property_list;
126
+ for (const [property, value] of Object.entries(properties)) {
127
+ const start_value = target_obj[property];
128
+ const amplitude = value;
129
+ property_list.set(property, {
130
+ amplitude: amplitude,
131
+ start_value: start_value,
132
+ current_value: start_value,
133
+ sample_value: start_value + (Math.random() * 2 - 1) * amplitude,
134
+ });
135
+ }
136
+ const shakeId = this._nextId;
137
+ this._nextId += 1;
138
+ this._shake_queue.set(shakeId, shake_obj);
139
+ this._shaking = true;
140
+ return shakeId;
141
+ }
142
+ else {
143
+ if (typeof console !== "undefined" &&
144
+ options?.suppressWarning === false) {
145
+ console.warn("playground has been garbage collected");
146
+ console.trace();
147
+ }
148
+ return -1;
149
+ }
150
+ }
151
+ removeShake(shakeId, options) {
152
+ if (shakeId === null) {
153
+ if (typeof console !== "undefined" &&
154
+ options?.suppressWarning === false) {
155
+ console.warn("shakeId is null");
156
+ console.trace();
157
+ }
158
+ return this;
159
+ }
160
+ const shake_obj = this._shake_queue.get(shakeId);
161
+ if (shake_obj !== undefined) {
162
+ const target_obj = shake_obj.target_obj;
163
+ // Restore every property to its initial value
164
+ for (const [property, shake_prop] of shake_obj.property_list) {
165
+ target_obj[property] = shake_prop.start_value;
166
+ }
167
+ this._shake_queue.delete(shakeId);
168
+ }
169
+ else {
170
+ if (typeof console !== "undefined" &&
171
+ options?.suppressWarning === false) {
172
+ console.warn("No shakes removed");
173
+ console.trace();
174
+ }
175
+ }
176
+ return this;
177
+ }
178
+ clearShakes() {
179
+ for (const shake_obj of this._shake_queue.values()) {
180
+ const target_obj = shake_obj.target_obj;
181
+ // Restore every property to its initial value
182
+ for (const [property, shake_prop] of shake_obj.property_list) {
183
+ target_obj[property] = shake_prop.start_value;
184
+ }
185
+ }
186
+ this._shake_queue.clear();
187
+ this.dispatchEvent(new Event("clearShakes"));
188
+ return this;
189
+ }
190
+ }
@@ -0,0 +1,42 @@
1
+ import type { Playground } from "../../Playground.js";
2
+ export type Speed = "slow" | "fast";
3
+ export interface TweenOptions {
4
+ duration: Speed | number;
5
+ easing: (t: number) => number;
6
+ callback: () => void;
7
+ suppressWarning: boolean;
8
+ }
9
+ interface TweenProp {
10
+ start_value: number;
11
+ target_value: number;
12
+ }
13
+ interface TweenObj<T extends object> {
14
+ target_obj: T;
15
+ current_step: number;
16
+ num_step: number;
17
+ easing: (t: number) => number;
18
+ callback?: () => void;
19
+ property_list: Map<keyof T, TweenProp>;
20
+ }
21
+ export declare class Tweener extends EventTarget {
22
+ _nextId: number;
23
+ _tween_queue: Map<number, TweenObj<Record<string, number>>>;
24
+ _tweening: boolean;
25
+ _playground: WeakRef<Playground>;
26
+ _callbackId: number | null;
27
+ constructor(playground: Playground);
28
+ _tweenStep: () => void;
29
+ registerCallback(options?: {
30
+ suppressWarning?: boolean;
31
+ }): this;
32
+ removeCallback(options?: {
33
+ suppressWarning?: boolean;
34
+ }): this;
35
+ _onClearCallbacks: () => void;
36
+ tween<T extends object>(target_obj: T, properties: Partial<Record<keyof T, number>>, options?: Partial<TweenOptions>): number;
37
+ removeTween(tweenId: number | null, options?: {
38
+ suppressWarning?: boolean;
39
+ }): this;
40
+ clearTweens(): this;
41
+ }
42
+ export {};