Merge branch 'master' of ctpug.org.za:tabakrolletjie
[tabakrolletjie.git] / tabakrolletjie / lights.py
index 8353c280ed76af9a40998fba750db5c79bba45a8..1eaba7c8e92ee3804026383fdc3ece6c06dfe203 100644 (file)
@@ -1,6 +1,8 @@
 """ May it be a light for you in dark places, when all other lights go out.
 """
 
+import math
+
 import pymunk
 import pymunk.pygame_util
 import pygame.display
@@ -12,7 +14,7 @@ from .constants import LIGHT_CATEGORY, FITTINGS_CATEGORY, COLOURS
 from .rays import RayPolyManager
 from .utils import DetailedTimer
 from .loader import loader
-from .transforms import Multiply
+from .transforms import Multiply, MultiplyImage
 
 LIGHT_FILTER = pymunk.ShapeFilter(
     mask=pymunk.ShapeFilter.ALL_MASKS ^ (
@@ -83,6 +85,9 @@ class LightManager(object):
             if light.on and light.ray_manager.reaches(shape.body.position)
         ]
 
+    def total_power_usage(self):
+        return sum(light.power_usage() for light in self._lights)
+
     def render_light(self, surface):
         for light in self._lights:
             light.render_light(surface)
@@ -108,9 +113,11 @@ class BaseLight(object):
     _surface_cache = {}
 
     def __init__(
-            self, colour, position, intensity=1.0, radius_limits=None,
+            self, colours, position, intensity=1.0, radius_limits=None,
             direction=None, spread=None):
-        self.colour = colour
+        self.colour_cycle = colours
+        self.colour_pos = 0
+        self.colour = colours[0]
         self.on = True
         self.intensity = intensity
         self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
@@ -122,6 +129,7 @@ class BaseLight(object):
             self.body, self.FITTING_RADIUS, self.ray_manager.position)
         self.fitting.filter = FITTINGS_FILTER
         self._fitting_image = None
+        self._colour_mult_image = None
 
     @property
     def position(self):
@@ -143,9 +151,6 @@ class BaseLight(object):
         self.ray_manager.set_space(space)
         self.ray_manager.update_shapes()
 
-    def toggle(self):
-        self.on = not self.on
-
     def _cached_surface(self, name, surface):
         surf = self._surface_cache.get(name)
         if surf is None:
@@ -201,10 +206,30 @@ class BaseLight(object):
 
     def fitting_image(self):
         if self._fitting_image is None:
-            fitting_colour = COLOURS[self.colour]
-            self._fitting_image = loader.load_image(
+            fitting_colours = [COLOURS[c] for c in self.colour_cycle]
+            ncolour = len(fitting_colours)
+            if ncolour > 3:
+                print "Multicoloured light should not have more than 3 colours"
+                ncolour = 3
+
+            if ncolour == 1:
+                self._fitting_image = loader.load_image(
                 "48", self.FITTING_IMG,
-                transform=Multiply(colour=fitting_colour))
+                transform=Multiply(colour=fitting_colours[0]))
+            else:
+                if self._colour_mult_image is None:
+                    self._colour_mult_image = pygame.surface.Surface((48, 48))
+
+                    for i in range(ncolour):
+                        sector = loader.load_image(
+                            "48", "light_mask_%d_%d.png" % (ncolour, i + 1),
+                            transform=Multiply(colour=fitting_colours[i]))
+                        self._colour_mult_image.blit(sector, (0,0), None, 0)
+                    
+                self._fitting_image = loader.load_image(
+                    "48", self.FITTING_IMG,
+                    transform=MultiplyImage(image=self._colour_mult_image))
+
         return self._fitting_image
 
     def invalidate_fitting_image(self):
@@ -214,25 +239,15 @@ class BaseLight(object):
         rx, ry = self.ray_manager.pygame_position(surface)
         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):
+    def power_usage(self):
+        if not self.on:
+            return 0.0
+        area = math.pi * (self.ray_manager.max_radius ** 2)  # radius
+        area = area * (self.ray_manager.spread / (2 * math.pi))  # spread
+        return 5 * area * self.intensity
 
-    FITTING_IMG = "lamp.png"
-    DEFAULT_COLOURS = sorted(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 base_damage(self):
+        return 5 * self.intensity
 
     def toggle(self):
         self.colour_pos += 1
@@ -245,6 +260,14 @@ class MultiColourLamp(BaseLight):
             self.on = True
         self.invalidate_fitting_image()
 
+    def tick(self):
+        pass
+
+
+class Lamp(BaseLight):
+
+    FITTING_IMG = "lamp.png"
+
 
 class PulsatingLamp(BaseLight):
 
@@ -291,5 +314,5 @@ class SpotLight(BaseLight):
 
     def tick(self):
         if self.angular_velocity:
-            self.ray_manager.rotate_degrees(self.angular_velocity)
+            self.ray_manager.direction -= self.angular_velocity
             self.ray_manager.update_shapes()