Mercurial > pyntnclick
diff gamelib/state.py @ 41:ad6f56bfa8b7
Cryo door, titanium leg and some interaction prototypes.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Mon, 23 Aug 2010 00:49:22 +0200 |
parents | 088a101f5b94 |
children | 8f1fccb8cadf |
line wrap: on
line diff
--- a/gamelib/state.py Sun Aug 22 22:09:59 2010 +0200 +++ b/gamelib/state.py Mon Aug 23 00:49:22 2010 +0200 @@ -54,6 +54,9 @@ def draw(self, surface): self.current_scene.draw(surface) + def message(self, msg): + print msg + class Scene(object): """Base class for scenes.""" @@ -67,6 +70,9 @@ # name of scene (optional, defaults to folder) NAME = None + # initial scene data (optional, defaults to none) + INITIAL_DATA = None + def __init__(self, state): # scene name self.name = self.NAME if self.NAME is not None else self.FOLDER @@ -75,12 +81,19 @@ # map of thing names -> Thing objects self.things = {} self._background = get_image(self.FOLDER, self.BACKGROUND) + self.data = {} + if self.INITIAL_DATA: + self.data.update(self.INITIAL_DATA) def add_item(self, item): self.state.add_item(item) def add_thing(self, thing): self.things[thing.name] = thing + thing.set_scene(self) + + def remove_thing(self, thing): + del self.things[thing.name] def draw_background(self, surface): surface.blit(self._background, (0, 0), None, BLEND_ADD) @@ -97,6 +110,12 @@ class Thing(object): """Base class for things in a scene that you can interact with.""" + # sub-folder to look for resources in + FOLDER = None + + # name of image resource + IMAGE = None + def __init__(self, name, rect): self.name = name # area within scene that triggers calls to interact @@ -112,8 +131,24 @@ self.scene = scene self.state = scene.state + def message(self, msg): + self.state.message(msg) + def interact(self, item): - pass + if item is None: + self.interact_without() + else: + handler = getattr(self, 'interact_with_' + item.name, None) + if handler is not None: + handler(item) + else: + self.interact_default(item) + + def interact_without(self): + self.interact_default(None) + + def interact_default(self, item): + self.message("It doesn't work.") def draw(self, surface): pass