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.
- package/LICENSE +373 -0
- package/README.md +80 -2
- package/dist/Animation.d.ts +42 -0
- package/dist/Animation.js +189 -0
- package/dist/BaseSprite.d.ts +116 -0
- package/dist/BaseSprite.js +518 -0
- package/dist/Gradient.d.ts +31 -0
- package/dist/Gradient.js +89 -0
- package/dist/Playground.d.ts +48 -0
- package/dist/Playground.js +167 -0
- package/dist/Rect.d.ts +63 -0
- package/dist/Rect.js +155 -0
- package/dist/Rectangle.d.ts +31 -0
- package/dist/Rectangle.js +69 -0
- package/dist/Renderer.d.ts +23 -0
- package/dist/Renderer.js +1 -0
- package/dist/ResourceManager.d.ts +21 -0
- package/dist/ResourceManager.js +100 -0
- package/dist/Sprite.d.ts +56 -0
- package/dist/Sprite.js +437 -0
- package/dist/SpriteGroup.d.ts +39 -0
- package/dist/SpriteGroup.js +176 -0
- package/dist/canvasRenderer/renderGroup.d.ts +15 -0
- package/dist/canvasRenderer/renderGroup.js +134 -0
- package/dist/canvasRenderer/renderPlayground.d.ts +9 -0
- package/dist/canvasRenderer/renderPlayground.js +67 -0
- package/dist/canvasRenderer/renderRectangle.d.ts +15 -0
- package/dist/canvasRenderer/renderRectangle.js +287 -0
- package/dist/canvasRenderer/renderSprite.d.ts +4 -0
- package/dist/canvasRenderer/renderSprite.js +93 -0
- package/dist/canvasRenderer.d.ts +2 -0
- package/dist/canvasRenderer.js +19 -0
- package/dist/dummyRenderer.d.ts +2 -0
- package/dist/dummyRenderer.js +41 -0
- package/dist/plugins/GamepadTracker.d.ts +15 -0
- package/dist/plugins/GamepadTracker.js +74 -0
- package/dist/plugins/Tilemap.d.ts +26 -0
- package/dist/plugins/Tilemap.js +85 -0
- package/dist/plugins/Vec2.d.ts +69 -0
- package/dist/plugins/Vec2.js +296 -0
- package/dist/plugins/fx/Shaker.d.ts +46 -0
- package/dist/plugins/fx/Shaker.js +190 -0
- package/dist/plugins/fx/Tweener.d.ts +42 -0
- package/dist/plugins/fx/Tweener.js +166 -0
- package/dist/plugins/fx/easing.d.ts +32 -0
- package/dist/plugins/fx/easing.js +192 -0
- package/dist/plugins/isometric/ISOMixin.d.ts +138 -0
- package/dist/plugins/isometric/ISOMixin.js +322 -0
- package/dist/plugins/isometric/ISORectangle.d.ts +151 -0
- package/dist/plugins/isometric/ISORectangle.js +127 -0
- package/dist/plugins/isometric/ISOSprite.d.ts +157 -0
- package/dist/plugins/isometric/ISOSprite.js +176 -0
- package/dist/plugins/isometric/ISOSpriteGroup.d.ts +151 -0
- package/dist/plugins/isometric/ISOSpriteGroup.js +116 -0
- package/dist/plugins/isometric/ISOTilemap.d.ts +24 -0
- package/dist/plugins/isometric/ISOTilemap.js +85 -0
- package/dist/plugins/isometric/utils.d.ts +2 -0
- package/dist/plugins/isometric/utils.js +10 -0
- package/dist/plugins/keyTracker.d.ts +3 -0
- package/dist/plugins/keyTracker.js +23 -0
- package/dist/plugins/mixer.d.ts +72 -0
- package/dist/plugins/mixer.js +396 -0
- package/dist/plugins/sorted/SortedAnimation.d.ts +13 -0
- package/dist/plugins/sorted/SortedAnimation.js +27 -0
- package/dist/plugins/sorted/SortedGroup.d.ts +137 -0
- package/dist/plugins/sorted/SortedGroup.js +61 -0
- package/dist/plugins/sorted/SortedMixin.d.ts +122 -0
- package/dist/plugins/sorted/SortedMixin.js +153 -0
- package/dist/plugins/sorted/SortedRectangle.d.ts +127 -0
- package/dist/plugins/sorted/SortedRectangle.js +17 -0
- package/dist/plugins/sorted/SortedSprite.d.ts +130 -0
- package/dist/plugins/sorted/SortedSprite.js +35 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.js +33 -0
- package/frigame.css +12 -0
- package/package.json +54 -4
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { playgroundMap } from "./renderPlayground.js";
|
|
2
|
+
export const fillMap = new WeakMap();
|
|
3
|
+
export const strokeMap = new WeakMap();
|
|
4
|
+
export function initRectangle(rectangle) {
|
|
5
|
+
for (const map of [fillMap, strokeMap]) {
|
|
6
|
+
map.set(rectangle, {
|
|
7
|
+
width: 0,
|
|
8
|
+
height: 0,
|
|
9
|
+
startColorStr: "rgba(0,0,0,1)",
|
|
10
|
+
endColorStr: "rgba(0,0,0,1)",
|
|
11
|
+
type: "vertical",
|
|
12
|
+
gradient: null,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function removeRectangle(_rectangle) {
|
|
17
|
+
// no-op
|
|
18
|
+
}
|
|
19
|
+
export function drawRectangle(rectangle, _interp) {
|
|
20
|
+
const playground = rectangle.playground;
|
|
21
|
+
const trunc = Math.trunc;
|
|
22
|
+
const left = rectangle._drawLeft;
|
|
23
|
+
const top = rectangle._drawTop;
|
|
24
|
+
const width = rectangle._drawWidth;
|
|
25
|
+
const height = rectangle._drawHeight;
|
|
26
|
+
const insidePlayground = playground._insidePlayground(left, top, width, height);
|
|
27
|
+
const background = rectangle._background;
|
|
28
|
+
const border_width = trunc(rectangle._borderWidth);
|
|
29
|
+
const border_color = rectangle._borderColor;
|
|
30
|
+
const opacity = rectangle._opacity;
|
|
31
|
+
const scaleh = rectangle._scaleh;
|
|
32
|
+
const scalev = rectangle._scalev;
|
|
33
|
+
if (insidePlayground &&
|
|
34
|
+
(background !== null || (border_width > 0 && border_color !== null)) &&
|
|
35
|
+
opacity !== 0 &&
|
|
36
|
+
scaleh !== 0 &&
|
|
37
|
+
scalev !== 0 &&
|
|
38
|
+
!rectangle._hidden) {
|
|
39
|
+
const playgroundData = playgroundMap.get(playground);
|
|
40
|
+
const ctx = playgroundData.ctx;
|
|
41
|
+
let old_alpha = 1;
|
|
42
|
+
let alpha_changed = false;
|
|
43
|
+
if (opacity !== 1) {
|
|
44
|
+
// Don't save the entire context only for opacity changes
|
|
45
|
+
old_alpha = ctx.globalAlpha;
|
|
46
|
+
ctx.globalAlpha = old_alpha * opacity;
|
|
47
|
+
alpha_changed = true;
|
|
48
|
+
}
|
|
49
|
+
let old_blend_mode = "source-over";
|
|
50
|
+
let blend_mode_changed = false;
|
|
51
|
+
const blendMode = rectangle._blendMode;
|
|
52
|
+
if (blendMode !== "normal") {
|
|
53
|
+
old_blend_mode = ctx.globalCompositeOperation;
|
|
54
|
+
if (blendMode === "add") {
|
|
55
|
+
ctx.globalCompositeOperation = "lighter";
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
ctx.globalCompositeOperation = blendMode;
|
|
59
|
+
}
|
|
60
|
+
blend_mode_changed = true;
|
|
61
|
+
}
|
|
62
|
+
const angle = rectangle._angle;
|
|
63
|
+
const border_radius = trunc(rectangle._borderRadius);
|
|
64
|
+
const border_half_width = border_width / 2;
|
|
65
|
+
if (angle !== 0 || scaleh !== 1 || scalev !== 1) {
|
|
66
|
+
ctx.save();
|
|
67
|
+
let transformOriginx = rectangle._transformOriginx;
|
|
68
|
+
if (typeof transformOriginx === "string") {
|
|
69
|
+
transformOriginx = rectangle[transformOriginx];
|
|
70
|
+
}
|
|
71
|
+
transformOriginx = trunc(transformOriginx);
|
|
72
|
+
let transformOriginy = rectangle._transformOriginy;
|
|
73
|
+
if (typeof transformOriginy === "string") {
|
|
74
|
+
transformOriginy = rectangle[transformOriginy];
|
|
75
|
+
}
|
|
76
|
+
transformOriginy = trunc(transformOriginy);
|
|
77
|
+
ctx.translate(left + transformOriginx, top + transformOriginy);
|
|
78
|
+
if (angle !== 0) {
|
|
79
|
+
ctx.rotate(angle);
|
|
80
|
+
}
|
|
81
|
+
if (scaleh !== 1 || scalev !== 1) {
|
|
82
|
+
ctx.scale(scaleh, scalev);
|
|
83
|
+
}
|
|
84
|
+
ctx.translate(-transformOriginx, -transformOriginy);
|
|
85
|
+
if (background !== null) {
|
|
86
|
+
// Prepare a rect path for the background and the clipping region
|
|
87
|
+
ctx.beginPath();
|
|
88
|
+
if (border_radius > 0) {
|
|
89
|
+
ctx.roundRect(0, 0, width, height, border_radius);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
ctx.rect(0, 0, width, height);
|
|
93
|
+
}
|
|
94
|
+
ctx.closePath();
|
|
95
|
+
setFillStyle(ctx, rectangle);
|
|
96
|
+
ctx.fill();
|
|
97
|
+
}
|
|
98
|
+
if (border_width > 0 && border_color !== null) {
|
|
99
|
+
ctx.beginPath();
|
|
100
|
+
if (border_radius > 0) {
|
|
101
|
+
ctx.roundRect(-border_half_width, -border_half_width, width + border_width, height + border_width, border_radius + border_half_width);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
ctx.rect(-border_half_width, -border_half_width, width + border_width, height + border_width);
|
|
105
|
+
}
|
|
106
|
+
ctx.closePath();
|
|
107
|
+
setStrokeStyle(ctx, rectangle);
|
|
108
|
+
ctx.lineWidth = border_width;
|
|
109
|
+
ctx.stroke();
|
|
110
|
+
}
|
|
111
|
+
ctx.restore();
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
let translated = false;
|
|
115
|
+
if (background !== null) {
|
|
116
|
+
const startColorStr = background.startColor._str;
|
|
117
|
+
if (startColorStr === background.endColor._str) {
|
|
118
|
+
// Solid background
|
|
119
|
+
ctx.beginPath();
|
|
120
|
+
if (border_radius > 0) {
|
|
121
|
+
ctx.roundRect(left, top, width, height, border_radius);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
ctx.rect(left, top, width, height);
|
|
125
|
+
}
|
|
126
|
+
ctx.closePath();
|
|
127
|
+
ctx.fillStyle = startColorStr;
|
|
128
|
+
ctx.fill();
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
// Gradient background
|
|
132
|
+
if (left !== 0 || top !== 0) {
|
|
133
|
+
ctx.translate(left, top);
|
|
134
|
+
}
|
|
135
|
+
translated = true;
|
|
136
|
+
// Prepare a rect path for the background and the clipping region
|
|
137
|
+
ctx.beginPath();
|
|
138
|
+
if (border_radius > 0) {
|
|
139
|
+
ctx.roundRect(0, 0, width, height, border_radius);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
ctx.rect(0, 0, width, height);
|
|
143
|
+
}
|
|
144
|
+
ctx.closePath();
|
|
145
|
+
setFillStyle(ctx, rectangle);
|
|
146
|
+
ctx.fill();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (border_width > 0 && border_color !== null) {
|
|
150
|
+
const startColorStr = border_color.startColor._str;
|
|
151
|
+
if (startColorStr === border_color.endColor._str) {
|
|
152
|
+
// Solid border
|
|
153
|
+
ctx.beginPath();
|
|
154
|
+
if (translated) {
|
|
155
|
+
if (border_radius > 0) {
|
|
156
|
+
ctx.roundRect(-border_half_width, -border_half_width, width + border_width, height + border_width, border_radius + border_half_width);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
ctx.rect(-border_half_width, -border_half_width, width + border_width, height + border_width);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
if (border_radius > 0) {
|
|
164
|
+
ctx.roundRect(left - border_half_width, top - border_half_width, width + border_width, height + border_width, border_radius + border_half_width);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
ctx.rect(left - border_half_width, top - border_half_width, width + border_width, height + border_width);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
ctx.closePath();
|
|
171
|
+
ctx.strokeStyle = startColorStr;
|
|
172
|
+
ctx.lineWidth = border_width;
|
|
173
|
+
ctx.stroke();
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
// Gradient border
|
|
177
|
+
if (!translated && (left !== 0 || top !== 0)) {
|
|
178
|
+
ctx.translate(left, top);
|
|
179
|
+
}
|
|
180
|
+
translated = true;
|
|
181
|
+
ctx.beginPath();
|
|
182
|
+
if (border_radius > 0) {
|
|
183
|
+
ctx.roundRect(-border_half_width, -border_half_width, width + border_width, height + border_width, border_radius + border_half_width);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
ctx.rect(-border_half_width, -border_half_width, width + border_width, height + border_width);
|
|
187
|
+
}
|
|
188
|
+
ctx.closePath();
|
|
189
|
+
setStrokeStyle(ctx, rectangle);
|
|
190
|
+
ctx.lineWidth = border_width;
|
|
191
|
+
ctx.stroke();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (translated && (left !== 0 || top !== 0)) {
|
|
195
|
+
ctx.translate(-left, -top);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (blend_mode_changed) {
|
|
199
|
+
ctx.globalCompositeOperation = old_blend_mode;
|
|
200
|
+
}
|
|
201
|
+
if (alpha_changed) {
|
|
202
|
+
ctx.globalAlpha = old_alpha;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function setFillStyle(ctx, rectangle) {
|
|
207
|
+
const background = rectangle._background;
|
|
208
|
+
if (background !== null) {
|
|
209
|
+
const startColorStr = background.startColor._str;
|
|
210
|
+
const endColorStr = background.endColor._str;
|
|
211
|
+
if (startColorStr === endColorStr) {
|
|
212
|
+
// Solid background
|
|
213
|
+
ctx.fillStyle = startColorStr;
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
// Gradient background
|
|
217
|
+
const gradient_obj = fillMap.get(rectangle);
|
|
218
|
+
const trunc = Math.trunc;
|
|
219
|
+
const width = trunc(rectangle._width);
|
|
220
|
+
const height = trunc(rectangle._height);
|
|
221
|
+
const type = background.type;
|
|
222
|
+
if (gradient_obj.gradient === null ||
|
|
223
|
+
gradient_obj.width !== width ||
|
|
224
|
+
gradient_obj.height !== height ||
|
|
225
|
+
gradient_obj.startColorStr !== startColorStr ||
|
|
226
|
+
gradient_obj.endColorStr !== endColorStr ||
|
|
227
|
+
gradient_obj.type !== type) {
|
|
228
|
+
gradient_obj.width = width;
|
|
229
|
+
gradient_obj.height = height;
|
|
230
|
+
gradient_obj.startColorStr = startColorStr;
|
|
231
|
+
gradient_obj.endColorStr = endColorStr;
|
|
232
|
+
gradient_obj.type = type;
|
|
233
|
+
const end_x = type === "vertical" ? 0 : width;
|
|
234
|
+
const end_y = type === "vertical" ? height : 0;
|
|
235
|
+
const gradient = ctx.createLinearGradient(0, 0, end_x, end_y);
|
|
236
|
+
gradient.addColorStop(0, startColorStr);
|
|
237
|
+
gradient.addColorStop(1, endColorStr);
|
|
238
|
+
gradient_obj.gradient = gradient;
|
|
239
|
+
ctx.fillStyle = gradient;
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
ctx.fillStyle = gradient_obj.gradient;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function setStrokeStyle(ctx, rectangle) {
|
|
248
|
+
const border_color = rectangle._borderColor;
|
|
249
|
+
if (border_color !== null) {
|
|
250
|
+
const startColorStr = border_color.startColor._str;
|
|
251
|
+
const endColorStr = border_color.endColor._str;
|
|
252
|
+
if (startColorStr === endColorStr) {
|
|
253
|
+
// Solid border
|
|
254
|
+
ctx.strokeStyle = startColorStr;
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
// Gradient border
|
|
258
|
+
const gradient_obj = strokeMap.get(rectangle);
|
|
259
|
+
const trunc = Math.trunc;
|
|
260
|
+
const width = trunc(rectangle._width);
|
|
261
|
+
const height = trunc(rectangle._height);
|
|
262
|
+
const type = border_color.type;
|
|
263
|
+
if (gradient_obj.gradient === null ||
|
|
264
|
+
gradient_obj.width !== width ||
|
|
265
|
+
gradient_obj.height !== height ||
|
|
266
|
+
gradient_obj.startColorStr !== startColorStr ||
|
|
267
|
+
gradient_obj.endColorStr !== endColorStr ||
|
|
268
|
+
gradient_obj.type !== type) {
|
|
269
|
+
gradient_obj.width = width;
|
|
270
|
+
gradient_obj.height = height;
|
|
271
|
+
gradient_obj.startColorStr = startColorStr;
|
|
272
|
+
gradient_obj.endColorStr = endColorStr;
|
|
273
|
+
gradient_obj.type = type;
|
|
274
|
+
const end_x = type === "vertical" ? 0 : width;
|
|
275
|
+
const end_y = type === "vertical" ? height : 0;
|
|
276
|
+
const gradient = ctx.createLinearGradient(0, 0, end_x, end_y);
|
|
277
|
+
gradient.addColorStop(0, startColorStr);
|
|
278
|
+
gradient.addColorStop(1, endColorStr);
|
|
279
|
+
gradient_obj.gradient = gradient;
|
|
280
|
+
ctx.strokeStyle = gradient;
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
ctx.strokeStyle = gradient_obj.gradient;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { playgroundMap } from "./renderPlayground.js";
|
|
2
|
+
export function initSprite(_sprite) {
|
|
3
|
+
// no-op
|
|
4
|
+
}
|
|
5
|
+
export function removeSprite(_sprite) {
|
|
6
|
+
// no-op
|
|
7
|
+
}
|
|
8
|
+
export function drawSprite(sprite, _interp) {
|
|
9
|
+
const playground = sprite.playground;
|
|
10
|
+
const left = sprite._drawLeft;
|
|
11
|
+
const top = sprite._drawTop;
|
|
12
|
+
const width = sprite._drawWidth;
|
|
13
|
+
const height = sprite._drawHeight;
|
|
14
|
+
const insidePlayground = playground._insidePlayground(left, top, width, height);
|
|
15
|
+
const animation = sprite._animation;
|
|
16
|
+
const opacity = sprite._opacity;
|
|
17
|
+
const scaleh = sprite._scaleh;
|
|
18
|
+
const scalev = sprite._scalev;
|
|
19
|
+
if (insidePlayground &&
|
|
20
|
+
animation !== null &&
|
|
21
|
+
opacity !== 0 &&
|
|
22
|
+
scaleh !== 0 &&
|
|
23
|
+
scalev !== 0 &&
|
|
24
|
+
!sprite._hidden) {
|
|
25
|
+
const playgroundData = playgroundMap.get(playground);
|
|
26
|
+
const ctx = playgroundData.ctx;
|
|
27
|
+
let old_alpha = 1;
|
|
28
|
+
let alpha_changed = false;
|
|
29
|
+
if (opacity !== 1) {
|
|
30
|
+
// Don't save the entire context only for opacity changes
|
|
31
|
+
old_alpha = ctx.globalAlpha;
|
|
32
|
+
ctx.globalAlpha = old_alpha * opacity;
|
|
33
|
+
alpha_changed = true;
|
|
34
|
+
}
|
|
35
|
+
let old_blend_mode = "source-over";
|
|
36
|
+
let blend_mode_changed = false;
|
|
37
|
+
const blendMode = sprite._blendMode;
|
|
38
|
+
if (blendMode !== "normal") {
|
|
39
|
+
old_blend_mode = ctx.globalCompositeOperation;
|
|
40
|
+
if (blendMode === "add") {
|
|
41
|
+
ctx.globalCompositeOperation = "lighter";
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
ctx.globalCompositeOperation = blendMode;
|
|
45
|
+
}
|
|
46
|
+
blend_mode_changed = true;
|
|
47
|
+
}
|
|
48
|
+
const angle = sprite._angle;
|
|
49
|
+
const sprite_sheet = animation.frameset[sprite._currentSpriteSheet];
|
|
50
|
+
const currentFrame = sprite._currentFrame;
|
|
51
|
+
if (angle !== 0 || scaleh !== 1 || scalev !== 1) {
|
|
52
|
+
const trunc = Math.trunc;
|
|
53
|
+
ctx.save();
|
|
54
|
+
let transformOriginx = sprite._transformOriginx;
|
|
55
|
+
if (typeof transformOriginx === "string") {
|
|
56
|
+
transformOriginx = sprite[transformOriginx];
|
|
57
|
+
}
|
|
58
|
+
transformOriginx = trunc(transformOriginx);
|
|
59
|
+
let transformOriginy = sprite._transformOriginy;
|
|
60
|
+
if (typeof transformOriginy === "string") {
|
|
61
|
+
transformOriginy = sprite[transformOriginy];
|
|
62
|
+
}
|
|
63
|
+
transformOriginy = trunc(transformOriginy);
|
|
64
|
+
ctx.translate(left + transformOriginx, top + transformOriginy);
|
|
65
|
+
if (angle !== 0) {
|
|
66
|
+
ctx.rotate(angle);
|
|
67
|
+
}
|
|
68
|
+
if (scaleh !== 1 || scalev !== 1) {
|
|
69
|
+
ctx.scale(scaleh, scalev);
|
|
70
|
+
}
|
|
71
|
+
ctx.translate(-transformOriginx, -transformOriginy);
|
|
72
|
+
ctx.drawImage(sprite_sheet._img, sprite_sheet.offsetx +
|
|
73
|
+
sprite._multix +
|
|
74
|
+
currentFrame * sprite_sheet._deltax, sprite_sheet.offsety +
|
|
75
|
+
sprite._multiy +
|
|
76
|
+
currentFrame * sprite_sheet._deltay, width, height, 0, 0, width, height);
|
|
77
|
+
ctx.restore();
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
ctx.drawImage(sprite_sheet._img, sprite_sheet.offsetx +
|
|
81
|
+
sprite._multix +
|
|
82
|
+
currentFrame * sprite_sheet._deltax, sprite_sheet.offsety +
|
|
83
|
+
sprite._multiy +
|
|
84
|
+
currentFrame * sprite_sheet._deltay, width, height, left, top, width, height);
|
|
85
|
+
}
|
|
86
|
+
if (blend_mode_changed) {
|
|
87
|
+
ctx.globalCompositeOperation = old_blend_mode;
|
|
88
|
+
}
|
|
89
|
+
if (alpha_changed) {
|
|
90
|
+
ctx.globalAlpha = old_alpha;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { drawGroupAfterChildren, drawGroupBeforeChildren, initGroup, removeGroup, } from "./canvasRenderer/renderGroup.js";
|
|
2
|
+
import { drawPlaygroundAfterChildren, drawPlaygroundBeforeChildren, initPlayground, } from "./canvasRenderer/renderPlayground.js";
|
|
3
|
+
import { drawRectangle, initRectangle, removeRectangle, } from "./canvasRenderer/renderRectangle.js";
|
|
4
|
+
import { drawSprite, initSprite, removeSprite, } from "./canvasRenderer/renderSprite.js";
|
|
5
|
+
export const canvasRenderer = {
|
|
6
|
+
initPlayground,
|
|
7
|
+
drawPlaygroundBeforeChildren,
|
|
8
|
+
drawPlaygroundAfterChildren,
|
|
9
|
+
initGroup,
|
|
10
|
+
removeGroup,
|
|
11
|
+
drawGroupBeforeChildren,
|
|
12
|
+
drawGroupAfterChildren,
|
|
13
|
+
initSprite,
|
|
14
|
+
removeSprite,
|
|
15
|
+
drawSprite,
|
|
16
|
+
initRectangle,
|
|
17
|
+
removeRectangle,
|
|
18
|
+
drawRectangle,
|
|
19
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export const dummyRenderer = {
|
|
2
|
+
initPlayground(_playground, _options) {
|
|
3
|
+
return [0, 0];
|
|
4
|
+
},
|
|
5
|
+
drawPlaygroundBeforeChildren(_playground, _interp) {
|
|
6
|
+
// no-op
|
|
7
|
+
},
|
|
8
|
+
drawPlaygroundAfterChildren(_playground, _interp) {
|
|
9
|
+
// no-op
|
|
10
|
+
},
|
|
11
|
+
initGroup(_group) {
|
|
12
|
+
// no-op
|
|
13
|
+
},
|
|
14
|
+
removeGroup(_group) {
|
|
15
|
+
// no-op
|
|
16
|
+
},
|
|
17
|
+
drawGroupBeforeChildren(_group, _interp) {
|
|
18
|
+
// no-op
|
|
19
|
+
},
|
|
20
|
+
drawGroupAfterChildren(_group, _interp) {
|
|
21
|
+
// no-op
|
|
22
|
+
},
|
|
23
|
+
initSprite(_sprite) {
|
|
24
|
+
// no-op
|
|
25
|
+
},
|
|
26
|
+
removeSprite(_sprite) {
|
|
27
|
+
// no-op
|
|
28
|
+
},
|
|
29
|
+
drawSprite(_sprite, _interp) {
|
|
30
|
+
// no-op
|
|
31
|
+
},
|
|
32
|
+
initRectangle(_rectangle) {
|
|
33
|
+
// no-op
|
|
34
|
+
},
|
|
35
|
+
removeRectangle(_rectangle) {
|
|
36
|
+
// no-op
|
|
37
|
+
},
|
|
38
|
+
drawRectangle(_rectangle, _interp) {
|
|
39
|
+
// no-op
|
|
40
|
+
},
|
|
41
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Playground } from "../Playground.js";
|
|
2
|
+
export declare class GamepadTracker extends EventTarget {
|
|
3
|
+
controllers: Map<number, Gamepad>;
|
|
4
|
+
_playground: WeakRef<Playground>;
|
|
5
|
+
_callbackId: number | null;
|
|
6
|
+
constructor(playground: Playground);
|
|
7
|
+
_scanGamepads: () => void;
|
|
8
|
+
registerCallback(options?: {
|
|
9
|
+
suppressWarning?: boolean;
|
|
10
|
+
}): this;
|
|
11
|
+
removeCallback(options?: {
|
|
12
|
+
suppressWarning?: boolean;
|
|
13
|
+
}): this;
|
|
14
|
+
_onClearCallbacks: () => void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export class GamepadTracker extends EventTarget {
|
|
2
|
+
controllers = new Map();
|
|
3
|
+
_playground;
|
|
4
|
+
_callbackId = null;
|
|
5
|
+
constructor(playground) {
|
|
6
|
+
super();
|
|
7
|
+
this._playground = new WeakRef(playground);
|
|
8
|
+
this.registerCallback();
|
|
9
|
+
playground.addEventListener("clearCallbacks", this._onClearCallbacks);
|
|
10
|
+
}
|
|
11
|
+
_scanGamepads = () => {
|
|
12
|
+
const controllers = this.controllers;
|
|
13
|
+
const gamepads = navigator.getGamepads();
|
|
14
|
+
// Step 1: Update controller status
|
|
15
|
+
for (const gamepad of gamepads) {
|
|
16
|
+
if (gamepad !== null) {
|
|
17
|
+
controllers.set(gamepad.index, gamepad);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// Step 2: Remove disconnected controllers
|
|
21
|
+
const indexes = new Set(gamepads
|
|
22
|
+
.filter((gamepad) => gamepad !== null)
|
|
23
|
+
.map((gamepad) => gamepad.index));
|
|
24
|
+
for (const index of controllers.keys()) {
|
|
25
|
+
if (!indexes.has(index)) {
|
|
26
|
+
controllers.delete(index);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
registerCallback(options) {
|
|
31
|
+
if (this._callbackId === null) {
|
|
32
|
+
const playground = this._playground.deref();
|
|
33
|
+
if (playground !== undefined) {
|
|
34
|
+
this._callbackId = playground.registerCallback(this._scanGamepads);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
if (typeof console !== "undefined" &&
|
|
38
|
+
options?.suppressWarning === false) {
|
|
39
|
+
console.warn("playground has been garbage collected");
|
|
40
|
+
console.trace();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
if (typeof console !== "undefined" &&
|
|
46
|
+
options?.suppressWarning === false) {
|
|
47
|
+
console.warn("Callback already registered");
|
|
48
|
+
console.trace();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
removeCallback(options) {
|
|
54
|
+
const playground = this._playground.deref();
|
|
55
|
+
if (playground !== undefined) {
|
|
56
|
+
playground.removeCallback(this._callbackId, options);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
if (typeof console !== "undefined" &&
|
|
60
|
+
options?.suppressWarning === false) {
|
|
61
|
+
console.warn("playground has been garbage collected");
|
|
62
|
+
console.trace();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
this._callbackId = null;
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
_onClearCallbacks = () => {
|
|
69
|
+
if (this._callbackId !== null) {
|
|
70
|
+
this._callbackId = null;
|
|
71
|
+
this.registerCallback();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { BaseSprite } from "../BaseSprite.js";
|
|
2
|
+
import type { RectangleOptions } from "../Rectangle.js";
|
|
3
|
+
import type { SpriteOptions } from "../Sprite.js";
|
|
4
|
+
import type { SpriteGroupOptions } from "../SpriteGroup.js";
|
|
5
|
+
import { SpriteGroup } from "../SpriteGroup.js";
|
|
6
|
+
import type { SpriteRef } from "../utils.js";
|
|
7
|
+
export type TileOptions = (Pick<SpriteOptions, "animation"> & Partial<SpriteOptions>) | (Pick<RectangleOptions, "background"> & Partial<RectangleOptions>);
|
|
8
|
+
export interface TilemapOptions {
|
|
9
|
+
sizex: number;
|
|
10
|
+
sizey: number;
|
|
11
|
+
tileWidth: number;
|
|
12
|
+
tileHeight: number;
|
|
13
|
+
data: number[];
|
|
14
|
+
animationList: Record<number, TileOptions>;
|
|
15
|
+
}
|
|
16
|
+
export declare class Tilemap extends SpriteGroup {
|
|
17
|
+
sizex: number;
|
|
18
|
+
sizey: number;
|
|
19
|
+
tileWidth: number;
|
|
20
|
+
tileHeight: number;
|
|
21
|
+
_locations: Uint32Array;
|
|
22
|
+
constructor(options: TilemapOptions & Partial<SpriteGroupOptions> & {
|
|
23
|
+
ref?: SpriteRef<Tilemap>;
|
|
24
|
+
});
|
|
25
|
+
getAt(row: number, col: number): BaseSprite | undefined;
|
|
26
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Rectangle } from "../Rectangle.js";
|
|
2
|
+
import { Sprite } from "../Sprite.js";
|
|
3
|
+
import { SpriteGroup } from "../SpriteGroup.js";
|
|
4
|
+
export class Tilemap extends SpriteGroup {
|
|
5
|
+
sizex = 0;
|
|
6
|
+
sizey = 0;
|
|
7
|
+
tileWidth = 0;
|
|
8
|
+
tileHeight = 0;
|
|
9
|
+
_locations;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
super(options);
|
|
12
|
+
const { sizex, sizey, tileWidth, tileHeight } = options;
|
|
13
|
+
this.sizex = sizex;
|
|
14
|
+
this.sizey = sizey;
|
|
15
|
+
this.tileWidth = tileWidth;
|
|
16
|
+
this.tileHeight = tileHeight;
|
|
17
|
+
const locations = new Uint32Array(sizex * sizey).fill(0xffffffff);
|
|
18
|
+
this._locations = locations;
|
|
19
|
+
const layers = this._layers;
|
|
20
|
+
if (options.width === undefined &&
|
|
21
|
+
options.halfWidth === undefined &&
|
|
22
|
+
options.radius === undefined) {
|
|
23
|
+
this.width = sizex * tileWidth;
|
|
24
|
+
}
|
|
25
|
+
if (options.height === undefined &&
|
|
26
|
+
options.halfHeight === undefined &&
|
|
27
|
+
options.radius === undefined) {
|
|
28
|
+
this.height = sizey * tileHeight;
|
|
29
|
+
}
|
|
30
|
+
const animationList = options.animationList;
|
|
31
|
+
let i_location = 0;
|
|
32
|
+
let row = 0;
|
|
33
|
+
let col = 0;
|
|
34
|
+
let left = 0;
|
|
35
|
+
let top = 0;
|
|
36
|
+
for (const data of options.data) {
|
|
37
|
+
const animation_options = animationList[data];
|
|
38
|
+
if (animation_options !== undefined && "animation" in animation_options) {
|
|
39
|
+
// Sprite
|
|
40
|
+
const sprite_options = Object.assign({}, animation_options, {
|
|
41
|
+
left,
|
|
42
|
+
top,
|
|
43
|
+
});
|
|
44
|
+
this.addChild(new Sprite(sprite_options));
|
|
45
|
+
locations[i_location] = layers.length - 1;
|
|
46
|
+
}
|
|
47
|
+
else if (animation_options !== undefined &&
|
|
48
|
+
"background" in animation_options) {
|
|
49
|
+
// Rectangle
|
|
50
|
+
const rectangle_options = Object.assign({}, animation_options, {
|
|
51
|
+
left,
|
|
52
|
+
top,
|
|
53
|
+
width: tileWidth,
|
|
54
|
+
height: tileHeight,
|
|
55
|
+
});
|
|
56
|
+
this.addChild(new Rectangle(rectangle_options));
|
|
57
|
+
locations[i_location] = layers.length - 1;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// TODO: Invalid
|
|
61
|
+
}
|
|
62
|
+
i_location += 1;
|
|
63
|
+
left += tileWidth;
|
|
64
|
+
col += 1;
|
|
65
|
+
if (col >= sizex) {
|
|
66
|
+
col = 0;
|
|
67
|
+
left = 0;
|
|
68
|
+
row += 1;
|
|
69
|
+
top += tileHeight;
|
|
70
|
+
if (row >= sizey) {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
getAt(row, col) {
|
|
77
|
+
const i = row * this.sizex + col;
|
|
78
|
+
if (i < this._locations.length) {
|
|
79
|
+
return this._layers[this._locations[i]];
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|