Add circular fitting to lights.
authorSimon Cross <hodgestar@gmail.com>
Tue, 6 Sep 2016 19:44:18 +0000 (21:44 +0200)
committerSimon Cross <hodgestar@gmail.com>
Tue, 6 Sep 2016 19:44:18 +0000 (21:44 +0200)
tabakrolletjie/lights.py

index 7620c480915c1e8b5bc2226ba1ec64028a7631f9..c5c72af0df43cb289f4528ea94fabb65c41b52ee 100644 (file)
@@ -5,13 +5,20 @@ import pymunk
 import pymunk.pygame_util
 import pygame.draw
 
-from .constants import SCREEN_SIZE, LIGHT_CATEGORY
+from .constants import (
+    SCREEN_SIZE, LIGHT_CATEGORY, FITTINGS_CATEGORY)
 from .utils import debug_timer
 
 LIGHT_FILTER = pymunk.ShapeFilter(
-    mask=pymunk.ShapeFilter.ALL_MASKS ^ LIGHT_CATEGORY,
+    mask=pymunk.ShapeFilter.ALL_MASKS ^ (
+        LIGHT_CATEGORY | FITTINGS_CATEGORY),
     categories=LIGHT_CATEGORY)
 
+FITTINGS_FILTER = pymunk.ShapeFilter(
+    mask=pymunk.ShapeFilter.ALL_MASKS ^ (
+        LIGHT_CATEGORY | FITTINGS_CATEGORY),
+    categories=FITTINGS_CATEGORY)
+
 
 def screen_rays(pos):
     """ An iterable that returns ordered rays from pos to the edge of the
@@ -70,8 +77,9 @@ class BaseLight(object):
     def __init__(self, colour, position):
         self.on = True
         self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
+        self.fitting = pymunk.Circle(self.body, 5.0)
         self.colour = colour
-        self.position = position
+        self.position = pymunk.Vec2d(position)
 
     @classmethod
     def load(cls, config):
@@ -89,7 +97,8 @@ class BaseLight(object):
             calculate_ray_polys(space, self.body, self.position))
         for shape in shapes:
             shape.filter = LIGHT_FILTER
-        space.add(self.body, *shapes)
+        self.fitting.filter = FITTINGS_FILTER
+        space.add(self.body, self.fitting, *shapes)
 
     def shapes_for_ray_polys(self, ray_polys):
         return ray_polys
@@ -103,6 +112,8 @@ class BaseLight(object):
         subsurface = surface.copy()
         light_colour = self.COLOURS[self.colour]
         for shape in self.body.shapes:
+            if shape is self.fitting:
+                continue
             pygame_poly = [
                 pymunk.pygame_util.to_pygame(v, surface) for v in
                 shape.get_vertices()]
@@ -114,9 +125,11 @@ class BaseLight(object):
         surface.blit(subsurface, (0, 0), None)
 
     def render_fittings(self, surface):
+        centre = self.position + self.fitting.offset
         pygame.draw.circle(
             surface, (255, 255, 0),
-            pymunk.pygame_util.to_pygame(self.position, surface), 5)
+            pymunk.pygame_util.to_pygame(centre, surface),
+            int(self.fitting.radius))
 
 
 class SpotLight(BaseLight):