Ignore:
Timestamp:
09/02/13 21:05:25 (8 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Phase:
public
Message:

Load game objects from level.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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):
Note: See TracChangeset for help on using the changeset viewer.