Hook up lights and refactor obstacle loading.
authorSimon Cross <hodgestar@gmail.com>
Sun, 4 Sep 2016 17:28:23 +0000 (19:28 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sun, 4 Sep 2016 17:28:23 +0000 (19:28 +0200)
tabakrolletjie/lights.py
tabakrolletjie/obstacles.py
tabakrolletjie/scenes/night.py

index 92b66e0a8d4b91907fdec632724e00b0ffa9fba8..81740d88fd2003603c3332323900d120f3f5d283 100644 (file)
@@ -2,6 +2,8 @@
 """
 
 import pymunk
+import pymunk.pygame_util
+import pygame.draw
 
 
 class BaseLight(object):
@@ -12,10 +14,11 @@ class BaseLight(object):
         self.colour = colour
         self.position = position
 
-    def add_to_space(self, space):
-        space.remove(self.body, **self.body.shapes())
+    def add(self, space):
+        if self.body.space is not None:
+            space.remove(self.body, *self.body.shapes)
         shapes = self.determine_ray_polys(space)
-        space.add(self.body, **shapes)
+        space.add(self.body, *shapes)
 
     def determine_ray_polys(self, space):
         raise NotImplementedError(
@@ -38,6 +41,18 @@ class SpotLight(BaseLight):
         self.direction = direction
         self.spread = spread
 
+    def determine_ray_polys(self, space):
+        x, y = self.position
+        return [pymunk.Poly(self.body, [
+            self.position, [x + 50, y], [x, y + 50]])]
+
+    def render(self, surface):
+        for shape in self.body.shapes:
+            pygame_poly = [
+                pymunk.pygame_util.to_pygame(v, surface) for v in
+                shape.get_vertices()]
+            pygame.draw.polygon(surface, (255, 255, 255), pygame_poly)
+
 
 class Lamp(BaseLight):
     def __init__(self, colour="white", position=None, radius=100.0):
index 61d4376e29da7080a71dbfcf16a38eb0ab29883a..d187a599294de659c54a70a3e08a27ba5692230f 100644 (file)
@@ -1,16 +1,42 @@
+""" Obstacles for light and space mould. """
+
 import pymunk
 import pymunk.pygame_util
 import pygame.draw
 
 
-class Wall(object):
+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)
+        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, space):
-        body = pymunk.Body(0, 0, pymunk.body.Body.STATIC)
-        self._shape = pymunk.Poly(body, vertices)
-        space.add(self._shape)
+    def __init__(self, vertices):
+        super(Wall, self).__init__()
+        self.shapes.append(pymunk.Poly(self.body, vertices))
 
     def render(self, surface):
-        pygame_poly = [pymunk.pygame_util.to_pygame(v, surface) for v in
-                       self._shape.get_vertices()]
-        pygame.draw.polygon(surface, (0, 0, 0), pygame_poly)
+        for shape in self.shapes:
+            pygame_poly = [
+                pymunk.pygame_util.to_pygame(v, surface) for v in
+                shape.get_vertices()]
+            pygame.draw.polygon(surface, (0, 0, 0), pygame_poly)
index 5d3721bd99e9c9a7cc195acb9f444e2e5736f01e..330530b029f947edc26729e0cf99c3dae4913d78 100644 (file)
@@ -5,27 +5,29 @@ import pygame.locals as pgl
 import pymunk
 
 from .base import BaseScene
-from ..obstacles import Wall
+from ..lights import BaseLight
+from ..obstacles import BaseObstacle
 from ..events import SceneChangeEvent
 
 
 class NightScene(BaseScene):
     def enter(self, gamestate):
-        import pprint
-        pprint.pprint(gamestate.station)
-
         self._space = pymunk.Space()
-
-        self._obstacles = []
-        self._lights = []
-        for obs in gamestate.station['obstacles']:
-            wall = Wall(obs['vertices'], self._space)
-            self._obstacles.append(wall)
+        self._obstacles = [
+            BaseObstacle.load(cfg) for cfg in gamestate.station["obstacles"]]
+        self._lights = [
+            BaseLight.load(cfg) for cfg in gamestate.station["lights"]]
+        for obs in self._obstacles:
+            obs.add(self._space)
+        for light in self._lights:
+            light.add(self._space)
 
     def render(self, surface, gamestate):
         surface.fill((0, 0, 255))
         for obs in self._obstacles:
             obs.render(surface)
+        for light in self._lights:
+            light.render(surface)
 
     def event(self, ev, gamestate):
         if ev.type == pgl.KEYDOWN: