view pytouhou/resource/anmwrapper.py @ 316:f0be7ea62330

Fix a bug with ECL instruction 96, and fix overall ECL handling. The issue with instruction 96 was about death callbacks, being executed on the caller of instruction 96 instead of the dying enemies. This was introduced by changeset 5930b33a0370. Additionnaly, ECL processes are now an attribute of the Enemy, and death/timeout conditions are checked right after the ECL frame, even if the ECL script has already ended, just like in the original game.
author Thibaut Girka <thib@sitedethib.com>
date Thu, 29 Mar 2012 21:18:35 +0200
parents 2100276c289d
children 40d5f3083ebc
line wrap: on
line source

# -*- encoding: utf-8 -*-
##
## Copyright (C) 2012 Thibaut Girka <thib@sitedethib.com>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 3 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##

from itertools import repeat


class AnmWrapper(object):
    def __init__(self, anm_files, offsets=None):
        """Wrapper for scripts and sprites described in “anm_files”.

        The optional “offsets” argument specifies a list of offsets to be added
        to script and sprite numbers of each file described in “anm_files”.

        That is, if anm_files[0] and anm_files[1] each have only one sprite,
        numbered 0 in both cases, and offsets=(0, 1), the first file's sprite
        will be numbered 0 and the second file's will be numbered 1.
        """
        self.scripts = {}
        self.sprites = {}

        if not offsets:
            offsets = repeat(0) # “offsets” defaults to zeroes

        for anm, offset in zip(anm_files, offsets):
            for script_id, script in anm.scripts.iteritems():
                self.scripts[script_id + offset] = (anm, script) #TODO: check
            for sprite_id, sprite in anm.sprites.iteritems():
                self.sprites[sprite_id + offset] = (anm, sprite)


    def get_sprite(self, sprite_index):
        return self.sprites[sprite_index]


    def get_script(self, script_index):
        return self.scripts[script_index]