1 """ Obstacles for light and space mould. """
4 import pymunk.pygame_util
7 from .constants import OBSTACLE_CATEGORY
9 OBSTACLE_FILTER = pymunk.ShapeFilter(categories=OBSTACLE_CATEGORY)
12 class ObstacleManager(object):
13 """ Manages a set of obstacles. """
15 def __init__(self, space, gamestate):
18 BaseObstacle.load(cfg) for cfg in gamestate.station["obstacles"]]
19 for obs in self._obstacles:
22 def render(self, surface):
23 for obs in self._obstacles:
27 class BaseObstacle(object):
29 self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
33 if self.body.space is not None:
34 space.remove(self.body, *self.body.shapes)
35 for shape in self.shapes:
36 shape.filter = OBSTACLE_FILTER
37 space.add(self.body, *self.shapes)
39 def render(self, surface):
40 raise NotImplementedError("Obstacles should implement .render().")
43 def load(cls, config):
45 obstacle_type = kw.pop("type")
47 c for c in cls.__subclasses__()
48 if c.__name__.lower() == obstacle_type]
49 return obstacle_class(**kw)
52 class Wall(BaseObstacle):
54 def __init__(self, vertices):
55 super(Wall, self).__init__()
56 self.shapes.append(pymunk.Poly(self.body, vertices))
58 def render(self, surface):
59 for shape in self.shapes:
61 pymunk.pygame_util.to_pygame(v, surface) for v in
63 pygame.draw.polygon(surface, (0, 0, 0), pygame_poly)