create-exo-app 0.19.1 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -346,12 +346,12 @@
346
346
  0,
347
347
  0,
348
348
  0,
349
- 0,
350
349
  6,
351
350
  0,
352
351
  0,
353
352
  0,
354
353
  0,
354
+ 6,
355
355
  0,
356
356
  0,
357
357
  0,
@@ -366,15 +366,15 @@
366
366
  0,
367
367
  0,
368
368
  0,
369
- 0,
370
369
  6,
371
370
  0,
372
371
  0,
373
372
  0,
374
373
  0,
375
374
  6,
376
- 6,
377
- 6,
375
+ 0,
376
+ 0,
377
+ 0,
378
378
  0,
379
379
  0,
380
380
  6,
@@ -387,7 +387,6 @@
387
387
  0,
388
388
  0,
389
389
  0,
390
- 6,
391
390
  0,
392
391
  0,
393
392
  0,
@@ -397,17 +396,12 @@
397
396
  0,
398
397
  0,
399
398
  0,
400
- 6,
401
- 6,
402
- 0,
403
399
  0,
404
400
  6,
405
401
  6,
406
- 6,
407
- 6,
408
402
  0,
409
403
  0,
410
- 6,
404
+ 0,
411
405
  0,
412
406
  0,
413
407
  0,
@@ -418,16 +412,16 @@
418
412
  0,
419
413
  0,
420
414
  6,
421
- 6,
422
415
  0,
423
416
  0,
424
417
  0,
425
418
  0,
426
419
  0,
427
420
  6,
421
+ 6,
422
+ 0,
428
423
  0,
429
424
  0,
430
- 6,
431
425
  0,
432
426
  0,
433
427
  0,
@@ -436,7 +430,13 @@
436
430
  6,
437
431
  6,
438
432
  6,
439
- 0,
433
+ 6,
434
+ 6,
435
+ 128,
436
+ 6,
437
+ 6,
438
+ 6,
439
+ 6,
440
440
  6,
441
441
  6,
442
442
  0,
@@ -444,15 +444,15 @@
444
444
  0,
445
445
  0,
446
446
  0,
447
- 6,
448
447
  0,
449
448
  0,
449
+ 6,
450
+ 0,
450
451
  0,
451
452
  0,
452
453
  0,
453
454
  0,
454
455
  0,
455
- 6,
456
456
  0,
457
457
  0,
458
458
  0,
@@ -464,12 +464,6 @@
464
464
  0,
465
465
  0,
466
466
  0,
467
- 6,
468
- 6,
469
- 6,
470
- 6,
471
- 6,
472
- 6,
473
467
  0,
474
468
  0,
475
469
  6,
@@ -477,14 +471,14 @@
477
471
  0,
478
472
  0,
479
473
  0,
480
- 6,
481
- 6,
482
474
  0,
483
475
  0,
484
476
  0,
485
477
  0,
486
478
  0,
487
479
  0,
480
+ 6,
481
+ 6,
488
482
  0,
489
483
  0,
490
484
  0,
@@ -492,28 +486,14 @@
492
486
  0,
493
487
  0,
494
488
  0,
495
- 6,
496
489
  0,
497
490
  0,
498
491
  0,
499
492
  0,
500
- 6,
501
- 6,
502
493
  0,
503
494
  0,
504
- 6,
505
- 6,
506
- 6,
507
- 6,
508
- 6,
509
- 6,
510
- 6,
511
- 6,
512
- 6,
513
495
  0,
514
496
  0,
515
- 6,
516
- 0,
517
497
  0,
518
498
  0,
519
499
  0,
@@ -521,7 +501,6 @@
521
501
  6,
522
502
  0,
523
503
  0,
524
- 6,
525
504
  0,
526
505
  0,
527
506
  0,
@@ -535,41 +514,51 @@
535
514
  0,
536
515
  0,
537
516
  0,
538
- 6,
539
517
  0,
540
- 6,
541
- 6,
542
518
  0,
543
519
  0,
544
520
  6,
545
- 0,
546
- 0,
547
521
  6,
548
522
  6,
549
523
  6,
550
524
  6,
551
525
  6,
552
526
  6,
527
+ 0,
528
+ 0,
553
529
  6,
530
+ 0,
531
+ 0,
532
+ 0,
533
+ 0,
534
+ 0,
535
+ 0,
536
+ 0,
537
+ 0,
538
+ 0,
539
+ 0,
554
540
  6,
555
541
  6,
556
542
  0,
557
543
  0,
558
544
  6,
559
545
  0,
560
- 6,
546
+ 0,
547
+ 0,
548
+ 0,
561
549
  6,
562
550
  0,
563
551
  0,
564
552
  0,
565
553
  0,
