Add total power and damage.
[tabakrolletjie.git] / tabakrolletjie / lights.py
index 8353c280ed76af9a40998fba750db5c79bba45a8..8b688f71907588608f698084615eb8f4b6c37e8f 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
@@ -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)
@@ -143,9 +150,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:
@@ -214,6 +218,27 @@ class BaseLight(object):
         rx, ry = self.ray_manager.pygame_position(surface)
         surface.blit(self.fitting_image(), (rx - 24, ry - 24), None, 0)
 
+    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
+
+    def base_damage(self):
+        return 5 * self.intensity
+
+    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()
+
     def tick(self):
         pass
 
@@ -226,25 +251,10 @@ class Lamp(BaseLight):
 class MultiColourLamp(BaseLight):
 
     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 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 PulsatingLamp(BaseLight):
 
@@ -291,5 +301,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()