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.
@@ -1,31 +1,37 @@
1
- import { Asset, Container, TextureRegion } from '@codexo/exojs';
2
- import { TILE_TRANSFORM_IDENTITY, TileLayer, TileMap, TileSet } from '@codexo/exojs-tilemap';
1
+ import { Texture, TextureRegion } from '@codexo/exojs';
2
+ import { ObjectKind, ObjectLayer, TILE_TRANSFORM_IDENTITY, TileLayer, TileMap, type TileMapObject, TileSet } from '@codexo/exojs/tilemap';
3
3
 
4
- import { COLUMNS, isWall, ROWS, TILE } from '../level';
4
+ import { cellAt, COLUMNS, DOOR_TILE, findMarker, FLOOR_TILE, ROWS, TILE, WALL_TILE } from '../level';
5
5
  import { TopDownScene } from './TopDownScene';
6
6
 
7
- /** Local tile ids in `map-pack.png`. `town-square.tmj` uses the same two. */
8
- const FLOOR_TILE = 0;
9
- const WALL_TILE = 5;
7
+ /** A collision shape covering one whole tile, as Tiled stores a tile's collision. */
8
+ const wholeTile: TileMapObject = {
9
+ id: 1,
10
+ name: '',
11
+ type: '',
12
+ kind: ObjectKind.Rectangle,
13
+ x: 0,
14
+ y: 0,
15
+ width: TILE,
16
+ height: TILE,
17
+ rotation: 0,
18
+ visible: true,
19
+ properties: {},
20
+ };
10
21
 
11
22
  /**
12
- * The level built in code: a `TileSet` over the atlas, one `TileLayer` filled
13
- * from `level.ts`, and a `TileMap` around it. No map file is involved.
23
+ * The level built in code from `src/level.ts`: the same layers, tile
24
+ * definitions and spawn object `town-square.tmj` carries, so the rest of the
25
+ * scene cannot tell the two apart.
14
26
  *
15
27
  * This is the right shape when the level is generated - procedural dungeons,
16
28
  * puzzle boards, anything whose layout is decided at runtime. When a designer
17
29
  * edits the level instead, see `TiledMapScene`.
18
30
  */
19
31
  export class ProceduralMapScene extends TopDownScene {
20
- protected override async loadMap(): Promise<void> {
21
- // Awaited rather than handed over as a `loader.get()` handle: `TileSet`
22
- // needs a region with real dimensions, which an unhydrated handle has not
23
- // got yet.
24
- await this.loader.load(Asset.type('texture', 'map-pack.png'));
25
- }
26
-
27
- protected override buildMap(): Container {
28
- const texture = this.loader.get('map-pack.png');
32
+ protected override async loadMap(): Promise<TileMap> {
33
+ // Awaited so the texture has real dimensions: `TileSet` needs a region.
34
+ const texture = await this.assets.load(new Texture('map-pack.png'));
29
35
  const tileset = new TileSet({
30
36
  name: 'map-pack',
31
37
  texture: new TextureRegion(texture, { x: 0, y: 0, width: texture.width, height: texture.height }),
@@ -35,40 +41,59 @@ export class ProceduralMapScene extends TopDownScene {
35
41
  columns: 17,
36
42
  });
37
43
 
38
- const ground = new TileLayer({
39
- id: 1,
40
- name: 'ground',
41
- width: COLUMNS,
42
- height: ROWS,
43
- tileWidth: TILE,
44
- tileHeight: TILE,
45
- tilesets: [tileset],
46
- });
44
+ tileset.setDefinitions([
45
+ { localTileId: WALL_TILE, collision: [wholeTile] },
46
+ { localTileId: DOOR_TILE, collision: [wholeTile], properties: { door: true } },
47
+ ]);
48
+
49
+ const layer = (id: number, name: string): TileLayer =>
50
+ new TileLayer({ id, name, width: COLUMNS, height: ROWS, tileWidth: TILE, tileHeight: TILE, tilesets: [tileset] });
51
+ const ground = layer(1, 'Ground');
52
+ const walls = layer(2, 'Walls');
47
53
 
48
54
  for (let row = 0; row < ROWS; row++) {
49
55
  for (let column = 0; column < COLUMNS; column++) {
50
- ground.setTileAt(column, row, {
51
- tileset,
52
- localTileId: isWall(column, row) ? WALL_TILE : FLOOR_TILE,
53
- transform: TILE_TRANSFORM_IDENTITY,
54
- });
55
- }
56
- }
56
+ ground.setTileAt(column, row, { tileset, localTileId: FLOOR_TILE, transform: TILE_TRANSFORM_IDENTITY });
57
57
 
58
- const map = new TileMap({
59
- name: 'town-square',
60
- width: COLUMNS,
61
- height: ROWS,
62
- tileWidth: TILE,
63
- tileHeight: TILE,
64
- tilesets: [tileset],
65
- layers: [ground],
66
- });
58
+ const cell = cellAt(column, row);
67
59
 
68
- const container = new Container();
60
+ if (cell === '#' || cell === 'D') {
61
+ walls.setTileAt(column, row, { tileset, localTileId: cell === '#' ? WALL_TILE : DOOR_TILE, transform: TILE_TRANSFORM_IDENTITY });
62
+ }
63
+ }
64
+ }
69
65
 
70
- container.addChild(map.createView({ bands: { ground: ['ground'] } }).band('ground'));
66
+ const spawn = findMarker('S');
71
67
 
72
- return container;
68
+ // Built here rather than by the loader, so the scene owns it.
69
+ return this.track(
70
+ new TileMap({
71
+ name: 'town-square',
72
+ width: COLUMNS,
73
+ height: ROWS,
74
+ tileWidth: TILE,
75
+ tileHeight: TILE,
76
+ tilesets: [tileset],
77
+ layers: [ground, walls],
78
+ objectLayers: [
79
+ new ObjectLayer({
80
+ id: 3,
81
+ name: 'Markers',
82
+ objects: [
83
+ {
84
+ ...wholeTile,
85
+ name: 'spawn',
86
+ type: 'spawn',
87
+ kind: ObjectKind.Point,
88
+ x: (spawn.column + 0.5) * TILE,
89
+ y: (spawn.row + 0.5) * TILE,
90
+ width: 0,
91
+ height: 0,
92
+ },
93
+ ],
94
+ }),
95
+ ],
96
+ }),
97
+ );
73
98
  }
74
99
  }
@@ -1,33 +1,21 @@
1
- import { Asset, Container } from '@codexo/exojs';
2
- import { TileMapNode } from '@codexo/exojs-tiled';
3
- import type { TileMap } from '@codexo/exojs-tilemap';
1
+ import type { TileMap } from '@codexo/exojs/tilemap';
4
2
 
5
3
  import { TopDownScene } from './TopDownScene';
6
4
 
7
5
  /**
8
- * The same level, loaded from `public/assets/town-square.tmj` instead of built
9
- * in code.
6
+ * The level loaded from `public/assets/town-square.tmj`, edited in Tiled.
10
7
  *
11
- * `Asset.type('tileMap', url)` is the common-case binding: it parses the Tiled
12
- * source and converts it to the runtime `TileMap` that `TileMapNode` renders.
8
+ * `{ type: 'tileMap', source: url }` parses the Tiled source and converts it to the
9
+ * runtime `TileMap`, tileset collision shapes and tile properties included.
13
10
  * The tileset image is resolved relative to the map file, so both live in
14
- * `public/assets/`.
11
+ * `public/assets/`. The map belongs to this scene's loader and is released
12
+ * with the scene.
15
13
  *
16
14
  * `tiledExtension` has to be registered on the Application for this asset type
17
15
  * to exist - see `main.ts`.
18
16
  */
19
17
  export class TiledMapScene extends TopDownScene {
20
- private _map!: TileMap;
21
-
22
- protected override async loadMap(): Promise<void> {
23
- this._map = await this.loader.load(Asset.type('tileMap', 'town-square.tmj'));
24
- }
25
-
26
- protected override buildMap(): Container {
27
- const container = new Container();
28
-
29
- container.addChild(new TileMapNode(this._map));
30
-
31
- return container;
18
+ protected override async loadMap(): Promise<TileMap> {
19
+ return await this.loader.load({ type: 'tileMap', source: 'town-square.tmj' });
32
20
  }
33
21
  }