Add named animations

This commit is contained in:
2023-12-16 16:01:39 +01:00
parent 5b6c49b2f1
commit dfe137bb93
5 changed files with 359 additions and 15 deletions

View File

@@ -115,9 +115,6 @@ class EnumWriter:
def to_enum(self, name):
return f"{self.prefix.upper()}_{name.upper()}"
def to_anim_enum(self, name):
return f"ANIM_{name.upper()}"
def output_enum(self):
self.writer.enum_list(self.enum_type, self.enums)
@@ -288,8 +285,146 @@ class EnumWriter:
writer.output("default:\n")
writer.indent()
writer.output("BZ_ASSERT(0);\n")
writer.output("return (AnimationFrame) {0, 0};\n")
writer.output("return (AnimationFrame) {-1, -1.0f};\n")
writer.unindent()
writer.block_end()
writer.block_end()
writer.empty_line()
class AnimationWriter(EnumWriter):
def __init__(self, writer: ExtractFileWriter, class_prefix, anim_prefix, tiles):
super().__init__(writer, class_prefix, tiles)
self.writer = writer
self.anim_prefix = anim_prefix
self.anim_type = f"{anim_prefix.capitalize()}Type"
self.anim_map = defaultdict(lambda: defaultdict(list))
self.animations = []
self.all_tiles = tiles
animations = []
for tile in self.all_tiles:
if 'animation' not in tile:
continue
if 'properties' not in tile:
continue
if 'type' not in tile:
continue
enum = self.to_enum(tile['type'])
props = tile['properties']
anim_type = [prop['value'] for prop in props if prop['name'] == 'animation'][0]
anim_type = self.to_anim_enum(anim_type)
animations.append(anim_type)
self.anim_map[enum][anim_type] = tile['animation']
animations.append(self.to_anim_enum("count"))
animations.append(self.to_anim_enum("none"))
anim_seen = set()
anim_add = anim_seen.add
self.animations = [x for x in animations if not (x in anim_seen or anim_add(x))]
def to_anim_enum(self, name):
return f"{self.anim_prefix.upper()}_{name.upper()}"
def output_anim_enum(self):
writer = self.writer
writer.enum_list(self.anim_type, self.animations)
pass
def output_has_anim(self, func_name):
writer = self.writer
writer.output(f"static bool {func_name}({self.enum_type} entity, {self.anim_type} type) ")
writer.block_start()
writer.output("switch (entity) ")
writer.block_start()
for entity, anims in self.anim_map.items():
writer.output(f"case {entity}:\n")
writer.indent()
writer.output("switch (type) ")
writer.block_start()
for anim in anims:
writer.output(f"case {anim}:\n")
writer.indent()
writer.output("return true;\n")
writer.unindent()
writer.output("default:\n")
writer.indent()
writer.output("return false;\n")
writer.unindent()
writer.block_end()
writer.unindent()
writer.output("default:\n")
writer.indent()
writer.output("return false;\n")
writer.unindent()
writer.block_end()
writer.block_end()
writer.empty_line()
def output_anim_sequence(self, func_name):
writer = self.writer
writer.output(f"static AnimationSequence {func_name}({self.enum_type} entity, {self.anim_type} type) ")
writer.block_start()
writer.output("switch (entity) ")
writer.block_start()
for entity, animation_types in self.anim_map.items():
writer.output(f"case {entity}:\n")
writer.indent()
writer.output("switch (type) ")
writer.block_start()
for anim_type in animation_types:
anim = self.anim_map[entity][anim_type]
num_frames = len(anim)
anim_id = anim[0]['tileid']
ret = f"(AnimationSequence) {{.startFrame = {anim_id}, .frameCount = {num_frames}}}"
writer.output(f"case {anim_type}: return {ret};\n")
writer.output("default: break;\n")
writer.block_end()
writer.unindent()
writer.output("default: break;\n")
writer.block_end()
writer.output("BZ_ASSERT(0);\n")
writer.output("return (AnimationSequence) {0, 0};\n")
writer.block_end()
writer.empty_line()
def output_anim_frame(self, func_name):
writer = self.writer
writer.output(
f"static AnimationFrame {func_name}({self.enum_type} entity, {self.anim_type} type, i32 frameIdx) ")
writer.block_start()
writer.output("switch (entity) ")
writer.block_start()
for entity, animation_types in self.anim_map.items():
writer.output(f"case {entity}:\n")
writer.indent()
writer.output("switch (type) ")
writer.block_start()
for anim_type in animation_types:
anim = self.anim_map[entity][anim_type]
frames = [str(x['tileid']) for x in anim]
durations = [str(x['duration']) for x in anim]
anim_frames = [f"{{{frame}, {duration}}}" for frame, duration in zip(frames, durations)]
ret = f"((AnimationFrame []) {{{', '.join(anim_frames)}}}) [frameIdx]"
writer.output(f"case {anim_type}: return {ret};\n")
writer.output("default: break;\n")
writer.block_end()
writer.unindent()
writer.output("default: break;\n")
writer.block_end()
writer.output("BZ_ASSERT(0);\n")
writer.output("return (AnimationFrame) {-1, -1.0f};\n")
writer.block_end()
writer.empty_line()