1 """ May it be a light for you in dark places, when all other lights go out.
5 import pymunk.pygame_util
9 class BaseLight(object):
10 """ Common light functionality. """
12 def __init__(self, colour, position):
13 self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
15 self.position = position
18 if self.body.space is not None:
19 space.remove(self.body, *self.body.shapes)
20 shapes = self.determine_ray_polys(space)
21 space.add(self.body, *shapes)
23 def determine_ray_polys(self, space):
24 raise NotImplementedError(
25 "Lights should implement .determine_ray_polys.")
28 def load(cls, config):
30 light_type = kw.pop("type")
32 c for c in cls.__subclasses__()
33 if c.__name__.lower() == light_type]
34 return light_class(**kw)
37 class SpotLight(BaseLight):
39 self, colour="white", position=None, direction=90.0, spread=45.0):
40 super(SpotLight, self).__init__(colour, position)
41 self.direction = direction
44 def determine_ray_polys(self, space):
46 return [pymunk.Poly(self.body, [
47 self.position, [x + 50, y], [x, y + 50]])]
49 def render(self, surface):
50 for shape in self.body.shapes:
52 pymunk.pygame_util.to_pygame(v, surface) for v in
54 pygame.draw.polygon(surface, (255, 255, 255), pygame_poly)
57 class Lamp(BaseLight):
58 def __init__(self, colour="white", position=None, radius=100.0):
59 super(Lamp, self).__init__(colour, position)