1 """ Obstacles for light and space mould. """
3 import pygame.locals as pgl
6 import pymunk.pygame_util
10 from .constants import (SCREEN_SIZE, OBSTACLE_CATEGORY)
11 from .loader import loader
13 OBSTACLE_FILTER = pymunk.ShapeFilter(categories=OBSTACLE_CATEGORY)
16 class ObstacleManager(object):
17 """ Manages a set of obstacles. """
19 def __init__(self, space, gamestate):
22 BaseObstacle.load(cfg) for cfg in gamestate.station["obstacles"]]
23 for obs in self._obstacles:
26 def render(self, surface):
27 for obs in self._obstacles:
31 class BaseObstacle(object):
33 self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
37 if self.body.space is not None:
38 space.remove(self.body, *self.body.shapes)
39 for shape in self.shapes:
40 shape.filter = OBSTACLE_FILTER
41 space.add(self.body, *self.shapes)
43 def render(self, surface):
44 raise NotImplementedError("Obstacles should implement .render().")
47 def load(cls, config):
49 obstacle_type = kw.pop("type")
51 c for c in cls.__subclasses__()
52 if c.__name__.lower() == obstacle_type]
53 return obstacle_class(**kw)
56 class Wall(BaseObstacle):
58 def __init__(self, vertices):
59 super(Wall, self).__init__()
60 self.shapes.append(pymunk.Poly(self.body, vertices))
64 if self._image is None:
65 self._image = pygame.surface.Surface(SCREEN_SIZE).convert_alpha()
66 self._image.fill((0, 0, 0, 0))
68 for shape in self.shapes:
70 pymunk.pygame_util.to_pygame(v, self._image) for v in
72 pygame.draw.polygon(self._image, (255, 255, 255), pygame_poly)
74 wall_texture = loader.load_image(
75 "textures", "stone.png").convert_alpha()
76 self._image.blit(wall_texture, (0, 0), None, pgl.BLEND_RGBA_MULT)
80 def render(self, surface):
81 surface.blit(self.get_image(), (0, 0), None, 0)
84 class Shrub(BaseObstacle):
86 def __init__(self, shrublets):
87 super(Shrub, self).__init__()
88 for (x, y, r) in shrublets:
89 vec = pymunk.Vec2d(0, int(r))
92 vec.rotated_degrees(angle) + (x, y)
93 for angle in range(0, 360, 360/STEPS)]
94 vertices = [(v.x, v.y) for v in vertices]
96 self.shapes.append(pymunk.Poly(self.body, vertices))
97 self.shrublets = shrublets
101 if self._image is None:
102 self._image = pygame.surface.Surface(SCREEN_SIZE).convert_alpha()
103 self._image.fill((0, 0, 0, 0))
105 for (x, y, r) in self.shrublets:
106 centre = pymunk.pygame_util.to_pygame((x, y), self._image)
107 pygame.draw.circle(self._image, (255, 255, 255), centre, r)
109 shrub_texture = loader.load_image(
110 "textures", "shrub.png").convert_alpha()
111 self._image.blit(shrub_texture, (0, 0), None, pgl.BLEND_RGBA_MULT)
115 def render(self, surface):
116 surface.blit(self.get_image(), (0, 0), None, 0)