@canvasengine/presets 2.0.0-beta.22 → 2.0.0-beta.24

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.js CHANGED
@@ -1,817 +1,4 @@
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(graphics) {
34
- if (borderRadius()) {
35
- graphics.roundRect(0, 0, width(), height(), borderRadius());
36
- } else {
37
- graphics.rect(0, 0, width(), height());
38
- }
39
- if (border) {
40
- graphics.stroke(border);
41
- }
42
- graphics.fill(backgroundColor());
43
- }
44
- },
45
- h(Graphics, {
46
- width,
47
- height,
48
- draw(graphics) {
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
- graphics.roundRect(margin, margin, w, h7, _borderRadius);
61
- } else {
62
- graphics.rect(margin, margin, w, h7);
63
- }
64
- const color = foregroundColor();
65
- if (color.startsWith("rgba")) {
66
- const [r, g, b, a] = color.match(/\d+(\.\d+)?/g).map(Number);
67
- graphics.fill({ color: rgbToHex(r, g, b), alpha: a });
68
- } else {
69
- graphics.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 {
428
- CompositeTilemap,
429
- settings
430
- } from "@canvasengine/tilemap";
431
- import { Layer } from "@rpgjs/tiled";
432
- import {
433
- createComponent,
434
- registerComponent,
435
- DisplayObject
436
- } from "canvasengine";
437
-
438
- // src/Tilemap/Tile.ts
439
- import { AnimatedSprite, groupD8 } from "pixi.js";
440
- var Tile = class _Tile extends AnimatedSprite {
441
- constructor(tile, tileSet) {
442
- super(_Tile.getTextures(tile, tileSet));
443
- this.tile = tile;
444
- this.tileSet = tileSet;
445
- this.animations = [];
446
- this._x = 0;
447
- this._y = 0;
448
- this.properties = {};
449
- this.animations = tile.animations || [];
450
- this.properties = tile.properties;
451
- this.textures = _Tile.getTextures(tile, tileSet);
452
- this.texture = this.textures[0];
453
- this.flip();
454
- }
455
- static getTextures(tile, tileSet) {
456
- const textures = [];
457
- if (tile.animations && tile.animations.length) {
458
- tile.animations.forEach((frame) => {
459
- textures.push(tileSet.textures[frame.tileid]);
460
- });
461
- } else {
462
- textures.push(tileSet.textures[tile.gid - tileSet.firstgid]);
463
- }
464
- return textures;
465
- }
466
- get z() {
467
- return this.properties.z ?? 0;
468
- }
469
- get gid() {
470
- return this.tile.gid;
471
- }
472
- setAnimation(frame) {
473
- const size = this.animations.length;
474
- if (size > 1) {
475
- const offset = (this.animations[1].tileid - this.animations[0].tileid) * this.width;
476
- frame.tileAnimX(offset, size);
477
- }
478
- }
479
- flip() {
480
- let symmetry;
481
- let i = 0;
482
- const add = (symmetrySecond) => {
483
- i++;
484
- if (symmetry) symmetry = groupD8.add(symmetry, symmetrySecond);
485
- else symmetry = symmetrySecond;
486
- };
487
- if (this.tile.horizontalFlip) {
488
- add(groupD8.MIRROR_HORIZONTAL);
489
- }
490
- if (this.tile.verticalFlip) {
491
- add(groupD8.MIRROR_VERTICAL);
492
- }
493
- if (this.tile.diagonalFlip) {
494
- if (i % 2 == 0) {
495
- add(groupD8.MAIN_DIAGONAL);
496
- } else {
497
- add(groupD8.REVERSE_DIAGONAL);
498
- }
499
- }
500
- }
501
- };
502
-
503
- // src/Tilemap/TileLayer.ts
504
- settings.use32bitIndex = true;
505
- var CanvasTileLayer = class _CanvasTileLayer extends DisplayObject(CompositeTilemap) {
506
- constructor() {
507
- super(...arguments);
508
- this._tiles = {};
509
- // TODO: fix this, remove any. replace with Layer
510
- this.frameTile = 0;
511
- this.frameRateAnimation = 10;
512
- }
513
- static findTileSet(gid, tileSets) {
514
- let tileset;
515
- for (let i = tileSets.length - 1; i >= 0; i--) {
516
- tileset = tileSets[i];
517
- if (tileset.firstgid && tileset.firstgid <= gid) {
518
- break;
519
- }
520
- }
521
- return tileset;
522
- }
523
- /** @internal */
524
- createTile(x, y, options = {}) {
525
- const { real, filter } = options;
526
- const { tilewidth, tileheight, width } = this._layer;
527
- if (real) {
528
- x = Math.floor(x / tilewidth);
529
- y = Math.floor(y / tileheight);
530
- }
531
- const i = x + y * width;
532
- const tiledTile = this._layer.getTileByIndex(i);
533
- if (!tiledTile || tiledTile && tiledTile.gid == 0) return;
534
- const tileset = _CanvasTileLayer.findTileSet(tiledTile.gid, this.tileSets);
535
- if (!tileset) return;
536
- const tile = new Tile(tiledTile, tileset);
537
- tile.x = x * tilewidth;
538
- tile.y = y * tileheight + (tileheight - tile.texture.height);
539
- tile._x = x;
540
- tile._y = y;
541
- if (tileset.tileoffset) {
542
- tile.x += tileset.tileoffset.x ?? 0;
543
- tile.y += tileset.tileoffset.y ?? 0;
544
- }
545
- if (filter) {
546
- const ret = filter(tile);
547
- if (!ret) return;
548
- }
549
- return tile;
550
- }
551
- _addFrame(tile, x, y) {
552
- const frame = this.tile(tile.texture, tile.x, tile.y, {
553
- rotate: tile.texture.rotate
554
- });
555
- tile.setAnimation(frame);
556
- this._tiles[x + ";" + y] = tile;
557
- }
558
- async onMount(args) {
559
- const { props } = args;
560
- this.tileSets = props.tilesets;
561
- this._layer = new Layer(
562
- {
563
- ...props
564
- },
565
- this.tileSets
566
- );
567
- const tick3 = props.context.tick;
568
- this.subscriptionTick = tick3.observable.subscribe(({ value }) => {
569
- if (value.frame % this.frameRateAnimation == 0) {
570
- this.tileAnim = [this.frameTile, this.frameTile];
571
- this.frameTile++;
572
- }
573
- });
574
- super.onMount(args);
575
- }
576
- onUpdate(props) {
577
- super.onUpdate(props);
578
- if (!this.isMounted) return;
579
- if (props.tileheight) this._layer.tileheight = props.tileheight;
580
- if (props.tilewidth) this._layer.tilewidth = props.tilewidth;
581
- if (props.width) this._layer.width = props.width;
582
- if (props.height) this._layer.height = props.height;
583
- if (props.parallaxX) this._layer.parallaxX = props.parallaxx;
584
- if (props.parallaxY) this._layer.parallaxY = props.parallaxy;
585
- this.removeChildren();
586
- for (let y = 0; y < this._layer.height; y++) {
587
- for (let x = 0; x < this._layer.width; x++) {
588
- const tile = this.createTile(x, y);
589
- if (tile) {
590
- this._addFrame(tile, x, y);
591
- }
592
- }
593
- }
594
- }
595
- async onDestroy(parent) {
596
- this.subscriptionTick.unsubscribe();
597
- await super.onDestroy(parent);
598
- }
599
- };
600
- registerComponent("CompositeTileLayer", CanvasTileLayer);
601
- function CompositeTileLayer(props) {
602
- return createComponent("CompositeTileLayer", props);
603
- }
604
-
605
- // src/Tilemap/TileSet.ts
606
- import { Tileset as TiledTilesetClass } from "@rpgjs/tiled";
607
- import { Assets as Assets2, Rectangle, Texture as Texture2 } from "pixi.js";
608
- var TileSet = class extends TiledTilesetClass {
609
- constructor(tileSet) {
610
- super(tileSet);
611
- this.textures = [];
612
- this.tileGroups = {};
613
- }
614
- loadGroup() {
615
- }
616
- /** @internal */
617
- async load(image) {
618
- const texture = await Assets2.load(image);
619
- for (let y = this.margin; y < this.image.height; y += this.tileheight + this.spacing) {
620
- for (let x = this.margin; x < this.image.width; x += this.tilewidth + this.spacing) {
621
- this.textures.push(
622
- new Texture2({
623
- source: texture.source,
624
- frame: new Rectangle(+x, +y, +this.tilewidth, +this.tileheight)
625
- })
626
- );
627
- }
628
- }
629
- this.loadGroup();
630
- return this;
631
- }
632
- };
633
-
634
- // src/Tilemap/index.ts
635
- function reorganizeLayersByTileZ(originalLayers, tilesets, mapData) {
636
- const reorganizedLayers = [];
637
- for (const layer of originalLayers) {
638
- if (layer.type !== TiledLayerType.Tile) {
639
- reorganizedLayers.push(layer);
640
- continue;
641
- }
642
- const layersByZ = /* @__PURE__ */ new Map();
643
- let layerData;
644
- if (Array.isArray(layer.data)) {
645
- layerData = layer.data.map((gid) => {
646
- if (typeof gid === "number") {
647
- return gid;
648
- } else {
649
- return parseInt(String(gid), 10);
650
- }
651
- });
652
- } else {
653
- reorganizedLayers.push(layer);
654
- continue;
655
- }
656
- let tilesProcessed = 0;
657
- let tilesWithZ = 0;
658
- for (let i = 0; i < layerData.length; i++) {
659
- const gid = layerData[i];
660
- if (gid === 0) continue;
661
- tilesProcessed++;
662
- let tileset;
663
- for (let j = tilesets.length - 1; j >= 0; j--) {
664
- if (tilesets[j].firstgid && tilesets[j].firstgid <= gid) {
665
- tileset = tilesets[j];
666
- break;
667
- }
668
- }
669
- if (!tileset) {
670
- if (!layersByZ.has(0)) {
671
- layersByZ.set(0, new Array(layerData.length).fill(0));
672
- }
673
- layersByZ.get(0)[i] = gid;
674
- continue;
675
- }
676
- const localTileId = gid - tileset.firstgid;
677
- const tileProperties = tileset.tileset.tiles?.[localTileId]?.properties;
678
- const zValue = tileProperties?.z ?? 0;
679
- if (tileProperties?.z !== void 0) {
680
- tilesWithZ++;
681
- }
682
- if (!layersByZ.has(zValue)) {
683
- layersByZ.set(zValue, new Array(layerData.length).fill(0));
684
- }
685
- layersByZ.get(zValue)[i] = gid;
686
- }
687
- const sortedZValues = Array.from(layersByZ.keys()).sort((a, b) => a - b);
688
- for (const zValue of sortedZValues) {
689
- const layerDataForZ = layersByZ.get(zValue);
690
- if (layerDataForZ.some((gid) => gid !== 0)) {
691
- const newLayer = {
692
- ...layer,
693
- name: `${layer.name}_z${zValue}`,
694
- // Always add _z suffix
695
- data: layerDataForZ,
696
- properties: {
697
- ...layer.properties,
698
- z: zValue
699
- }
700
- };
701
- reorganizedLayers.push(newLayer);
702
- }
703
- }
704
- }
705
- reorganizedLayers.sort((a, b) => {
706
- const zA = a.properties?.z ?? 0.5;
707
- const zB = b.properties?.z ?? 0.5;
708
- return zA - zB;
709
- });
710
- return reorganizedLayers;
711
- }
712
- function TiledMap(props) {
713
- const { map, basePath, createLayersPerTilesZ } = useProps4(props, {
714
- createLayersPerTilesZ: false,
715
- basePath: ""
716
- });
717
- const layers = signal3([]);
718
- const objectLayer = props.objectLayer;
719
- let tilesets = [];
720
- let mapData = {};
721
- const parseTmx = async (file, relativePath = "") => {
722
- if (typeof file !== "string") {
723
- return file;
724
- }
725
- const parser = new TiledParserFile(
726
- file,
727
- {
728
- basePath: "",
729
- staticDir: "",
730
- relativePath
731
- }
732
- );
733
- const data = await parser.parseFilePromise({
734
- getOnlyBasename: false
735
- });
736
- return data;
737
- };
738
- effect2(async () => {
739
- const _map = map();
740
- if (_map) {
741
- mapData = await parseTmx(_map, basePath());
742
- tilesets = [];
743
- for (let tileSet of mapData.tilesets) {
744
- if (tileSet.tile) tileSet.tiles = tileSet.tile;
745
- if (!tileSet.tiles) tileSet.tiles = [];
746
- tilesets.push(await new TileSet(tileSet).load(tileSet.image.source));
747
- }
748
- let finalLayers = mapData.layers;
749
- if (createLayersPerTilesZ()) {
750
- finalLayers = reorganizeLayersByTileZ(mapData.layers, tilesets, mapData);
751
- }
752
- layers.set(finalLayers);
753
- }
754
- });
755
- const createLayer = (layers2, props2 = {}) => {
756
- return h5(Container4, props2, loop(layers2, (layer) => {
757
- switch (layer.type) {
758
- case TiledLayerType.Tile:
759
- return h5(CompositeTileLayer, {
760
- tilewidth: mapData.tilewidth,
761
- tileheight: mapData.tileheight,
762
- // @ts-ignore
763
- width: mapData.width,
764
- // @ts-ignore
765
- height: mapData.height,
766
- ...layer,
767
- tilesets
768
- });
769
- case TiledLayerType.Image:
770
- const { width, height, source } = layer.image;
771
- return h5(TilingSprite, {
772
- image: source,
773
- ...layer,
774
- width: layer.repeatx ? layer.width * layer.tilewidth : width,
775
- height: layer.repeaty ? layer.height * layer.tileheight : height
776
- });
777
- case TiledLayerType.Group:
778
- return createLayer(signal3(layer.layers), layer);
779
- case TiledLayerType.ObjectGroup:
780
- const child = objectLayer?.(layer);
781
- return h5(Container4, layer, child);
782
- default:
783
- return h5(Container4);
784
- }
785
- }));
786
- };
787
- return h5(Container4, props, createLayer(layers));
788
- }
789
-
790
- // src/Weathers/index.ts
791
- import {
792
- tick as tick2,
793
- useProps as useProps5,
794
- h as h6,
795
- Mesh,
796
- signal as signal4
797
- } from "canvasengine";
798
- import { Geometry, Shader, GlProgram, UniformGroup } from "pixi.js";
799
- var WeatherEffect = (options) => {
800
- const {
801
- speed = signal4(0.5),
802
- windDirection = signal4(0),
803
- windStrength = signal4(0.2),
804
- density = signal4(180),
805
- resolution = signal4([1e3, 1e3])
806
- } = useProps5(options);
807
- const speedSignal = typeof speed === "function" ? speed : signal4(speed);
808
- const windDirectionSignal = typeof windDirection === "function" ? windDirection : signal4(windDirection);
809
- const windStrengthSignal = typeof windStrength === "function" ? windStrength : signal4(windStrength);
810
- const densitySignal = typeof density === "function" ? density : signal4(density);
811
- const resolutionSignal = typeof resolution === "function" ? resolution : signal4(resolution);
812
- const vertexSrc = (
813
- /* glsl */
814
- `
1
+ import{Graphics as Z,h as j,useProps as ee}from"canvasengine";function X(c){var a=c.toString(16);return a.length==1?"0"+a:a}function te(c,a,e){return"#"+X(c)+X(a)+X(e)}function He(c){let{width:a,height:e,value:r,maxValue:i,backgroundColor:n,foregroundColor:d,border:f,innerMargin:l,borderRadius:u}=ee(c,{backgroundColor:"#000000",foregroundColor:"#FFFFFF",innerMargin:0,borderRadius:0});return j(Z,{...c,width:a,height:e,draw(t){u()?t.roundRect(0,0,a(),e(),u()):t.rect(0,0,a(),e()),f&&t.stroke(f),t.fill(n())}},j(Z,{width:a,height:e,draw(t){let s=l(),o=u(),m=Math.max(0,Math.min(a()-2*s,r()/i()*(a()-2*s))),g=e()-2*s;u?t.roundRect(s,s,m,g,o):t.rect(s,s,m,g);let P=d();if(P.startsWith("rgba")){let[T,S,L,b]=P.match(/\d+(\.\d+)?/g).map(Number);t.fill({color:te(T,S,L),alpha:b})}else t.fill(P)}}))}import*as A from"pixi.js";import{FX as ie}from"revolt-fx";import{h as re,mount as ne,tick as se,Container as oe,on as ae,useProps as le}from"canvasengine";function qe(c){let{emit:a,settings:e={}}=c,{name:r}=le(c),i=new ie,n;return A.Assets.add({alias:"fx_settings",src:"/default-bundle.json"}),A.Assets.add({alias:"fx_spritesheet",src:"/revoltfx-spritesheet.json"}),se(({deltaRatio:d})=>{i.update(d)}),ne(async d=>{n=d;let l={...(await A.Assets.load(["fx_settings","fx_spritesheet"])).fx_settings};if(e.emitters){let t=e.emitters.map((s,o)=>({...s,id:1e4+o}));l.emitters=[...l.emitters,...t]}i.initBundle(l,!0)}),ae(a,()=>{i.getParticleEmitter(r()).init(n.componentInstance)}),re(oe)}import{Container as fe,Graphics as N,h as z,mount as ue,useProps as ce,animatedSignal as de,RadialGradient as he,effect as me,isSignal as W,signal as H,isObservable as pe}from"canvasengine";function Qe(c){let{radius:a}=ce(c),e=de(1),r=1,i=2,n=.001,d=()=>{let l=Date.now()*n,u=(Math.sin(l)+Math.sin(l*1.3)+Math.sin(l*.7))/3,t=r+(i-r)*(u*.5+.5);e.update(()=>t),requestAnimationFrame(d)};return d(),z(N,{draw:l=>{let u=a()*2,t=new he(u,u,0,u,u,0);t.addColorStop(0,"rgba(255, 255, 0, 1)"),t.addColorStop(.5,"rgba(255, 255, 0, 0.3)"),t.addColorStop(.8,"rgba(255, 255, 0, 0)");let s=u/2;l.rect(-s,-s,u,u).fill(t.render({translate:{x:s,y:s}}))},...c,scale:e})}function et(c){let{children:a}=c,e,r=H(0),i=H(0),n,d=f=>{f.rect(-80,-80,r()+80*2,i()+80*2),f.fill(0);let u=t=>{let s=W(t.propObservables.x)?t.propObservables.x():t.props.x,o=W(t.propObservables.y)?t.propObservables.y():t.props.y,m=W(t.propObservables.radius)?t.propObservables.radius():t.props.radius;f.circle(s,o,m),f.cut()};for(let t of a){if(pe(t)){n&&n.unsubscribe(),n=t.subscribe(s=>{for(let o of s.fullElements)u(o)});return}u(t)}};return ue(f=>{me(()=>{let{displayWidth:l,displayHeight:u}=f.componentInstance,t=+l(),s=+u();setTimeout(()=>{r.update(()=>t),i.update(()=>s)},0)})}),z(fe,{width:"100%",height:"100%",...c},z(N,{draw:d,alpha:.8,blur:80}),...a)}import*as q from"pixi.js";import{Container as ge,Graphics as Y,Sprite as J,h as C,signal as B}from"canvasengine";var ye=(l=>(l.LEFT="left",l.TOP="top",l.BOTTOM="bottom",l.RIGHT="right",l.TOP_LEFT="top_left",l.TOP_RIGHT="top_right",l.BOTTOM_LEFT="bottom_left",l.BOTTOM_RIGHT="bottom_right",l))(ye||{});function rt(c={}){let a=Object.assign({outerScale:{x:1,y:1},innerScale:{x:1,y:1}},c),e=70,r=10,i=.5,n=!1,d=null,f=0,l=B(0),u=B(0),t=B(i);function s(b){let h=b.x-0,p=b.y-0;return Math.min(1,Math.sqrt(h*h+p*p)/e)}function o(b){let h=Math.atan2(b.y,b.x);return h>=-Math.PI/8&&h<0||h>=0&&h<Math.PI/8?"right":h>=Math.PI/8&&h<3*Math.PI/8?"bottom_right":h>=3*Math.PI/8&&h<5*Math.PI/8?"bottom":h>=5*Math.PI/8&&h<7*Math.PI/8?"bottom_left":h>=7*Math.PI/8&&h<Math.PI||h>=-Math.PI&&h<-7*Math.PI/8?"left":h>=-7*Math.PI/8&&h<-5*Math.PI/8?"top_left":h>=-5*Math.PI/8&&h<-3*Math.PI/8?"top":"top_right"}function m(b){d=b.getLocalPosition(this),n=!0,t.set(1),a.onStart?.()}function g(){n&&(l.set(0),u.set(0),n=!1,t.set(i),a.onEnd?.())}function P(b){if(n==!1)return;let h=b.getLocalPosition(this),p=h.x-(d?.x??0),y=h.y-(d?.y??0),x=new q.Point(0,0),w=0;if(p==0&&y==0)return;let U=0;p*p+y*y>=e*e?U=e:U=e-r;let M="left";if(p==0){y>0?(x.set(0,y>e?e:y),w=270,M="bottom"):(x.set(0,-(Math.abs(y)>e?e:Math.abs(y))),w=90,M="top"),l.set(x.x),u.set(x.y),f=s(x),a.onChange?.({angle:w,direction:M,power:f});return}if(y==0){p>0?(x.set(Math.abs(p)>e?e:Math.abs(p),0),w=0,M="left"):(x.set(-(Math.abs(p)>e?e:Math.abs(p)),0),w=180,M="right"),l.set(x.x),u.set(x.y),f=s(x),a.onChange?.({angle:w,direction:M,power:f});return}let Q=Math.abs(y/p),k=Math.atan(Q);w=k*180/Math.PI;let R=0,_=0;p*p+y*y>=e*e?(R=e*Math.cos(k),_=e*Math.sin(k)):(R=Math.abs(p)>e?e:Math.abs(p),_=Math.abs(y)>e?e:Math.abs(y)),y<0&&(_=-Math.abs(_)),p<0&&(R=-Math.abs(R)),p>0&&y<0||(p<0&&y<0?w=180-w:p<0&&y>0?w=w+180:p>0&&y>0&&(w=360-w)),x.set(R,_),f=s(x),M=o(x),l.set(x.x),u.set(x.y),a.onChange?.({angle:w,direction:M,power:f})}let T,S;a.outer?S=C(J,{image:a.outer,anchor:{x:.5,y:.5},scale:a.outerScale}):S=C(Y,{draw:b=>{b.circle(0,0,e).fill(0)},alpha:.5});let L={scale:a.innerScale,x:l,y:u,alpha:t};return a.inner?T=C(J,{image:a.inner,anchor:{x:.5,y:.5},...L}):T=C(Y,{draw:b=>{b.circle(0,0,r*2.5).fill(0)},...L}),C(ge,{...c,pointerdown:m,pointerup:g,pointerupoutside:g,pointermove:P},S,T)}import{TiledLayerType as D,TiledParserFile as Ce}from"@rpgjs/tiled";import{loop as Oe,h as I,Container as G,TilingSprite as De,useProps as Le,effect as Ae,signal as K}from"canvasengine";import{CompositeTilemap as be,settings as xe}from"@canvasengine/tilemap";import{Layer as we}from"@rpgjs/tiled";import{createComponent as Pe,registerComponent as ve,DisplayObject as Me}from"canvasengine";import{AnimatedSprite as Te,groupD8 as O}from"pixi.js";var F=class c extends Te{constructor(e,r){super(c.getTextures(e,r));this.tile=e;this.tileSet=r;this.animations=[];this._x=0;this._y=0;this.properties={};this.animations=e.animations||[],this.properties=e.properties,this.textures=c.getTextures(e,r),this.texture=this.textures[0],this.flip()}static getTextures(e,r){let i=[];return e.animations&&e.animations.length?e.animations.forEach(n=>{i.push(r.textures[n.tileid])}):i.push(r.textures[e.gid-r.firstgid]),i}get z(){return this.properties.z??0}get gid(){return this.tile.gid}setAnimation(e){let r=this.animations.length;if(r>1){let i=(this.animations[1].tileid-this.animations[0].tileid)*this.width;e.tileAnimX(i,r)}}flip(){let e,r=0,i=n=>{r++,e?e=O.add(e,n):e=n};this.tile.horizontalFlip&&i(O.MIRROR_HORIZONTAL),this.tile.verticalFlip&&i(O.MIRROR_VERTICAL),this.tile.diagonalFlip&&(r%2==0?i(O.MAIN_DIAGONAL):i(O.REVERSE_DIAGONAL))}};xe.use32bitIndex=!0;var V=class c extends Me(be){constructor(){super(...arguments);this._tiles={};this.frameTile=0;this.frameRateAnimation=10}static findTileSet(e,r){let i;for(let n=r.length-1;n>=0&&(i=r[n],!(i.firstgid&&i.firstgid<=e));n--);return i}createTile(e,r,i={}){let{real:n,filter:d}=i,{tilewidth:f,tileheight:l,width:u}=this._layer;n&&(e=Math.floor(e/f),r=Math.floor(r/l));let t=e+r*u,s=this._layer.getTileByIndex(t);if(!s||s&&s.gid==0)return;let o=c.findTileSet(s.gid,this.tileSets);if(!o)return;let m=new F(s,o);if(m.x=e*f,m.y=r*l+(l-m.texture.height),m._x=e,m._y=r,o.tileoffset&&(m.x+=o.tileoffset.x??0,m.y+=o.tileoffset.y??0),!(d&&!d(m)))return m}_addFrame(e,r,i){let n=this.tile(e.texture,e.x,e.y,{rotate:e.texture.rotate});e.setAnimation(n),this._tiles[r+";"+i]=e}async onMount(e){let{props:r}=e;this.tileSets=r.tilesets,this._layer=new we({...r},this.tileSets);let i=r.context.tick;this.subscriptionTick=i.observable.subscribe(({value:n})=>{n.frame%this.frameRateAnimation==0&&(this.tileAnim=[this.frameTile,this.frameTile],this.frameTile++)}),super.onMount(e)}onUpdate(e){if(super.onUpdate(e),!!this.isMounted){e.tileheight&&(this._layer.tileheight=e.tileheight),e.tilewidth&&(this._layer.tilewidth=e.tilewidth),e.width&&(this._layer.width=e.width),e.height&&(this._layer.height=e.height),e.parallaxX&&(this._layer.parallaxX=e.parallaxx),e.parallaxY&&(this._layer.parallaxY=e.parallaxy),this.removeChildren();for(let r=0;r<this._layer.height;r++)for(let i=0;i<this._layer.width;i++){let n=this.createTile(i,r);n&&this._addFrame(n,i,r)}}}async onDestroy(e){this.subscriptionTick.unsubscribe(),await super.onDestroy(e)}};ve("CompositeTileLayer",V);function $(c){return Pe("CompositeTileLayer",c)}import{Tileset as Se}from"@rpgjs/tiled";import{Assets as Ie,Rectangle as Re,Texture as _e}from"pixi.js";var E=class extends Se{constructor(e){super(e);this.textures=[];this.tileGroups={}}loadGroup(){}async load(e){let r=await Ie.load(e);for(let i=this.margin;i<this.image.height;i+=this.tileheight+this.spacing)for(let n=this.margin;n<this.image.width;n+=this.tilewidth+this.spacing)this.textures.push(new _e({source:r.source,frame:new Re(+n,+i,+this.tilewidth,+this.tileheight)}));return this.loadGroup(),this}};function Fe(c,a,e){let r=[];for(let i of c){if(i.type!==D.Tile){r.push(i);continue}let n=new Map,d;if(Array.isArray(i.data))d=i.data.map(t=>typeof t=="number"?t:parseInt(String(t),10));else{r.push(i);continue}let f=0,l=0;for(let t=0;t<d.length;t++){let s=d[t];if(s===0)continue;f++;let o;for(let T=a.length-1;T>=0;T--)if(a[T].firstgid&&a[T].firstgid<=s){o=a[T];break}if(!o){n.has(0)||n.set(0,new Array(d.length).fill(0)),n.get(0)[t]=s;continue}let m=s-o.firstgid,g=o.tileset.tiles?.[m]?.properties,P=g?.z??0;g?.z!==void 0&&l++,n.has(P)||n.set(P,new Array(d.length).fill(0)),n.get(P)[t]=s}let u=Array.from(n.keys()).sort((t,s)=>t-s);for(let t of u){let s=n.get(t);if(s.some(o=>o!==0)){let o={...i,name:`${i.name}_z${t}`,data:s,properties:{...i.properties,z:t}};r.push(o)}}}return r.sort((i,n)=>{let d=i.properties?.z??.5,f=n.properties?.z??.5;return d-f}),r}function Ee(c){let{map:a,basePath:e,createLayersPerTilesZ:r}=Le(c,{createLayersPerTilesZ:!1,basePath:""}),i=K([]),n=c.objectLayer,d=[],f={},l=async(t,s="")=>typeof t!="string"?t:await new Ce(t,{basePath:"",staticDir:"",relativePath:s}).parseFilePromise({getOnlyBasename:!1});Ae(async()=>{let t=a();if(t){f=await l(t,e()),d=[];for(let o of f.tilesets)o.tile&&(o.tiles=o.tile),o.tiles||(o.tiles=[]),d.push(await new E(o).load(o.image.source));let s=f.layers;r()&&(s=Fe(f.layers,d,f)),i.set(s)}});let u=(t,s={})=>I(G,s,Oe(t,o=>{switch(o.type){case D.Tile:return I($,{tilewidth:f.tilewidth,tileheight:f.tileheight,width:f.width,height:f.height,...o,tilesets:d});case D.Image:let{width:m,height:g,source:P}=o.image;return I(De,{image:P,...o,width:o.repeatx?o.width*o.tilewidth:m,height:o.repeaty?o.height*o.tileheight:g});case D.Group:return u(K(o.layers),o);case D.ObjectGroup:let T=n?.(o);return I(G,o,T);default:return I(G)}}));return I(G,c,u(i))}import{tick as Ge,useProps as ke,h as Xe,Mesh as We,signal as v}from"canvasengine";import{Geometry as ze,Shader as Be,GlProgram as Ve,UniformGroup as Ue}from"pixi.js";var Ze=c=>{let{speed:a=v(.5),windDirection:e=v(0),windStrength:r=v(.2),density:i=v(180),resolution:n=v([1e3,1e3])}=ke(c),d=typeof a=="function"?a:v(a),f=typeof e=="function"?e:v(e),l=typeof r=="function"?r:v(r),u=typeof i=="function"?i:v(i),t=typeof n=="function"?n:v(n),s=`
815
2
  precision mediump float;
816
3
  attribute vec2 aPosition;
817
4
  attribute vec2 aUV;
@@ -820,11 +7,7 @@ var WeatherEffect = (options) => {
820
7
  vUV = aUV;
821
8
  gl_Position = vec4(aPosition, 0.0, 1.0);
822
9
  }
823
- `
824
- );
825
- const fragmentSrc = (
826
- /* glsl */
827
- `
10
+ `,o=`
828
11
  precision mediump float;
829
12
  varying vec2 vUV;
830
13
  uniform float uTime;
@@ -904,53 +87,5 @@ var WeatherEffect = (options) => {
904
87
 
905
88
  gl_FragColor = vec4(rainColor * rain, rain * 0.8);
906
89
  }
907
- `
908
- );
909
- const glProgram = new GlProgram({ vertex: vertexSrc, fragment: fragmentSrc });
910
- const uniformGroup = new UniformGroup({
911
- uTime: { value: 0, type: "f32" },
912
- uResolution: { value: resolutionSignal(), type: "vec2<f32>" },
913
- uRainSpeed: { value: speedSignal(), type: "f32" },
914
- uWindDirection: { value: windDirectionSignal(), type: "f32" },
915
- uWindStrength: { value: windStrengthSignal(), type: "f32" },
916
- uRainDensity: { value: densitySignal(), type: "f32" }
917
- });
918
- const shader = new Shader({
919
- glProgram,
920
- resources: {
921
- uniforms: uniformGroup
922
- }
923
- });
924
- const geometry = new Geometry({
925
- attributes: {
926
- aPosition: [-1, -1, 1, -1, 1, 1, -1, 1],
927
- aUV: [0, 0, 1, 0, 1, 1, 0, 1]
928
- },
929
- indexBuffer: [0, 1, 2, 0, 2, 3]
930
- });
931
- tick2(({ deltaTime }) => {
932
- uniformGroup.uniforms.uTime += deltaTime;
933
- uniformGroup.uniforms.uRainSpeed = speedSignal();
934
- uniformGroup.uniforms.uWindDirection = windDirectionSignal();
935
- uniformGroup.uniforms.uWindStrength = windStrengthSignal();
936
- uniformGroup.uniforms.uRainDensity = densitySignal();
937
- uniformGroup.uniforms.uResolution = resolutionSignal();
938
- });
939
- return h6(Mesh, {
940
- geometry,
941
- shader
942
- });
943
- };
944
- var Weather = WeatherEffect;
945
- export {
946
- Bar,
947
- Direction,
948
- Joystick,
949
- LightSpot,
950
- NightAmbiant,
951
- Particle,
952
- TiledMap,
953
- Weather,
954
- WeatherEffect
955
- };
90
+ `,m=new Ve({vertex:s,fragment:o}),g=new Ue({uTime:{value:0,type:"f32"},uResolution:{value:t(),type:"vec2<f32>"},uRainSpeed:{value:d(),type:"f32"},uWindDirection:{value:f(),type:"f32"},uWindStrength:{value:l(),type:"f32"},uRainDensity:{value:u(),type:"f32"}}),P=new Be({glProgram:m,resources:{uniforms:g}}),T=new ze({attributes:{aPosition:[-1,-1,1,-1,1,1,-1,1],aUV:[0,0,1,0,1,1,0,1]},indexBuffer:[0,1,2,0,2,3]});return Ge(({deltaTime:S})=>{g.uniforms.uTime+=S,g.uniforms.uRainSpeed=d(),g.uniforms.uWindDirection=f(),g.uniforms.uWindStrength=l(),g.uniforms.uRainDensity=u(),g.uniforms.uResolution=t()}),Xe(We,{geometry:T,shader:P})},Ot=Ze;export{He as Bar,ye as Direction,rt as Joystick,Qe as LightSpot,et as NightAmbiant,qe as Particle,Ee as TiledMap,Ot as Weather,Ze as WeatherEffect};
956
91
  //# sourceMappingURL=index.js.map