Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#285] Use Implicit Tiled layer order to default z index #290

Merged
merged 17 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions example/example-city.tmx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.5" tiledversion="1.5.0" orientation="orthogonal" renderorder="right-down" width="100" height="100" tilewidth="16" tileheight="16" infinite="0" nextlayerid="9" nextobjectid="8">
<map version="1.5" tiledversion="1.7.2" orientation="orthogonal" renderorder="right-down" width="100" height="100" tilewidth="16" tileheight="16" infinite="0" nextlayerid="9" nextobjectid="8">
<tileset firstgid="1" name="Kenny RPG Urban Pack" tilewidth="16" tileheight="16" tilecount="486" columns="27">
<image source="assets/kenny-rpg-urban-pack/tilemap_packed.png" width="432" height="288"/>
</tileset>
<layer id="1" name="Ground" width="100" height="100" locked="1">
<properties>
<property name="zindex" type="int" value="-1"/>
</properties>
<data encoding="csv">
29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,30,462,463,464,36,37,37,37,37,37,37,37,37,37,37,38,462,463,464,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,
29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,30,462,469,464,36,37,37,37,37,37,37,37,37,37,37,38,462,469,464,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,
Expand Down Expand Up @@ -111,9 +108,6 @@
</data>
</layer>
<layer id="4" name="Ground Level 2" width="100" height="100">
<properties>
<property name="zindex" type="int" value="-1"/>
</properties>
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
Expand Down Expand Up @@ -220,7 +214,6 @@
<layer id="2" name="Solid" width="100" height="100">
<properties>
<property name="solid" type="bool" value="true"/>
<property name="zindex" type="int" value="0"/>
</properties>
<data encoding="csv">
236,236,236,236,236,236,236,236,236,236,236,236,236,237,0,0,0,0,0,0,0,109,110,110,110,111,0,90,91,92,0,400,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
Expand Down Expand Up @@ -328,7 +321,6 @@
<layer id="3" name="Solid Level 2" width="100" height="100">
<properties>
<property name="solid" type="bool" value="true"/>
<property name="zindex" type="int" value="0"/>
</properties>
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,334,0,0,335,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
Expand Down Expand Up @@ -659,20 +651,8 @@
<properties>
<property name="excalibur" type="bool" value="true"/>
</properties>
<object id="1" gid="409" x="304" y="112" width="16" height="16">
<properties>
<property name="ZIndex" type="float" value="3"/>
</properties>
</object>
<object id="4" gid="1073741992" x="640" y="176" width="16" height="16">
<properties>
<property name="zindex" type="int" value="3"/>
</properties>
</object>
<object id="5" gid="2147484058" x="592" y="176" width="16" height="16">
<properties>
<property name="zindex" type="int" value="3"/>
</properties>
</object>
<object id="1" gid="409" x="304" y="112" width="16" height="16"/>
<object id="4" gid="1073741992" x="640" y="176" width="16" height="16"/>
<object id="5" gid="2147484058" x="592" y="176" width="16" height="16"/>
</objectgroup>
</map>
2 changes: 1 addition & 1 deletion example/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const start = (mapFile: string) => {
}
game.add(player);

const map = new TiledMapResource(mapFile);
const map = new TiledMapResource(mapFile, { startingLayerZIndex: -2 });
const loader = new ex.Loader([map]);
game.start(loader).then(() => {
player.pos = ex.vec(100, 100);
Expand Down
21 changes: 21 additions & 0 deletions src/tiled-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,29 @@ export class TiledLayer extends TiledEntity {
*/
public compression?: TiledCompression;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the other class members here aligned correctly?


/**
* Original order of the Tiled layer
*/
public order!: number;

/**
* Reference to the raw tiled layer data
*/
public rawLayer!: RawTiledLayer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too


public static parse(layer: RawTiledLayer): TiledLayer {
if (layer.type !== 'tilelayer') throw Error('Cannot parse a non tilelayer type layer');
const resultLayer = new TiledLayer();
resultLayer.id = +layer.id;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get this is a clever way to coerce but it's not clear what this does at first glance especially for someone not as familiar with hacks in JS.

Might as well use the standard, it's available in all browsers we support and it's clearer.

Suggested change
resultLayer.id = +layer.id;
resultLayer.id = Number.parseInt(layer.id);

Note radix is optional and is 10 by default anyway!

Note also that there was code before that didn't coerce, because this one character can get lost in the noise.

resultLayer.name = layer.name;
resultLayer.data = (layer.data as number[]);
resultLayer.width = layer.width;
resultLayer.height = layer.height;
resultLayer.encoding = layer.encoding ?? 'csv';
resultLayer.compression = layer.compression;
resultLayer.order = layer.order;
resultLayer.properties = layer.properties ?? [];
resultLayer.rawLayer = layer;
return resultLayer
}
}
Loading