Skip to content

Commit

Permalink
fix: [#384] type->class rename in tiled 1.9 (breaking change) (#385)
Browse files Browse the repository at this point in the history
Closes #384

This PR fixes the plugin a breaking change introduced in tiled 1.9 https://doc.mapeditor.org/en/stable/reference/tmx-changelog/#tiled-1-9
  • Loading branch information
eonarheim authored Jun 29, 2022
1 parent 7175e01 commit ddf7996
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ tiledMapResource.addTiledMapToScene(game.currentScene);
![](./readme/camera.png)
- In an object layer with a custom property "excalibur"=true
- **Note** Only the first Camera in the first "excalibur"=true layer will be used
- Create a Tiled "Point" with the Tiled Type "Camera"
- Create a Tiled "Point" with the Tiled Class "Camera"
- Optionally, to set zoom other than the default of 1.0, create a custom property named "Zoom" with a numeric value

* **Solid layers** - You can mark a particular layers tiles as solid in Tiled
Expand All @@ -89,7 +89,7 @@ tiledMapResource.addTiledMapToScene(game.currentScene);
![](./readme/collider.png)
- In an object layer with a custom property "excalibur"=true
- Create a "Circle" (ellipses are not supported) or "Rectangle"
- Set the Tiled type to "BoxCollider" or "CircleCollider"
- Set the Tiled Class to "BoxCollider" or "CircleCollider"
- Optionally, to set an Excalibur collision type specify a custom property named "CollisionType" with the value
- "Fixed" (default for colliders) - non-movable object
- "Passive" - triggers events, does not participate in collision
Expand Down
6 changes: 3 additions & 3 deletions example/example-city.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.8" tiledversion="1.8.4" orientation="orthogonal" renderorder="right-down" width="100" height="100" tilewidth="16" tileheight="16" infinite="0" nextlayerid="11" nextobjectid="9">
<map version="1.9" tiledversion="1.9.0" orientation="orthogonal" renderorder="right-down" width="100" height="100" tilewidth="16" tileheight="16" infinite="0" nextlayerid="12" nextobjectid="9">
<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"/>
<tile id="190">
Expand Down Expand Up @@ -1002,10 +1002,10 @@
<properties>
<property name="excalibur" type="bool" value="true"/>
</properties>
<object id="6" name="player-start" type="player-start" x="375" y="104">
<object id="6" name="player-start" class="player-start" x="375" y="104">
<point/>
</object>
<object id="7" name="Camera" type="Camera" x="444.5" y="404.5">
<object id="7" name="Camera" class="Camera" x="444.5" y="404.5">
<properties>
<property name="Zoom" type="float" value="4"/>
</properties>
Expand Down
4 changes: 4 additions & 0 deletions src/raw-tiled-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export interface RawTiledObject {
* Angle in degrees clockwise
*/
rotation: number;
/**
* @deprecated Removed in Tiled 1.9 https://doc.mapeditor.org/en/stable/reference/tmx-changelog/#tiled-1-9
*/
type: string;
class: string;
visible: boolean;
width: number;
/**
Expand Down
4 changes: 4 additions & 0 deletions src/raw-tileset-tile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { TiledFrame, TiledProperty } from './tiled-types';

export interface RawTilesetTile {
id: number;
/**
* @deprecated Removed in Tiled 1.9 https://doc.mapeditor.org/en/stable/reference/tmx-changelog/#tiled-1-9
*/
type: string;
class: string;
image: string;
imageheight: number;
imagewidth: number;
Expand Down
6 changes: 3 additions & 3 deletions src/tiled-map-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ export class TiledMapResource implements Loadable<TiledMap> {
const ex: ExcaliburData = {};
if (excaliburObjectLayers.length > 0) {
// Parse cameras find the first
ex.camera = excaliburObjectLayers.find(objectlayer => objectlayer.getObjectByType('camera'))?.getCamera();
ex.camera = excaliburObjectLayers.find(objectlayer => objectlayer.getObjectByClass('camera'))?.getCamera();
// Parse colliders
ex.colliders = [];
for (let objectLayer of excaliburObjectLayers) {

const boxColliders = objectLayer.getObjectsByType('boxcollider');
const boxColliders = objectLayer.getObjectsByClass('boxcollider');

for (let box of boxColliders) {
const collisionType = box.getProperty<CollisionType>('collisiontype');
Expand All @@ -303,7 +303,7 @@ export class TiledMapResource implements Loadable<TiledMap> {
});
}

const circleColliders = objectLayer.getObjectsByType('circlecollider');
const circleColliders = objectLayer.getObjectsByClass('circlecollider');
for (let circle of circleColliders) {
const collisionType = circle.getProperty<CollisionType>('collisiontype');
const color = circle.getProperty<string>('color');
Expand Down
21 changes: 20 additions & 1 deletion src/tiled-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class TiledObjectGroup extends TiledEntity {
public order!: number;

public getCamera(): ExcaliburCamera | undefined {
const camera = this.getObjectByType('camera');
const camera = this.getObjectByClass('camera');
if (camera) {
const zoom = camera.getProperty<number>('zoom');
return ({
Expand All @@ -45,14 +45,28 @@ export class TiledObjectGroup extends TiledEntity {
}
}

/**
* @deprecated Removed in Tiled 1.9 https://doc.mapeditor.org/en/stable/reference/tmx-changelog/#tiled-1-9
*/
public getObjectByType(type: string): TiledObject | undefined {
return this.getObjectsByType(type)[0];
}

public getObjectByClass(type: string): TiledObject | undefined {
return this.getObjectsByClass(type)[0];
}

/**
* @deprecated Removed in Tiled 1.9 https://doc.mapeditor.org/en/stable/reference/tmx-changelog/#tiled-1-9
*/
public getObjectsByType(type: string): TiledObject[] {
return this.objects.filter(o => o.type?.toLocaleLowerCase() === type.toLocaleLowerCase());
}

public getObjectsByClass(type: string): TiledObject[] {
return this.objects.filter(o => o.class?.toLocaleLowerCase() === type.toLocaleLowerCase());
}

public getObjectByName(name: string): TiledObject | undefined {
return this.getObjectsByName(name)[0];
}
Expand Down Expand Up @@ -105,7 +119,11 @@ export class TiledObjectGroup extends TiledEntity {
}

export class TiledObject extends TiledEntity {
/**
* @deprecated Removed in tiled 1.9 https://doc.mapeditor.org/en/stable/reference/tmx-changelog/#tiled-1-9
*/
public type?: string;
public class?: string;
public x!: number;
public y!: number;
public visible!: boolean;
Expand Down Expand Up @@ -149,6 +167,7 @@ export class TiledObject extends TiledEntity {
resultObject.visible = object.visible ?? true;
resultObject.name = object.name;
resultObject.type = object.type;
resultObject.class = object.class;
resultObject.x = +object.x;
resultObject.y = +object.y;
resultObject.rotation = object.rotation ? toRadians(object.rotation) : 0;
Expand Down
4 changes: 2 additions & 2 deletions test/unit/camera.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
<properties>
<property name="excalibur" type="bool" value="true"/>
</properties>
<object id="6" name="player-start" type="player-start" x="0" y="0">
<object id="6" name="player-start" class="player-start" x="0" y="0">
<point/>
</object>
</objectgroup>
<objectgroup id="3" name="Camera">
<properties>
<property name="excalibur" type="bool" value="true"/>
</properties>
<object id="7" name="Camera" type="Camera" x="50" y="50">
<object id="7" name="Camera" class="Camera" x="50" y="50">
<properties>
<property name="Zoom" type="float" value="4"/>
</properties>
Expand Down
2 changes: 1 addition & 1 deletion test/unit/objects.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"name":"",
"point":true,
"rotation":0,
"type":"Camera",
"class":"Camera",
"visible":true,
"width":0,
"x":16,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/objects.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<properties>
<property name="excalibur" type="bool" value="true"/>
</properties>
<object id="1" type="Camera" x="16" y="16">
<object id="1" class="Camera" x="16" y="16">
<point/>
</object>
<object id="2" x="9.125" y="31.625">
Expand Down

0 comments on commit ddf7996

Please sign in to comment.