@canvasengine/presets 2.0.0-beta.10
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/dist/index.d.ts +54 -0
- package/dist/index.js +776 -0
- package/dist/index.js.map +1 -0
- package/package.json +29 -0
- package/src/Bar.ts +87 -0
- package/src/Button.ts +0 -0
- package/src/DrawMap/index.ts +60 -0
- package/src/Joystick.ts +284 -0
- package/src/NightAmbiant.ts +116 -0
- package/src/Particle.ts +50 -0
- package/src/Tilemap/Tile.ts +79 -0
- package/src/Tilemap/TileGroup.ts +207 -0
- package/src/Tilemap/TileLayer.ts +162 -0
- package/src/Tilemap/TileSet.ts +41 -0
- package/src/Tilemap/index.ts +79 -0
- package/src/index.ts +6 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,776 @@
|
|
|
1
|
+
// src/Bar.ts
|
|
2
|
+
import { Graphics, h, useProps } from "canvasengine";
|
|
3
|
+
function componentToHex(c) {
|
|
4
|
+
var hex = c.toString(16);
|
|
5
|
+
return hex.length == 1 ? "0" + hex : hex;
|
|
6
|
+
}
|
|
7
|
+
function rgbToHex(r, g, b) {
|
|
8
|
+
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
|
|
9
|
+
}
|
|
10
|
+
function Bar(opts) {
|
|
11
|
+
const {
|
|
12
|
+
width,
|
|
13
|
+
height,
|
|
14
|
+
value,
|
|
15
|
+
maxValue,
|
|
16
|
+
backgroundColor,
|
|
17
|
+
foregroundColor,
|
|
18
|
+
border,
|
|
19
|
+
innerMargin,
|
|
20
|
+
borderRadius
|
|
21
|
+
} = useProps(opts, {
|
|
22
|
+
backgroundColor: "#000000",
|
|
23
|
+
foregroundColor: "#FFFFFF",
|
|
24
|
+
innerMargin: 0,
|
|
25
|
+
borderRadius: 0
|
|
26
|
+
});
|
|
27
|
+
return h(
|
|
28
|
+
Graphics,
|
|
29
|
+
{
|
|
30
|
+
...opts,
|
|
31
|
+
width,
|
|
32
|
+
height,
|
|
33
|
+
draw(g) {
|
|
34
|
+
if (borderRadius()) {
|
|
35
|
+
g.roundRect(0, 0, width(), height(), borderRadius());
|
|
36
|
+
} else {
|
|
37
|
+
g.rect(0, 0, width(), height());
|
|
38
|
+
}
|
|
39
|
+
if (border) {
|
|
40
|
+
g.stroke(border);
|
|
41
|
+
}
|
|
42
|
+
g.fill(backgroundColor());
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
h(Graphics, {
|
|
46
|
+
width,
|
|
47
|
+
height,
|
|
48
|
+
draw(g) {
|
|
49
|
+
const margin = innerMargin();
|
|
50
|
+
const _borderRadius = borderRadius();
|
|
51
|
+
const w = Math.max(
|
|
52
|
+
0,
|
|
53
|
+
Math.min(
|
|
54
|
+
width() - 2 * margin,
|
|
55
|
+
value() / maxValue() * (width() - 2 * margin)
|
|
56
|
+
)
|
|
57
|
+
);
|
|
58
|
+
const h7 = height() - 2 * margin;
|
|
59
|
+
if (borderRadius) {
|
|
60
|
+
g.roundRect(margin, margin, w, h7, _borderRadius);
|
|
61
|
+
} else {
|
|
62
|
+
g.rect(margin, margin, w, h7);
|
|
63
|
+
}
|
|
64
|
+
const color = foregroundColor();
|
|
65
|
+
if (color.startsWith("rgba")) {
|
|
66
|
+
const [r, g2, b, a] = color.match(/\d+(\.\d+)?/g).map(Number);
|
|
67
|
+
g2.fill({ color: rgbToHex(r, g2, b), alpha: a });
|
|
68
|
+
} else {
|
|
69
|
+
g.fill(color);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/Particle.ts
|
|
77
|
+
import * as PIXI from "pixi.js";
|
|
78
|
+
import { FX } from "revolt-fx";
|
|
79
|
+
import { h as h2, mount, tick, Container, on, useProps as useProps2 } from "canvasengine";
|
|
80
|
+
function Particle(options) {
|
|
81
|
+
const { emit, settings: settings2 = {} } = options;
|
|
82
|
+
const { name } = useProps2(options);
|
|
83
|
+
const fx = new FX();
|
|
84
|
+
let element;
|
|
85
|
+
PIXI.Assets.add({ alias: "fx_settings", src: "/default-bundle.json" });
|
|
86
|
+
PIXI.Assets.add({
|
|
87
|
+
alias: "fx_spritesheet",
|
|
88
|
+
src: "/revoltfx-spritesheet.json"
|
|
89
|
+
});
|
|
90
|
+
tick(({ deltaRatio }) => {
|
|
91
|
+
fx.update(deltaRatio);
|
|
92
|
+
});
|
|
93
|
+
mount(async (_element) => {
|
|
94
|
+
element = _element;
|
|
95
|
+
const data = await PIXI.Assets.load(["fx_settings", "fx_spritesheet"]);
|
|
96
|
+
let fxSettings = { ...data.fx_settings };
|
|
97
|
+
if (settings2.emitters) {
|
|
98
|
+
const lastId = 1e4;
|
|
99
|
+
const emittersWithIds = settings2.emitters.map((emitter, index) => ({
|
|
100
|
+
...emitter,
|
|
101
|
+
id: lastId + index
|
|
102
|
+
}));
|
|
103
|
+
fxSettings.emitters = [
|
|
104
|
+
...fxSettings.emitters,
|
|
105
|
+
...emittersWithIds
|
|
106
|
+
];
|
|
107
|
+
}
|
|
108
|
+
fx.initBundle(fxSettings, true);
|
|
109
|
+
});
|
|
110
|
+
on(emit, () => {
|
|
111
|
+
const emitter = fx.getParticleEmitter(name());
|
|
112
|
+
emitter.init(element.componentInstance);
|
|
113
|
+
});
|
|
114
|
+
return h2(Container);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/NightAmbiant.ts
|
|
118
|
+
import { Container as Container2, Graphics as Graphics2, h as h3, mount as mount2, useProps as useProps3, animatedSignal, RadialGradient, effect, isSignal, signal, isObservable } from "canvasengine";
|
|
119
|
+
function LightSpot(opts) {
|
|
120
|
+
const { radius } = useProps3(opts);
|
|
121
|
+
const scale = animatedSignal(1);
|
|
122
|
+
const minScale = 1;
|
|
123
|
+
const maxScale = 2;
|
|
124
|
+
const scintillationSpeed = 1e-3;
|
|
125
|
+
const animate = () => {
|
|
126
|
+
const time = Date.now() * scintillationSpeed;
|
|
127
|
+
const scintillationFactor = (Math.sin(time) + Math.sin(time * 1.3) + Math.sin(time * 0.7)) / 3;
|
|
128
|
+
const newScale = minScale + (maxScale - minScale) * (scintillationFactor * 0.5 + 0.5);
|
|
129
|
+
scale.update(() => newScale);
|
|
130
|
+
requestAnimationFrame(animate);
|
|
131
|
+
};
|
|
132
|
+
animate();
|
|
133
|
+
const draw = (g) => {
|
|
134
|
+
const size = radius() * 2;
|
|
135
|
+
const gradient = new RadialGradient(size, size, 0, size, size, 0);
|
|
136
|
+
gradient.addColorStop(0, "rgba(255, 255, 0, 1)");
|
|
137
|
+
gradient.addColorStop(0.5, "rgba(255, 255, 0, 0.3)");
|
|
138
|
+
gradient.addColorStop(0.8, "rgba(255, 255, 0, 0)");
|
|
139
|
+
const translate = size / 2;
|
|
140
|
+
g.rect(-translate, -translate, size, size).fill(
|
|
141
|
+
gradient.render({ translate: { x: translate, y: translate } })
|
|
142
|
+
);
|
|
143
|
+
};
|
|
144
|
+
return h3(Graphics2, {
|
|
145
|
+
draw,
|
|
146
|
+
...opts,
|
|
147
|
+
scale
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
function NightAmbiant(props) {
|
|
151
|
+
const { children } = props;
|
|
152
|
+
let el;
|
|
153
|
+
const width = signal(0);
|
|
154
|
+
const height = signal(0);
|
|
155
|
+
let subscription;
|
|
156
|
+
const draw = (rectAndHole) => {
|
|
157
|
+
const margin = 80;
|
|
158
|
+
rectAndHole.rect(-margin, -margin, width() + margin * 2, height() + margin * 2);
|
|
159
|
+
rectAndHole.fill(0);
|
|
160
|
+
const applyChildren = (child) => {
|
|
161
|
+
const x = isSignal(child.propObservables.x) ? child.propObservables.x() : child.props.x;
|
|
162
|
+
const y = isSignal(child.propObservables.y) ? child.propObservables.y() : child.props.y;
|
|
163
|
+
const radius = isSignal(child.propObservables.radius) ? child.propObservables.radius() : child.props.radius;
|
|
164
|
+
rectAndHole.circle(x, y, radius);
|
|
165
|
+
rectAndHole.cut();
|
|
166
|
+
};
|
|
167
|
+
for (let child of children) {
|
|
168
|
+
if (isObservable(child)) {
|
|
169
|
+
if (subscription) {
|
|
170
|
+
subscription.unsubscribe();
|
|
171
|
+
}
|
|
172
|
+
subscription = child.subscribe((event) => {
|
|
173
|
+
for (let child2 of event.fullElements) {
|
|
174
|
+
applyChildren(child2);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
applyChildren(child);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
mount2((el2) => {
|
|
183
|
+
effect(() => {
|
|
184
|
+
const { displayWidth, displayHeight } = el2.componentInstance;
|
|
185
|
+
const w = +displayWidth();
|
|
186
|
+
const h7 = +displayHeight();
|
|
187
|
+
setTimeout(() => {
|
|
188
|
+
width.update(() => w);
|
|
189
|
+
height.update(() => h7);
|
|
190
|
+
}, 0);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
return h3(
|
|
194
|
+
Container2,
|
|
195
|
+
{
|
|
196
|
+
width: "100%",
|
|
197
|
+
height: "100%",
|
|
198
|
+
...props
|
|
199
|
+
},
|
|
200
|
+
h3(Graphics2, {
|
|
201
|
+
draw,
|
|
202
|
+
alpha: 0.8,
|
|
203
|
+
blur: 80
|
|
204
|
+
}),
|
|
205
|
+
...children
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// src/Joystick.ts
|
|
210
|
+
import * as PIXI2 from "pixi.js";
|
|
211
|
+
import { Container as Container3, Graphics as Graphics3, Sprite, h as h4, signal as signal2 } from "canvasengine";
|
|
212
|
+
var Direction = /* @__PURE__ */ ((Direction2) => {
|
|
213
|
+
Direction2["LEFT"] = "left";
|
|
214
|
+
Direction2["TOP"] = "top";
|
|
215
|
+
Direction2["BOTTOM"] = "bottom";
|
|
216
|
+
Direction2["RIGHT"] = "right";
|
|
217
|
+
Direction2["TOP_LEFT"] = "top_left";
|
|
218
|
+
Direction2["TOP_RIGHT"] = "top_right";
|
|
219
|
+
Direction2["BOTTOM_LEFT"] = "bottom_left";
|
|
220
|
+
Direction2["BOTTOM_RIGHT"] = "bottom_right";
|
|
221
|
+
return Direction2;
|
|
222
|
+
})(Direction || {});
|
|
223
|
+
function Joystick(opts = {}) {
|
|
224
|
+
const settings2 = Object.assign(
|
|
225
|
+
{
|
|
226
|
+
outerScale: { x: 1, y: 1 },
|
|
227
|
+
innerScale: { x: 1, y: 1 }
|
|
228
|
+
},
|
|
229
|
+
opts
|
|
230
|
+
);
|
|
231
|
+
let outerRadius = 70;
|
|
232
|
+
let innerRadius = 10;
|
|
233
|
+
const innerAlphaStandby = 0.5;
|
|
234
|
+
let dragging = false;
|
|
235
|
+
let startPosition = null;
|
|
236
|
+
let power = 0;
|
|
237
|
+
const innerPositionX = signal2(0);
|
|
238
|
+
const innerPositionY = signal2(0);
|
|
239
|
+
const innerAlpha = signal2(innerAlphaStandby);
|
|
240
|
+
function getPower(centerPoint) {
|
|
241
|
+
const a = centerPoint.x - 0;
|
|
242
|
+
const b = centerPoint.y - 0;
|
|
243
|
+
return Math.min(1, Math.sqrt(a * a + b * b) / outerRadius);
|
|
244
|
+
}
|
|
245
|
+
function getDirection(center) {
|
|
246
|
+
let rad = Math.atan2(center.y, center.x);
|
|
247
|
+
if (rad >= -Math.PI / 8 && rad < 0 || rad >= 0 && rad < Math.PI / 8) {
|
|
248
|
+
return "right" /* RIGHT */;
|
|
249
|
+
} else if (rad >= Math.PI / 8 && rad < 3 * Math.PI / 8) {
|
|
250
|
+
return "bottom_right" /* BOTTOM_RIGHT */;
|
|
251
|
+
} else if (rad >= 3 * Math.PI / 8 && rad < 5 * Math.PI / 8) {
|
|
252
|
+
return "bottom" /* BOTTOM */;
|
|
253
|
+
} else if (rad >= 5 * Math.PI / 8 && rad < 7 * Math.PI / 8) {
|
|
254
|
+
return "bottom_left" /* BOTTOM_LEFT */;
|
|
255
|
+
} else if (rad >= 7 * Math.PI / 8 && rad < Math.PI || rad >= -Math.PI && rad < -7 * Math.PI / 8) {
|
|
256
|
+
return "left" /* LEFT */;
|
|
257
|
+
} else if (rad >= -7 * Math.PI / 8 && rad < -5 * Math.PI / 8) {
|
|
258
|
+
return "top_left" /* TOP_LEFT */;
|
|
259
|
+
} else if (rad >= -5 * Math.PI / 8 && rad < -3 * Math.PI / 8) {
|
|
260
|
+
return "top" /* TOP */;
|
|
261
|
+
} else {
|
|
262
|
+
return "top_right" /* TOP_RIGHT */;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function handleDragStart(event) {
|
|
266
|
+
startPosition = event.getLocalPosition(this);
|
|
267
|
+
dragging = true;
|
|
268
|
+
innerAlpha.set(1);
|
|
269
|
+
settings2.onStart?.();
|
|
270
|
+
}
|
|
271
|
+
function handleDragEnd() {
|
|
272
|
+
if (!dragging) return;
|
|
273
|
+
innerPositionX.set(0);
|
|
274
|
+
innerPositionY.set(0);
|
|
275
|
+
dragging = false;
|
|
276
|
+
innerAlpha.set(innerAlphaStandby);
|
|
277
|
+
settings2.onEnd?.();
|
|
278
|
+
}
|
|
279
|
+
function handleDragMove(event) {
|
|
280
|
+
if (dragging == false) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
let newPosition = event.getLocalPosition(this);
|
|
284
|
+
let sideX = newPosition.x - (startPosition?.x ?? 0);
|
|
285
|
+
let sideY = newPosition.y - (startPosition?.y ?? 0);
|
|
286
|
+
let centerPoint = new PIXI2.Point(0, 0);
|
|
287
|
+
let angle = 0;
|
|
288
|
+
if (sideX == 0 && sideY == 0) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
let calRadius = 0;
|
|
292
|
+
if (sideX * sideX + sideY * sideY >= outerRadius * outerRadius) {
|
|
293
|
+
calRadius = outerRadius;
|
|
294
|
+
} else {
|
|
295
|
+
calRadius = outerRadius - innerRadius;
|
|
296
|
+
}
|
|
297
|
+
let direction = "left" /* LEFT */;
|
|
298
|
+
if (sideX == 0) {
|
|
299
|
+
if (sideY > 0) {
|
|
300
|
+
centerPoint.set(0, sideY > outerRadius ? outerRadius : sideY);
|
|
301
|
+
angle = 270;
|
|
302
|
+
direction = "bottom" /* BOTTOM */;
|
|
303
|
+
} else {
|
|
304
|
+
centerPoint.set(
|
|
305
|
+
0,
|
|
306
|
+
-(Math.abs(sideY) > outerRadius ? outerRadius : Math.abs(sideY))
|
|
307
|
+
);
|
|
308
|
+
angle = 90;
|
|
309
|
+
direction = "top" /* TOP */;
|
|
310
|
+
}
|
|
311
|
+
innerPositionX.set(centerPoint.x);
|
|
312
|
+
innerPositionY.set(centerPoint.y);
|
|
313
|
+
power = getPower(centerPoint);
|
|
314
|
+
settings2.onChange?.({ angle, direction, power });
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
if (sideY == 0) {
|
|
318
|
+
if (sideX > 0) {
|
|
319
|
+
centerPoint.set(
|
|
320
|
+
Math.abs(sideX) > outerRadius ? outerRadius : Math.abs(sideX),
|
|
321
|
+
0
|
|
322
|
+
);
|
|
323
|
+
angle = 0;
|
|
324
|
+
direction = "left" /* LEFT */;
|
|
325
|
+
} else {
|
|
326
|
+
centerPoint.set(
|
|
327
|
+
-(Math.abs(sideX) > outerRadius ? outerRadius : Math.abs(sideX)),
|
|
328
|
+
0
|
|
329
|
+
);
|
|
330
|
+
angle = 180;
|
|
331
|
+
direction = "right" /* RIGHT */;
|
|
332
|
+
}
|
|
333
|
+
innerPositionX.set(centerPoint.x);
|
|
334
|
+
innerPositionY.set(centerPoint.y);
|
|
335
|
+
power = getPower(centerPoint);
|
|
336
|
+
settings2.onChange?.({ angle, direction, power });
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
let tanVal = Math.abs(sideY / sideX);
|
|
340
|
+
let radian = Math.atan(tanVal);
|
|
341
|
+
angle = radian * 180 / Math.PI;
|
|
342
|
+
let centerX = 0;
|
|
343
|
+
let centerY = 0;
|
|
344
|
+
if (sideX * sideX + sideY * sideY >= outerRadius * outerRadius) {
|
|
345
|
+
centerX = outerRadius * Math.cos(radian);
|
|
346
|
+
centerY = outerRadius * Math.sin(radian);
|
|
347
|
+
} else {
|
|
348
|
+
centerX = Math.abs(sideX) > outerRadius ? outerRadius : Math.abs(sideX);
|
|
349
|
+
centerY = Math.abs(sideY) > outerRadius ? outerRadius : Math.abs(sideY);
|
|
350
|
+
}
|
|
351
|
+
if (sideY < 0) {
|
|
352
|
+
centerY = -Math.abs(centerY);
|
|
353
|
+
}
|
|
354
|
+
if (sideX < 0) {
|
|
355
|
+
centerX = -Math.abs(centerX);
|
|
356
|
+
}
|
|
357
|
+
if (sideX > 0 && sideY < 0) {
|
|
358
|
+
} else if (sideX < 0 && sideY < 0) {
|
|
359
|
+
angle = 180 - angle;
|
|
360
|
+
} else if (sideX < 0 && sideY > 0) {
|
|
361
|
+
angle = angle + 180;
|
|
362
|
+
} else if (sideX > 0 && sideY > 0) {
|
|
363
|
+
angle = 360 - angle;
|
|
364
|
+
}
|
|
365
|
+
centerPoint.set(centerX, centerY);
|
|
366
|
+
power = getPower(centerPoint);
|
|
367
|
+
direction = getDirection(centerPoint);
|
|
368
|
+
innerPositionX.set(centerPoint.x);
|
|
369
|
+
innerPositionY.set(centerPoint.y);
|
|
370
|
+
settings2.onChange?.({ angle, direction, power });
|
|
371
|
+
}
|
|
372
|
+
let innerElement;
|
|
373
|
+
let outerElement;
|
|
374
|
+
if (!settings2.outer) {
|
|
375
|
+
outerElement = h4(Graphics3, {
|
|
376
|
+
draw: (g) => {
|
|
377
|
+
g.circle(0, 0, outerRadius).fill(0);
|
|
378
|
+
},
|
|
379
|
+
alpha: 0.5
|
|
380
|
+
});
|
|
381
|
+
} else {
|
|
382
|
+
outerElement = h4(Sprite, {
|
|
383
|
+
image: settings2.outer,
|
|
384
|
+
anchor: { x: 0.5, y: 0.5 },
|
|
385
|
+
scale: settings2.outerScale
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
const innerOptions = {
|
|
389
|
+
scale: settings2.innerScale,
|
|
390
|
+
x: innerPositionX,
|
|
391
|
+
y: innerPositionY,
|
|
392
|
+
alpha: innerAlpha
|
|
393
|
+
};
|
|
394
|
+
if (!settings2.inner) {
|
|
395
|
+
innerElement = h4(Graphics3, {
|
|
396
|
+
draw: (g) => {
|
|
397
|
+
g.circle(0, 0, innerRadius * 2.5).fill(0);
|
|
398
|
+
},
|
|
399
|
+
...innerOptions
|
|
400
|
+
});
|
|
401
|
+
} else {
|
|
402
|
+
innerElement = h4(Sprite, {
|
|
403
|
+
image: settings2.inner,
|
|
404
|
+
anchor: { x: 0.5, y: 0.5 },
|
|
405
|
+
...innerOptions
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
return h4(
|
|
409
|
+
Container3,
|
|
410
|
+
{
|
|
411
|
+
...opts,
|
|
412
|
+
pointerdown: handleDragStart,
|
|
413
|
+
pointerup: handleDragEnd,
|
|
414
|
+
pointerupoutside: handleDragEnd,
|
|
415
|
+
pointermove: handleDragMove
|
|
416
|
+
},
|
|
417
|
+
outerElement,
|
|
418
|
+
innerElement
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// src/Tilemap/index.ts
|
|
423
|
+
import { TiledLayerType, TiledParserFile } from "@rpgjs/tiled";
|
|
424
|
+
import { loop, h as h5, Container as Container4, TilingSprite, useProps as useProps4, effect as effect2, signal as signal3 } from "canvasengine";
|
|
425
|
+
|
|
426
|
+
// src/Tilemap/TileLayer.ts
|
|
427
|
+
import { CompositeTilemap, POINT_STRUCT_SIZE, settings } from "@pixi/tilemap";
|
|
428
|
+
import { Layer } from "@rpgjs/tiled";
|
|
429
|
+
import { createComponent, registerComponent, DisplayObject } from "canvasengine";
|
|
430
|
+
|
|
431
|
+
// src/Tilemap/Tile.ts
|
|
432
|
+
import { AnimatedSprite, groupD8 } from "pixi.js";
|
|
433
|
+
var Tile = class _Tile extends AnimatedSprite {
|
|
434
|
+
constructor(tile, tileSet) {
|
|
435
|
+
super(_Tile.getTextures(tile, tileSet));
|
|
436
|
+
this.tile = tile;
|
|
437
|
+
this.tileSet = tileSet;
|
|
438
|
+
this.animations = [];
|
|
439
|
+
this._x = 0;
|
|
440
|
+
this._y = 0;
|
|
441
|
+
this.properties = {};
|
|
442
|
+
this.animations = tile.animations || [];
|
|
443
|
+
this.properties = tile.properties;
|
|
444
|
+
this.textures = _Tile.getTextures(tile, tileSet);
|
|
445
|
+
this.texture = this.textures[0];
|
|
446
|
+
this.flip();
|
|
447
|
+
}
|
|
448
|
+
static getTextures(tile, tileSet) {
|
|
449
|
+
const textures = [];
|
|
450
|
+
if (tile.animations && tile.animations.length) {
|
|
451
|
+
tile.animations.forEach((frame) => {
|
|
452
|
+
textures.push(tileSet.textures[frame.tileid]);
|
|
453
|
+
});
|
|
454
|
+
} else {
|
|
455
|
+
textures.push(tileSet.textures[tile.gid - tileSet.firstgid]);
|
|
456
|
+
}
|
|
457
|
+
return textures;
|
|
458
|
+
}
|
|
459
|
+
get gid() {
|
|
460
|
+
return this.tile.gid;
|
|
461
|
+
}
|
|
462
|
+
setAnimation(frame) {
|
|
463
|
+
const size = this.animations.length;
|
|
464
|
+
if (size > 1) {
|
|
465
|
+
const offset = (this.animations[1].tileid - this.animations[0].tileid) * this.width;
|
|
466
|
+
frame.tileAnimX(offset, size);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
flip() {
|
|
470
|
+
let symmetry;
|
|
471
|
+
let i = 0;
|
|
472
|
+
const add = (symmetrySecond) => {
|
|
473
|
+
i++;
|
|
474
|
+
if (symmetry) symmetry = groupD8.add(symmetry, symmetrySecond);
|
|
475
|
+
else symmetry = symmetrySecond;
|
|
476
|
+
};
|
|
477
|
+
if (this.tile.horizontalFlip) {
|
|
478
|
+
add(groupD8.MIRROR_HORIZONTAL);
|
|
479
|
+
}
|
|
480
|
+
if (this.tile.verticalFlip) {
|
|
481
|
+
add(groupD8.MIRROR_VERTICAL);
|
|
482
|
+
}
|
|
483
|
+
if (this.tile.diagonalFlip) {
|
|
484
|
+
if (i % 2 == 0) {
|
|
485
|
+
add(groupD8.MAIN_DIAGONAL);
|
|
486
|
+
} else {
|
|
487
|
+
add(groupD8.REVERSE_DIAGONAL);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
// src/Tilemap/TileLayer.ts
|
|
494
|
+
settings.use32bitIndex = true;
|
|
495
|
+
var CanvasTileLayer = class _CanvasTileLayer extends DisplayObject(CompositeTilemap) {
|
|
496
|
+
constructor() {
|
|
497
|
+
super(...arguments);
|
|
498
|
+
this._tiles = {};
|
|
499
|
+
}
|
|
500
|
+
// TODO: fix this, remove any. replace with Layer
|
|
501
|
+
static findTileSet(gid, tileSets) {
|
|
502
|
+
let tileset;
|
|
503
|
+
for (let i = tileSets.length - 1; i >= 0; i--) {
|
|
504
|
+
tileset = tileSets[i];
|
|
505
|
+
if (tileset.firstgid && tileset.firstgid <= gid) {
|
|
506
|
+
break;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
return tileset;
|
|
510
|
+
}
|
|
511
|
+
/** @internal */
|
|
512
|
+
createTile(x, y, options = {}) {
|
|
513
|
+
const { real, filter } = options;
|
|
514
|
+
const { tilewidth, tileheight, width } = this._layer;
|
|
515
|
+
if (real) {
|
|
516
|
+
x = Math.floor(x / tilewidth);
|
|
517
|
+
y = Math.floor(y / tileheight);
|
|
518
|
+
}
|
|
519
|
+
const i = x + y * width;
|
|
520
|
+
const tiledTile = this._layer.getTileByIndex(i);
|
|
521
|
+
if (!tiledTile || tiledTile && tiledTile.gid == 0) return;
|
|
522
|
+
const tileset = _CanvasTileLayer.findTileSet(
|
|
523
|
+
tiledTile.gid,
|
|
524
|
+
this.tileSets
|
|
525
|
+
);
|
|
526
|
+
if (!tileset) return;
|
|
527
|
+
const tile = new Tile(
|
|
528
|
+
tiledTile,
|
|
529
|
+
tileset
|
|
530
|
+
);
|
|
531
|
+
tile.x = x * tilewidth;
|
|
532
|
+
tile.y = y * tileheight + (tileheight - tile.texture.height);
|
|
533
|
+
tile._x = x;
|
|
534
|
+
tile._y = y;
|
|
535
|
+
if (tileset.tileoffset) {
|
|
536
|
+
tile.x += tileset.tileoffset.x ?? 0;
|
|
537
|
+
tile.y += tileset.tileoffset.y ?? 0;
|
|
538
|
+
}
|
|
539
|
+
if (filter) {
|
|
540
|
+
const ret = filter(tile);
|
|
541
|
+
if (!ret) return;
|
|
542
|
+
}
|
|
543
|
+
return tile;
|
|
544
|
+
}
|
|
545
|
+
/** @internal */
|
|
546
|
+
changeTile(x, y) {
|
|
547
|
+
const { tilewidth, tileheight } = this._layer;
|
|
548
|
+
x = Math.floor(x / tilewidth);
|
|
549
|
+
y = Math.floor(y / tileheight);
|
|
550
|
+
const oldTile = this._tiles[x + ";" + y];
|
|
551
|
+
const newTile = this.createTile(x, y);
|
|
552
|
+
if (!oldTile && newTile) {
|
|
553
|
+
this._addFrame(newTile, x, y);
|
|
554
|
+
} else {
|
|
555
|
+
if (newTile) {
|
|
556
|
+
const bufComposite = new CompositeTilemap();
|
|
557
|
+
const frame = bufComposite.tile(newTile.texture, newTile.x, newTile.y);
|
|
558
|
+
newTile.setAnimation(frame);
|
|
559
|
+
this._tiles[x + ";" + y] = newTile;
|
|
560
|
+
const pointsBufComposite = bufComposite.children[0].pointsBuf;
|
|
561
|
+
[0, 1, 4, 6, 7, 8].forEach((i) => {
|
|
562
|
+
if (this.pointsBuf) this.pointsBuf[oldTile.pointsBufIndex + i] = pointsBufComposite[i];
|
|
563
|
+
});
|
|
564
|
+
this.children[0].modificationMarker = 0;
|
|
565
|
+
this._addFrame(newTile, x, y);
|
|
566
|
+
this["modificationMarker"] = 0;
|
|
567
|
+
} else {
|
|
568
|
+
delete this._tiles[x + ";" + y];
|
|
569
|
+
if (this.pointsBuf) this.pointsBuf.splice(oldTile.pointsBufIndex, POINT_STRUCT_SIZE);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
/** @internal */
|
|
574
|
+
get pointsBuf() {
|
|
575
|
+
const child = this.children[0];
|
|
576
|
+
if (!child) return null;
|
|
577
|
+
return child["pointsBuf"];
|
|
578
|
+
}
|
|
579
|
+
_addFrame(tile, x, y) {
|
|
580
|
+
const frame = this.tile(tile.texture, tile.x, tile.y, {
|
|
581
|
+
rotate: tile.texture.rotate
|
|
582
|
+
});
|
|
583
|
+
const pb = this.pointsBuf;
|
|
584
|
+
if (!pb) return null;
|
|
585
|
+
tile.pointsBufIndex = pb.length - POINT_STRUCT_SIZE;
|
|
586
|
+
tile.setAnimation(frame);
|
|
587
|
+
this._tiles[x + ";" + y] = tile;
|
|
588
|
+
}
|
|
589
|
+
onMount(args) {
|
|
590
|
+
const { props } = args;
|
|
591
|
+
this.tileSets = props.tilesets;
|
|
592
|
+
this._layer = new Layer({
|
|
593
|
+
...props
|
|
594
|
+
}, this.tileSets);
|
|
595
|
+
super.onMount(args);
|
|
596
|
+
}
|
|
597
|
+
onUpdate(props) {
|
|
598
|
+
super.onUpdate(props);
|
|
599
|
+
if (!this.isMounted) return;
|
|
600
|
+
if (props.tileheight) this._layer.tileheight = props.tileheight;
|
|
601
|
+
if (props.tilewidth) this._layer.tilewidth = props.tilewidth;
|
|
602
|
+
if (props.width) this._layer.width = props.width;
|
|
603
|
+
if (props.height) this._layer.height = props.height;
|
|
604
|
+
if (props.parallaxX) this._layer.parallaxX = props.parallaxx;
|
|
605
|
+
if (props.parallaxY) this._layer.parallaxY = props.parallaxy;
|
|
606
|
+
this.removeChildren();
|
|
607
|
+
for (let y = 0; y < this._layer.height; y++) {
|
|
608
|
+
for (let x = 0; x < this._layer.width; x++) {
|
|
609
|
+
const tile = this.createTile(x, y);
|
|
610
|
+
if (tile) {
|
|
611
|
+
this._addFrame(tile, x, y);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
registerComponent("CompositeTileLayer", CanvasTileLayer);
|
|
618
|
+
function CompositeTileLayer(props) {
|
|
619
|
+
return createComponent("CompositeTileLayer", props);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// src/Tilemap/TileSet.ts
|
|
623
|
+
import { Tileset as TiledTilesetClass } from "@rpgjs/tiled";
|
|
624
|
+
import { Assets as Assets2, Rectangle, Texture as Texture2 } from "pixi.js";
|
|
625
|
+
var TileSet = class extends TiledTilesetClass {
|
|
626
|
+
constructor(tileSet) {
|
|
627
|
+
super(tileSet);
|
|
628
|
+
this.textures = [];
|
|
629
|
+
this.tileGroups = {};
|
|
630
|
+
}
|
|
631
|
+
loadGroup() {
|
|
632
|
+
}
|
|
633
|
+
/** @internal */
|
|
634
|
+
async load(image) {
|
|
635
|
+
const texture = await Assets2.load(image);
|
|
636
|
+
for (let y = this.margin; y < this.image.height; y += this.tileheight + this.spacing) {
|
|
637
|
+
for (let x = this.margin; x < this.image.width; x += this.tilewidth + this.spacing) {
|
|
638
|
+
this.textures.push(
|
|
639
|
+
new Texture2({
|
|
640
|
+
source: texture.source,
|
|
641
|
+
frame: new Rectangle(+x, +y, +this.tilewidth, +this.tileheight)
|
|
642
|
+
})
|
|
643
|
+
);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
this.loadGroup();
|
|
647
|
+
return this;
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
// src/Tilemap/index.ts
|
|
652
|
+
function TiledMap(props) {
|
|
653
|
+
const { map, basePath } = useProps4(props);
|
|
654
|
+
const layers = signal3([]);
|
|
655
|
+
const objectLayer = props.objectLayer;
|
|
656
|
+
const child = props.children[0];
|
|
657
|
+
let tilesets = [];
|
|
658
|
+
let mapData = {};
|
|
659
|
+
const parseTmx = async (file, relativePath = "") => {
|
|
660
|
+
if (typeof file !== "string") {
|
|
661
|
+
return file;
|
|
662
|
+
}
|
|
663
|
+
const parser = new TiledParserFile(
|
|
664
|
+
file,
|
|
665
|
+
{
|
|
666
|
+
basePath: "",
|
|
667
|
+
staticDir: "",
|
|
668
|
+
relativePath
|
|
669
|
+
}
|
|
670
|
+
);
|
|
671
|
+
const data = await parser.parseFilePromise({
|
|
672
|
+
getOnlyBasename: false
|
|
673
|
+
});
|
|
674
|
+
return data;
|
|
675
|
+
};
|
|
676
|
+
effect2(async () => {
|
|
677
|
+
const _map = map();
|
|
678
|
+
if (_map) {
|
|
679
|
+
mapData = await parseTmx(_map, basePath());
|
|
680
|
+
for (let tileSet of mapData.tilesets) {
|
|
681
|
+
tilesets.push(await new TileSet(tileSet).load(tileSet.image.source));
|
|
682
|
+
}
|
|
683
|
+
layers.set(mapData.layers);
|
|
684
|
+
}
|
|
685
|
+
});
|
|
686
|
+
const createLayer = (layers2, props2 = {}) => {
|
|
687
|
+
return h5(Container4, props2, loop(layers2, (layer) => {
|
|
688
|
+
switch (layer.type) {
|
|
689
|
+
case TiledLayerType.Tile:
|
|
690
|
+
return h5(CompositeTileLayer, {
|
|
691
|
+
tilewidth: mapData.tilewidth,
|
|
692
|
+
tileheight: mapData.tileheight,
|
|
693
|
+
// @ts-ignore
|
|
694
|
+
width: mapData.width,
|
|
695
|
+
// @ts-ignore
|
|
696
|
+
height: mapData.height,
|
|
697
|
+
...layer,
|
|
698
|
+
tilesets
|
|
699
|
+
});
|
|
700
|
+
case TiledLayerType.Image:
|
|
701
|
+
const { width, height, source } = layer.image;
|
|
702
|
+
return h5(TilingSprite, {
|
|
703
|
+
image: source,
|
|
704
|
+
...layer,
|
|
705
|
+
width: layer.repeatx ? layer.width * layer.tilewidth : width,
|
|
706
|
+
height: layer.repeaty ? layer.height * layer.tileheight : height
|
|
707
|
+
});
|
|
708
|
+
case TiledLayerType.Group:
|
|
709
|
+
return createLayer(signal3(layer.layers), layer);
|
|
710
|
+
case TiledLayerType.ObjectGroup:
|
|
711
|
+
const child2 = objectLayer?.(layer);
|
|
712
|
+
return h5(Container4, layer, child2);
|
|
713
|
+
default:
|
|
714
|
+
return h5(Container4);
|
|
715
|
+
}
|
|
716
|
+
}));
|
|
717
|
+
};
|
|
718
|
+
return h5(Container4, props, createLayer(layers));
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
// src/DrawMap/index.ts
|
|
722
|
+
import { effect as effect3, signal as signal4, loop as loop2, h as h6, Container as Container5, Sprite as Sprite2, useProps as useProps5 } from "canvasengine";
|
|
723
|
+
function ImageMap(props) {
|
|
724
|
+
const { imageSource, tileData } = useProps5(props);
|
|
725
|
+
const tiles = signal4([]);
|
|
726
|
+
effect3(async () => {
|
|
727
|
+
const data = await fetch(tileData()).then((response) => response.json());
|
|
728
|
+
const objects = data;
|
|
729
|
+
if (props.objects) {
|
|
730
|
+
objects.push(...props.objects(data));
|
|
731
|
+
}
|
|
732
|
+
tiles.set(objects);
|
|
733
|
+
});
|
|
734
|
+
const createLayeredTiles = () => {
|
|
735
|
+
const layers = [createTileLayer(0), createTileLayer(1, true), createTileLayer(2)];
|
|
736
|
+
return h6(Container5, props, ...layers);
|
|
737
|
+
};
|
|
738
|
+
const createTileLayer = (layerIndex, sortableChildren = false) => {
|
|
739
|
+
return h6(
|
|
740
|
+
Container5,
|
|
741
|
+
{
|
|
742
|
+
sortableChildren
|
|
743
|
+
},
|
|
744
|
+
// TODO: fix this (remove any)
|
|
745
|
+
loop2(tiles, (object) => {
|
|
746
|
+
if (object.tag && layerIndex == 1) {
|
|
747
|
+
return object;
|
|
748
|
+
}
|
|
749
|
+
object.layerIndex || (object.layerIndex = 1);
|
|
750
|
+
if (object.layerIndex !== layerIndex) return null;
|
|
751
|
+
const [x, y, width, height] = object.rect;
|
|
752
|
+
const [drawX, drawY] = object.drawIn;
|
|
753
|
+
return h6(Sprite2, {
|
|
754
|
+
image: imageSource(),
|
|
755
|
+
x: drawX,
|
|
756
|
+
y: drawY,
|
|
757
|
+
rectangle: { x, y, width, height },
|
|
758
|
+
zIndex: drawY + height - 70
|
|
759
|
+
// zIndex: 0
|
|
760
|
+
});
|
|
761
|
+
})
|
|
762
|
+
);
|
|
763
|
+
};
|
|
764
|
+
return createLayeredTiles();
|
|
765
|
+
}
|
|
766
|
+
export {
|
|
767
|
+
Bar,
|
|
768
|
+
Direction,
|
|
769
|
+
ImageMap,
|
|
770
|
+
Joystick,
|
|
771
|
+
LightSpot,
|
|
772
|
+
NightAmbiant,
|
|
773
|
+
Particle,
|
|
774
|
+
TiledMap
|
|
775
|
+
};
|
|
776
|
+
//# sourceMappingURL=index.js.map
|