Document windows 7 permissions workaround
[tabakrolletjie.git] / tabakrolletjie / obstacles.py
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..071ca9f935c1d4cd87fec3de82e13bf6ca0f82e9 100644 (file)
@@ -0,0 +1,117 @@
+""" Obstacles for light and space mould. """
+
+import pygame.locals as pgl
+
+import pymunk
+import pymunk.pygame_util
+import pygame.draw
+import pygame.surface
+from pygame.math import Vector2
+
+from .constants import (SCREEN_SIZE, OBSTACLE_CATEGORY)
+from .loader import loader
+
+OBSTACLE_FILTER = pymunk.ShapeFilter(categories=OBSTACLE_CATEGORY)
+
+
+class ObstacleManager(object):
+    """ Manages a set of obstacles. """
+
+    def __init__(self, space, gamestate):
+        self._space = space
+        self._obstacles = [
+            BaseObstacle.load(cfg) for cfg in gamestate.station["obstacles"]]
+        for obs in self._obstacles:
+            obs.add(self._space)
+
+    def render(self, surface):
+        for obs in self._obstacles:
+            obs.render(surface)
+
+
+class BaseObstacle(object):
+    def __init__(self):
+        self.body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
+        self.shapes = []
+
+    def add(self, space):
+        if self.body.space is not None:
+            space.remove(self.body, *self.body.shapes)
+        for shape in self.shapes:
+            shape.filter = OBSTACLE_FILTER
+        space.add(self.body, *self.shapes)
+
+    def render(self, surface):
+        raise NotImplementedError("Obstacles should implement .render().")
+
+    @classmethod
+    def load(cls, config):
+        kw = config.copy()
+        obstacle_type = kw.pop("type")
+        [obstacle_class] = [
+            c for c in cls.__subclasses__()
+            if c.__name__.lower() == obstacle_type]
+        return obstacle_class(**kw)
+
+
+class Wall(BaseObstacle):
+
+    def __init__(self, vertices):
+        super(Wall, self).__init__()
+        self.shapes.append(pymunk.Poly(self.body, vertices))
+        self._image = None
+
+    def get_image(self):
+        if self._image is None:
+            self._image = pygame.surface.Surface(SCREEN_SIZE).convert_alpha()
+            self._image.fill((0, 0, 0, 0))
+
+            for shape in self.shapes:
+                pygame_poly = [
+                    pymunk.pygame_util.to_pygame(v, self._image) for v in
+                    shape.get_vertices()]
+                pygame.draw.polygon(self._image, (255, 255, 255), pygame_poly)
+
+            wall_texture = loader.load_image(
+                "textures", "stone.png").convert_alpha()
+            self._image.blit(wall_texture, (0, 0), None, pgl.BLEND_RGBA_MULT)
+
+        return self._image
+
+    def render(self, surface):
+        surface.blit(self.get_image(), (0, 0), None, 0)
+
+
+class Shrub(BaseObstacle):
+
+    def __init__(self, shrublets):
+        super(Shrub, self).__init__()
+        for (x, y, r) in shrublets:
+            vec = Vector2(0, int(r))
+            STEPS = 18
+            vertices = [
+                vec.rotate(angle) + (x, y)
+                for angle in range(0, 360, 360/STEPS)]
+            vertices = [(v.x, v.y) for v in vertices]
+
+            self.shapes.append(pymunk.Poly(self.body, vertices))
+        self.shrublets = shrublets
+        self._image = None
+
+    def get_image(self):
+        if self._image is None:
+            self._image = pygame.surface.Surface(SCREEN_SIZE).convert_alpha()
+            self._image.fill((0, 0, 0, 0))
+
+            for (x, y, r) in self.shrublets:
+                centre = pymunk.pygame_util.to_pygame((x, y), self._image)
+                pygame.draw.circle(self._image, (255, 255, 255), centre, r)
+
+            shrub_texture = loader.load_image(
+                "textures", "shrub.png").convert_alpha()
+            self._image.blit(shrub_texture, (0, 0), None, pgl.BLEND_RGBA_MULT)
+
+        return self._image
+
+    def render(self, surface):
+        surface.blit(self.get_image(), (0, 0), None, 0)