Skip to content

Commit

Permalink
feat: Add new tile accessor to get at tile props (#445)
Browse files Browse the repository at this point in the history
Based on this conversation excaliburjs/Excalibur#2673

We want a convenient way to get at custom tile properties!

```typescript

// Grab tiles by worldspace point and the named layer
const tile = map.getTileByPoint('Ground', evt.worldPos);
// or by coordinate
const tile = map.getTileByCoordiante('Ground, 10, 100);

console.log('tile', tile);
console.log('id', tile?.id);
console.log('tile props', tile?.properties);
```
  • Loading branch information
eonarheim authored Jul 6, 2023
1 parent d38bb58 commit a30dc97
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
20 changes: 4 additions & 16 deletions example/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,10 @@ const start = (mapFile: string) => {
game.start(loader).then(() => {

game.input.pointers.primary.on('down', evt => {
// ex TileMap data structure by name
const tilemap = map.getTileMapLayers().filter(tm => tm.name === 'Ground')[0];
const tile = tilemap.getTileByPoint(evt.worldPos);
const tileIndex = tilemap.tiles.indexOf(tile);

// Tiled data
// gid can be found by looking up the original data, locate layer by name
const tiledLayer = map.data.getTileLayerByName('Ground');
const tileGid = tiledLayer.data[tileIndex];

// tileset properties
const tiledTileset = map.getTilesetForTile(tileGid);
// odd quirk of Tiled's data the gid's here are off by 1 from the data array :/
const tiledTile = tiledTileset.tiles.find(t => t.id === (tileGid - 1));
console.log('gid', tileGid);
console.log('tile props', tiledTile?.properties);
const tile = map.getTileByPoint('Ground', evt.worldPos);
console.log('tile', tile);
console.log('id', tile?.id);
console.log('tile props', tile?.properties);
});

player.pos = ex.vec(100, 100);
Expand Down
49 changes: 47 additions & 2 deletions src/tiled-map-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ import {
IsometricMap,
IsometricEntityComponent,
Animation,
ParallaxComponent
ParallaxComponent,
Tile
} from 'excalibur';
import { ExcaliburData } from './tiled-types';
import { RawTiledTileset } from "./raw-tiled-tileset";
import { RawTiledLayer } from "./raw-tiled-layer";
import { RawTiledMap } from "./raw-tiled-map";
import { TiledMap } from './tiled-map-parser';
import { parseExternalJson, parseExternalTsx, TiledTileset } from './tiled-tileset';
import { parseExternalJson, parseExternalTsx, TiledTileset, TiledTilesetTile } from './tiled-tileset';
import { getCanonicalGid, isFlippedDiagonally, isFlippedHorizontally, isFlippedVertically } from './tiled-layer';
import { getProperty, TiledEntity } from './tiled-entity';
import { TiledObjectComponent } from './tiled-object-component';
Expand Down Expand Up @@ -705,4 +706,48 @@ export class TiledMapResource implements Loadable<TiledMap> {
}
return [];
}

private _lookupTile(tilemap: TileMap, tile: Tile, layerName: string) {
const tileIndex = tilemap.tiles.indexOf(tile); // both ex and tiled share the same index

// Tiled data
// gid can be found by looking up the original data, locate layer by name
const tiledLayer = this.data.getTileLayerByName(layerName);
const tileGid = getCanonicalGid(tiledLayer.data[tileIndex]);

// Tiled tileset properties
const tiledTileset = this.getTilesetForTile(tileGid);
// odd quirk of Tiled's data the gid's here are off by 1 from the data array :/
const tiledTile = tiledTileset.tiles.find(t => t.id === (tileGid - 1));
if (!tiledTile) {
return {
id: tileGid - 1,
tileset: tiledTileset,
properties: {}
} as TiledTilesetTile
}
return tiledTile;
}

public getTileByPoint(layerName: string, worldPos: Vector): TiledTilesetTile | null {
// ex TileMap data structure by name
const tilemap = this.getTileMapLayers().find(tm => tm.name === layerName);
if (tilemap) {

const tile = tilemap.getTileByPoint(worldPos);
return this._lookupTile(tilemap, tile, layerName);
}
return null;
}

public getTileByCoordinate(layerName: string, x: number, y: number): TiledTilesetTile | null {
// ex TileMap data structure by name
const tilemap = this.getTileMapLayers().find(tm => tm.name === layerName);
if (tilemap) {

const tile = tilemap.getTile(x, y);
return this._lookupTile(tilemap, tile, layerName);
}
return null;
}
}

0 comments on commit a30dc97

Please sign in to comment.