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,258 @@
1
+ import type { BaseSpriteOptions } from "./BaseSprite.js";
2
+ import { BaseSprite } from "./BaseSprite.js";
3
+ import type { SpriteRef } from "./utils.js";
4
+ import { pick } from "./utils.js";
5
+
6
+ export interface SpriteGroupOptions extends BaseSpriteOptions {
7
+ crop: boolean;
8
+ borderRadius: number;
9
+ children: BaseSprite[];
10
+ }
11
+
12
+ export class SpriteGroup extends BaseSprite {
13
+ _crop = false;
14
+ _borderRadius = 0;
15
+
16
+ get crop(): boolean {
17
+ return this._crop;
18
+ }
19
+
20
+ set crop(value: boolean) {
21
+ this._crop = value;
22
+ }
23
+
24
+ get borderRadius(): number {
25
+ return this._borderRadius;
26
+ }
27
+
28
+ set borderRadius(value: number) {
29
+ this._borderRadius = value;
30
+ }
31
+
32
+ // Implementation details
33
+
34
+ _layers: BaseSprite[] = [];
35
+ _updateList: BaseSprite[] = [];
36
+
37
+ constructor(
38
+ options?: Partial<SpriteGroupOptions> & { ref?: SpriteRef<SpriteGroup> },
39
+ ) {
40
+ super(options);
41
+
42
+ if (options !== undefined) {
43
+ Object.assign(this, pick(options, ["crop", "borderRadius"]));
44
+
45
+ if (options.children !== undefined) {
46
+ this.addChildren(options.children);
47
+ }
48
+
49
+ if (options.ref !== undefined) {
50
+ options.ref.current = this;
51
+ }
52
+ }
53
+
54
+ this.teleport();
55
+ }
56
+
57
+ // Public functions
58
+
59
+ clear(): this {
60
+ for (const sprite of this._layers) {
61
+ sprite._remove();
62
+ }
63
+
64
+ this._layers.splice(0, this._layers.length);
65
+ this._updateList.splice(0, this._updateList.length);
66
+
67
+ this._checkUpdate();
68
+
69
+ return this;
70
+ }
71
+
72
+ addChild<T extends BaseSprite>(
73
+ child: T,
74
+ options?: { suppressWarning?: boolean },
75
+ ): T {
76
+ if (child.parent === undefined) {
77
+ this._layers.push(child);
78
+
79
+ this._reparentChild(child);
80
+
81
+ this._checkUpdate();
82
+ } else {
83
+ if (
84
+ typeof console !== "undefined" &&
85
+ options?.suppressWarning === false
86
+ ) {
87
+ console.warn("child already has a parent");
88
+ console.trace();
89
+ }
90
+ }
91
+
92
+ return child;
93
+ }
94
+
95
+ insertChild<T extends BaseSprite>(
96
+ child: T,
97
+ options?: { suppressWarning?: boolean },
98
+ ): T {
99
+ if (child.parent === undefined) {
100
+ this._layers.unshift(child);
101
+
102
+ this._reparentChild(child);
103
+
104
+ this._checkUpdate();
105
+ } else {
106
+ if (
107
+ typeof console !== "undefined" &&
108
+ options?.suppressWarning === false
109
+ ) {
110
+ console.warn("child already has a parent");
111
+ console.trace();
112
+ }
113
+ }
114
+
115
+ return child;
116
+ }
117
+
118
+ removeChild(
119
+ child: BaseSprite | null,
120
+ options?: { suppressWarning?: boolean },
121
+ ): this {
122
+ if (child === null) {
123
+ if (
124
+ typeof console !== "undefined" &&
125
+ options?.suppressWarning === false
126
+ ) {
127
+ console.warn("child is null");
128
+ console.trace();
129
+ }
130
+ return this;
131
+ }
132
+
133
+ const layers = this._layers;
134
+
135
+ const index = layers.indexOf(child);
136
+ if (index >= 0) {
137
+ layers.splice(index, 1);
138
+
139
+ const update_list = this._updateList;
140
+
141
+ const i = update_list.indexOf(child);
142
+ if (i >= 0) {
143
+ update_list.splice(i, 1);
144
+ }
145
+
146
+ child._remove();
147
+
148
+ this._checkUpdate();
149
+ } else {
150
+ if (
151
+ typeof console !== "undefined" &&
152
+ options?.suppressWarning === false
153
+ ) {
154
+ console.warn("No children removed");
155
+ console.trace();
156
+ }
157
+ }
158
+
159
+ return this;
160
+ }
161
+
162
+ addChildren(children: BaseSprite[]): this {
163
+ for (const child of children) {
164
+ this.addChild(child);
165
+ }
166
+
167
+ return this;
168
+ }
169
+
170
+ // Implementation details
171
+
172
+ _reparentChild(child: BaseSprite): void {
173
+ child._parent = new WeakRef(this);
174
+ child._needsUpdate = false;
175
+ child._checkUpdate();
176
+
177
+ this._addPlaygroundToChild(child);
178
+ }
179
+
180
+ _addPlaygroundToChild(child: BaseSprite): void {
181
+ const playground = this.playground;
182
+
183
+ if (playground !== undefined) {
184
+ child._playground = new WeakRef(playground);
185
+ child._frameCounterLastMove = playground.frameCounter;
186
+ child._initRenderer();
187
+ child._onReparent();
188
+
189
+ if ("_layers" in child) {
190
+ for (const layer of child._layers as BaseSprite[]) {
191
+ this._addPlaygroundToChild(layer);
192
+ }
193
+ }
194
+ }
195
+ }
196
+
197
+ override _checkUpdate(): void {
198
+ const oldNeedsUpdate = this._needsUpdate;
199
+
200
+ if (this._layers.length === 0) {
201
+ this._needsUpdate = false;
202
+ } else {
203
+ this._needsUpdate = true;
204
+ }
205
+
206
+ this._updateNeedsUpdate(oldNeedsUpdate);
207
+ }
208
+
209
+ override _initRenderer(): void {
210
+ this.playground?._renderer.initGroup(this);
211
+ }
212
+
213
+ override _update(): void {
214
+ for (const sprite of this._updateList) {
215
+ sprite._update();
216
+ }
217
+ }
218
+
219
+ override _draw(interp: number): void {
220
+ super._draw(interp);
221
+
222
+ const playground = this.playground;
223
+
224
+ if (playground !== undefined) {
225
+ const absLeft = playground._absLeft;
226
+ const absTop = playground._absTop;
227
+
228
+ playground._absLeft += this._drawLeft;
229
+ playground._absTop += this._drawTop;
230
+
231
+ const renderer = playground._renderer;
232
+
233
+ renderer.drawGroupBeforeChildren(this, interp);
234
+
235
+ if (
236
+ this._opacity !== 0 &&
237
+ this._scaleh !== 0 &&
238
+ this._scalev !== 0 &&
239
+ !this._hidden
240
+ ) {
241
+ for (const sprite of this._layers) {
242
+ sprite._draw(interp);
243
+ }
244
+ }
245
+
246
+ renderer.drawGroupAfterChildren(this, interp);
247
+
248
+ playground._absLeft = absLeft;
249
+ playground._absTop = absTop;
250
+ }
251
+ }
252
+
253
+ override _remove(): void {
254
+ this.clear();
255
+
256
+ this.playground?._renderer.removeGroup(this);
257
+ }
258
+ }
@@ -0,0 +1,194 @@
1
+ import type { SpriteGroup } from "../SpriteGroup.js";
2
+ import { playgroundMap } from "./renderPlayground.js";
3
+
4
+ export interface GroupObj {
5
+ oldAlpha: number;
6
+ alphaChanged: boolean;
7
+ oldBlendMode: GlobalCompositeOperation;
8
+ blendModeChanged: boolean;
9
+ left: number;
10
+ top: number;
11
+ contextSaved: boolean;
12
+ }
13
+
14
+ export const spriteGroupMap: WeakMap<SpriteGroup, GroupObj> = new WeakMap<
15
+ SpriteGroup,
16
+ GroupObj
17
+ >();
18
+
19
+ export function initGroup(group: SpriteGroup): void {
20
+ spriteGroupMap.set(group, {
21
+ oldAlpha: 1,
22
+ alphaChanged: false,
23
+ oldBlendMode: "source-over",
24
+ blendModeChanged: false,
25
+ left: 0,
26
+ top: 0,
27
+ contextSaved: false,
28
+ });
29
+ }
30
+
31
+ export function removeGroup(_group: SpriteGroup): void {
32
+ // no-op
33
+ }
34
+
35
+ export function drawGroupBeforeChildren(
36
+ group: SpriteGroup,
37
+ _interp: number,
38
+ ): void {
39
+ const opacity = group._opacity;
40
+ const scaleh = group._scaleh;
41
+ const scalev = group._scalev;
42
+
43
+ if (
44
+ group._layers.length !== 0 &&
45
+ opacity !== 0 &&
46
+ scaleh !== 0 &&
47
+ scalev !== 0 &&
48
+ !group._hidden
49
+ ) {
50
+ const playground = group.playground!;
51
+
52
+ const playgroundData = playgroundMap.get(playground)!;
53
+ const ctx = playgroundData.ctx;
54
+
55
+ const groupData = spriteGroupMap.get(group)!;
56
+
57
+ groupData.oldAlpha = playgroundData.globalAlpha;
58
+ groupData.alphaChanged = false;
59
+ if (opacity !== 1) {
60
+ // Don't save the entire context only for opacity changes
61
+ playgroundData.globalAlpha *= opacity;
62
+
63
+ ctx.globalAlpha = playgroundData.globalAlpha;
64
+
65
+ groupData.alphaChanged = true;
66
+ }
67
+
68
+ groupData.blendModeChanged = false;
69
+ const blendMode = group._blendMode;
70
+ if (blendMode !== "normal") {
71
+ groupData.oldBlendMode = ctx.globalCompositeOperation;
72
+
73
+ if (blendMode === "add") {
74
+ ctx.globalCompositeOperation = "lighter";
75
+ } else {
76
+ ctx.globalCompositeOperation = blendMode;
77
+ }
78
+
79
+ groupData.blendModeChanged = true;
80
+ }
81
+
82
+ const angle = group._angle;
83
+ const crop = group._crop;
84
+
85
+ groupData.contextSaved = false;
86
+ if (angle !== 0 || scaleh !== 1 || scalev !== 1 || crop) {
87
+ ctx.save();
88
+ groupData.contextSaved = true;
89
+ }
90
+
91
+ const trunc = Math.trunc;
92
+ const left = group._drawLeft;
93
+ const top = group._drawTop;
94
+
95
+ if (angle !== 0 || scaleh !== 1 || scalev !== 1) {
96
+ let transformOriginx = group._transformOriginx;
97
+
98
+ if (typeof transformOriginx === "string") {
99
+ transformOriginx = group[transformOriginx];
100
+ }
101
+
102
+ transformOriginx = trunc(transformOriginx);
103
+
104
+ let transformOriginy = group._transformOriginy;
105
+
106
+ if (typeof transformOriginy === "string") {
107
+ transformOriginy = group[transformOriginy];
108
+ }
109
+
110
+ transformOriginy = trunc(transformOriginy);
111
+
112
+ ctx.translate(left + transformOriginx, top + transformOriginy);
113
+
114
+ if (angle !== 0) {
115
+ ctx.rotate(angle);
116
+ }
117
+
118
+ if (scaleh !== 1 || scalev !== 1) {
119
+ ctx.scale(scaleh, scalev);
120
+ }
121
+
122
+ ctx.translate(-transformOriginx, -transformOriginy);
123
+ } else {
124
+ groupData.left = left;
125
+ groupData.top = top;
126
+
127
+ if (left !== 0 || top !== 0) {
128
+ ctx.translate(left, top);
129
+ }
130
+ }
131
+
132
+ if (crop) {
133
+ // Prepare a rect path for the clipping region
134
+ ctx.beginPath();
135
+
136
+ const width = group._drawWidth;
137
+ const height = group._drawHeight;
138
+ const border_radius = trunc(group._borderRadius);
139
+
140
+ if (border_radius > 0) {
141
+ ctx.roundRect(0, 0, width, height, border_radius);
142
+ } else {
143
+ ctx.rect(0, 0, width, height);
144
+ }
145
+
146
+ ctx.closePath();
147
+
148
+ ctx.clip();
149
+ }
150
+ }
151
+ }
152
+
153
+ export function drawGroupAfterChildren(
154
+ group: SpriteGroup,
155
+ _interp: number,
156
+ ): void {
157
+ if (
158
+ group._layers.length !== 0 &&
159
+ group._opacity !== 0 &&
160
+ group._scaleh !== 0 &&
161
+ group._scalev !== 0 &&
162
+ !group._hidden
163
+ ) {
164
+ const playground = group.playground!;
165
+ const playgroundData = playgroundMap.get(playground)!;
166
+ const groupData = spriteGroupMap.get(group)!;
167
+
168
+ const ctx = playgroundData.ctx;
169
+
170
+ if (groupData.contextSaved) {
171
+ // ctx.restore restores also the globalCompositeOperation and globalAlpha values
172
+ ctx.restore();
173
+
174
+ playgroundData.globalAlpha = groupData.oldAlpha;
175
+ } else {
176
+ const left = groupData.left;
177
+ const top = groupData.top;
178
+
179
+ if (left !== 0 || top !== 0) {
180
+ ctx.translate(-left, -top);
181
+ }
182
+ }
183
+
184
+ if (groupData.blendModeChanged) {
185
+ ctx.globalCompositeOperation = groupData.oldBlendMode;
186
+ }
187
+
188
+ if (groupData.alphaChanged) {
189
+ ctx.globalAlpha = groupData.oldAlpha;
190
+
191
+ playgroundData.globalAlpha = groupData.oldAlpha;
192
+ }
193
+ }
194
+ }
@@ -0,0 +1,103 @@
1
+ import type { Playground, PlaygroundOptions } from "../Playground.js";
2
+
3
+ export interface PlaygroundObj {
4
+ ctx: CanvasRenderingContext2D;
5
+ globalAlpha: number;
6
+ }
7
+
8
+ const canvasRegistry = new FinalizationRegistry(
9
+ (canvas: HTMLCanvasElement): void => {
10
+ if (canvas.parentNode !== null) {
11
+ canvas.parentNode.removeChild(canvas);
12
+ }
13
+ },
14
+ );
15
+
16
+ export const playgroundMap: WeakMap<Playground, PlaygroundObj> = new WeakMap<
17
+ Playground,
18
+ PlaygroundObj
19
+ >();
20
+
21
+ export function initPlayground(
22
+ playground: Playground,
23
+ options?: Partial<PlaygroundOptions>,
24
+ ): [number, number] {
25
+ const dom = options?.dom;
26
+
27
+ const parentDOM = ((): HTMLElement => {
28
+ if (typeof dom === "string") {
29
+ // Allow the ID to start with the '#' symbol
30
+ if (dom.startsWith("#")) {
31
+ return document.getElementById(dom.slice(1))!;
32
+ } else {
33
+ return document.getElementById(dom)!;
34
+ }
35
+ } else if (dom === undefined) {
36
+ // Default to the element with id of 'playground'
37
+ return document.getElementById("playground")!;
38
+ } else {
39
+ return dom as HTMLElement;
40
+ }
41
+ })();
42
+
43
+ const [ctx, width, height] = ((): [
44
+ CanvasRenderingContext2D,
45
+ number,
46
+ number,
47
+ ] => {
48
+ if (parentDOM instanceof HTMLCanvasElement) {
49
+ const ctx = parentDOM.getContext("2d");
50
+
51
+ const width = parentDOM.width || 300;
52
+ const height = parentDOM.height || 150;
53
+
54
+ return [ctx!, width, height];
55
+ } else {
56
+ const width = parentDOM.offsetWidth || 300;
57
+ const height = parentDOM.offsetHeight || 150;
58
+
59
+ const canvas = document.createElement("canvas");
60
+ canvas.width = width;
61
+ canvas.height = height;
62
+ parentDOM.insertBefore(canvas, parentDOM.firstChild);
63
+ canvas.className = options?.cssClass ?? "friGame"; // Reset background properties set by external CSS
64
+ Object.assign(canvas.style, {
65
+ left: "0px",
66
+ top: "0px",
67
+ width: `${width}px`,
68
+ height: `${height}px`,
69
+ overflow: "hidden",
70
+ });
71
+
72
+ canvasRegistry.register(playground, canvas);
73
+
74
+ const ctx = canvas.getContext("2d");
75
+
76
+ return [ctx!, width, height];
77
+ }
78
+ })();
79
+
80
+ playgroundMap.set(playground, {
81
+ ctx,
82
+ globalAlpha: 1,
83
+ });
84
+
85
+ return [width, height];
86
+ }
87
+
88
+ export function drawPlaygroundBeforeChildren(
89
+ playground: Playground,
90
+ _interp: number,
91
+ ): void {
92
+ const playgroundData = playgroundMap.get(playground)!;
93
+
94
+ playgroundData.ctx.clearRect(0, 0, playground._width, playground._height);
95
+ playgroundData.globalAlpha = 1;
96
+ }
97
+
98
+ export function drawPlaygroundAfterChildren(
99
+ _playground: Playground,
100
+ _interp: number,
101
+ ): void {
102
+ // no-op
103
+ }