Add serialize methods to lights and rays
[tabakrolletjie.git] / tabakrolletjie / rays.py
index b4142a3e6694f8ecaeb4f67264a4bb270ca7a025..8b532784a46a80e97f354cb9686b6161fe2bbe8b 100644 (file)
@@ -12,7 +12,7 @@ from .constants import SCREEN_SIZE
 from .utils import debug_timer
 
 
 from .utils import debug_timer
 
 
-def screen_rays(pos):
+def screen_rays():
     """ An iterable that returns ordered rays from pos to the edge of the
         screen, starting with the edge point (0, 0) and continuing clockwise
         in pymunk coordinates.
     """ An iterable that returns ordered rays from pos to the edge of the
         screen, starting with the edge point (0, 0) and continuing clockwise
         in pymunk coordinates.
@@ -40,7 +40,7 @@ def calculate_ray_polys(space, position, light_filter):
     vertices = [position]
     start, end = None, None
     ray_polys = []
     vertices = [position]
     start, end = None, None
     ray_polys = []
-    for ray in screen_rays(position):
+    for ray in screen_rays():
         info = space.segment_query_first(position, ray, 1, light_filter)
         point = ray if info is None else info.point
         vertices.append(point)
         info = space.segment_query_first(position, ray, 1, light_filter)
         point = ray if info is None else info.point
         vertices.append(point)
@@ -88,6 +88,8 @@ class RayPolyManager(object):
         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(direction, spread)
         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(direction, spread)
+        if direction:
+            self.direction = direction  # Update direction
         self._max_radius = None  # maximum radius in pixels
         self._min_radius = None  # minimum radius in pixels
         self._set_radius_limits(radius_limits)
         self._max_radius = None  # maximum radius in pixels
         self._min_radius = None  # minimum radius in pixels
         self._set_radius_limits(radius_limits)
@@ -127,8 +129,22 @@ class RayPolyManager(object):
     def min_radius(self, value):
         self._min_radius = value or 0.0
 
     def min_radius(self, value):
         self._min_radius = value or 0.0
 
+    def serialize(self):
+        """ Return the required information from the ray_manager """
+        if self._direction is None:
+            direction = None
+            spread = None
+        else:
+            direction = self._direction.angle_degrees
+            spread = math.degrees(self.spread)
+        return {
+            "radius_limits": (self._min_radius, self._max_radius),
+            "direction": direction,
+            "spread": spread,
+        }
+
     def reaches(self, position):
     def reaches(self, position):
-        distance = self.position.get_distance(self.position)
+        distance = self.position.get_distance(position)
         return (self._min_radius <= distance <= self._max_radius)
 
     def _set_radius_limits(self, radius_limits):
         return (self._min_radius <= distance <= self._max_radius)
 
     def _set_radius_limits(self, radius_limits):
@@ -146,6 +162,8 @@ class RayPolyManager(object):
 
     @property
     def direction(self):
 
     @property
     def direction(self):
+        if self._direction is None:
+            return 0
         return self._direction.angle_degrees
 
     @direction.setter
         return self._direction.angle_degrees
 
     @direction.setter