Mercurial > boomslang
comparison gamelib/state.py @ 86:593bddfacf18
Refactor Things a bit to render images.
author | Simon Cross <hodgestar+bzr@gmail.com> |
---|---|
date | Tue, 24 Aug 2010 00:14:23 +0200 |
parents | d7c0a702a0b4 |
children | 053bc8e167fc |
comparison
equal
deleted
inserted
replaced
85:5a7c35f4de8d | 86:593bddfacf18 |
---|---|
199 self._current_description = self._make_description( | 199 self._current_description = self._make_description( |
200 thing.get_description()) | 200 thing.get_description()) |
201 break | 201 break |
202 | 202 |
203 | 203 |
204 class Interact(object): | |
205 | |
206 def __init__(self, image, rect, interact_rect): | |
207 self.image = image | |
208 self.rect = rect | |
209 self.interact_rect = interact_rect | |
210 | |
211 def set_thing(self, thing): | |
212 pass | |
213 | |
214 def draw(self, surface): | |
215 if self.image is not None: | |
216 surface.blit(self.image, self.rect, None, BLEND_ADD) | |
217 | |
218 | |
219 class InteractNoImage(Interact): | |
220 | |
221 def __init__(self, x, y, w, h): | |
222 super(InteractNoImage, self).__init__(None, None, Rect(x, y, w, h)) | |
223 | |
224 | |
225 class InteractImage(Interact): | |
226 | |
227 def __init__(self, x, y, image_name): | |
228 super(InteractImage, self).__init__(None, None, None) | |
229 self._pos = (x, y) | |
230 self._image_name = image_name | |
231 | |
232 def set_thing(self, thing): | |
233 self.image = get_image(thing.folder, self._image_name) | |
234 self.rect = Rect(self._pos, self.image.get_size()) | |
235 self.interact_rect = self.rect | |
236 | |
237 | |
204 class Thing(StatefulGizmo): | 238 class Thing(StatefulGizmo): |
205 """Base class for things in a scene that you can interact with.""" | 239 """Base class for things in a scene that you can interact with.""" |
206 | 240 |
207 # sub-folder to look for resources in | 241 # name of thing |
242 NAME = None | |
243 | |
244 # sub-folder to look for resources in (defaults to scenes folder) | |
208 FOLDER = None | 245 FOLDER = None |
209 | 246 |
210 # name of image resource | 247 # list of Interact objects |
211 IMAGE = None | 248 INTERACTS = {} |
249 | |
250 # name first interact | |
251 INITIAL = None | |
212 | 252 |
213 # Interact rectangle hi-light color (for debugging) | 253 # Interact rectangle hi-light color (for debugging) |
214 # (set to None to turn off) | 254 # (set to None to turn off) |
215 if constants.DEBUG: | 255 if constants.DEBUG: |
216 _interact_hilight_color = Color('red') | 256 _interact_hilight_color = Color('red') |
217 else: | 257 else: |
218 _interact_hilight_color = None | 258 _interact_hilight_color = None |
219 | 259 |
220 def __init__(self, name, rect): | 260 def __init__(self): |
221 StatefulGizmo.__init__(self) | 261 StatefulGizmo.__init__(self) |
222 self.name = name | 262 # name of the thing |
223 # area within scene to render to | 263 self.name = self.NAME |
224 self.rect = Rect(rect) | 264 # folder for resource (None is overridden by scene folder) |
225 # area within scene that triggers calls to interact | 265 self.folder = self.FOLDER |
226 self.interact_rect = Rect(rect) | 266 # interacts |
267 self.interacts = self.INTERACTS | |
227 # these are set by set_scene | 268 # these are set by set_scene |
228 self.scene = None | 269 self.scene = None |
229 self.state = None | 270 self.state = None |
271 self.current_interact = None | |
272 self.rect = None | |
230 # TODO: add masks | 273 # TODO: add masks |
231 # TODO: add images | |
232 | 274 |
233 def set_scene(self, scene): | 275 def set_scene(self, scene): |
234 assert self.scene is None | 276 assert self.scene is None |
235 self.scene = scene | 277 self.scene = scene |
278 if self.folder is None: | |
279 self.folder = scene.FOLDER | |
236 self.state = scene.state | 280 self.state = scene.state |
281 for interact in self.interacts.itervalues(): | |
282 interact.set_thing(self) | |
283 self.set_interact(self.INITIAL) | |
284 | |
285 def set_interact(self, name): | |
286 self.current_interact = self.interacts[name] | |
287 self.rect = self.current_interact.interact_rect | |
288 assert self.rect is not None, name | |
237 | 289 |
238 def get_description(self): | 290 def get_description(self): |
239 return None | 291 return None |
240 | 292 |
241 def is_interactive(self): | 293 def is_interactive(self): |
266 | 318 |
267 def interact_default(self, item): | 319 def interact_default(self, item): |
268 return Result("It doesn't work.") | 320 return Result("It doesn't work.") |
269 | 321 |
270 def draw(self, surface): | 322 def draw(self, surface): |
323 self.current_interact.draw(surface) | |
271 if self._interact_hilight_color is not None: | 324 if self._interact_hilight_color is not None: |
272 frame_rect(surface, self._interact_hilight_color, | 325 frame_rect(surface, self._interact_hilight_color, |
273 self.interact_rect.inflate(1, 1), 1) | 326 self.rect.inflate(1, 1), 1) |
274 # TODO: draw image if there is one | |
275 | 327 |
276 | 328 |
277 class Item(object): | 329 class Item(object): |
278 """Base class for inventory items.""" | 330 """Base class for inventory items.""" |
279 | 331 |