Move radius and angle limits into ray manager.
[tabakrolletjie.git] / tabakrolletjie / rays.py
index 32ae1a749bbb86c539e8c46e95709eb0292bd0d0..9e5a1bf6d0050534ec79fda916e55c7c01d0a595 100644 (file)
@@ -2,6 +2,8 @@
 
 import math
 
+import pygame.rect
+
 import pymunk
 import pymunk.autogeometry
 import pymunk.pygame_util
@@ -75,19 +77,70 @@ def to_pymunk_radians(deg):
 
 
 class RayPolyManager(object):
-    def __init__(self, body, ray_filter):
+    def __init__(
+            self, body, position, ray_filter, radius_limits, angle_limits):
         self._body = body  # light's body
+        self._position = pymunk.Vec2d(position)  # light's position
         self._ray_filter = ray_filter  # light filter
         self._rays = []  # list of RayPolys
         self._start = None  # normal vector in direction of start angle limit
         self._end = None  # normal vector in direction of end angle limit
+        self._set_angle_limits(angle_limits)
+        self._max_radius = None  # maximum radius in pixels
+        self._min_radius = None  # minimum radius in pixels
+        self._set_radius_limits(radius_limits)
+        self._old_poly_cache = None  # last polys added to the space
         self._poly_cache = None  # list of pymunk.Polys for rays
+        self._space = None  # space the rays form part of
+
+    def set_space(self, space):
+        self._space = space
+        self._rays = calculate_ray_polys(
+            self._space, self._position, self._ray_filter)
+        self._poly_cache = None
+
+    def update_shapes(self):
+        if self._old_poly_cache:
+            self._space.remove(*self._old_poly_cache)
+        new_polys = self._old_poly_cache = self.polys()
+        self._space.add(*new_polys)
+
+    @property
+    def position(self):
+        return self._position
+
+    @property
+    def max_radius(self):
+        return self._max_radius
+
+    @max_radius.setter
+    def max_radius_setter(self, value):
+        self._max_radius = value or 0.0
 
-    def generate_rays(self, space, position):
-        self._rays = calculate_ray_polys(space, position, self._ray_filter)
+    @property
+    def min_radius(self):
+        return self._min_radius
+
+    @min_radius.setter
+    def min_radius_setter(self, value):
+        self._min_radius = value or 0.0
+
+    def _set_radius_limits(self, radius_limits):
+        if radius_limits is None or not radius_limits[0]:
+            self._min_radius = 0
+        else:
+            self._min_radius = radius_limits[0]
+        if radius_limits is None or not radius_limits[1]:
+            self._max_radius = 50.0
+        else:
+            self._max_radius = radius_limits[1]
+
+    def rotate_degrees(self, degrees):
+        self._start.rotate_degrees(degrees)
+        self._end.rotate_degrees(degrees)
         self._poly_cache = None
 
-    def set_angle_limits(self, angle_limits):
+    def _set_angle_limits(self, angle_limits):
         if angle_limits is None:
             self._start = None
             self._end = None
@@ -109,6 +162,17 @@ class RayPolyManager(object):
                     poly_cache.append(poly)
         return self._poly_cache
 
+    def pygame_position(self, surface):
+        return pymunk.pygame_util.to_pygame(self._position, surface)
+
+    def pygame_rect(self, surface):
+        half_width = self.max_radius
+        rect_width = half_width * 2
+        rect_x, rect_y = pymunk.pygame_util.to_pygame(self._position, surface)
+        dest_rect = pygame.rect.Rect(rect_x, rect_y, rect_width, rect_width)
+        dest_rect.move_ip(-half_width, -half_width)
+        return dest_rect
+
     def pygame_polys(self, surface):
         return [
             [pymunk.pygame_util.to_pygame(v, surface)