Changeset 145:0c49627920eb


Ignore:
Timestamp:
Sep 2, 2013, 9:05:25 PM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

Load game objects from level.

Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • data/levels/level1

    r141 r145  
    1818  - [60, 780]
    1919size: [1200, 900]
     20game_objects:
     21  - classname: Box
     22    args:
     23      - [250, 350]
     24  - classname: FloorSwitch
     25    args:
     26      - [300, 400]
     27    name: switch
     28  - classname: FloorSwitch
     29    args:
     30      - [300, 600]
     31    name: switch2
     32  - classname: FloorLight
     33    args:
     34      - [300, 500]
     35      - switch
     36    name: light
     37  - classname: FloorLight
     38    args:
     39      - [250, 500]
     40      - both_switches
     41    name: light2
     42  - classname: StateLogicalAndPuzzler
     43    args:
     44      - switch
     45      - switch2
     46    name: both_switches
  • nagslang/game_object.py

    r143 r145  
    1717
    1818    def add_component(self, name, puzzler):
     19        if not isinstance(puzzler, Puzzler):
     20            puzzler = puzzler.puzzler
    1921        self._components[name] = puzzler
    2022        puzzler.set_glue(self)
     
    277279def make_body(mass, moment, position, damping=None):
    278280    body = pymunk.Body(mass, moment)
    279     body.position = position
     281    body.position = tuple(position)
    280282    if damping is not None:
    281283        body.damping = damping
     
    322324class FloorSwitch(GameObject):
    323325    def __init__(self, space, position):
    324         body = pymunk.Body()
    325         body.position = position
     326        body = make_body(None, None, position)
    326327        self.shape = pymunk.Circle(body, 30)
    327328        self.shape.collision_type = COLLISION_TYPE_SWITCH
     
    336337class FloorLight(GameObject):
    337338    def __init__(self, space, position, state_source):
    338         body = pymunk.Body()
    339         body.position = position
     339        body = make_body(None, None, position)
    340340        self.shape = pymunk.Circle(body, 10)
    341341        self.shape.collision_type = COLLISION_TYPE_SWITCH
  • nagslang/level.py

    r139 r145  
    22import pygame.locals as pgl
    33
     4from nagslang import game_object as go
    45from nagslang.resources import resources
    56from nagslang.yamlish import load, dump
     
    2728        self._surface = None
    2829        self._exterior = False
     30        self._glue = go.PuzzleGlue()
     31        self._drawables = []
    2932
    30     def load(self):
     33    def _get_data(self):
     34        # For overriding in tests.
    3135        with resources.get_file('levels', self.name) as f:
    32             data = load(f)
     36            return load(f)
     37
     38    def load(self, space):
     39        data = self._get_data()
    3340        self.x, self.y = data['size']
    3441        self.base_tile = data['base_tile']
     
    3744            for point in points:
    3845                self.polygons[i].append(tuple(point))
     46        for game_object_dict in data.get('game_objects', []):
     47            self._create_game_object(space, **game_object_dict)
     48
     49    def _create_game_object(self, space, classname, args, name=None):
     50        # We should probably build a registry of game objects or something.
     51        # At least this is better than just calling `eval`, right?
     52        cls = getattr(go, classname)
     53        if issubclass(cls, go.Puzzler):
     54            gobj = cls(*args)
     55        elif issubclass(cls, go.GameObject):
     56            gobj = cls(space, *args)
     57            self._drawables.append(gobj)
     58        else:
     59            raise TypeError(
     60                "Expected a subclass of Puzzler or GameObject, got %s" % (
     61                    classname))
     62        if name is not None:
     63            self._glue.add_component(name, gobj)
    3964
    4065    def all_closed(self):
     
    80105    def get_walls(self):
    81106        return self.polygons.values()
     107
     108    def get_drawables(self):
     109        return self._drawables
    82110
    83111    def _draw_walls(self):
  • nagslang/screens/area.py

    r143 r145  
    6969        self.keys = ControlKeys()
    7070        self._level = Level(self.name)
    71         self._level.load()
     71        self._level.load(self.space)
    7272        self._drawables = Drawables()
    7373        self.add_walls()
    7474        self.add_protagonist()
    75         self._setup_demo_objects()
     75        self.add_game_objects()
     76        # self._setup_demo_objects()
    7677
    7778    def _setup_demo_objects(self):
     
    8384        light2 = FloorLight(self.space, (250, 500), 'both_switches')
    8485        light2.zorder = ZORDER_HIGH
    85         glue.add_component('switch', switch.puzzler)
    86         glue.add_component('switch2', switch2.puzzler)
    87         glue.add_component('light', light.puzzler)
    88         glue.add_component('light2', light2.puzzler)
     86        glue.add_component('switch', switch)
     87        glue.add_component('switch2', switch2)
     88        glue.add_component('light', light)
     89        glue.add_component('light2', light2)
    8990        glue.add_component(
    9091            'both_switches', StateLogicalAndPuzzler('switch', 'switch2'))
     
    110111                corner = next_corner
    111112        self.space.add(*self.walls)
     113
     114    def add_game_objects(self):
     115        for drawable in self._level.get_drawables():
     116            self._drawables.add(drawable)
    112117
    113118    def add_protagonist(self):
  • nagslang/tests/test_game_object.py

    r125 r145  
    6868        faker.fake_state = 'bar'
    6969        self.assertEqual('bar', puzzler.get_state())
     70
     71    def test_glue_add_component(self):
     72        glue = game_object.PuzzleGlue()
     73        puzzler = FakePuzzler('foo')
     74        gobj = FakeGameObject(None, None)
     75        gobj.puzzler = FakePuzzler('bar')
     76
     77        self.assertEqual({}, glue._components)
     78        glue.add_component('foo', puzzler)
     79        self.assertEqual({'foo': puzzler}, glue._components)
     80        glue.add_component('bar', gobj)
     81        self.assertEqual(
     82            {'foo': puzzler, 'bar': gobj.puzzler}, glue._components)
Note: See TracChangeset for help on using the changeset viewer.