From: Simon Cross Date: Sun, 4 Sep 2016 17:28:23 +0000 (+0200) Subject: Hook up lights and refactor obstacle loading. X-Git-Tag: tabakrolletjie-v1.0.0~251 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=2e20539ebb1efedfdc675962961c10b6985c09a7;p=tabakrolletjie.git Hook up lights and refactor obstacle loading. --- diff --git a/tabakrolletjie/lights.py b/tabakrolletjie/lights.py index 92b66e0..81740d8 100644 --- a/tabakrolletjie/lights.py +++ b/tabakrolletjie/lights.py @@ -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): diff --git a/tabakrolletjie/obstacles.py b/tabakrolletjie/obstacles.py index 61d4376..d187a59 100644 --- a/tabakrolletjie/obstacles.py +++ b/tabakrolletjie/obstacles.py @@ -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) diff --git a/tabakrolletjie/scenes/night.py b/tabakrolletjie/scenes/night.py index 5d3721b..330530b 100644 --- a/tabakrolletjie/scenes/night.py +++ b/tabakrolletjie/scenes/night.py @@ -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: