Mercurial > boomslang
comparison gamelib/state.py @ 94:ce23fad8ecb3
More complex shaped interactables
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Tue, 24 Aug 2010 00:56:31 +0200 |
parents | 350ce4ebe122 |
children | 19d784fd3918 |
comparison
equal
deleted
inserted
replaced
93:350ce4ebe122 | 94:ce23fad8ecb3 |
---|---|
176 Item may be an item in the list of items or None for the hand. | 176 Item may be an item in the list of items or None for the hand. |
177 | 177 |
178 Returns a Result object to provide feedback to the player. | 178 Returns a Result object to provide feedback to the player. |
179 """ | 179 """ |
180 for thing in self.things.itervalues(): | 180 for thing in self.things.itervalues(): |
181 if thing.rect.collidepoint(pos): | 181 if thing.contains(pos): |
182 result = thing.interact(item) | 182 result = thing.interact(item) |
183 if result: | 183 if result: |
184 if self._current_thing: | 184 if self._current_thing: |
185 # Also update descriptions if needed | 185 # Also update descriptions if needed |
186 self._current_description = self._make_description( | 186 self._current_description = self._make_description( |
191 """Call to check whether the cursor has entered / exited a thing. | 191 """Call to check whether the cursor has entered / exited a thing. |
192 | 192 |
193 Item may be an item in the list of items or None for the hand. | 193 Item may be an item in the list of items or None for the hand. |
194 """ | 194 """ |
195 if self._current_thing is not None: | 195 if self._current_thing is not None: |
196 if self._current_thing.rect.collidepoint(pos): | 196 if self._current_thing.contains(pos): |
197 return | 197 return |
198 else: | 198 else: |
199 self._current_thing.leave() | 199 self._current_thing.leave() |
200 self._current_thing = None | 200 self._current_thing = None |
201 self._current_description = None | 201 self._current_description = None |
202 for thing in self.things.itervalues(): | 202 for thing in self.things.itervalues(): |
203 if thing.rect.collidepoint(pos): | 203 if thing.contains(pos): |
204 thing.enter(item) | 204 thing.enter(item) |
205 self._current_thing = thing | 205 self._current_thing = thing |
206 self._current_description = self._make_description( | 206 self._current_description = self._make_description( |
207 thing.get_description()) | 207 thing.get_description()) |
208 break | 208 break |
225 | 225 |
226 class InteractNoImage(Interact): | 226 class InteractNoImage(Interact): |
227 | 227 |
228 def __init__(self, x, y, w, h): | 228 def __init__(self, x, y, w, h): |
229 super(InteractNoImage, self).__init__(None, None, Rect(x, y, w, h)) | 229 super(InteractNoImage, self).__init__(None, None, Rect(x, y, w, h)) |
230 | |
231 class InteractRectUnion(Interact): | |
232 | |
233 def __init__(self, rect_list): | |
234 # pygame.rect.Rect.unionall should do this, but is broken | |
235 # in some pygame versions (including 1.8, it appears) | |
236 rect_list = [Rect(x) for x in rect_list] | |
237 union_rect = rect_list[0] | |
238 for rect in rect_list[1:]: | |
239 union_rect = union_rect.union(rect) | |
240 super(InteractRectUnion, self).__init__(None, None, union_rect) | |
241 self.interact_rect = rect_list | |
230 | 242 |
231 | 243 |
232 class InteractImage(Interact): | 244 class InteractImage(Interact): |
233 | 245 |
234 def __init__(self, x, y, image_name): | 246 def __init__(self, x, y, image_name): |
292 def set_interact(self, name): | 304 def set_interact(self, name): |
293 self.current_interact = self.interacts[name] | 305 self.current_interact = self.interacts[name] |
294 self.rect = self.current_interact.interact_rect | 306 self.rect = self.current_interact.interact_rect |
295 assert self.rect is not None, name | 307 assert self.rect is not None, name |
296 | 308 |
309 def contains(self, pos): | |
310 if hasattr(self.rect, 'collidepoint'): | |
311 return self.rect.collidepoint(pos) | |
312 else: | |
313 # FIXME: add sanity check | |
314 for rect in list(self.rect): | |
315 if rect.collidepoint(pos): | |
316 return True | |
317 return False | |
318 | |
297 def get_description(self): | 319 def get_description(self): |
298 return None | 320 return None |
299 | 321 |
300 def is_interactive(self): | 322 def is_interactive(self): |
301 return True | 323 return True |
327 return Result("It doesn't work.") | 349 return Result("It doesn't work.") |
328 | 350 |
329 def draw(self, surface): | 351 def draw(self, surface): |
330 self.current_interact.draw(surface) | 352 self.current_interact.draw(surface) |
331 if self._interact_hilight_color is not None: | 353 if self._interact_hilight_color is not None: |
332 frame_rect(surface, self._interact_hilight_color, | 354 if hasattr(self.rect, 'collidepoint'): |
333 self.rect.inflate(1, 1), 1) | 355 frame_rect(surface, self._interact_hilight_color, |
356 self.rect.inflate(1, 1), 1) | |
357 else: | |
358 for rect in self.rect: | |
359 frame_rect(surface, self._interact_hilight_color, | |
360 rect.inflate(1, 1), 1) | |
334 | 361 |
335 | 362 |
336 class Item(object): | 363 class Item(object): |
337 """Base class for inventory items.""" | 364 """Base class for inventory items.""" |
338 | 365 |