changeset 323:2fcdb8966957

Display lives and bombs.
author Thibaut Girka <thib@sitedethib.com>
date Wed, 13 Jun 2012 22:53:57 +0200
parents 4e8192aadcaa
children c412df42aa15
files pytouhou/game/text.py pytouhou/games/eosd.py pytouhou/ui/gamerunner.py
diffstat 3 files changed, 100 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/game/text.py
+++ b/pytouhou/game/text.py
@@ -26,51 +26,20 @@ class Glyph(object):
         self.x, self.y = pos
 
 
-class Text(object):
-    def __init__(self, pos, ascii_wrapper=None, front_wrapper=None, text=''):
-        self.sprite = Sprite()
+class Widget(object):
+    def __init__(self, pos, back_wrapper=None):
+        self.sprite = None
         self.removed = False
         self.changed = True
 
-        self.text = ''
-        self.glyphes = []
-
-        self.front_wrapper = front_wrapper
-        self.ascii_wrapper = ascii_wrapper
-
-        if front_wrapper:
-            self.anmrunner = ANMRunner(front_wrapper, 22, self.sprite)
+        # Set up the backround sprite
+        self.back_wrapper = back_wrapper
+        if back_wrapper:
+            self.sprite = Sprite()
+            self.anmrunner = ANMRunner(back_wrapper, 22, self.sprite)
             self.anmrunner.run_frame()
 
         self.x, self.y = pos
-        self.set_text(text)
-
-
-    def objects(self):
-        return self.glyphes + ([self] if self.front_wrapper else [])
-
-
-    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:
@@ -78,3 +47,81 @@ class Text(object):
                 self.anmrunner = None
             self.changed = False
 
+
+
+class GlyphCollection(Widget):
+    def __init__(self, pos, anm_wrapper, back_wrapper=None, ref_script=0, xspacing=14):
+        Widget.__init__(self, pos, back_wrapper)
+
+        self.ref_sprite = Sprite()
+        self.anm_wrapper = anm_wrapper
+        self.glyphes = []
+        self.xspacing = xspacing
+
+        # Set up ref sprite
+        anm_runner = ANMRunner(anm_wrapper, ref_script, self.ref_sprite)
+        anm_runner.run_frame()
+        self.ref_sprite.corner_relative_placement = True #TODO: perhaps not right
+
+
+    def objects(self):
+        return self.glyphes
+
+
+    def set_length(self, length):
+        current_length = len(self.glyphes)
+        if length > current_length:
+            self.glyphes.extend(Glyph(copy(self.ref_sprite),
+                                      (self.x + self.xspacing * i, self.y))
+                                        for i in range(current_length, length))
+        elif length < current_length:
+            self.glyphes[:] = self.glyphes[:length]
+
+
+    def set_sprites(self, sprite_indexes):
+        self.set_length(len(sprite_indexes))
+        for glyph, idx in zip(self.glyphes, sprite_indexes):
+            glyph.sprite.anm, glyph.sprite.texcoords = self.anm_wrapper.get_sprite(idx)
+            glyph.sprite.changed = True
+
+
+
+class Text(GlyphCollection):
+    def __init__(self, pos, ascii_wrapper, back_wrapper=None, text=''):
+        GlyphCollection.__init__(self, pos, ascii_wrapper, back_wrapper)
+        self.text = ''
+
+        self.set_text(text)
+
+
+    def set_text(self, text):
+        if text == self.text:
+            return
+
+        self.set_sprites([ord(c) - 21 for c in text])
+        self.text = text
+        self.changed = True
+
+
+
+class Counter(GlyphCollection):
+    def __init__(self, pos, anm_wrapper, back_wrapper=None, script=0, xspacing=16, value=0):
+        GlyphCollection.__init__(self, pos,
+                                 anm_wrapper, back_wrapper=back_wrapper,
+                                 ref_script=script, xspacing=xspacing)
+
+        self.value = value
+        self.set_value(value)
+
+
+    def set_value(self, value):
+        if value < 0:
+            value = 0
+        if value == self.value:
+            return
+
+        self.set_length(value)
+        self.value = value
+        self.changed = True
+
+
--- a/pytouhou/games/eosd.py
+++ b/pytouhou/games/eosd.py
@@ -21,7 +21,7 @@ from pytouhou.game.itemtype import ItemT
 from pytouhou.game.player import Player
 from pytouhou.game.orb import Orb
 from pytouhou.game.effect import Effect
-from pytouhou.game.text import Text
+from pytouhou.game.text import Text, Counter
 
 from os.path import join
 from pytouhou.ui.music import InfiniteWaveSource
@@ -139,8 +139,8 @@ class EoSDInterface(object):
         self.labels = {
             'highscore': Text((500, 58), ascii_wrapper, front, text='0'),
             'score': Text((500, 82), ascii_wrapper, front, text='0'),
-            'player': Text((500, 122), ascii_wrapper, front, text='TODO'),
-            'bombs': Text((500, 146), ascii_wrapper, front, text='TODO'),
+            'player': Counter((500, 122), front, front, script=16, value=0),
+            'bombs': Counter((500, 146), front, front, script=17, value=0),
             'power': Text((500, 186), ascii_wrapper, front, text='0'),
             'graze': Text((500, 206), ascii_wrapper, front, text='0'),
             'points': Text((500, 226), ascii_wrapper, front, text='0'),
@@ -161,6 +161,8 @@ class EoSDInterface(object):
         self.labels['power'].set_text('%d' % player_state.power)
         self.labels['graze'].set_text('%d' % player_state.graze)
         self.labels['points'].set_text('%d' % player_state.points)
+        self.labels['player'].set_value(player_state.lives)
+        self.labels['bombs'].set_value(player_state.bombs)
 
 
 
--- a/pytouhou/ui/gamerunner.py
+++ b/pytouhou/ui/gamerunner.py
@@ -178,16 +178,17 @@ class GameRunner(pyglet.window.Window, G
         glViewport(0, 0, self.width, self.height)
 
         items = [item for item in interface.items if item.anmrunner and item.anmrunner.running]
-        labels = interface.labels
+        labels = interface.labels.values()
+
         if items:
-            # Force rendering of labels
+            # Redraw all the interface
             self.render_elements(items)
-            self.render_elements(chain(*(label.objects()
-                                            for label in labels.itervalues())))
         else:
-            self.render_elements(chain(*(label.objects()
-                                            for label in labels.itervalues()
-                                                if label.changed)))
-        for label in interface.labels.itervalues():
+            # Redraw only changed labels
+            labels = [label for label in labels if label.changed]
+
+        self.render_elements(labels)
+        self.render_elements(chain(*(label.objects() for label in labels)))
+        for label in labels:
             label.changed = False