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

Conversation

eonarheim
Copy link
Member

@eonarheim eonarheim commented Jan 12, 2022

Closes #285

This PR implements implicit z-ordering based on the Tiled editor order. Z-index can still be overridden with a custom property if desired. The first layer will start at -1 z-index (the previous default) and will increment based on the Tiled order.

Example of inserted tiles using the implicit z-index, no more custom property!
implicit-z

Small-ish breaking change that moves all the optional properties to an option bag in the constructor

// New constructor
const map = new TiledMapResource('path/to/map.tmx', { firstLayerZIndex: -2 });

// Old constructor
const map = new TiledMapResource('path/to/map.tmx', undefined, -2);

TODOs before merge:

@kamranayub
Copy link
Member

Oh yeah I prefer the new constructor for sure

Copy link
Member

@kamranayub kamranayub left a comment

Choose a reason for hiding this comment

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

Looks pretty good. Left some suggestions to improve clarity and making property handling consistent for z index values.

example/game.ts Outdated Show resolved Hide resolved
src/tiled-map-resource.ts Outdated Show resolved Hide resolved
src/tiled-map-resource.ts Outdated Show resolved Hide resolved
src/tiled-map-resource.ts Outdated Show resolved Hide resolved
src/tiled-map-resource.ts Outdated Show resolved Hide resolved
src/tiled-map-resource.ts Show resolved Hide resolved
src/tiled-map-resource.ts Show resolved Hide resolved
src/tiled-map-resource.ts Outdated Show resolved Hide resolved
src/tiled-map.ts Outdated Show resolved Hide resolved
@eonarheim eonarheim merged commit 0f8d363 into main Jan 14, 2022
@eonarheim eonarheim deleted the fix/tiled-layer-order-z-index branch January 14, 2022 14:24
Copy link
Member

@kamranayub kamranayub left a comment

Choose a reason for hiding this comment

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

I won't block on these suggestions but I do think they make the code intent clear.

@@ -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.

Comment on lines +446 to +447
// coerce to integer
return +finalZ
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// coerce to integer
return +finalZ
return Number.parseInt(finalZ);

😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tiled layer order should help decide zIndex
3 participants