2D_Engine_Casio_Tool/assets/converters/map_converter.py

80 lines
2.6 KiB
Python

import fxconv
from fxconv import u8, u16, u32, ref, 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)
with open(input.replace(".json", "_entities.json"), "r") as fp:
map_entities = 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 e in map_entities["Entities"]:
entities += u16(e["ID"])
entities += u32(len(e["Components"]))
components = fxconv.Structure()
for c in e["Components"]:
component_data = fxconv.Structure()
match c["Type"]:
case "TransformComponent":
components += u32(TRANSFORM_COMPONENT)
component_data += u32(c["x"])
component_data += u32(c["y"])
component_data += u32(c["w"])
component_data += u32(c["h"])
component_data += u32(c["Speed"])
case "SpriteComponent":
components += u32(SPRITE_COMPONENT)
component_data += string(c["TextureName"])
case "AnimationSystem":
components += u32(ANIMATION_SYSTEM)
component_data += u32(c["NbFrames"])
component_data += u32(c["ActualFrame"])
component_data += u32(c["FrameDelayMs"])
component_data += u8(c["Play"])
component_data += u8(c["Loop"])
component_data += u8(c["Reverse"])
case _:
raise fxconv.FxconvError(f"unknown component type {c['Type']}")
components += ref(component_data)
entities += ref(components)
entities += bytes(2)
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["Entities"]))
o += ref(entities)
fxconv.elf(o, output, params["name"], **target)