pokemon/tools/parse/spritesheets.py
Zack Buhman a921a44b66 parse: move tokenize_line/tokenize_lines to a new module
At times I forget what I have already written. I hope that this might help keep
things more organized.
2023-07-31 16:23:55 -07:00

37 lines
1000 B
Python

from dataclasses import dataclass
from functools import partial
from parse import number
from parse.generic import macro_line
tokenize_lines = partial(macro_line.tokenize_lines, prefix='overworld_sprite')
@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))