Skip to content

Support specified z index #24

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

Merged
merged 4 commits into from
Jan 22, 2021
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ if Input.is_action_just_pressed("reload_scene"):
```
Don't forget to add key/mouse/controller mapping for the "reload_scene" action ;)

### Tile layer z index

Can set the z-index for the tilelayer by adding a custom property `z_index`, this will set the z-index of the tile layer in the scene file.

### Tileset objects for collisions & navigation

To setup a collision shape for a tile, first edit the tileset in Tiled:
Expand Down
14 changes: 9 additions & 5 deletions export_to_godot_tilemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class GodotTilemapExporter {

/**
* Adds a new subresource to the genrated file
*
*
* @param {string} type the type of subresource
* @param {object} contentProperties key:value map of properties
* @returns {int} the created sub resource id
Expand Down Expand Up @@ -100,7 +100,7 @@ class GodotTilemapExporter {
if (!ld.isEmpty) {
const tileMapName = idx === 0 ? layer.name || "TileMap " + i : ld.tileset.name || "TileMap " + i + "_" + idx;
this.mapLayerToTileset(layer.name, ld.tilesetID);
this.tileMapsString += this.getTileMapTemplate(tileMapName, ld.tilesetID, ld.poolIntArrayString, ld.parent, layer.map.tileWidth, layer.map.tileHeight, layer.property("groups"));
this.tileMapsString += this.getTileMapTemplate(tileMapName, ld.tilesetID, ld.poolIntArrayString, layer, ld.parent);
}
}
} else if (layer.isObjectLayer) {
Expand Down Expand Up @@ -446,8 +446,11 @@ ${this.tileMapsString}
* Template for a tilemap node
* @returns {string}
*/
getTileMapTemplate(tileMapName, tilesetID, poolIntArrayString, parent = ".", tileWidth = 16, tileHeight = 16, groups = undefined) {
groups = splitCommaSeparated(groups);
getTileMapTemplate(tileMapName, tilesetID, poolIntArrayString, layer, parent = ".") {
const tileWidth = layer.map.tileWidth === undefined ? 16 : layer.map.tileWidth;
const tileHeight = layer.map.tileHeight === undefined ? 16 : layer.map.tileHeight;
const groups = splitCommaSeparated(layer.property("groups"));
const zIndex = parseInt(layer.properties()['z_index'], 10);
return stringifyNode({
name: tileMapName,
type: "TileMap",
Expand All @@ -458,7 +461,8 @@ ${this.tileMapsString}
cell_size: `Vector2( ${tileWidth}, ${tileHeight} )`,
cell_custom_transform: `Transform2D( 16, 0, 0, 16, 0, 0 )`,
format: "1",
tile_data: `PoolIntArray( ${poolIntArrayString} )`
tile_data: `PoolIntArray( ${poolIntArrayString} )`,
z_index: typeof zIndex === 'number' && !isNaN(zIndex) ? zIndex : undefined
});
}

Expand Down
18 changes: 10 additions & 8 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function splitCommaSeparated(str) {

/**
* Removes any undefined value with its key from an object
* @param {object} obj
* @param {object} obj
*/
function removeUndefined(obj) {
Object.keys(obj).forEach(key => obj[key] === undefined && delete obj[key])
Expand All @@ -70,14 +70,14 @@ function removeUndefined(obj) {
/**
* Translates key values defining a godot scene node to the expected TSCN format output
* Passed keys must be strings. Values can be arrays (e.g. for groups)
*
*
* @param {object} nodeProperties pair key/values for the "node" properties
* @param {object} contentProperties pair key/values for the content properties
* @return {string} TSCN scene node like so :
* ```
* [node key="value"]
* content_key = AnyValue
* ```
* ```
*/
function stringifyNode(nodeProperties, contentProperties = {}) {
// remove undefined values from objects
Expand All @@ -91,19 +91,21 @@ function stringifyNode(nodeProperties, contentProperties = {}) {
}
str += ']\n';
for (const [key, value] of Object.entries(contentProperties)) {
str += this.stringifyKeyValue(key, value, false, true) + '\n';
if (value !== undefined) {
str += this.stringifyKeyValue(key, value, false, true) + '\n';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added this to have a cleaner set of options above for passing the z_index, but don't know if theres legit cases to include undefined in the scene file.

}
}

return str;
}

/**
* Processes a key/value pair for a TSCN node
*
* @param {string} key
* @param {string|array} value
*
* @param {string} key
* @param {string|array} value
* @param {bool} quote
* @param {bool} spaces
* @param {bool} spaces
*/
function stringifyKeyValue(key, value, quote, spaces) {
// flatten arrays
Expand Down