54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
import fxconv
|
|
from fxconv import u16, u32, ref
|
|
import json
|
|
|
|
def convert(input, output, params, target):
|
|
if params["custom-type"] == "map":
|
|
convert_map(input, output, params, target)
|
|
return 0
|
|
else:
|
|
return 1
|
|
|
|
def convert_map(input, output, params, target):
|
|
with open(input, "r") as fp:
|
|
map = json.load(fp)
|
|
|
|
w = map["MapWidth"]
|
|
h = map["MapHeight"]
|
|
|
|
background_layer1 = bytes(map["BackgroundLayer1"])
|
|
background_layer2 = bytes(map["BackgroundLayer2"])
|
|
background_layer3 = bytes(map["BackgroundLayer3"])
|
|
foreground = bytes(map["Foreground"])
|
|
|
|
entities = fxconv.Structure()
|
|
|
|
for entity in map["Entities"]:
|
|
e = fxconv.Structure()
|
|
e += u16(entity["ID"])
|
|
e += u32(entity["Components"].size())
|
|
|
|
for component in entity["Components"]:
|
|
c = fxconv.Structure()
|
|
match component["Type"]:
|
|
case "TransformComponent":
|
|
c += u32(component["x"])
|
|
c += u32(component["y"])
|
|
c += u32(component["w"])
|
|
c += u32(component["h"])
|
|
c += u32(component["Speed"])
|
|
case _:
|
|
raise fxconv.FxconvError(f"unknown component type {component["Type"]}")
|
|
e += ref(c)
|
|
|
|
o = fxconv.ObjectData()
|
|
o += u32(w) + u32(h)
|
|
o += ref(background_layer1)
|
|
o += ref(background_layer2)
|
|
o += ref(background_layer3)
|
|
o += ref(foreground)
|
|
o += u32(map["Entities"].size())
|
|
o += ref(entities)
|
|
|
|
fxconv.elf(o, output, params["name"], **target)
|