Add direction property to ray manager.
authorSimon Cross <hodgestar@gmail.com>
Sat, 10 Sep 2016 10:28:12 +0000 (12:28 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 10 Sep 2016 10:28:12 +0000 (12:28 +0200)
tabakrolletjie/lights.py
tabakrolletjie/rays.py

index 8353c280ed76af9a40998fba750db5c79bba45a8..ed18fdf9cfef629002310652fc0cd609c9969b53 100644 (file)
@@ -291,5 +291,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()
index 65fbc3d628b19de13a237124c89f73484ab5e5a5..7c8b5beab712c0fe756afbb7645afc92fc3b563c 100644 (file)
@@ -84,6 +84,7 @@ class RayPolyManager(object):
         self._position = pymunk.Vec2d(position)  # light's position
         self._ray_filter = ray_filter  # light filter
         self._rays = []  # list of RayPolys
+        self._direction = None  # normal vector for direction
         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)
@@ -141,21 +142,29 @@ class RayPolyManager(object):
             self._max_radius = radius_limits[1]
 
     def rotatable(self):
-        return self._start is not None
+        return self._direction is not None
 
-    def rotate_degrees(self, degrees):
-        self._start.rotate_degrees(degrees)
-        self._end.rotate_degrees(degrees)
+    @property
+    def direction(self):
+        return self._direction.angle_degrees
+
+    @direction.setter
+    def direction(self, degrees):
+        spread = self._direction.get_angle_between(self._start)
+        self._direction.angle_degrees = degrees
+        self._start = self._direction.rotated(spread)
+        self._end = self._direction.rotated(-spread)
         self._poly_cache = None
 
     def _set_angle_limits(self, direction, spread):
         if direction is None or spread is None:
+            self._direction = None
             self._start = None
             self._end = None
         else:
-            n = pymunk.Vec2d(1, 0)
-            self._start = n.rotated_degrees(-spread/2.)
-            self._end = n.rotated_degrees(spread/2.)
+            self._direction = pymunk.Vec2d(1, 0)
+            self._start = self._direction.rotated_degrees(-spread/2.)
+            self._end = self._direction.rotated_degrees(spread/2.)
         self._poly_cache = None
 
     def polys(self):