Add multi-colour lamp.
authorSimon Cross <hodgestar@gmail.com>
Fri, 9 Sep 2016 22:17:43 +0000 (00:17 +0200)
committerSimon Cross <hodgestar@gmail.com>
Fri, 9 Sep 2016 22:17:43 +0000 (00:17 +0200)
data/stations/station-alpha.json
tabakrolletjie/lights.py

index 40adfeaf61ac0950c2f7ab26321b5e9f3f721036..84b391019cf9358633fe5496184fefbe637cadb2 100644 (file)
       "colour": "yellow",
       "position": [500, 500],
       "intensity": 0.5
+    },
+    {
+      "type": "multicolourlamp",
+      "colours": ["red", "yellow", "green"],
+      "position": [300, 300],
+      "intensity": 0.5
     }
   ]
 }
index 4c54a49f32f9143cc6367e79b94d4bc4651b6fc0..6f39b70876b475e1fe34c3ad3982838f822d6493 100644 (file)
@@ -130,7 +130,7 @@ class BaseLight(object):
         self.fitting = pymunk.Circle(
             self.body, self.FITTING_RADIUS, self.ray_manager.position)
         self.fitting.filter = FITTINGS_FILTER
-        self._image = None
+        self._fitting_image = None
 
     @property
     def position(self):
@@ -208,27 +208,55 @@ class BaseLight(object):
         dt.lap("blitted surface")
         dt.end()
 
-    def get_image(self):
-        if self._image is None:
+    def fitting_image(self):
+        if self._fitting_image is None:
             fitting_colour = self.COLOURS[self.colour]
-            self._image = loader.load_image(
+            self._fitting_image = loader.load_image(
                 "48", self.FITTING_IMG,
                 transform=Multiply(colour=fitting_colour))
-        return self._image
+        return self._fitting_image
+
+    def invalidate_fitting_image(self):
+        self._fitting_image = None
 
     def render_fitting(self, surface):
         rx, ry = self.ray_manager.pygame_position(surface)
-        surface.blit(self.get_image(), (rx - 24, ry - 24), None, 0)
+        surface.blit(self.fitting_image(), (rx - 24, ry - 24), None, 0)
 
     def tick(self):
         pass
 
 
 class Lamp(BaseLight):
+
     FITTING_IMG = "lamp.png"
 
 
+class MultiColourLamp(BaseLight):
+
+    FITTING_IMG = "lamp.png"
+    DEFAULT_COLOURS = sorted(BaseLight.COLOURS.keys())
+
+    def __init__(self, **kw):
+        self.colour_cycle = kw.pop("colours", None)
+        self.colour_pos = 0
+        kw["colour"] = self.colour_cycle[0]
+        super(MultiColourLamp, self).__init__(**kw)
+
+    def toggle(self):
+        self.colour_pos += 1
+        if self.colour_pos >= len(self.colour_cycle):
+            self.colour = self.colour_cycle[0]
+            self.colour_pos = -1
+            self.on = False
+        else:
+            self.colour = self.colour_cycle[self.colour_pos]
+            self.on = True
+        self.invalidate_fitting_image()
+
+
 class SpotLight(BaseLight):
+
     FITTING_IMG = "spotlight.png"
 
     def __init__(self, **kw):