+""" May it be a light for you in dark places, when all other lights go out.
+"""
+
+import pymunk
+import pymunk.pygame_util
+import pygame.draw
+
+
+class BaseLight(object):
+ """ Common light functionality. """
+
+ def __init__(self, colour, position):
+ self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
+ self.colour = colour
+ self.position = position
+
+ def add(self, space):
+ if self.body.space is not None:
+ space.remove(self.body, *self.body.shapes)
+ shapes = self.determine_ray_polys(space)
+ space.add(self.body, *shapes)
+
+ def determine_ray_polys(self, space):
+ raise NotImplementedError(
+ "Lights should implement .determine_ray_polys.")
+
+ @classmethod
+ def load(cls, config):
+ kw = config.copy()
+ light_type = kw.pop("type")
+ [light_class] = [
+ c for c in cls.__subclasses__()
+ if c.__name__.lower() == light_type]
+ return light_class(**kw)
+
+
+class SpotLight(BaseLight):
+ def __init__(
+ self, colour="white", position=None, direction=90.0, spread=45.0):
+ super(SpotLight, self).__init__(colour, position)
+ self.direction = direction
+ self.spread = spread
+
+ def determine_ray_polys(self, space):
+ x, y = self.position
+ return [pymunk.Poly(self.body, [
+ self.position, [x + 50, y], [x, y + 50]])]
+
+ def render(self, surface):
+ for shape in self.body.shapes:
+ pygame_poly = [
+ pymunk.pygame_util.to_pygame(v, surface) for v in
+ shape.get_vertices()]
+ pygame.draw.polygon(surface, (255, 255, 255), pygame_poly)
+
+
+class Lamp(BaseLight):
+ def __init__(self, colour="white", position=None, radius=100.0):
+ super(Lamp, self).__init__(colour, position)
+ self.radius = radius