Mercurial > pyntnclick
comparison 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 |
comparison
equal
deleted
inserted
replaced
40:9fdbfbc02a60 | 41:ad6f56bfa8b7 |
---|---|
52 self.inventory.remove(self.items[name]) | 52 self.inventory.remove(self.items[name]) |
53 | 53 |
54 def draw(self, surface): | 54 def draw(self, surface): |
55 self.current_scene.draw(surface) | 55 self.current_scene.draw(surface) |
56 | 56 |
57 def message(self, msg): | |
58 print msg | |
59 | |
57 | 60 |
58 class Scene(object): | 61 class Scene(object): |
59 """Base class for scenes.""" | 62 """Base class for scenes.""" |
60 | 63 |
61 # sub-folder to look for resources in | 64 # sub-folder to look for resources in |
65 BACKGROUND = None | 68 BACKGROUND = None |
66 | 69 |
67 # name of scene (optional, defaults to folder) | 70 # name of scene (optional, defaults to folder) |
68 NAME = None | 71 NAME = None |
69 | 72 |
73 # initial scene data (optional, defaults to none) | |
74 INITIAL_DATA = None | |
75 | |
70 def __init__(self, state): | 76 def __init__(self, state): |
71 # scene name | 77 # scene name |
72 self.name = self.NAME if self.NAME is not None else self.FOLDER | 78 self.name = self.NAME if self.NAME is not None else self.FOLDER |
73 # link back to state object | 79 # link back to state object |
74 self.state = state | 80 self.state = state |
75 # map of thing names -> Thing objects | 81 # map of thing names -> Thing objects |
76 self.things = {} | 82 self.things = {} |
77 self._background = get_image(self.FOLDER, self.BACKGROUND) | 83 self._background = get_image(self.FOLDER, self.BACKGROUND) |
84 self.data = {} | |
85 if self.INITIAL_DATA: | |
86 self.data.update(self.INITIAL_DATA) | |
78 | 87 |
79 def add_item(self, item): | 88 def add_item(self, item): |
80 self.state.add_item(item) | 89 self.state.add_item(item) |
81 | 90 |
82 def add_thing(self, thing): | 91 def add_thing(self, thing): |
83 self.things[thing.name] = thing | 92 self.things[thing.name] = thing |
93 thing.set_scene(self) | |
94 | |
95 def remove_thing(self, thing): | |
96 del self.things[thing.name] | |
84 | 97 |
85 def draw_background(self, surface): | 98 def draw_background(self, surface): |
86 surface.blit(self._background, (0, 0), None, BLEND_ADD) | 99 surface.blit(self._background, (0, 0), None, BLEND_ADD) |
87 | 100 |
88 def draw_things(self, surface): | 101 def draw_things(self, surface): |
95 | 108 |
96 | 109 |
97 class Thing(object): | 110 class Thing(object): |
98 """Base class for things in a scene that you can interact with.""" | 111 """Base class for things in a scene that you can interact with.""" |
99 | 112 |
113 # sub-folder to look for resources in | |
114 FOLDER = None | |
115 | |
116 # name of image resource | |
117 IMAGE = None | |
118 | |
100 def __init__(self, name, rect): | 119 def __init__(self, name, rect): |
101 self.name = name | 120 self.name = name |
102 # area within scene that triggers calls to interact | 121 # area within scene that triggers calls to interact |
103 self.rect = rect | 122 self.rect = rect |
104 # these are set by set_scene | 123 # these are set by set_scene |
110 def set_scene(self, scene): | 129 def set_scene(self, scene): |
111 assert self.scene is None | 130 assert self.scene is None |
112 self.scene = scene | 131 self.scene = scene |
113 self.state = scene.state | 132 self.state = scene.state |
114 | 133 |
134 def message(self, msg): | |
135 self.state.message(msg) | |
136 | |
115 def interact(self, item): | 137 def interact(self, item): |
116 pass | 138 if item is None: |
139 self.interact_without() | |
140 else: | |
141 handler = getattr(self, 'interact_with_' + item.name, None) | |
142 if handler is not None: | |
143 handler(item) | |
144 else: | |
145 self.interact_default(item) | |
146 | |
147 def interact_without(self): | |
148 self.interact_default(None) | |
149 | |
150 def interact_default(self, item): | |
151 self.message("It doesn't work.") | |
117 | 152 |
118 def draw(self, surface): | 153 def draw(self, surface): |
119 pass | 154 pass |
120 | 155 |
121 | 156 |