566
554
  0,
567
- 6,
568
555
  0,
569
556
  0,
570
557
  0,
571
558
  0,
572
559
  0,
560
+ 6,
561
+ 6,
573
562
  0,
574
563
  0,
575
564
  6,
@@ -578,21 +567,32 @@
578
567
  0,
579
568
  0,
580
569
  6,
581
- 6,
582
570
  0,
583
571
  0,
584
572
  0,
573
+ 6,
574
+ 6,
575
+ 6,
576
+ 6,
577
+ 0,
585
578
  0,
586
579
  0,
587
580
  6,
581
+ 6,
582
+ 6,
583
+ 6,
584
+ 6,
588
585
  0,
589
586
  0,
590
587
  0,
591
588
  0,
589
+ 6,
590
+ 0,
591
+ 0,
592
+ 0,
592
593
  0,
593
594
  0,
594
595
  0,
595
- 6,
596
596
  0,
597
597
  0,
598
598
  0,
@@ -679,7 +679,68 @@
679
679
  "spacing": 0,
680
680
  "tilecount": 204,
681
681
  "tileheight": 64,
682
- "tilewidth": 64
682
+ "tilewidth": 64,
683
+ "tiles": [
684
+ {
685
+ "id": 5,
686
+ "objectgroup": {
687
+ "draworder": "index",
688
+ "id": 2,
689
+ "name": "",
690
+ "opacity": 1,
691
+ "type": "objectgroup",
692
+ "visible": true,
693
+ "x": 0,
694
+ "y": 0,
695
+ "objects": [
696
+ {
697
+ "height": 64,
698
+ "id": 1,
699
+ "name": "",
700
+ "rotation": 0,
701
+ "type": "",
702
+ "visible": true,
703
+ "width": 64,
704
+ "x": 0,
705
+ "y": 0
706
+ }
707
+ ]
708
+ }
709
+ },
710
+ {
711
+ "id": 127,
712
+ "objectgroup": {
713
+ "draworder": "index",
714
+ "id": 2,
715
+ "name": "",
716
+ "opacity": 1,
717
+ "type": "objectgroup",
718
+ "visible": true,
719
+ "x": 0,
720
+ "y": 0,
721
+ "objects": [
722
+ {
723
+ "height": 64,
724
+ "id": 1,
725
+ "name": "",
726
+ "rotation": 0,
727
+ "type": "",
728
+ "visible": true,
729
+ "width": 64,
730
+ "x": 0,
731
+ "y": 0
732
+ }
733
+ ]
734
+ },
735
+ "properties": [
736
+ {
737
+ "name": "door",
738
+ "type": "bool",
739
+ "value": true
740
+ }
741
+ ]
742
+ }
743
+ ]
683
744
  }
684
745
  ],
685
746
  "tilewidth": 64,
