Hook up lights and refactor obstacle loading.
[tabakrolletjie.git] / tabakrolletjie / obstacles.py
1 """ Obstacles for light and space mould. """
2
3 import pymunk
4 import pymunk.pygame_util
5 import pygame.draw
6
7
8 class BaseObstacle(object):
9     def __init__(self):
10         self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
11         self.shapes = []
12
13     def add(self, space):
14         if self.body.space is not None:
15             space.remove(self.body, *self.body.shapes)
16         space.add(self.body, *self.shapes)
17
18     def render(self, surface):
19         raise NotImplementedError("Obstacles should implement .render().")
20
21     @classmethod
22     def load(cls, config):
23         kw = config.copy()
24         obstacle_type = kw.pop("type")
25         [obstacle_class] = [
26             c for c in cls.__subclasses__()
27             if c.__name__.lower() == obstacle_type]
28         return obstacle_class(**kw)
29
30
31 class Wall(BaseObstacle):
32
33     def __init__(self, vertices):
34         super(Wall, self).__init__()
35         self.shapes.append(pymunk.Poly(self.body, vertices))
36
37     def render(self, surface):
38         for shape in self.shapes:
39             pygame_poly = [
40                 pymunk.pygame_util.to_pygame(v, surface) for v in
41                 shape.get_vertices()]
42             pygame.draw.polygon(surface, (0, 0, 0), pygame_poly)