from dataclasses import dataclass from parse.map_header import tokenize_line from parse import number def tokenize_lines(lines): for line in lines: if 'overworld_sprite ' in line: yield tokenize_line(line) @dataclass class Spritesheet: name: str sprite_count: int def translate_tile_count(tile_count): # this is the number of non-animation sprites in the spritesheet # the animation sprite is always at tile_index * 2 # all animated sprites have 2 animation frames return int(tile_count) // 4 def flatten(tokens): for ts in tokens: if ts[0] != 'overworld_sprite': continue _, (name, tile_count) = ts sprite_count = translate_tile_count(tile_count) yield Spritesheet( name=name, sprite_count=sprite_count, ) def parse(prefix): path = prefix / 'data/sprites/sprites.asm' with open(path) as f: tokens = tokenize_lines(f.read().split('\n')) return list(flatten(tokens))