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