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,183 @@
1
+ import type { ColorArr, ColorObj, Gradient } from "../../Gradient.js";
2
+ import type { RectSizeX, RectSizeY } from "../../Rect.js";
3
+ import { Rectangle } from "../../Rectangle.js";
4
+ import type { SpriteRef } from "../../utils.js";
5
+ import { pick } from "../../utils.js";
6
+ import type { SortedRectangleOptions } from "../sorted/SortedRectangle.js";
7
+ import { SortedRectangle } from "../sorted/SortedRectangle.js";
8
+ import type { ISOOptions } from "./ISOMixin.js";
9
+ import { ISO } from "./ISOMixin.js";
10
+ import type { ISOSpriteGroup } from "./ISOSpriteGroup.js";
11
+
12
+ export interface ISORectangleOptions
13
+ extends SortedRectangleOptions,
14
+ ISOOptions {}
15
+
16
+ const ISOBaseRectangle = ISO(Rectangle);
17
+
18
+ export class ISORectangle
19
+ extends ISOBaseRectangle
20
+ implements ISORectangleOptions
21
+ {
22
+ override _screen_obj: SortedRectangle | null = null;
23
+
24
+ // Proxy getters & setters
25
+
26
+ override get background(): Gradient | null {
27
+ return super.background;
28
+ }
29
+
30
+ override set background(value:
31
+ | Gradient
32
+ | Partial<ColorObj>
33
+ | Partial<ColorArr>
34
+ | null) {
35
+ super.background = value;
36
+
37
+ if (this._screen_obj !== null) {
38
+ this._screen_obj.background = value;
39
+ }
40
+ }
41
+
42
+ override get borderRadius(): number {
43
+ return super.borderRadius;
44
+ }
45
+
46
+ override set borderRadius(value: number) {
47
+ super.borderRadius = value;
48
+
49
+ if (this._screen_obj !== null) {
50
+ this._screen_obj.borderRadius = value;
51
+ }
52
+ }
53
+
54
+ override get borderWidth(): number {
55
+ return super.borderWidth;
56
+ }
57
+
58
+ override set borderWidth(value: number) {
59
+ super.borderWidth = value;
60
+
61
+ if (this._screen_obj !== null) {
62
+ this._screen_obj.borderWidth = value;
63
+ }
64
+ }
65
+
66
+ override get borderColor(): Gradient | null {
67
+ return super.borderColor;
68
+ }
69
+
70
+ override set borderColor(value:
71
+ | Gradient
72
+ | Partial<ColorObj>
73
+ | Partial<ColorArr>
74
+ | null) {
75
+ super.borderColor = value;
76
+
77
+ if (this._screen_obj !== null) {
78
+ this._screen_obj.borderColor = value;
79
+ }
80
+ }
81
+
82
+ constructor(
83
+ options?: Partial<ISORectangleOptions> & { ref?: SpriteRef<ISORectangle> },
84
+ ) {
85
+ super();
86
+
87
+ if (options !== undefined) {
88
+ Object.assign(
89
+ this,
90
+ pick(options, [
91
+ "left",
92
+ "right",
93
+ "centerx",
94
+ "top",
95
+ "bottom",
96
+ "centery",
97
+ "width",
98
+ "halfWidth",
99
+ "height",
100
+ "halfHeight",
101
+ "radius",
102
+ "transformOrigin",
103
+ "transformOriginx",
104
+ "transformOriginy",
105
+ "angle",
106
+ "scale",
107
+ "scalex",
108
+ "scaley",
109
+ "flip",
110
+ "fliph",
111
+ "flipv",
112
+ "opacity",
113
+ "hidden",
114
+ "blendMode",
115
+ "background",
116
+ "borderRadius",
117
+ "borderWidth",
118
+ "borderColor",
119
+ "originx",
120
+ "originy",
121
+ "referencex",
122
+ "referencey",
123
+ "elevation",
124
+ ]),
125
+ );
126
+
127
+ if (options.ref !== undefined) {
128
+ options.ref.current = this;
129
+ }
130
+ }
131
+
132
+ this._move(null, 0);
133
+ this.teleport();
134
+ }
135
+
136
+ // Implementation details
137
+
138
+ override _resize(
139
+ prop: keyof RectSizeX | keyof RectSizeY,
140
+ value: number,
141
+ ): void {
142
+ // TODO: I'm not sure that proxying the new size is the right approach here,
143
+ // as the screen object is merely a graphical representation of a projection.
144
+ // In theory the size of any ISO should be independent of the size of its
145
+ // screen object.
146
+ super._resize(prop, value);
147
+
148
+ this._screen_obj?._resize(prop, value);
149
+ }
150
+
151
+ override _onReparent(): void {
152
+ // The screen rectangle must be created in the screen layer
153
+ const parent = this.parent as ISOSpriteGroup;
154
+ const parent_screen_obj = parent._screen_obj!;
155
+
156
+ // TODO: Is the newly created screen object in the correct drawing order?
157
+ this._screen_obj = parent_screen_obj.addChild(
158
+ new SortedRectangle({
159
+ width: this.width,
160
+ height: this.height,
161
+ transformOriginx: this.transformOriginx,
162
+ transformOriginy: this.transformOriginy,
163
+ angle: this.angle,
164
+ scalex: this.scalex,
165
+ scaley: this.scaley,
166
+ fliph: this.fliph,
167
+ flipv: this.flipv,
168
+ opacity: this.opacity,
169
+ hidden: this.hidden,
170
+ blendMode: this.blendMode,
171
+ background: this.background,
172
+ borderRadius: this.borderRadius,
173
+ borderWidth: this.borderWidth,
174
+ borderColor: this.borderColor,
175
+ originx: this.originx,
176
+ originy: this.originy,
177
+ }),
178
+ );
179
+
180
+ this._move("elevation", this.elevation);
181
+ this.teleport();
182
+ }
183
+ }
@@ -0,0 +1,228 @@
1
+ import { Sprite } from "../../Sprite.js";
2
+ import type { SpriteRef } from "../../utils.js";
3
+ import { pick } from "../../utils.js";
4
+ import type { SortedAnimation } from "../sorted/SortedAnimation.js";
5
+ import type { SortedSpriteOptions } from "../sorted/SortedSprite.js";
6
+ import { SortedSprite } from "../sorted/SortedSprite.js";
7
+ import type { ISOOptions } from "./ISOMixin.js";
8
+ import { ISO } from "./ISOMixin.js";
9
+ import type { ISOSpriteGroup } from "./ISOSpriteGroup.js";
10
+
11
+ export interface ISOSpriteOptions extends SortedSpriteOptions, ISOOptions {}
12
+
13
+ const ISOBaseSprite = ISO(Sprite);
14
+
15
+ export class ISOSprite extends ISOBaseSprite implements ISOSpriteOptions {
16
+ override _screen_obj: SortedSprite | null = null;
17
+
18
+ // Proxy getters & setters
19
+
20
+ override get animation(): SortedAnimation | null {
21
+ return super.animation as SortedAnimation | null;
22
+ }
23
+
24
+ override set animation(value: SortedAnimation | null) {
25
+ if (value !== super.animation) {
26
+ const width = super.width;
27
+ const height = super.height;
28
+
29
+ super.animation = value;
30
+
31
+ super.width = width;
32
+ super.height = height;
33
+
34
+ if (this._screen_obj !== null) {
35
+ this._screen_obj.animation = value;
36
+ }
37
+
38
+ // Given that changing the animation changes the originy,
39
+ // we need to update the originy *after* changing the animation
40
+ // on the screen obj
41
+ if (value !== null) {
42
+ this.originx = value.originx;
43
+ this.originy = value.originy;
44
+ }
45
+ }
46
+ }
47
+
48
+ override get animationIndex(): number {
49
+ return super.animationIndex;
50
+ }
51
+
52
+ override set animationIndex(value: number) {
53
+ super.animationIndex = value;
54
+
55
+ if (this._screen_obj !== null) {
56
+ this._screen_obj.animationIndex = value;
57
+ }
58
+ }
59
+
60
+ override get callback(): (() => void) | null {
61
+ return super.callback;
62
+ }
63
+
64
+ override set callback(value: (() => void) | null) {
65
+ super.callback = value;
66
+
67
+ if (this._screen_obj !== null) {
68
+ this._screen_obj.callback = value;
69
+ }
70
+ }
71
+
72
+ override get paused(): boolean {
73
+ return super.paused;
74
+ }
75
+
76
+ override set paused(value: boolean) {
77
+ super.paused = value;
78
+
79
+ if (this._screen_obj !== null) {
80
+ this._screen_obj.paused = value;
81
+ }
82
+ }
83
+
84
+ override get rate(): number {
85
+ return super.rate;
86
+ }
87
+
88
+ override set rate(value: number) {
89
+ super.rate = value;
90
+
91
+ if (this._screen_obj !== null) {
92
+ this._screen_obj.rate = value;
93
+ }
94
+ }
95
+
96
+ override get once(): boolean {
97
+ return super.once;
98
+ }
99
+
100
+ override set once(value: boolean) {
101
+ super.once = value;
102
+
103
+ if (this._screen_obj !== null) {
104
+ this._screen_obj.once = value;
105
+ }
106
+ }
107
+
108
+ override get pingpong(): boolean {
109
+ return super.pingpong;
110
+ }
111
+
112
+ override set pingpong(value: boolean) {
113
+ super.pingpong = value;
114
+
115
+ if (this._screen_obj !== null) {
116
+ this._screen_obj.pingpong = value;
117
+ }
118
+ }
119
+
120
+ override get backwards(): boolean {
121
+ return super.backwards;
122
+ }
123
+
124
+ override set backwards(value: boolean) {
125
+ super.backwards = value;
126
+
127
+ if (this._screen_obj !== null) {
128
+ this._screen_obj.backwards = value;
129
+ }
130
+ }
131
+
132
+ constructor(
133
+ options?: Partial<ISOSpriteOptions> & { ref?: SpriteRef<ISOSprite> },
134
+ ) {
135
+ super();
136
+
137
+ if (options !== undefined) {
138
+ Object.assign(
139
+ this,
140
+ pick(options, [
141
+ "left",
142
+ "right",
143
+ "centerx",
144
+ "top",
145
+ "bottom",
146
+ "centery",
147
+ "width",
148
+ "halfWidth",
149
+ "height",
150
+ "halfHeight",
151
+ "radius",
152
+ "transformOrigin",
153
+ "transformOriginx",
154
+ "transformOriginy",
155
+ "angle",
156
+ "scale",
157
+ "scalex",
158
+ "scaley",
159
+ "flip",
160
+ "fliph",
161
+ "flipv",
162
+ "opacity",
163
+ "hidden",
164
+ "blendMode",
165
+ "animation",
166
+ "animationIndex",
167
+ "callback",
168
+ "paused",
169
+ "rate",
170
+ "once",
171
+ "pingpong",
172
+ "backwards",
173
+ "originx",
174
+ "originy",
175
+ "referencex",
176
+ "referencey",
177
+ "elevation",
178
+ ]),
179
+ );
180
+
181
+ if (options.ref !== undefined) {
182
+ options.ref.current = this;
183
+ }
184
+ }
185
+
186
+ this._move(null, 0);
187
+ this.teleport();
188
+ }
189
+
190
+ // The _resize function has been deliberately omitted,
191
+ // as the screen object automatically gets the size of the animation
192
+ // and the size of the ISOSprite is independent of it
193
+
194
+ override _onReparent(): void {
195
+ // The screen sprite must be created in the screen layer
196
+ const parent = this.parent as ISOSpriteGroup;
197
+ const parent_screen_obj = parent._screen_obj!;
198
+
199
+ // TODO: Is the newly created screen object in the correct drawing order?
200
+ this._screen_obj = parent_screen_obj.addChild(
201
+ new SortedSprite({
202
+ transformOriginx: this.transformOriginx,
203
+ transformOriginy: this.transformOriginy,
204
+ angle: this.angle,
205
+ scalex: this.scalex,
206
+ scaley: this.scaley,
207
+ fliph: this.fliph,
208
+ flipv: this.flipv,
209
+ opacity: this.opacity,
210
+ hidden: this.hidden,
211
+ blendMode: this.blendMode,
212
+ animation: this.animation,
213
+ animationIndex: this.animationIndex,
214
+ callback: this.callback,
215
+ paused: this.paused,
216
+ rate: this.rate,
217
+ once: this.once,
218
+ pingpong: this.pingpong,
219
+ backwards: this.backwards,
220
+ originx: this.originx,
221
+ originy: this.originy,
222
+ }),
223
+ );
224
+
225
+ this._move("elevation", this.elevation);
226
+ this.teleport();
227
+ }
228
+ }
@@ -0,0 +1,155 @@
1
+ import type { RectSizeX, RectSizeY } from "../../Rect.js";
2
+ import { SpriteGroup } from "../../SpriteGroup.js";
3
+ import type { SpriteRef } from "../../utils.js";
4
+ import { pick } from "../../utils.js";
5
+ import type { SortedGroupOptions } from "../sorted/SortedGroup.js";
6
+ import { SortedGroup } from "../sorted/SortedGroup.js";
7
+ import type { ISOOptions } from "./ISOMixin.js";
8
+ import { ISO } from "./ISOMixin.js";
9
+
10
+ export interface ISOGroupOptions extends SortedGroupOptions, ISOOptions {}
11
+
12
+ const ISOBaseGroup = ISO(SpriteGroup);
13
+
14
+ export class ISOSpriteGroup extends ISOBaseGroup {
15
+ override _originx: keyof RectSizeX | number = 0;
16
+ override _originy: keyof RectSizeY | number = 0;
17
+ override _referencex: keyof RectSizeX | number = 0;
18
+ override _referencey: keyof RectSizeY | number = 0;
19
+ override _screen_obj: SortedGroup | null = null;
20
+
21
+ // Proxy getters & setters
22
+
23
+ override get crop(): boolean {
24
+ return super.crop;
25
+ }
26
+
27
+ override set crop(value: boolean) {
28
+ super.crop = value;
29
+
30
+ if (this._screen_obj !== null) {
31
+ this._screen_obj.crop = value;
32
+ }
33
+ }
34
+
35
+ override get borderRadius(): number {
36
+ return super.borderRadius;
37
+ }
38
+
39
+ override set borderRadius(value: number) {
40
+ super.borderRadius = value;
41
+
42
+ if (this._screen_obj !== null) {
43
+ this._screen_obj.borderRadius = value;
44
+ }
45
+ }
46
+
47
+ constructor(
48
+ options?: Partial<ISOGroupOptions> & { ref?: SpriteRef<ISOSpriteGroup> },
49
+ ) {
50
+ super();
51
+
52
+ if (options !== undefined) {
53
+ Object.assign(
54
+ this,
55
+ pick(options, [
56
+ "left",
57
+ "right",
58
+ "centerx",
59
+ "top",
60
+ "bottom",
61
+ "centery",
62
+ "width",
63
+ "halfWidth",
64
+ "height",
65
+ "halfHeight",
66
+ "radius",
67
+ "transformOrigin",
68
+ "transformOriginx",
69
+ "transformOriginy",
70
+ "angle",
71
+ "scale",
72
+ "scalex",
73
+ "scaley",
74
+ "flip",
75
+ "fliph",
76
+ "flipv",
77
+ "opacity",
78
+ "hidden",
79
+ "blendMode",
80
+ "crop",
81
+ "borderRadius",
82
+ "originx",
83
+ "originy",
84
+ "referencex",
85
+ "referencey",
86
+ "elevation",
87
+ ]),
88
+ );
89
+
90
+ if (options.children !== undefined) {
91
+ this.addChildren(options.children);
92
+ }
93
+
94
+ if (options.ref !== undefined) {
95
+ options.ref.current = this;
96
+ }
97
+ }
98
+
99
+ this._move(null, 0);
100
+ this.teleport();
101
+ }
102
+
103
+ // Implementation details
104
+
105
+ override _resize(
106
+ prop: keyof RectSizeX | keyof RectSizeY,
107
+ value: number,
108
+ ): void {
109
+ // TODO: I'm not sure that proxying the new size is the right approach here,
110
+ // as the screen object is merely a graphical representation of a projection.
111
+ // In theory the size of any ISO should be independent of the size of its
112
+ // screen object.
113
+ super._resize(prop, value);
114
+
115
+ this._screen_obj?._resize(prop, value);
116
+ }
117
+
118
+ override _onReparent(): void {
119
+ // The screen sprite group must be created in the screen layer
120
+ const parent = this.parent!;
121
+ const parent_screen_obj =
122
+ "_screen_obj" in parent ? (parent._screen_obj as SortedGroup) : parent;
123
+
124
+ // TODO: Is the newly created screen object in the correct drawing order?
125
+ this._screen_obj = parent_screen_obj.addChild(
126
+ new SortedGroup({
127
+ width: this.width,
128
+ height: this.height,
129
+ transformOriginx: this.transformOriginx,
130
+ transformOriginy: this.transformOriginy,
131
+ angle: this.angle,
132
+ scalex: this.scalex,
133
+ scaley: this.scaley,
134
+ fliph: this.fliph,
135
+ flipv: this.flipv,
136
+ opacity: this.opacity,
137
+ hidden: this.hidden,
138
+ blendMode: this.blendMode,
139
+ crop: this.crop,
140
+ borderRadius: this.borderRadius,
141
+ originx: this.originx,
142
+ originy: this.originy,
143
+ }),
144
+ );
145
+
146
+ this._move("elevation", this.elevation);
147
+ this.teleport();
148
+ }
149
+
150
+ override _remove(): void {
151
+ super._remove();
152
+
153
+ this.clear();
154
+ }
155
+ }
@@ -0,0 +1,122 @@
1
+ import type { BaseSprite } from "../../BaseSprite.js";
2
+ import type { SpriteRef } from "../../utils.js";
3
+ import type { ISORectangleOptions } from "./ISORectangle.js";
4
+ import { ISORectangle } from "./ISORectangle.js";
5
+ import type { ISOSpriteOptions } from "./ISOSprite.js";
6
+ import { ISOSprite } from "./ISOSprite.js";
7
+ import type { ISOGroupOptions } from "./ISOSpriteGroup.js";
8
+ import { ISOSpriteGroup } from "./ISOSpriteGroup.js";
9
+
10
+ // animationList MUST have at least `animation` or `background`
11
+ export type ISOTileOptions =
12
+ | (Pick<ISOSpriteOptions, "animation"> & Partial<ISOSpriteOptions>)
13
+ | (Pick<ISORectangleOptions, "background"> & Partial<ISORectangleOptions>);
14
+
15
+ export interface ISOTilemapOptions {
16
+ sizex: number; // Num tiles
17
+ sizey: number; // Num tiles
18
+ tileSize: number; // Pixel
19
+ data: number[]; // (sizex * sizey) members, indices of animationList
20
+ animationList: Record<number, ISOTileOptions>;
21
+ }
22
+
23
+ export class ISOTilemap extends ISOSpriteGroup {
24
+ sizex = 0;
25
+ sizey = 0;
26
+ tileSize = 0;
27
+
28
+ _locations: Uint32Array;
29
+
30
+ constructor(
31
+ options: ISOTilemapOptions &
32
+ Partial<ISOGroupOptions> & { ref?: SpriteRef<ISOTilemap> },
33
+ ) {
34
+ super(options);
35
+
36
+ const { sizex, sizey, tileSize } = options;
37
+
38
+ this.sizex = sizex;
39
+ this.sizey = sizey;
40
+ this.tileSize = tileSize;
41
+
42
+ const locations = new Uint32Array(sizex * sizey).fill(0xffffffff);
43
+ this._locations = locations;
44
+ const layers = this._layers;
45
+
46
+ if (
47
+ options.width === undefined &&
48
+ options.halfWidth === undefined &&
49
+ options.radius === undefined
50
+ ) {
51
+ this.width = sizex * tileSize;
52
+ }
53
+
54
+ if (
55
+ options.height === undefined &&
56
+ options.halfHeight === undefined &&
57
+ options.radius === undefined
58
+ ) {
59
+ this.height = sizey * tileSize;
60
+ }
61
+
62
+ const animationList = options.animationList;
63
+
64
+ let i_location = 0;
65
+ let row = 0;
66
+ let col = 0;
67
+ let left = 0;
68
+ let top = 0;
69
+ for (const data of options.data) {
70
+ const animation_options = animationList[data];
71
+ if (animation_options !== undefined && "animation" in animation_options) {
72
+ // Sprite
73
+ const sprite_options = Object.assign({}, animation_options, {
74
+ left,
75
+ top,
76
+ width: tileSize,
77
+ height: tileSize,
78
+ });
79
+ this.addChild(new ISOSprite(sprite_options));
80
+ locations[i_location] = layers.length - 1;
81
+ } else if (
82
+ animation_options !== undefined &&
83
+ "background" in animation_options
84
+ ) {
85
+ // Rectangle
86
+ const rectangle_options = Object.assign({}, animation_options, {
87
+ left,
88
+ top,
89
+ width: tileSize,
90
+ height: tileSize,
91
+ });
92
+ this.addChild(new ISORectangle(rectangle_options));
93
+ locations[i_location] = layers.length - 1;
94
+ } else {
95
+ // TODO: Invalid
96
+ }
97
+
98
+ i_location += 1;
99
+ left += tileSize;
100
+ col += 1;
101
+ if (col >= sizex) {
102
+ col = 0;
103
+ left = 0;
104
+ row += 1;
105
+ top += tileSize;
106
+ if (row >= sizey) {
107
+ break;
108
+ }
109
+ }
110
+ }
111
+ }
112
+
113
+ getAt(row: number, col: number): BaseSprite | undefined {
114
+ const i = row * this.sizex + col;
115
+
116
+ if (i < this._locations.length) {
117
+ return this._layers[this._locations[i]!];
118
+ } else {
119
+ return undefined;
120
+ }
121
+ }
122
+ }
@@ -0,0 +1,19 @@
1
+ export function screenFromGrid(
2
+ grid_x: number,
3
+ grid_y: number,
4
+ ): [screen_x: number, screen_y: number] {
5
+ const screen_x = grid_x - grid_y;
6
+ const screen_y = (grid_x + grid_y) / 2;
7
+
8
+ return [screen_x, screen_y];
9
+ }
10
+
11
+ export function gridFromScreen(
12
+ screen_x: number,
13
+ screen_y: number,
14
+ ): [grid_x: number, grid_y: number] {
15
+ const grid_x = screen_y + screen_x / 2;
16
+ const grid_y = screen_y - screen_x / 2;
17
+
18
+ return [grid_x, grid_y];
19
+ }