comparison gamelib/gameboard.py @ 192:a490ee2ef446

Allow selling of chickens in henhouses
author Neil Muller <drnlmuller@gmail.com>
date Fri, 04 Sep 2009 18:17:55 +0000
parents 3c80f49d7d74
children db246683d5da
comparison
equal deleted inserted replaced
191:3c80f49d7d74 192:a490ee2ef446
252 elif e.button != 1: # Left button 252 elif e.button != 1: # Left button
253 return 253 return
254 if self.selected_tool == constants.TOOL_SELL_CHICKEN: 254 if self.selected_tool == constants.TOOL_SELL_CHICKEN:
255 self.sell_chicken(self.tv.screen_to_tile(e.pos)) 255 self.sell_chicken(self.tv.screen_to_tile(e.pos))
256 elif self.selected_tool == constants.TOOL_SELL_EGG: 256 elif self.selected_tool == constants.TOOL_SELL_EGG:
257 pass 257 self.sell_egg(self.tv.screen_to_tile(e.pos))
258 elif self.selected_tool == constants.TOOL_PLACE_ANIMALS: 258 elif self.selected_tool == constants.TOOL_PLACE_ANIMALS:
259 self.place_animal(self.tv.screen_to_tile(e.pos)) 259 self.place_animal(self.tv.screen_to_tile(e.pos))
260 elif self.selected_tool == constants.TOOL_BUY_FENCE: 260 elif self.selected_tool == constants.TOOL_BUY_FENCE:
261 self.buy_fence(self.tv.screen_to_tile(e.pos)) 261 self.buy_fence(self.tv.screen_to_tile(e.pos))
262 elif self.selected_tool == constants.TOOL_SELL_BUILDING: 262 elif self.selected_tool == constants.TOOL_SELL_BUILDING:
281 if building.covers(tile_pos): 281 if building.covers(tile_pos):
282 return building 282 return building
283 return None 283 return None
284 284
285 def sell_chicken(self, tile_pos): 285 def sell_chicken(self, tile_pos):
286
287 def do_sell(chicken):
288 if not chicken:
289 return False # sanity check
290 if len(self.chickens) == 1:
291 print "You can't sell your last chicken!"
292 return False
293 self.add_cash(constants.SELL_PRICE_CHICKEN)
294 sound.play_sound("sell-chicken.ogg")
295 self.remove_chicken(chicken)
296 return True
297
286 chick = self.get_outside_chicken(tile_pos) 298 chick = self.get_outside_chicken(tile_pos)
287 if chick is None: 299 if chick is None:
288 return 300 building = self.get_building(tile_pos)
289 if len(self.chickens) == 1: 301 if building and building.NAME in buildings.HENHOUSES:
290 print "You can't sell your last chicken!" 302 self.open_building_dialog(building, do_sell)
291 return 303 return
292 self.add_cash(constants.SELL_PRICE_CHICKEN) 304 do_sell(chick)
293 sound.play_sound("sell-chicken.ogg") 305
294 self.remove_chicken(chick) 306
307 def sell_egg(self, tile_pos):
308 def do_sell(chicken):
309 # Placeholde
310 return False
311 building = self.get_building(tile_pos)
312 if building and building.NAME in buildings.HENHOUSES:
313 self.open_building_dialog(building)
295 314
296 def place_animal(self, tile_pos): 315 def place_animal(self, tile_pos):
297 """Handle an TOOL_PLACE_ANIMALS click. 316 """Handle an TOOL_PLACE_ANIMALS click.
298 317
299 This will either select an animal or 318 This will either select an animal or
348 tbl.td(close_button, align=1) 367 tbl.td(close_button, align=1)
349 368
350 self.disp.open(tbl) 369 self.disp.open(tbl)
351 return tbl 370 return tbl
352 371
353 def open_building_dialog(self, building): 372 def open_building_dialog(self, building, sell_callback=None):
354 """Create dialog for manipulating the contents of a building.""" 373 """Create dialog for manipulating the contents of a building."""
355 def select_occupant(place, button): 374 def select_occupant(place, button, sell_callback):
356 """Select occupant in place.""" 375 """Select occupant in place."""
376 # sell_callback should return true if we need to remove the
377 # occupant
357 self.animal_to_place = place.occupant 378 self.animal_to_place = place.occupant
358 pygame.mouse.set_cursor(*cursors.cursors['chicken']) 379 if not sell_callback:
359 380 pygame.mouse.set_cursor(*cursors.cursors['chicken'])
360 def set_occupant(place, button): 381 else:
382 # Attempt to sell the occupant
383 self.animal_to_place = None
384 if sell_callback(place.occupant):
385 button.value = icons.EMPTY_NEST_ICON
386 button.disconnect(gui.CLICK, select_occupant)
387 button.connect(gui.CLICK, set_occupant, place, button,
388 sell_callback)
389
390 def set_occupant(place, button, sell_callback):
361 """Set occupant of a given place.""" 391 """Set occupant of a given place."""
362 if self.animal_to_place is not None: 392 if self.animal_to_place is not None:
363 button.value = icons.CHKN_NEST_ICON 393 button.value = icons.CHKN_NEST_ICON
364 button.disconnect(gui.CLICK, set_occupant) 394 button.disconnect(gui.CLICK, set_occupant)
365 button.connect(gui.CLICK, select_occupant, place, button) 395 button.connect(gui.CLICK, select_occupant, place, button,
396 sell_callback)
366 397
367 old_abode = self.animal_to_place.abode 398 old_abode = self.animal_to_place.abode
368 if old_abode is not None: 399 if old_abode is not None:
369 old_abode.clear_occupant() 400 old_abode.clear_occupant()
370 if id(old_abode) in place_button_map: 401 if id(old_abode) in place_button_map:
371 old_button = place_button_map[id(old_abode)] 402 old_button = place_button_map[id(old_abode)]
372 old_button.value = icons.EMPTY_NEST_ICON 403 old_button.value = icons.EMPTY_NEST_ICON
373 old_button.disconnect(gui.CLICK, select_occupant) 404 old_button.disconnect(gui.CLICK, select_occupant)
374 old_button.connect(gui.CLICK, set_occupant, place, button) 405 old_button.connect(gui.CLICK, set_occupant, place, button,
406 sell_callback)
375 407
376 chicken = self.animal_to_place 408 chicken = self.animal_to_place
377 place.set_occupant(chicken) 409 place.set_occupant(chicken)
378 chicken.set_pos(place.get_pos()) 410 chicken.set_pos(place.get_pos())
379 self.set_visibility(self.animal_to_place) 411 self.set_visibility(self.animal_to_place)
390 for row in floor.rows(): 422 for row in floor.rows():
391 tbl.tr() 423 tbl.tr()
392 for place in row: 424 for place in row:
393 if place.occupant is None: 425 if place.occupant is None:
394 button = gui.Button(icons.EMPTY_NEST_ICON) 426 button = gui.Button(icons.EMPTY_NEST_ICON)
395 button.connect(gui.CLICK, set_occupant, place, button) 427 button.connect(gui.CLICK, set_occupant, place, button,
428 sell_callback)
396 else: 429 else:
397 button = gui.Button(icons.CHKN_NEST_ICON) 430 button = gui.Button(icons.CHKN_NEST_ICON)
398 button.connect(gui.CLICK, select_occupant, place, button) 431 button.connect(gui.CLICK, select_occupant, place, button,
432 sell_callback)
399 place_button_map[id(place)] = button 433 place_button_map[id(place)] = button
400 tbl.td(button, **kwargs) 434 tbl.td(button, **kwargs)
401 435
402 building.selected(True) 436 building.selected(True)
403 def close_callback(): 437 def close_callback():