frigame3 0.0.0-stage → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE +373 -0
  3. package/README.md +80 -2
  4. package/biome.json +93 -0
  5. package/demo/package-lock.json +515 -0
  6. package/demo/package.json +19 -0
  7. package/demo/snake/index.html +16 -0
  8. package/demo/snake/index.ts +110 -0
  9. package/demo/tetris/index.html +16 -0
  10. package/demo/tetris/index.ts +536 -0
  11. package/demo/tsconfig.json +53 -0
  12. package/frigame.css +12 -0
  13. package/package.json +50 -4
  14. package/src/Animation.ts +266 -0
  15. package/src/BaseSprite.ts +682 -0
  16. package/src/Gradient.ts +120 -0
  17. package/src/Playground.ts +247 -0
  18. package/src/Rect.ts +237 -0
  19. package/src/Rectangle.ts +108 -0
  20. package/src/Renderer.ts +28 -0
  21. package/src/ResourceManager.ts +145 -0
  22. package/src/Sprite.ts +484 -0
  23. package/src/SpriteGroup.ts +258 -0
  24. package/src/canvasRenderer/renderGroup.ts +194 -0
  25. package/src/canvasRenderer/renderPlayground.ts +103 -0
  26. package/src/canvasRenderer/renderRectangle.ts +421 -0
  27. package/src/canvasRenderer/renderSprite.ts +148 -0
  28. package/src/canvasRenderer.ts +38 -0
  29. package/src/dummyRenderer.ts +62 -0
  30. package/src/plugins/GamepadTracker.ts +97 -0
  31. package/src/plugins/Tilemap.ts +123 -0
  32. package/src/plugins/Vec2.ts +420 -0
  33. package/src/plugins/fx/Shaker.ts +293 -0
  34. package/src/plugins/fx/Tweener.ts +252 -0
  35. package/src/plugins/fx/easing.ts +276 -0
  36. package/src/plugins/isometric/ISOMixin.ts +444 -0
  37. package/src/plugins/isometric/ISORectangle.ts +183 -0
  38. package/src/plugins/isometric/ISOSprite.ts +228 -0
  39. package/src/plugins/isometric/ISOSpriteGroup.ts +155 -0
  40. package/src/plugins/isometric/ISOTilemap.ts +122 -0
  41. package/src/plugins/isometric/utils.ts +19 -0
  42. package/src/plugins/keyTracker.ts +31 -0
  43. package/src/plugins/mixer.ts +523 -0
  44. package/src/plugins/sorted/SortedAnimation.ts +53 -0
  45. package/src/plugins/sorted/SortedGroup.ts +94 -0
  46. package/src/plugins/sorted/SortedMixin.ts +203 -0
  47. package/src/plugins/sorted/SortedRectangle.ts +35 -0
  48. package/src/plugins/sorted/SortedSprite.ts +56 -0
  49. package/src/utils.ts +53 -0
  50. package/test/animation/index.html +20 -0
  51. package/test/animation/index.ts +253 -0
  52. package/test/animation/mh.png +0 -0
  53. package/test/animation/moh.png +0 -0
  54. package/test/animation/mov.png +0 -0
  55. package/test/animation/mv.png +0 -0
  56. package/test/animation/rebound.png +0 -0
  57. package/test/animation/reboundm.png +0 -0
  58. package/test/animation/sh.png +0 -0
  59. package/test/animation/soh.png +0 -0
  60. package/test/animation/sov.png +0 -0
  61. package/test/animation/sv.png +0 -0
  62. package/test/isometric1/beach.png +0 -0
  63. package/test/isometric1/foglie.png +0 -0
  64. package/test/isometric1/grass.png +0 -0
  65. package/test/isometric1/index.html +17 -0
  66. package/test/isometric1/index.ts +68 -0
  67. package/test/isometric1/tronco.png +0 -0
  68. package/test/isometric1/untitled.tmx +25 -0
  69. package/test/isometric1/water.png +0 -0
  70. package/test/isometric2/banner.png +0 -0
  71. package/test/isometric2/books.png +0 -0
  72. package/test/isometric2/candlestick.png +0 -0
  73. package/test/isometric2/ice.png +0 -0
  74. package/test/isometric2/index.html +16 -0
  75. package/test/isometric2/index.ts +129 -0
  76. package/test/isometric2/knight_se.png +0 -0
  77. package/test/isometric2/tile.png +0 -0
  78. package/test/isometric2/untitled.tmx +36 -0
  79. package/test/isometric2/wall.png +0 -0
  80. package/test/layers/boy.png +0 -0
  81. package/test/layers/catgirl.png +0 -0
  82. package/test/layers/fri1.svg +8 -0
  83. package/test/layers/grassblock.png +0 -0
  84. package/test/layers/horngirl.png +0 -0
  85. package/test/layers/index.html +17 -0
  86. package/test/layers/index.ts +191 -0
  87. package/test/layers/pinkgirl.png +0 -0
  88. package/test/layers/render_ruler.py +57 -0
  89. package/test/layers/ruler.png +0 -0
  90. package/test/layers/sh.png +0 -0
  91. package/test/mixer/dieci.opus +0 -0
  92. package/test/mixer/dieci.wav +0 -0
  93. package/test/mixer/index.html +33 -0
  94. package/test/mixer/index.ts +206 -0
  95. package/test/mixer/news_intro.mp3 +0 -0
  96. package/test/mixer/news_intro.ogg +0 -0
  97. package/test/mixer/prova.mp3 +0 -0
  98. package/test/mixer/prova.ogg +0 -0
  99. package/test/package-lock.json +515 -0
  100. package/test/package.json +19 -0
  101. package/test/sorted/Brown Block.png +0 -0
  102. package/test/sorted/index.html +16 -0
  103. package/test/sorted/index.ts +38 -0
  104. package/test/transformation1/index.html +17 -0
  105. package/test/transformation1/index.ts +51 -0
  106. package/test/transformation1/sh.png +0 -0
  107. package/test/transformation2/index.html +20 -0
  108. package/test/transformation2/index.ts +71 -0
  109. package/test/transformation2/sh.png +0 -0
  110. package/test/tsconfig.json +53 -0
  111. package/tsconfig.json +51 -0
