Skip to content

Commit

Permalink
Add imagePathAccessor override
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranayub committed Oct 27, 2015
1 parent 91e804c commit f3a23b4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,23 @@ game.start(loader).then(function() {

## Documentation

The `TiledResource` loadable will load the map file you specify along with any referenced tile set assets (images). The image paths
loaded will be relative to where the original TMX file lived.
The `TiledResource` loadable will load the map file you specify along with any referenced tile set assets (images).

### Handling Tile Image Paths

The image paths loaded will be relative to where the exported file was saved.

If you need to override this behavior, you can set `imagePathAccessor` to a custom function that takes two parameters: path and `ITiledTileSet` data.

```js
// Create a new TiledResource loadable
var map = new ex.Extensions.Tiled.TiledResource("test.json");

map.imagePathAccessor = function (path, tileset) {
return "/maps/tx/" + path;
}
```

### Supported Formats

Only supports JSON file format with CSV or Base64 (uncompressed) tile layer format.
1 change: 1 addition & 0 deletions dist/excalibur-tiled.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ declare namespace ex.Extensions.Tiled {
}
class TiledResource extends ex.Resource<ITiledMap> {
protected mapFormat: TiledMapFormat;
imagePathAccessor: (string, ITiledTileSet) => string;
constructor(path: string, mapFormat?: TiledMapFormat);
load(): Promise<ITiledMap>;
processDownload(data: any): ITiledMap;
Expand Down
3 changes: 2 additions & 1 deletion dist/excalibur-tiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var ex;
throw "The format " + mapFormat + " is not currently supported. Please export Tiled map as JSON.";
}
this.mapFormat = mapFormat;
this.imagePathAccessor = function (s) { return s; };
}
TiledResource.prototype.load = function () {
var _this = this;
Expand All @@ -41,7 +42,7 @@ var ex;
var promises = [];
// retrieve images from tilesets and create textures
_this.data.tilesets.forEach(function (ts) {
var tx = new ex.Texture(ts.image);
var tx = new ex.Texture(_this.imagePathAccessor(ts.image, ts));
ts.imageTexture = tx;
promises.push(tx.load());
ex.Logger.getInstance().debug("[Tiled] Loading associated tileset: " + ts.image);
Expand Down
5 changes: 4 additions & 1 deletion src/TiledResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace ex.Extensions.Tiled {
export class TiledResource extends ex.Resource<ITiledMap> {

protected mapFormat: TiledMapFormat;

public imagePathAccessor: (string, ITiledTileSet) => string;

constructor(path: string, mapFormat = TiledMapFormat.JSON) {
switch (mapFormat) {
Expand All @@ -28,6 +30,7 @@ namespace ex.Extensions.Tiled {
}

this.mapFormat = mapFormat;
this.imagePathAccessor = (s) => s;
}

public load(): Promise<ITiledMap> {
Expand All @@ -39,7 +42,7 @@ namespace ex.Extensions.Tiled {

// retrieve images from tilesets and create textures
this.data.tilesets.forEach(ts => {
var tx = new ex.Texture(ts.image);
var tx = new ex.Texture(this.imagePathAccessor(ts.image, ts));
ts.imageTexture = tx;
promises.push(tx.load());

Expand Down

0 comments on commit f3a23b4

Please sign in to comment.