Mercurial > nagslang
view nagslang/puzzle.py @ 398:e36e6101b766
Restarting the level restores health
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sat, 07 Sep 2013 01:12:21 +0200 |
parents | 14339d2d46bc |
children | 547320870aed |
line wrap: on
line source
from nagslang.constants import COLLISION_TYPE_PLAYER def get_editable_puzzlers(): classes = [] for cls_name, cls in globals().iteritems(): if isinstance(cls, type) and hasattr(cls, 'requires'): classes.append((cls_name, cls)) return classes class PuzzleGlue(object): """Glue that holds bits of a puzzle together. """ def __init__(self): self._components = {} def add_component(self, name, puzzler): if not isinstance(puzzler, Puzzler): puzzler = puzzler.puzzler if puzzler is None: # We've got a name, but no puzzler, # so we shouldn't actually be stuck # in here return self._components[name] = puzzler puzzler.set_glue(self) def get_state_of(self, name): return self._components[name].get_state() class Puzzler(object): """Behaviour specific to a puzzle component. """ def set_glue(self, glue): self.glue = glue def set_game_object(self, game_object): self.game_object = game_object def get_state(self): raise NotImplementedError() @classmethod def requires(cls): """Tell the level editor the arguments we require Format is a list of name: type hint tuples""" return [("name", "string")] class YesPuzzler(Puzzler): """Yes sir, I'm always on. """ def get_state(self): return True class NoPuzzler(Puzzler): """No sir, I'm always off. """ def get_state(self): return False class CollidePuzzler(Puzzler): def __init__(self, *collision_types): if not collision_types: collision_types = (COLLISION_TYPE_PLAYER,) self._collision_types = collision_types def get_state(self): space = self.game_object.get_space() for shape in space.shape_query(self.game_object.get_shape()): if shape.collision_type in self._collision_types: return True return False @classmethod def requires(cls): return [("name", "string"), ("collision_types", "list of ints")] class ParentAttrPuzzler(Puzzler): def __init__(self, attr_name): self._attr_name = attr_name def get_state(self): return getattr(self.game_object, self._attr_name) @classmethod def requires(cls): return [("name", "string"), ("attr_name", "string")] class StateProxyPuzzler(Puzzler): def __init__(self, state_source): self._state_source = state_source def get_state(self): return self.glue.get_state_of(self._state_source) @classmethod def requires(cls): return [("name", "string"), ("sources", "list of names")] class StateLogicalAndPuzzler(Puzzler): def __init__(self, *state_sources): self._state_sources = state_sources def get_state(self): for state_source in self._state_sources: if not self.glue.get_state_of(state_source): return False return True @classmethod def requires(cls): return [("name", "string"), ("sources", "list of names")]