@@ -0,0 +1,293 @@
1
+ import type { Playground } from "../../Playground.js";
2
+ import { lerp } from "../../utils.js";
3
+ import { linear } from "./easing.js";
4
+
5
+ // Inspired by https://jonny.morrill.me/en/blog/gamedev-how-to-implement-a-camera-shake-effect/
6
+
7
+ export interface ShakeOptions {
8
+ duration: number;
9
+ frequency: number;
10
+ decay: (t: number) => number;
11
+ callback: () => void;
12
+ suppressWarning: boolean;
13
+ }
14
+
15
+ interface ShakeProp {
16
+ amplitude: number;
17
+ start_value: number;
18
+ current_value: number;
19
+ sample_value: number;
20
+ }
21
+
22
+ interface ShakeObj<T extends object> {
23
+ target_obj: T;
24
+ current_sample: number;
25
+ num_sample: number;
26
+ current_step: number;
27
+ steps_per_sample: number;
28
+ decay: (t: number) => number;
29
+ callback?: () => void;
30
+ property_list: Map<keyof T, ShakeProp>;
31
+ }
32
+
33
+ export class Shaker extends EventTarget {
34
+ _nextId = 1; // Start from 1 to guarantee that shakeId is always truthy
35
+ _shake_queue: Map<number, ShakeObj<Record<string, number>>> = new Map<
36
+ number,
37
+ ShakeObj<Record<string, number>>
38
+ >();
39
+ _shaking = false;
40
+ _playground: WeakRef<Playground>;
41
+ _callbackId: number | null = null;
42
+
43
+ constructor(playground: Playground) {
44
+ super();
45
+
46
+ this._playground = new WeakRef(playground);
47
+
48
+ this.registerCallback();
49
+
50
+ playground.addEventListener("clearCallbacks", this._onClearCallbacks);
51
+ }
52
+
53
+ _shakeStep = (): void => {
54
+ if (this._shaking) {
55
+ // Process all the shakes in the queue
56
+ for (const [shakeId, shake_obj] of this._shake_queue) {
57
+ const target_obj = shake_obj.target_obj;
58
+
59
+ shake_obj.current_step += 1;
60
+
61
+ if (shake_obj.current_step >= shake_obj.steps_per_sample) {
62
+ shake_obj.current_step = 0;
63
+ shake_obj.current_sample += 1;
64
+
65
+ if (shake_obj.current_sample >= shake_obj.num_sample) {
66
+ // This object has finished its shaking
67
+
68
+ // Restore every property to its initial value
69
+ for (const [property, shake_prop] of shake_obj.property_list) {
70
+ target_obj[property] = shake_prop.start_value;
71
+ }
72
+
73
+ // Call the complete callback
74
+ const callback = shake_obj.callback;
75
+ callback?.();
76
+
77
+ // Remove this shake
78
+ this._shake_queue.delete(shakeId);
79
+ } else {
80
+ for (const [property, shake_prop] of shake_obj.property_list) {
81
+ // Set the properties to the current value
82
+ target_obj[property] = shake_prop.sample_value;
83
+
84
+ // Update the property list with a new sample
85
+ shake_prop.current_value = shake_prop.sample_value;
86
+ shake_prop.sample_value =
87
+ shake_prop.start_value +
88
+ lerp(
89
+ (Math.random() * 2 - 1) * shake_prop.amplitude,
90
+ 0,
91
+ shake_obj.decay(
92
+ shake_obj.current_sample / shake_obj.num_sample,
93
+ ),
94
+ );
95
+ }
96
+ }
97
+ } else {
98
+ // Set the properties to the current value
99
+ for (const [property, shake_prop] of shake_obj.property_list) {
100
+ target_obj[property] = lerp(
101
+ shake_prop.current_value,
102
+ shake_prop.sample_value,
103
+ shake_obj.current_step / shake_obj.steps_per_sample,
104
+ );
105
+ }
106
+ }
107
+ }
108
+
109
+ // If there are no more shakes in the queue, this callback can be stopped
110
+ if (this._shake_queue.size === 0) {
111
+ this._shaking = false;
112
+ }
113
+ }
114
+ };
115
+
116
+ registerCallback(options?: { suppressWarning?: boolean }): this {
117
+ if (this._callbackId === null) {
118
+ const playground = this._playground.deref();
119
+
120
+ if (playground !== undefined) {
121
+ this._callbackId = playground.registerCallback(this._shakeStep);
122
+ } else {
123
+ if (
124
+ typeof console !== "undefined" &&
125
+ options?.suppressWarning === false
126
+ ) {
127
+ console.warn("playground has been garbage collected");
128
+ console.trace();
129
+ }
130
+ }
131
+ } else {
132
+ if (
133
+ typeof console !== "undefined" &&
134
+ options?.suppressWarning === false
135
+ ) {
136
+ console.warn("Callback already registered");
137
+ console.trace();
138
+ }
139
+ }
140
+
141
+ return this;
142
+ }
143
+
144
+ removeCallback(options?: { suppressWarning?: boolean }): this {
145
+ const playground = this._playground.deref();
146
+
147
+ if (playground !== undefined) {
148
+ playground.removeCallback(this._callbackId, options);
149
+ } else {
150
+ if (
151
+ typeof console !== "undefined" &&
152
+ options?.suppressWarning === false
153
+ ) {
154
+ console.warn("playground has been garbage collected");
155
+ console.trace();
156
+ }
157
+ }
158
+
159
+ this._callbackId = null;
160
+
161
+ return this;
162
+ }
163
+
164
+ _onClearCallbacks = (): void => {
165
+ if (this._callbackId !== null) {
166
+ this._callbackId = null;
167
+ this.registerCallback();
168
+ }
169
+ };
170
+
171
+ shake<T extends object>(
172
+ target_obj: T,
173
+ properties: Partial<Record<keyof T, number>>,
174
+ options?: Partial<ShakeOptions>,
175
+ ): number {
176
+ const playground = this._playground.deref();
177
+
178
+ if (playground !== undefined) {
179
+ const duration = options?.duration || 1;
180
+ const frequency = options?.frequency || 60;
181
+ const decay = options?.decay ?? linear;
182
+
183
+ const steps_per_sample = playground.framesFromMs(1000 / frequency);
184
+ const num_sample = Math.ceil(
185
+ playground.framesFromMs(duration) / steps_per_sample,
186
+ );
187
+
188
+ const shake_obj: ShakeObj<T> = {
189
+ target_obj: target_obj,
190
+ current_sample: 0,
191
+ num_sample: num_sample,
192
+ current_step: 0,
193
+ steps_per_sample: steps_per_sample,
194
+ decay: decay,
195
+ property_list: new Map<keyof T, ShakeProp>(),
196
+ };
197
+
198
+ if (options?.callback !== undefined) {
199
+ shake_obj.callback = options.callback;
200
+ }
201
+
202
+ const property_list = shake_obj.property_list;
203
+
204
+ for (const [property, value] of Object.entries(properties)) {
205
+ const start_value = target_obj[property as keyof T] as number;
206
+ const amplitude = value as number;
207
+
208
+ property_list.set(property as keyof T, {
209
+ amplitude: amplitude,
210
+ start_value: start_value,
211
+ current_value: start_value,
212
+ sample_value: start_value + (Math.random() * 2 - 1) * amplitude,
213
+ });
214
+ }
215
+
216
+ const shakeId = this._nextId;
217
+ this._nextId += 1;
218
+
219
+ this._shake_queue.set(
220
+ shakeId,
221
+ shake_obj as unknown as ShakeObj<Record<string, number>>,
222
+ );
223
+ this._shaking = true;
224
+
225
+ return shakeId;
226
+ } else {
227
+ if (
228
+ typeof console !== "undefined" &&
229
+ options?.suppressWarning === false
230
+ ) {
231
+ console.warn("playground has been garbage collected");
232
+ console.trace();
233
+ }
234
+
235
+ return -1;
236
+ }
237
+ }
238
+
239
+ removeShake(
240
+ shakeId: number | null,
241
+ options?: { suppressWarning?: boolean },
242
+ ): this {
243
+ if (shakeId === null) {
244
+ if (
245
+ typeof console !== "undefined" &&
246
+ options?.suppressWarning === false
247
+ ) {
248
+ console.warn("shakeId is null");
249
+ console.trace();
250
+ }
251
+ return this;
252
+ }
253
+
254
+ const shake_obj = this._shake_queue.get(shakeId);
255
+ if (shake_obj !== undefined) {
256
+ const target_obj = shake_obj.target_obj;
257
+
258
+ // Restore every property to its initial value
259
+ for (const [property, shake_prop] of shake_obj.property_list) {
260
+ target_obj[property] = shake_prop.start_value;
261
+ }
262
+
263
+ this._shake_queue.delete(shakeId);
264
+ } else {
265
+ if (
266
+ typeof console !== "undefined" &&
267
+ options?.suppressWarning === false
268
+ ) {
269
+ console.warn("No shakes removed");
270
+ console.trace();
271
+ }
272
+ }
273
+
274
+ return this;
275
+ }
276
+
277
+ clearShakes(): this {
278
+ for (const shake_obj of this._shake_queue.values()) {
279
+ const target_obj = shake_obj.target_obj;
280
+
281
+ // Restore every property to its initial value
282
+ for (const [property, shake_prop] of shake_obj.property_list) {
283
+ target_obj[property] = shake_prop.start_value;
284
+ }
285
+ }
286
+
287
+ this._shake_queue.clear();
288
+
289
+ this.dispatchEvent(new Event("clearShakes"));
290
+
291
+ return this;
292
+ }
293
+ }
@@ -0,0 +1,252 @@
1
+ import type { Playground } from "../../Playground.js";
2
+ import { lerp } from "../../utils.js";
3
+ import { swing } from "./easing.js";
4
+
5
+ export type Speed = "slow" | "fast";
6
+
7
+ export interface TweenOptions {
8
+ duration: Speed | number;
9
+ easing: (t: number) => number;
10
+ callback: () => void;
11
+ suppressWarning: boolean;
12
+ }
13
+
14
+ interface TweenProp {
15
+ start_value: number;
16
+ target_value: number;
17
+ }
18
+
19
+ interface TweenObj<T extends object> {
20
+ target_obj: T;
21
+ current_step: number;
22
+ num_step: number;
23
+ easing: (t: number) => number;
24
+ callback?: () => void;
25
+ property_list: Map<keyof T, TweenProp>;
26
+ }
27
+
28
+ const speeds = {
29
+ slow: 600,
30
+ fast: 200,
31
+ _default: 400,
32
+ };
33
+
34
+ export class Tweener extends EventTarget {
35
+ _nextId = 1; // Start from 1 to guarantee that tweenId is always truthy
36
+ _tween_queue: Map<number, TweenObj<Record<string, number>>> = new Map<
37
+ number,
38
+ TweenObj<Record<string, number>>
39
+ >();
40
+ _tweening = false;
41
+ _playground: WeakRef<Playground>;
42
+ _callbackId: number | null = null;
43
+
44
+ constructor(playground: Playground) {
45
+ super();
46
+
47
+ this._playground = new WeakRef(playground);
48
+
49
+ this.registerCallback();
50
+
51
+ playground.addEventListener("clearCallbacks", this._onClearCallbacks);
52
+ }
53
+
54
+ _tweenStep = (): void => {
55
+ if (this._tweening) {
56
+ // Process all the tweens in the queue
57
+ for (const [tweenId, tween_obj] of this._tween_queue) {
58
+ const target_obj = tween_obj.target_obj;
59
+
60
+ tween_obj.current_step += 1;
61
+
62
+ if (tween_obj.current_step >= tween_obj.num_step) {
63
+ // This object has finished its tweening
64
+
65
+ // Set every property to its target value
66
+ for (const [property, tween_prop] of tween_obj.property_list) {
67
+ target_obj[property] = tween_prop.target_value;
68
+ }
69
+
70
+ // Call the complete callback
71
+ const callback = tween_obj.callback;
72
+ callback?.();
73
+
74
+ // Remove this tween
75
+ this._tween_queue.delete(tweenId);
76
+ } else {
77
+ // Set the properties to the current value
78
+ for (const [property, tween_prop] of tween_obj.property_list) {
79
+ target_obj[property] = lerp(
80
+ tween_prop.start_value,
81
+ tween_prop.target_value,
82
+ tween_obj.easing(tween_obj.current_step / tween_obj.num_step),
83
+ );
84
+ }
85
+ }
86
+ }
87
+
88
+ // If there are no more tweens in the queue, this callback can be stopped
89
+ if (this._tween_queue.size === 0) {
90
+ this._tweening = false;
91
+ }
92
+ }
93
+ };
94
+
95
+ registerCallback(options?: { suppressWarning?: boolean }): this {
96
+ if (this._callbackId === null) {
97
+ const playground = this._playground.deref();
98
+
99
+ if (playground !== undefined) {
100
+ this._callbackId = playground.registerCallback(this._tweenStep);
101
+ } else {
102
+ if (
103
+ typeof console !== "undefined" &&
104
+ options?.suppressWarning === false
105
+ ) {
106
+ console.warn("playground has been garbage collected");
107
+ console.trace();
108
+ }
109
+ }
110
+ } else {
111
+ if (
112
+ typeof console !== "undefined" &&
113
+ options?.suppressWarning === false
114
+ ) {
115
+ console.warn("Callback already registered");
116
+ console.trace();
117
+ }
118
+ }
119
+
120
+ return this;
121
+ }
122
+
123
+ removeCallback(options?: { suppressWarning?: boolean }): this {
124
+ const playground = this._playground.deref();
125
+
126
+ if (playground !== undefined) {
127
+ playground.removeCallback(this._callbackId, options);
128
+ } else {
129
+ if (
130
+ typeof console !== "undefined" &&
131
+ options?.suppressWarning === false
132
+ ) {
133
+ console.warn("playground has been garbage collected");
134
+ console.trace();
135
+ }
136
+ }
137
+
138
+ this._callbackId = null;
139
+
140
+ return this;
141
+ }
142
+
143
+ _onClearCallbacks = (): void => {
144
+ if (this._callbackId !== null) {
145
+ this._callbackId = null;
146
+ this.registerCallback();
147
+ }
148
+ };
149
+
150
+ tween<T extends object>(
151
+ target_obj: T,
152
+ properties: Partial<Record<keyof T, number>>,
153
+ options?: Partial<TweenOptions>,
154
+ ): number {
155
+ const playground = this._playground.deref();
156
+
157
+ if (playground !== undefined) {
158
+ const duration = ((): number => {
159
+ const duration = options?.duration;
160
+
161
+ if (typeof duration === "string") {
162
+ return speeds[duration];
163
+ } else if (duration === 0) {
164
+ return 0;
165
+ } else {
166
+ return duration || speeds._default;
167
+ }
168
+ })();
169
+
170
+ const easing = options?.easing ?? swing;
171
+
172
+ const tween_obj: TweenObj<T> = {
173
+ target_obj: target_obj,
174
+ current_step: 0,
175
+ num_step: playground.framesFromMs(duration),
176
+ easing: easing,
177
+ property_list: new Map<keyof T, TweenProp>(),
178
+ };
179
+
180
+ if (options?.callback !== undefined) {
181
+ tween_obj.callback = options.callback;
182
+ }
183
+
184
+ const property_list = tween_obj.property_list;
185
+
186
+ for (const [property, value] of Object.entries(properties)) {
187
+ property_list.set(property as keyof T, {
188
+ start_value: target_obj[property as keyof T] as number,
189
+ target_value: value as number,
190
+ });
191
+ }
192
+
193
+ const tweenId = this._nextId;
194
+ this._nextId += 1;
195
+
196
+ this._tween_queue.set(
197
+ tweenId,
198
+ tween_obj as unknown as TweenObj<Record<string, number>>,
199
+ );
200
+ this._tweening = true;
201
+
202
+ return tweenId;
203
+ } else {
204
+ if (
205
+ typeof console !== "undefined" &&
206
+ options?.suppressWarning === false
207
+ ) {
208
+ console.warn("playground has been garbage collected");
209
+ console.trace();
210
+ }
211
+
212
+ return -1;
213
+ }
214
+ }
215
+
216
+ removeTween(
217
+ tweenId: number | null,
218
+ options?: { suppressWarning?: boolean },
219
+ ): this {
220
+ if (tweenId === null) {
221
+ if (
222
+ typeof console !== "undefined" &&
223
+ options?.suppressWarning === false
224
+ ) {
225
+ console.warn("tweenId is null");
226
+ console.trace();
227
+ }
228
+ return this;
229
+ }
230
+
231
+ const deleted = this._tween_queue.delete(tweenId);
232
+
233
+ if (
234
+ !deleted &&
235
+ typeof console !== "undefined" &&
236
+ options?.suppressWarning === false
237
+ ) {
238
+ console.warn("No tweens removed");
239
+ console.trace();
240
+ }
241
+
242
+ return this;
243
+ }
244
+
245
+ clearTweens(): this {
246
+ this._tween_queue.clear();
247
+
248
+ this.dispatchEvent(new Event("clearTweens"));
249
+
250
+ return this;
251
+ }
252
+ }