view pytouhou/game/text.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 f3099ebf4f61
children 4e8192aadcaa
line wrap: on
line source

# -*- encoding: utf-8 -*-
##
## Copyright (C) 2011 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
##
## 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 copy import copy

from pytouhou.game.sprite import Sprite
from pytouhou.vm.anmrunner import ANMRunner


class Glyph(object):
    def __init__(self, sprite, pos):
        self.sprite = sprite
        self.removed = False

        self.x, self.y = pos


class Text(object):
    def __init__(self, pos, text, front_wrapper, ascii_wrapper):
        self.sprite = Sprite()
        self.anmrunner = ANMRunner(front_wrapper, 22, self.sprite)
        self.anmrunner.run_frame()
        self.removed = False
        self.changed = True

        self.text = ''
        self.glyphes = []

        self.front_wrapper = front_wrapper
        self.ascii_wrapper = ascii_wrapper

        self.x, self.y = pos
        self.set_text(text)


    def objects(self):
        return self.glyphes + [self]


    def set_text(self, text):
        if text == self.text:
            return

        if len(text) > len(self.glyphes):
            ref_sprite = Sprite()
            anm_runner = ANMRunner(self.ascii_wrapper, 0, ref_sprite)
            anm_runner.run_frame()
            ref_sprite.corner_relative_placement = True #TODO: perhaps not right, investigate.
            self.glyphes.extend(Glyph(copy(ref_sprite), (self.x + 14*i, self.y))
                for i in range(len(self.glyphes), len(text)))
        elif len(text) < len(self.glyphes):
            self.glyphes[:] = self.glyphes[:len(text)]

        for glyph, character in zip(self.glyphes, text):
            glyph.sprite.anm, glyph.sprite.texcoords = self.ascii_wrapper.get_sprite(ord(character) - 21)
            glyph.sprite.changed = True

        self.text = text
        self.changed = True


    def update(self):
        if self.changed:
            if self.anmrunner and not self.anmrunner.run_frame():
                self.anmrunner = None
            self.changed = False