@@ -0,0 +1,159 @@
1
+ import { Container, type PointLike, Vector } from '@codexo/exojs';
2
+ import { NavigationAgentComponent, type TileNavigationSpace } from '@codexo/exojs/pathfinding';
3
+ import { CircleShape, KinematicMovementComponent, type PhysicsBody, PhysicsBodyComponent, type PhysicsWorld } from '@codexo/exojs/physics';
4
+ import type { ResolvedTile } from '@codexo/exojs/tilemap';
5
+
6
+ export interface ActorOptions {
7
+ /** Where the actor's centre starts, in world pixels. */
8
+ readonly position: PointLike;
9
+ /** Collision radius, in pixels. Navigation keeps the actor to corridors at least this wide on both sides. */
10
+ readonly radius: number;
11
+ /** Walking speed, in pixels per second. */
12
+ readonly speed: number;
13
+ }
14
+
15
+ /**
16
+ * A circular character that walks the level: a kinematic body that the tile
17
+ * colliders stop, and a navigation agent that routes it around them.
18
+ *
19
+ * Both read the same `Walls` layer. Navigation treats a cell with any
20
+ * collision shape as fully blocked, so with whole-tile shapes a route it plans
21
+ * is one the body can walk; a partial shape only makes routes more cautious.
22
+ */
23
+ export interface Actor {
24
+ /** The scene node the body moves; attach visuals to it as children. */
25
+ readonly node: Container;
26
+ readonly body: PhysicsBody;
27
+ readonly movement: KinematicMovementComponent;
28
+ /**
29
+ * Steers the actor. It works in navigation space, which is world space
30
+ * shifted by {@link offset}: its path, destination and position read that
31
+ * much up and left of the body. Command the actor through {@link moveTo} and
32
+ * {@link follow}, which convert.
33
+ */
34
+ readonly agent: NavigationAgentComponent;
35
+ readonly radius: number;
36
+ /** Width, in tiles, of the square of free cells the actor needs to pass. */
37
+ readonly footprint: number;
38
+ /** How far navigation space is shifted up and left of world space, in pixels. */
39
+ readonly offset: number;
40
+ /**
41
+ * Walks the actor to the tile under `point`, in world pixels. The actor
42
+ * stops in the middle of the free square of {@link footprint} tiles that
43
+ * covers that tile and lies closest to `point`, not on `point` itself: a
44
+ * spot beside a wall is one the body cannot stand on. A tile no such square
45
+ * covers, such as a wall, ends as `unreachable`.
46
+ */
47
+ moveTo(point: PointLike): void;
48
+ /** Keeps walking after `target`, a live world-space position such as another actor's body. */
49
+ follow(target: PointLike): void;
50
+ }
51
+
52
+ /**
53
+ * The navigation-space centre of the free square of `footprint` cells that
54
+ * covers the cell under `point` and lies closest to it, or the cell under
55
+ * `point` when no free square covers it, so the route search reports it.
56
+ */
57
+ const standingPoint = (navigation: TileNavigationSpace<ResolvedTile>, footprint: number, point: PointLike): Vector => {
58
+ const size = navigation.cellSize;
59
+ const column = Math.floor(point.x / size);
60
+ const row = Math.floor(point.y / size);
61
+ const offset = ((footprint - 1) * size) / 2;
62
+ const best = new Vector((column + 0.5) * size - offset, (row + 0.5) * size - offset);
63
+ let bestDistance = Infinity;
64
+
65
+ // A door edit earlier in this step is not in the grid until it syncs.
66
+ navigation.sync();
67
+
68
+ for (let top = row - footprint + 1; top <= row; top++) {
69
+ for (let left = column - footprint + 1; left <= column; left++) {
70
+ if (!isFree(navigation, left, top, footprint)) {
71
+ continue;
72
+ }
73
+
74
+ const distance = Math.hypot((left + footprint / 2) * size - point.x, (top + footprint / 2) * size - point.y);
75
+
76
+ if (distance < bestDistance) {
77
+ bestDistance = distance;
78
+ best.set((left + 0.5) * size, (top + 0.5) * size);
79
+ }
80
+ }
81
+ }
82
+
83
+ return best;
84
+ };
85
+
86
+ const isFree = (navigation: TileNavigationSpace<ResolvedTile>, left: number, top: number, footprint: number): boolean => {
87
+ for (let row = top; row < top + footprint; row++) {
88
+ for (let column = left; column < left + footprint; column++) {
89
+ if (!navigation.isWalkable(column, row)) {
90
+ return false;
91
+ }
92
+ }
93
+ }
94
+
95
+ return true;
96
+ };
97
+
98
+ /**
99
+ * Creates an {@link Actor} on `parent`, colliding in `world` and routing
100
+ * through `navigation`.
101
+ *
102
+ * Routes are planned for the smallest square of whole tiles the actor's
103
+ * diameter fits in. The pathfinder measures that square from its top-left
104
+ * cell, so for an actor wider than one tile the navigation agent sees a
105
+ * position, and receives destinations, shifted by half the extra width:
106
+ * steering the body's centre along the square's corner cell instead would
107
+ * walk it into the wall beside that cell, and would test the wrong cells for
108
+ * a destination next to a wall.
109
+ */
110
+ export const spawnActor = (
111
+ parent: Container,
112
+ world: PhysicsWorld,
113
+ navigation: TileNavigationSpace<ResolvedTile>,
114
+ options: ActorOptions,
115
+ ): Actor => {
116
+ const { position, radius, speed } = options;
117
+ const footprint = Math.max(1, Math.ceil((radius * 2) / navigation.cellSize));
118
+ const offset = ((footprint - 1) * navigation.cellSize) / 2;
119
+ const node = new Container();
120
+
121
+ node.setPosition(position.x, position.y);
122
+
123
+ const physics = node.addComponent(
124
+ new PhysicsBodyComponent(world, { type: 'kinematic', colliders: [{ shape: new CircleShape(radius) }] }),
125
+ );
126
+ const movement = node.addComponent(new KinematicMovementComponent(physics, { maxSpeed: speed }));
127
+ const body = physics.body;
128
+ const shifted = (point: PointLike): PointLike => ({
129
+ get x() {
130
+ return point.x - offset;
131
+ },
132
+ get y() {
133
+ return point.y - offset;
134
+ },
135
+ });
136
+ const agent = node.addComponent(
137
+ new NavigationAgentComponent({
138
+ space: navigation,
139
+ movement,
140
+ position: shifted(body),
141
+ search: { smooth: true, agentSize: footprint },
142
+ arrivalDistance: 2,
143
+ }),
144
+ );
145
+
146
+ parent.addChild(node);
147
+
148
+ return {
149
+ node,
150
+ body,
151
+ movement,
152
+ agent,
153
+ radius,
154
+ footprint,
155
+ offset,
156
+ moveTo: point => agent.moveTo(standingPoint(navigation, footprint, point)),
157
+ follow: target => agent.follow(shifted(target)),
158
+ };
159
+ };
@@ -1,27 +1,31 @@
1
1
  /**
2
- * The level both scenes use, one character per tile: `#` is a wall, `S` is the
3
- * spawn, everything else is walkable floor.
2
+ * The level `ProceduralMapScene` builds, one character per tile: `#` is a wall,
3
+ * `D` a closed door, `S` the spawn, everything else is walkable floor.
4
4
  *
5
- * Keeping it here rather than in a scene is what lets the procedural scene and
6
- * the Tiled scene claim to show the same level. `town-square.tmj` was generated
7
- * from these rows, so a change here means regenerating that file - see the
8
- * README.
5
+ * `town-square.tmj` holds the same layout for `TiledMapScene`, so a change here
6
+ * means redrawing that file too - see the README. Neither scene reads this file
7
+ * for collision or navigation: both derive those from their map's `Walls`
8
+ * layer, so what the player sees is always what blocks them.
9
+ *
10
+ * The layout exercises what a top-down level needs: a one-tile gap only a
11
+ * narrow actor fits through, a two-tile opening a wide one can use, a door that
12
+ * opens and closes, and a sealed room nothing can reach.
9
13
  */
