Skip to content

Commit

Permalink
inherit props and class
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Jan 1, 2024
1 parent ba09af6 commit 6491901
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 15 deletions.
2 changes: 2 additions & 0 deletions example/isometric/isometric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ game.input.pointers.primary.on('wheel', (wheelEvent) => {
game.start(loader).then(() => {
tiledMap.addToScene(game.currentScene);
currentPointer = game.currentScene.camera.pos;

(window as any).tiledMap = tiledMap;
});
6 changes: 5 additions & 1 deletion example/orthogonal/coin.tx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<template>
<tileset firstgid="1" source="external-fantasy.tsx"/>
<object gid="94" width="16" height="16"/>
<object name="Coin" type="Collectable" gid="94" width="16" height="16">
<properties>
<property name="item" value="coin"/>
</properties>
</object>
</template>
6 changes: 5 additions & 1 deletion example/orthogonal/external-fantasy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
<export target="external-fantasy..tsj" format="json"/>
</editorsettings>
<image source="tilemap_packed.png" width="192" height="176"/>
<tile id="93">
<tile id="93" type="TilesetClass">
<properties>
<property name="item" value="test"/>
<property name="tileset" value="prop"/>
</properties>
<objectgroup draworder="index" id="2">
<object id="1" type="coin" x="3.27273" y="11.6364">
<polygon points="0,0 -0.272727,-8.18182 1.54545,-9.90909 7.36364,-9.90909 9.72727,-7.72727 9.72727,0.545455 7.63636,2.27273 2,2.27273"/>
Expand Down
16 changes: 15 additions & 1 deletion example/orthogonal/orthogonal.tmx
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="16" tileheight="16" infinite="0" backgroundcolor="#2df6f9" nextlayerid="5" nextobjectid="4">
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="16" tileheight="16" infinite="0" backgroundcolor="#2df6f9" nextlayerid="5" nextobjectid="5">
<tileset firstgid="1" name="fantasy" tilewidth="16" tileheight="16" tilecount="132" columns="12">
<image source="tilemap_packed.png" width="192" height="176"/>
<tile id="105" type="Collectable"/>
<tile id="119" type="Collectable">
<properties>
<property name="item" value="arrow"/>
</properties>
<objectgroup draworder="index" id="2">
<object id="1" x="0" y="0" width="16" height="16"/>
</objectgroup>
</tile>
</tileset>
<tileset firstgid="133" source="external-fantasy.tsx"/>
<layer id="1" name="Ground" width="10" height="10">
Expand Down Expand Up @@ -61,5 +70,10 @@
<object id="3" name="Player" type="player-start" x="38.9091" y="40">
<point/>
</object>
<object id="4" gid="120" x="128.134" y="111.847" width="16" height="16">
<properties>
<property name="otherprop" value="something else"/>
</properties>
</object>
</objectgroup>
</map>
2 changes: 2 additions & 0 deletions example/orthogonal/orthogonal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ game.input.pointers.primary.on('wheel', (wheelEvent) => {
game.start(loader).then(() => {
tiledMap.addToScene(game.currentScene);
currentPointer = game.currentScene.camera.pos;

(window as any).tiledMap = tiledMap;
});
2 changes: 1 addition & 1 deletion src/resource/object-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class ObjectLayer implements Layer {
const opacity = this.tiledObjectLayer.opacity;
const offset = vec(this.tiledObjectLayer.offsetx ?? 0, this.tiledObjectLayer.offsety ?? 0);

const objects = parseObjects(this.tiledObjectLayer, this.resource.templates, this.resource.textQuality);
const objects = parseObjects(this.tiledObjectLayer, this.resource);

for (let object of objects) {
let worldPos = vec((object.x ?? 0) + offset.x, (object.y ?? 0) + offset.y);
Expand Down
60 changes: 53 additions & 7 deletions src/resource/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { TiledObject, TiledObjectGroup, TiledText } from "../parser/tiled-parser
import { Properties, mapProps } from "./properties";
import { Template } from "./template";
import { filenameFromPath } from "./path-util";
import { Tileset } from "./tileset";
import { TiledResource } from "./tiled-resource";

export interface PluginObjectProps {
tiledObject: TiledObject;
Expand Down Expand Up @@ -38,6 +40,32 @@ export class TemplateObject extends PluginObject {
this.source = tiledObject.template
this.tiledTemplate = tiledObject;
this.template = template;

// Inherited from template object
if (template.object) {
this.name = this.name || template.object.name;
this.class = this.class || template.object.class;
for (const [key, value] of template.object.properties.entries()) {
if (!this.properties.has(key)) {
this.properties.set(key, value);
}
}
}

// Inherited from tileset
if (template.tileset && template.object.tiledObject.gid) {
const tile = template.tileset.getTileByGid(template.object.tiledObject.gid);
if (tile) {
this.class = this.class || tile.class;
for (const [key, value] of tile.properties.entries()) {
if (!this.properties.has(key)) {
this.properties.set(key, value);
}
}
}
}


}
}
export class InsertedTile extends PluginObject {
Expand Down Expand Up @@ -146,7 +174,7 @@ export class Polyline extends PluginObject {
}

export type ObjectTypes = Polygon | Polyline | Rectangle | Ellipse | Text | Point | InsertedTile | PluginObject;
export function parseObject(object: TiledObject, templates: Template[], textQuality = 4,): PluginObject {
export function parseObject(object: TiledObject, resource?: TiledResource): PluginObject {
let newObject: PluginObject;
if (object.point) {
// Template objects don't have an id for some reason
Expand All @@ -166,14 +194,32 @@ export function parseObject(object: TiledObject, templates: Template[], textQual
} else if (object.polyline) {
newObject = new Polyline(object, object.polyline);
} else if(object.text) {
newObject = new Text(object, object.text, object.width ?? 0, textQuality);
newObject = new Text(object, object.text, object.width ?? 0, resource?.textQuality ?? 4);
} else if (object.gid) {
newObject = new InsertedTile(object, object.gid, object.width ?? 0, object.height ?? 0);
} else if (object.template) {

// Check for inherited class names & properties from tileset
const tileset = resource?.getTilesetForTileGid(object.gid);
let className = object.type;
if (tileset) {
const tile = tileset?.getTileByGid(object.gid);
className = className || tile?.class;
if (tile?.properties) {
for (const [key, value] of tile.properties.entries()) {
if (!newObject.properties.has(key)) {
newObject.properties.set(key, value);
}
}
}
}
newObject.class = className;
} else if (object.template && resource) {
// FIXME This is problematic if you have files with the same name but different paths
const template = templates.find(t => filenameFromPath(t.templatePath) === filenameFromPath(object.template!));
const template = resource.templates.find(t => filenameFromPath(t.templatePath) === filenameFromPath(object.template!));
if (template) {
newObject = new TemplateObject(object, template);

// TODO check for inherited class/name/props
} else {
// This is truly an error situation
throw new Error(`Template object id ${object.id} with name ${object.name} is missing loaded template file, there should be one loaded from ${object.template}! Is your tiled map or template corrupted?`);
Expand All @@ -187,14 +233,14 @@ export function parseObject(object: TiledObject, templates: Template[], textQual
newObject = new Rectangle(object, 20, 20, Vector.Half);
}
}
mapProps(newObject, object.properties);
return newObject;
}

export function parseObjects(tiledObjectGroup: TiledObjectGroup, templates: Template[], textQuality: number) {
export function parseObjects(tiledObjectGroup: TiledObjectGroup, resource?: TiledResource) {
const objects: PluginObject[] = [];
for (const object of tiledObjectGroup.objects) {
let newObject: PluginObject = parseObject(object, templates, textQuality);
mapProps(newObject, object.properties);
let newObject: PluginObject = parseObject(object, resource);
objects.push(newObject);
}
return objects;
Expand Down
2 changes: 1 addition & 1 deletion src/resource/template-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class TemplateResource implements Loadable<Template> {
}
}
const tiledTemplate = template;
const object = parseObject(template.object, []);
const object = parseObject(template.object);
let tileset: Tileset | undefined = undefined;
if (template.tileset) {
// Template tilesets are not included in the TiledResource list because their gids can collide with map tilesets
Expand Down
4 changes: 1 addition & 3 deletions src/resource/tileset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ export class Tile implements Properties {
mapProps(this, tiledTile.properties);

if (tiledTile.objectgroup && tiledTile.objectgroup.objects) {
// templates are not possibel at the moment insed a tile so []
// text isn't possible at the moment inside a tile so -1
this.objects = parseObjects(tiledTile.objectgroup, [], -1);
this.objects = parseObjects(tiledTile.objectgroup);
}

if (tiledTile.animation) {
Expand Down

0 comments on commit 6491901

Please sign in to comment.