Started work on using tiled
This commit is contained in:
@ -11,4 +11,5 @@ set(
|
||||
|
||||
# Tools
|
||||
add_subdirectory(assetstool)
|
||||
add_subdirectory(copytool)
|
||||
add_subdirectory(copytool)
|
||||
add_subdirectory(maptool)
|
19
tools/maptool/CMakeLists.txt
Normal file
19
tools/maptool/CMakeLists.txt
Normal file
@ -0,0 +1,19 @@
|
||||
# Copyright (c) 2023 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
|
||||
function(tool_map target file)
|
||||
add_custom_target(${target}
|
||||
COMMAND
|
||||
${Python3_EXECUTABLE}
|
||||
${DAWN_TOOLS_DIR}/maptool/maptool.py
|
||||
--input=${DAWN_ASSETS_SOURCE_DIR}/${file}
|
||||
--output=${DAWN_ASSETS_BUILD_DIR}/${target}.map
|
||||
COMMENT "Compiling map ${file}..."
|
||||
USES_TERMINAL
|
||||
DEPENDS ${DAWN_ASSETS}
|
||||
)
|
||||
add_dependencies(dawnassets ${target})
|
||||
endfunction()
|
92
tools/maptool/maptool.py
Executable file
92
tools/maptool/maptool.py
Executable file
@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python
|
||||
# Copyright (c) 2023 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
import os
|
||||
import argparse
|
||||
|
||||
# Args
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Bundles all assets into the internal archive format.'
|
||||
)
|
||||
parser.add_argument('-i', '--input');
|
||||
parser.add_argument('-o', '--output');
|
||||
args = parser.parse_args()
|
||||
|
||||
# Ensure the directory for the output path exists
|
||||
if not os.path.exists(os.path.dirname(args.output)):
|
||||
os.makedirs(os.path.dirname(args.output))
|
||||
|
||||
# Read XML file
|
||||
import xml.etree.ElementTree as ET
|
||||
tree = ET.parse(args.input)
|
||||
root = tree.getroot()
|
||||
|
||||
width = int(root.attrib['width'])
|
||||
height = int(root.attrib['height'])
|
||||
|
||||
# Prep Output
|
||||
output = {
|
||||
"width": width,
|
||||
"height": height,
|
||||
"layers": [
|
||||
],
|
||||
"entities": [
|
||||
],
|
||||
"triggers": [
|
||||
]
|
||||
}
|
||||
|
||||
# for each layer element of root
|
||||
for layer in root.findall('layer'):
|
||||
layerOut = {
|
||||
"tiles": []
|
||||
}
|
||||
# Find data (CSV)
|
||||
data = layer.find('data')
|
||||
data = data.text.replace('\n', '').replace(' ', '').split(',')
|
||||
# for each tile in data
|
||||
for i in range(0, len(data)):
|
||||
# minus 1 because tiled is 1 indexed
|
||||
tileId = int(data[i]) - 1
|
||||
layerOut["tiles"].append(tileId)
|
||||
|
||||
output["layers"].append(layerOut)
|
||||
|
||||
# Find objectgroup elements
|
||||
for objectgroup in root.findall('objectgroup'):
|
||||
# for each object element of objectgroup
|
||||
|
||||
# if layer name attribute containts "ent"
|
||||
isEnt = objectgroup.attrib['name'].lower().find('ent') != -1
|
||||
|
||||
for tiledObject in objectgroup.findall('object'):
|
||||
# /8 because tiled uses pixels for objects but we use tile indexes (8x8px)
|
||||
obj = {
|
||||
"x": int(round(float(tiledObject.attrib['x']) / 8)),
|
||||
"y": int(round(float(tiledObject.attrib['y']) / 8)),
|
||||
}
|
||||
|
||||
if isEnt:
|
||||
if not 'type' in tiledObject.attrib:
|
||||
raise Exception('Entity missing type attribute')
|
||||
entType = tiledObject.attrib['type']
|
||||
if entType == 'player':
|
||||
obj['type'] = 1
|
||||
else:
|
||||
raise Exception('Unknown entity type: ' + entType)
|
||||
|
||||
output["entities"].append(obj)
|
||||
else:
|
||||
# trigger
|
||||
raise Exception('Triggers not yet implemented')
|
||||
|
||||
|
||||
# write to args output as JSON
|
||||
import json
|
||||
with open(args.output, 'w') as f:
|
||||
# pretty print to debug
|
||||
f.write(json.dumps(output, indent=2))
|
||||
# f.write(json.dumps(output))
|
Reference in New Issue
Block a user