Check correct distance
[tabakrolletjie.git] / tabakrolletjie / lights.py
index b2e081201ae27bf9113146ba4a3106389a4bda44..7b2c26380c9800ded9540fcb281766d87903b679 100644 (file)
@@ -1,12 +1,15 @@
 """ 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
 import pygame.draw
 import pygame.locals as pgl
 import pygame.rect
+import pygame.transform
 
 from .constants import LIGHT_CATEGORY, FITTINGS_CATEGORY, COLOURS
 from .rays import RayPolyManager
@@ -83,6 +86,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)
@@ -209,8 +215,8 @@ class BaseLight(object):
 
             if ncolour == 1:
                 self._fitting_image = loader.load_image(
-                "48", self.FITTING_IMG,
-                transform=Multiply(colour=fitting_colours[0]))
+                    "48", self.FITTING_IMG,
+                    transform=Multiply(colour=fitting_colours[0]))
             else:
                 if self._colour_mult_image is None:
                     self._colour_mult_image = pygame.surface.Surface((48, 48))
@@ -219,8 +225,8 @@ class BaseLight(object):
                         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._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))
@@ -234,6 +240,16 @@ 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 / 6400  # 80x80 unit area
+
+    def base_damage(self):
+        return 5 * self.intensity
+
     def toggle(self):
         self.colour_pos += 1
         if self.colour_pos >= len(self.colour_cycle):
@@ -243,7 +259,6 @@ class BaseLight(object):
         else:
             self.colour = self.colour_cycle[self.colour_pos]
             self.on = True
-        self.invalidate_fitting_image()
 
     def tick(self):
         pass
@@ -297,6 +312,17 @@ class SpotLight(BaseLight):
         self.angular_velocity = kw.pop("angular_velocity", None)
         super(SpotLight, self).__init__(**kw)
 
+    def fitting_image(self):
+        fitting_image = super(SpotLight, self).fitting_image()
+        rot_fitting_image = pygame.transform.rotozoom(fitting_image, self.ray_manager.direction - 90, 1)
+
+        rot_rect = fitting_image.get_rect().copy()
+        rot_rect.center = rot_fitting_image.get_rect().center
+        rot_fitting_image = rot_fitting_image.subsurface(rot_rect).copy()
+        
+        return rot_fitting_image
+        
+
     def tick(self):
         if self.angular_velocity:
             self.ray_manager.direction -= self.angular_velocity