Add idle animation
This commit is contained in:
64
scripts/extract_common.py
Normal file
64
scripts/extract_common.py
Normal file
@@ -0,0 +1,64 @@
|
||||
class ExtractFileWriter:
|
||||
indent_width = 4
|
||||
indent_level = 0
|
||||
indention = ""
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name.upper()
|
||||
|
||||
def indent(self):
|
||||
self.indent_level += self.indent_width
|
||||
self.indention = " " * self.indent_level
|
||||
|
||||
def unindent(self):
|
||||
self.indent_level -= self.indent_width
|
||||
self.indention = " " * self.indent_level
|
||||
|
||||
def header_guard_start(self):
|
||||
print(f"#ifndef {self.name}")
|
||||
print(f"#define {self.name}")
|
||||
print()
|
||||
|
||||
def header_guard_stop(self):
|
||||
print(f"#endif // {self.name}")
|
||||
|
||||
def include(self, header):
|
||||
print(f"#include {header}")
|
||||
|
||||
def enum_start(self, name):
|
||||
print(f"{self.indention}typedef enum {name} {{")
|
||||
self.indent()
|
||||
|
||||
def enum_stop(self, name):
|
||||
self.unindent()
|
||||
print(f"}} {name};")
|
||||
print()
|
||||
|
||||
def enum_list(self, name, enums):
|
||||
self.enum_start(name)
|
||||
for enum in enums:
|
||||
print(f"{self.indention}{enum},")
|
||||
self.enum_stop(name)
|
||||
|
||||
def enum_dict(self, name, enums):
|
||||
self.enum_start(name)
|
||||
for enum, v in enums.items():
|
||||
print(f"{self.indention}{enum} = {v},")
|
||||
self.enum_stop(name)
|
||||
|
||||
def output(self, out):
|
||||
print(f"{self.indention}{out}", end="")
|
||||
|
||||
def output_line(self):
|
||||
print()
|
||||
|
||||
def only_output(self, out):
|
||||
print(out, end="")
|
||||
|
||||
def block_start(self):
|
||||
print(f"{{")
|
||||
self.indent()
|
||||
|
||||
def block_end(self):
|
||||
self.unindent()
|
||||
print(f"{self.indention}}}")
|
||||
154
scripts/extract_entities.py
Normal file
154
scripts/extract_entities.py
Normal file
@@ -0,0 +1,154 @@
|
||||
#!/usr/bin/python
|
||||
"""
|
||||
Utility script that extracts entities tileset with animations
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
from collections import defaultdict
|
||||
from extract_common import *
|
||||
|
||||
raw_entities = defaultdict(list)
|
||||
entities = defaultdict(list)
|
||||
|
||||
content = open("../assets/entities.tsj").read()
|
||||
tiles = json.loads(content)["tiles"]
|
||||
|
||||
for tile in tiles:
|
||||
if "type" not in tile:
|
||||
continue
|
||||
raw_entities[tile["type"]].append(tile)
|
||||
|
||||
entity_enum_prefix = "ENTITY_"
|
||||
animation_enum_prefix = "ANIM_"
|
||||
|
||||
entity_enums = {}
|
||||
animations = set()
|
||||
entity_animations = {}
|
||||
|
||||
|
||||
def entity_enum(type):
|
||||
return f"{entity_enum_prefix}{type.upper()}"
|
||||
|
||||
|
||||
def animation_enum(value):
|
||||
value = value.split("_")[0]
|
||||
return f"{animation_enum_prefix}{value.upper()}"
|
||||
|
||||
|
||||
def simple_entity(entity):
|
||||
type = entity["type"]
|
||||
id = entity["id"]
|
||||
entity_enums[entity_enum(type)] = id
|
||||
|
||||
|
||||
def animated_entity(entity):
|
||||
simple_entity(entity[0])
|
||||
entity_name = entity_enum(entity[0]["type"])
|
||||
cur_animations = defaultdict(list)
|
||||
for frame in entity:
|
||||
assert entity_name == entity_enum(frame["type"])
|
||||
for prop in frame["properties"]:
|
||||
if prop["name"] != "animation":
|
||||
continue
|
||||
animation_name = animation_enum(prop["value"])
|
||||
animations.add(animation_name)
|
||||
cur_animations[animation_name].append(frame["id"])
|
||||
break
|
||||
entity_animations[entity_name] = cur_animations
|
||||
|
||||
|
||||
for k, v in raw_entities.items():
|
||||
num_frames = len(v)
|
||||
if num_frames == 1:
|
||||
simple_entity(v[0])
|
||||
else:
|
||||
animated_entity(v)
|
||||
|
||||
|
||||
writer = ExtractFileWriter("entity_types")
|
||||
writer.header_guard_start()
|
||||
|
||||
writer.include("<breeze.h>")
|
||||
|
||||
writer.output_line()
|
||||
|
||||
# ============================
|
||||
|
||||
writer.enum_dict("EntityType", entity_enums)
|
||||
writer.enum_list("AnimationType", animations)
|
||||
|
||||
# ============================
|
||||
|
||||
writer.output("typedef struct AnimationSequence {\n")
|
||||
writer.indent()
|
||||
writer.output("i32 startFrame;\n")
|
||||
writer.output("i32 frameCount;\n")
|
||||
writer.unindent()
|
||||
writer.output("} AnimationSequence;\n")
|
||||
writer.output_line()
|
||||
|
||||
# ============================
|
||||
|
||||
# ============================
|
||||
|
||||
writer.output("static bool entityHasAnimation(EntityType entity, AnimationType anim) ")
|
||||
writer.block_start()
|
||||
|
||||
writer.output("switch (entity) ")
|
||||
writer.block_start()
|
||||
|
||||
for entity, anims in entity_animations.items():
|
||||
writer.output(f"case {entity}: ")
|
||||
writer.block_start()
|
||||
|
||||
writer.output("switch (anim) ")
|
||||
writer.block_start()
|
||||
for anim in anims:
|
||||
writer.output(f"case {anim}:")
|
||||
writer.output_line()
|
||||
writer.indent()
|
||||
writer.output("return true;\n")
|
||||
writer.unindent()
|
||||
writer.block_end()
|
||||
|
||||
writer.output("break;\n")
|
||||
writer.block_end()
|
||||
|
||||
writer.block_end()
|
||||
|
||||
writer.output("return false;\n")
|
||||
writer.block_end()
|
||||
|
||||
writer.output_line()
|
||||
# ============================
|
||||
|
||||
writer.output("static AnimationSequence getEntityAnimation(EntityType entity, AnimationType anim) ")
|
||||
writer.block_start()
|
||||
writer.output("switch (entity) ")
|
||||
writer.block_start()
|
||||
for entity, anims in entity_animations.items():
|
||||
writer.output(f"case {entity}: ")
|
||||
writer.block_start()
|
||||
|
||||
writer.output("switch (anim) ")
|
||||
writer.block_start()
|
||||
for anim in anims:
|
||||
ids = sorted(anims[anim])
|
||||
for id0, id1 in zip(ids, ids[1:]):
|
||||
assert id1 - id0 == 1
|
||||
writer.output(f"case {anim}: return (AnimationSequence) {{{min(ids)}, {len(ids)}}};")
|
||||
writer.output_line()
|
||||
writer.block_end()
|
||||
|
||||
writer.block_end()
|
||||
writer.block_end()
|
||||
writer.output("BZ_ASSERT(0);\n")
|
||||
writer.block_end()
|
||||
writer.output_line()
|
||||
|
||||
# ============================
|
||||
|
||||
writer.header_guard_stop()
|
||||
|
||||
Reference in New Issue
Block a user