Factor out ray manager.
[tabakrolletjie.git] / tabakrolletjie / lights.py
index 097abd235f2e4b8d53e97f1ecdcc5ed08f2531fe..39b1815fc782b02972ef9d91233d44d7b9b739d8 100644 (file)
@@ -8,7 +8,7 @@ import pygame.draw
 import pygame.locals as pgl
 
 from .constants import LIGHT_CATEGORY, FITTINGS_CATEGORY
-from .rays import calculate_ray_polys
+from .rays import RayPolyManager
 
 LIGHT_FILTER = pymunk.ShapeFilter(
     mask=pymunk.ShapeFilter.ALL_MASKS ^ (
@@ -102,7 +102,9 @@ class BaseLight(object):
         self.angle_limits = angle_limits
         self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
         self.fitting = pymunk.Circle(self.body, 10.0, self.position)
+        self.fitting.filter = FITTINGS_FILTER
         self.body.light = self
+        self.ray_manager = RayPolyManager(self.body, LIGHT_FILTER)
 
     @classmethod
     def load(cls, config):
@@ -116,12 +118,9 @@ class BaseLight(object):
     def add(self, space):
         if self.body.space is not None:
             space.remove(self.body, *self.body.shapes)
-        shapes = self.shapes_for_ray_polys(
-            calculate_ray_polys(space, self.body, self.position, LIGHT_FILTER))
-        for shape in shapes:
-            shape.filter = LIGHT_FILTER
-        self.fitting.filter = FITTINGS_FILTER
-        space.add(self.body, self.fitting, *shapes)
+        self.ray_manager.generate_rays(space, self.position)
+        ray_shapes = self.ray_manager.polys()
+        space.add(self.body, self.fitting, *ray_shapes)
 
     def shapes_for_ray_polys(self, ray_polys):
         return ray_polys