80 lines
2.6 KiB
Python
80 lines
2.6 KiB
Python
import fxconv
|
|
from fxconv import u16, u32, ref, chars, string
|
|
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)
|
|
|
|
TRANSFORM_COMPONENT = 0
|
|
SPRITE_COMPONENT = 1
|
|
ANIMATION_SYSTEM = 2
|
|
|
|
background_layer1 = b''.join(u16(tile) for tile in map["BackgroundLayer1"])
|
|
background_layer2 = b''.join(u16(tile) for tile in map["BackgroundLayer2"])
|
|
background_layer3 = b''.join(u16(tile) for tile in map["BackgroundLayer3"])
|
|
foreground = b''.join(u16(tile) for tile in map["Foreground"])
|
|
|
|
entities = fxconv.Structure()
|
|
|
|
for entity in map["Entities"]:
|
|
e = fxconv.Structure()
|
|
e += u16(entity["ID"])
|
|
e += u32(len(entity["Components"]))
|
|
|
|
components = fxconv.Structure()
|
|
|
|
for component in entity["Components"]:
|
|
c = fxconv.Structure()
|
|
c_data = fxconv.Structure()
|
|
|
|
match component["Type"]:
|
|
case "TransformComponent":
|
|
c += u32(TRANSFORM_COMPONENT)
|
|
c_data += u32(component["x"])
|
|
c_data += u32(component["y"])
|
|
c_data += u32(component["w"])
|
|
c_data += u32(component["h"])
|
|
c_data += u32(component["Speed"])
|
|
|
|
case "SpriteComponent":
|
|
c += u32(SPRITE_COMPONENT)
|
|
c_data += string(component["TextureName"])
|
|
|
|
case "AnimationSystem":
|
|
c += u32(SPRITE_COMPONENT)
|
|
c_data += u32(component["NbFrames"])
|
|
c_data += u32(component["ActualFrame"])
|
|
c_data += u32(component["FrameDelayMs"])
|
|
c_data += u32(component["Play"])
|
|
c_data += u32(component["Loop"])
|
|
c_data += u32(component["Reverse"])
|
|
|
|
case _:
|
|
raise fxconv.FxconvError(f"unknown component type {component['Type']}")
|
|
|
|
c += ref(c_data)
|
|
components += c
|
|
|
|
e += ref(components)
|
|
entities += e
|
|
|
|
o = fxconv.ObjectData()
|
|
o += string(params["name"])
|
|
o += u32(map["MapWidth"]) + u32(map["MapHeight"])
|
|
o += ref(background_layer1)
|
|
o += ref(background_layer2)
|
|
o += ref(background_layer3)
|
|
o += ref(foreground)
|
|
o += u32(len(map["Entities"]))
|
|
o += ref(entities)
|
|
|
|
fxconv.elf(o, output, params["name"], **target)
|