Add shape-based light query
authorNeil <neil@dip.sun.ac.za>
Wed, 7 Sep 2016 13:30:30 +0000 (15:30 +0200)
committerNeil <neil@dip.sun.ac.za>
Wed, 7 Sep 2016 13:30:30 +0000 (15:30 +0200)
tabakrolletjie/lights.py

index b22bc35a61e2644766a19656cd4bce6a13131c1f..2c691fefa9b508b53a07872eadff5702fff1e8f9 100644 (file)
@@ -20,6 +20,9 @@ FITTINGS_FILTER = pymunk.ShapeFilter(
         LIGHT_CATEGORY | FITTINGS_CATEGORY),
     categories=FITTINGS_CATEGORY)
 
+# Just match lights, nothing else
+LIT_BY_FILTER = pymunk.ShapeFilter(mask=LIGHT_CATEGORY)
+
 
 def screen_rays(pos):
     """ An iterable that returns ordered rays from pos to the edge of the
@@ -98,6 +101,16 @@ class LightManager(object):
         lights = [p.shape.body.light for p in point_info_list]
         return [light for light in lights if light.on]
 
+    def light_query(self, shape):
+        """Query the lights by shape"""
+        old_filter = shape.filter
+        # We need to restrict matches to only the lights
+        shape.filter = LIT_BY_FILTER
+        shape_info_list = self._space.shape_query(shape)
+        shape.filter = old_filter
+        lights = [p.shape.body.light for p in shape_info_list]
+        return [light for light in lights if light.on]
+
     def render_light(self, surface):
         for light in self._lights:
             light.render_light(surface)