Factor out flash light into utils.
[naja.git] / naja / utils.py
index a40b8412f0adaba2389533f25445b998a79761b3..7ad4f582c2188a9c6fdcc914fa535eddd70fdc3f 100644 (file)
@@ -1,5 +1,7 @@
 import pygame
 
+from naja.constants import BITS
+
 
 def convert_colour(colour):
     if isinstance(colour, pygame.Color):
@@ -9,3 +11,44 @@ def convert_colour(colour):
     if isinstance(colour, basestring):
         return pygame.Color(colour)
     raise ValueError()
+
+
+def bit_glyphs(bits):
+    bit_names = dict((v, k) for k, v in BITS.items())
+    return '{%s}' % ','.join(bit_names[bit] for bit in reversed(range(8))
+                             if bit in bits)
+
+
+def move_glyph(move_name):
+    return {
+        'CASTLE': u'\u265c',
+        'BISHOP': u'\u265d',
+        'KNIGHT': u'\u265e',
+    }.get(move_name, move_name.lower())
+
+
+def parse_bits(bit_list):
+    # Convert names to numbers if applicable.
+    return frozenset(BITS.get(bit, bit) for bit in bit_list)
+
+
+def warp_to_game_state(game_state):
+    from naja.events import LoadGameEvent, SceneChangeEvent
+    from naja.scenes.game import GameScene
+    LoadGameEvent.post(game_state)
+    SceneChangeEvent.post(GameScene)
+
+
+class Flashlight(object):
+    def __init__(self, rate):
+        self.rate = rate
+        self.ticks = 0
+        self.on = False
+
+    def tick(self):
+        self.ticks += 1
+        if self.ticks >= self.rate:
+            self.on = not self.on
+            self.ticks = 0
+            return True
+        return False