t pushMerge branch 'master' of ctpug.org.za:tabakrolletjie
[tabakrolletjie.git] / tabakrolletjie / lights.py
1 """ May it be a light for you in dark places, when all other lights go out.
2 """
3
4 import pymunk
5
6
7 class BaseLight(object):
8     """ Common light functionality. """
9
10     def __init__(self, colour, position):
11         self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
12         self.colour = colour
13         self.position = position
14
15     def add_to_space(self, space):
16         space.remove(self.body, **self.body.shapes())
17         shapes = self.determine_ray_polys(space)
18         space.add(self.body, **shapes)
19
20     def determine_ray_polys(self, space):
21         raise NotImplementedError(
22             "Lights should implement .determine_ray_polys.")
23
24     @classmethod
25     def load(cls, config):
26         kw = config.copy()
27         light_type = kw.pop("type")
28         [light_class] = [
29             c for c in cls.__subclasses__()
30             if c.__name__.lower() == light_type]
31         return light_class(**kw)
32
33
34 class SpotLight(BaseLight):
35     def __init__(
36             self, colour="white", position=None, direction=90.0, spread=45.0):
37         super(SpotLight, self).__init__(colour, position)
38         self.direction = direction
39         self.spread = spread
40
41
42 class Lamp(BaseLight):
43     def __init__(self, colour="white", position=None, radius=100.0):
44         super(Lamp, self).__init__(colour, position)
45         self.radius = radius