changeset 387:e1f5dcd4b83e

Display something at the start of a stage.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 20 Oct 2012 20:48:40 +0200
parents dc6ed7b1c6de
children ac2891afb0bb
files pytouhou/game/text.py pytouhou/games/eosd.py pytouhou/ui/gamerunner.py
diffstat 3 files changed, 54 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/game/text.py
+++ b/pytouhou/game/text.py
@@ -16,6 +16,7 @@ from copy import copy
 
 from pytouhou.game.sprite import Sprite
 from pytouhou.vm.anmrunner import ANMRunner
+from pytouhou.utils.interpolator import Interpolator
 
 
 class Glyph(object):
@@ -32,6 +33,7 @@ class Widget(object):
         self.removed = False
         self.changed = True
         self.anmrunner = None
+        self.frame = 0
 
         # Set up the backround sprite
         self.back_wrapper = back_wrapper
@@ -43,6 +45,7 @@ class Widget(object):
         self.x, self.y = pos
 
     def update(self):
+        self.frame += 1
         if self.changed:
             if self.anmrunner and not self.anmrunner.run_frame():
                 self.anmrunner = None
@@ -118,19 +121,50 @@ class Text(GlyphCollection):
             glyph.sprite.color = colors[color]
 
 
-    def timeout_update(self):
+    def move_timeout_update(self):
         GlyphCollection.update(self)
-        if self.timeout % 2:
+        if self.frame % 2:
             for glyph in self.glyphes:
                 glyph.y -= 1
-        self.timeout -= 1
-        if self.timeout == 0:
+        if self.frame == self.timeout:
             self.removed = True
 
 
-    def set_timeout(self, timeout):
-        self.timeout = timeout
-        self.update = self.timeout_update
+    def fadeout_timeout_update(self):
+        GlyphCollection.update(self)
+        if self.frame >= self.start:
+            if self.frame == self.start:
+                self.fade(self.effect, 255, lambda x: x)
+            elif self.frame == self.timeout - self.effect:
+                self.fade(self.effect, 0, lambda x: x)
+            if self.fade_interpolator:
+                self.fade_interpolator.update(self.frame)
+                self.alpha = int(self.fade_interpolator.values[0])
+                for glyph in self.glyphes:
+                    glyph.sprite.alpha = self.alpha
+                    glyph.sprite.changed = True
+        if self.frame == self.timeout:
+            self.removed = True
+
+
+    def fade(self, duration, alpha, formula):
+        self.fade_interpolator = Interpolator((self.alpha,), self.frame,
+                                              (alpha,), self.frame + duration,
+                                              formula)
+
+
+    def set_timeout(self, timeout, effect=None, start=0):
+        if effect == None: #XXX
+            self.update = self.move_timeout_update
+            self.timeout = timeout + start
+        else:
+            self.alpha = 0
+            for glyph in self.glyphes:
+                glyph.sprite.alpha = 0
+            self.update = self.fadeout_timeout_update
+            self.effect = effect
+            self.start = start
+            self.timeout = timeout + start
 
 
 
--- a/pytouhou/games/eosd.py
+++ b/pytouhou/games/eosd.py
@@ -91,6 +91,7 @@ class EoSDGame(Game):
         characters = resource_loader.get_eosd_characters()
         players = [EoSDPlayer(state, self, resource_loader, characters[state.character]) for state in player_states]
 
+        self.stage = stage #XXX
         interface = EoSDInterface(self, resource_loader)
 
         Game.__init__(self, resource_loader, players, stage, rank, difficulty,
@@ -119,6 +120,11 @@ class EoSDInterface(object):
         for item in self.items:
             item.sprite.allow_dest_offset = True #XXX
 
+        self.level_start = [Text((176, 200), ascii_wrapper, text='STAGE %d' % game.stage)] #TODO: find the exact location.
+        self.level_start[0].set_timeout(240, effect=60, start=120)
+        self.level_start[0].set_color('yellow')
+        #TODO: use the system text for the stage name, and the song name.
+
         self.labels = {
             'highscore': Text((500, 58), ascii_wrapper, front, text='0'),
             'score': Text((500, 82), ascii_wrapper, front, text='0'),
@@ -160,6 +166,11 @@ class EoSDInterface(object):
         for elem in self.items:
             elem.update()
 
+        for elem in self.level_start:
+            elem.update()
+            if elem.removed: #XXX
+                self.level_start = []
+
         player_state = self.game.players[0].state
 
         self.highscore = max(self.highscore, player_state.effective_score)
--- a/pytouhou/ui/gamerunner.py
+++ b/pytouhou/ui/gamerunner.py
@@ -218,6 +218,8 @@ class GameRunner(pyglet.window.Window, G
             # Redraw only changed labels
             labels = [label for label in labels if label.changed]
 
+        self.render_elements(interface.level_start)
+
         if self.game.boss:
             self.render_elements(interface.boss_items)