2D_Engine_Casio_Tool/assets/maps/map_converter.py

72 lines
2.2 KiB
Python

import fxconv
from fxconv import 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)
TRANSFORM_COMPONENT = 0
SPRITE_COMPONENT = 1
ANIMATION_SYSTEM = 2
entities = fxconv.Structure()
for entity in map["Entities"]:
e = fxconv.Structure()
e += u16(entity["ID"])
e += u32(len(entity["Components"]))
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)
e += ref(c)
entities += e
o = fxconv.ObjectData()
o += string(params["name"])
o += u32(map["MapWidth"]) + u32(map["MapHeight"])
o += bytes(map["BackgroundLayer1"])
o += bytes(map["BackgroundLayer2"])
o += bytes(map["BackgroundLayer3"])
o += bytes(map["Foreground"])
o += u32(len(map["Entities"]))
o += ref(entities)
fxconv.elf(o, output, params["name"], **target)