1 """ Obstacles for light and space mould. """
3 import pygame.locals as pgl
6 import pymunk.pygame_util
9 from pygame.math import Vector2
11 from .constants import (SCREEN_SIZE, OBSTACLE_CATEGORY)
12 from .loader import loader
14 OBSTACLE_FILTER = pymunk.ShapeFilter(categories=OBSTACLE_CATEGORY)
17 class ObstacleManager(object):
18 """ Manages a set of obstacles. """
20 def __init__(self, space, gamestate):
23 BaseObstacle.load(cfg) for cfg in gamestate.station["obstacles"]]
24 for obs in self._obstacles:
27 def render(self, surface):
28 for obs in self._obstacles:
32 class BaseObstacle(object):
34 self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
38 if self.body.space is not None:
39 space.remove(self.body, *self.body.shapes)
40 for shape in self.shapes:
41 shape.filter = OBSTACLE_FILTER
42 space.add(self.body, *self.shapes)
44 def render(self, surface):
45 raise NotImplementedError("Obstacles should implement .render().")
48 def load(cls, config):
50 obstacle_type = kw.pop("type")
52 c for c in cls.__subclasses__()
53 if c.__name__.lower() == obstacle_type]
54 return obstacle_class(**kw)
57 class Wall(BaseObstacle):
59 def __init__(self, vertices):
60 super(Wall, self).__init__()
61 self.shapes.append(pymunk.Poly(self.body, vertices))
65 if self._image is None:
66 self._image = pygame.surface.Surface(SCREEN_SIZE).convert_alpha()
67 self._image.fill((0, 0, 0, 0))
69 for shape in self.shapes:
71 pymunk.pygame_util.to_pygame(v, self._image) for v in
73 pygame.draw.polygon(self._image, (255, 255, 255), pygame_poly)
75 wall_texture = loader.load_image(
76 "textures", "stone.png").convert_alpha()
77 self._image.blit(wall_texture, (0, 0), None, pgl.BLEND_RGBA_MULT)
81 def render(self, surface):
82 surface.blit(self.get_image(), (0, 0), None, 0)
85 class Shrub(BaseObstacle):
87 def __init__(self, shrublets):
88 super(Shrub, self).__init__()
89 for (x, y, r) in shrublets:
90 vec = Vector2(0, int(r))
93 vec.rotate(angle) + (x, y)
94 for angle in range(0, 360, 360/STEPS)]
95 vertices = [(v.x, v.y) for v in vertices]
97 self.shapes.append(pymunk.Poly(self.body, vertices))
98 self.shrublets = shrublets
102 if self._image is None:
103 self._image = pygame.surface.Surface(SCREEN_SIZE).convert_alpha()
104 self._image.fill((0, 0, 0, 0))
106 for (x, y, r) in self.shrublets:
107 centre = pymunk.pygame_util.to_pygame((x, y), self._image)
108 pygame.draw.circle(self._image, (255, 255, 255), centre, r)
110 shrub_texture = loader.load_image(
111 "textures", "shrub.png").convert_alpha()
112 self._image.blit(shrub_texture, (0, 0), None, pgl.BLEND_RGBA_MULT)
116 def render(self, surface):
117 surface.blit(self.get_image(), (0, 0), None, 0)