10
14
  export const LEVEL = [
11
15
  '####################',
12
- '# # #',
13
- '# S # ### #',
14
- '# # # #',
15
- '# #### # # #',
16
- '# # # #### #',
17
- '# # # #',
18
- '# ###### # #',
19
- '# # #',
20
- '# ######### # #',
21
- '# # # #',
22
- '# # ######### # #',
23
- '# # # #',
24
- '# # # #',
16
+ '# # # #',
17
+ '# S # # #',
18
+ '# # #',
19
+ '# # # #',
20
+ '# ######D#####',
21
+ '# # #',
22
+ '# # #',
23
+ '# #',
24
+ '# #',
25
+ '###### # #',
26
+ '# # # #',
27
+ '# # # #### #',
28
+ '#### # #',
25
29
  '####################',
26
30
  ] as const;
27
31
 
@@ -31,19 +35,13 @@ export const TILE = 64;
31
35
  export const COLUMNS = LEVEL[0].length;
32
36
  export const ROWS = LEVEL.length;
33
37
 
34
- export const isWall = (column: number, row: number): boolean => LEVEL[row]?.[column] === '#';
35
-
36
- /**
37
- * Traversal cost of one cell for the pathfinder: `0` means impassable, and any
38
- * positive number is a relative cost.
39
- */
40
- export const cellCost = (column: number, row: number): number => (isWall(column, row) ? 0 : 1);
38
+ /** Local tile ids in `map-pack.png`. `town-square.tmj` uses the same ones. */
39
+ export const FLOOR_TILE = 0;
40
+ export const WALL_TILE = 5;
41
+ export const DOOR_TILE = 127;
41
42
 
42
- /** World-space centre of a tile. */
43
- export const tileCenter = (column: number, row: number): { x: number; y: number } => ({
44
- x: (column + 0.5) * TILE,
45
- y: (row + 0.5) * TILE,
46
- });
43
+ /** The level character at a tile, or `undefined` outside the level. */
44
+ export const cellAt = (column: number, row: number): string | undefined => LEVEL[row]?.[column];
47
45
 
48
46
  /** Tile coordinates of the first occurrence of `marker`. */
49
47
  export const findMarker = (marker: string): { column: number; row: number } => {
@@ -22,8 +22,9 @@ const app = new Application({
22
22
  clearColor: new Color(19, 26, 23),
23
23
  });
24
24
 
25
- // Two scenes, one level. `ProceduralMapScene` builds the map in code and
26
- // `TiledMapScene` loads it from `town-square.tmj`; both share everything else
27
- // through `TopDownScene`. Start whichever suits the project and delete the
28
- // other - or switch at runtime with `app.scenes.change(TiledMapScene)`.
29
- await app.start(ProceduralMapScene);
25
+ // Two scenes, one level. `TiledMapScene` loads it from `town-square.tmj`, the
26
+ // map a designer edits; `ProceduralMapScene` builds the same map in code. Both
27
+ // derive collision and navigation from the map they load, through
28
+ // `TopDownScene`. Start whichever suits the project and delete the other - or
29
+ // switch at runtime with `app.scenes.change(ProceduralMapScene)`.
30
+ await app.start(TiledMapScene);