comparison gamelib/gameboard.py @ 164:ab90040013a7

Implement equipment selling.
author Simon Cross <hodgestar@gmail.com>
date Thu, 03 Sep 2009 22:34:26 +0000
parents fa57868123d7
children c7d496556475
comparison
equal deleted inserted replaced
163:0d6e23dcd3af 164:ab90040013a7
61 cursors.cursors['select']) 61 cursors.cursors['select'])
62 self.add_tool_button("Egg", constants.TOOL_SELL_EGG, 62 self.add_tool_button("Egg", constants.TOOL_SELL_EGG,
63 cursors.cursors['select']) 63 cursors.cursors['select'])
64 self.add_tool_button("Building", constants.TOOL_SELL_BUILDING, 64 self.add_tool_button("Building", constants.TOOL_SELL_BUILDING,
65 cursors.cursors['select']) 65 cursors.cursors['select'])
66 self.add_tool_button("Equipment", constants.TOOL_SELL_EQUIPMENT)
66 self.add_spacer(20) 67 self.add_spacer(20)
67 68
68 self.add_heading("Buy ...") 69 self.add_heading("Buy ...")
69 self.add_tool_button("Fence", constants.TOOL_BUY_FENCE) 70 self.add_tool_button("Fence", constants.TOOL_BUY_FENCE)
70 for building_cls in buildings.BUILDINGS: 71 for building_cls in buildings.BUILDINGS:
218 self.place_animal(self.tv.screen_to_tile(e.pos)) 219 self.place_animal(self.tv.screen_to_tile(e.pos))
219 elif self.selected_tool == constants.TOOL_BUY_FENCE: 220 elif self.selected_tool == constants.TOOL_BUY_FENCE:
220 self.buy_fence(self.tv.screen_to_tile(e.pos)) 221 self.buy_fence(self.tv.screen_to_tile(e.pos))
221 elif self.selected_tool == constants.TOOL_SELL_BUILDING: 222 elif self.selected_tool == constants.TOOL_SELL_BUILDING:
222 self.sell_building(self.tv.screen_to_tile(e.pos)) 223 self.sell_building(self.tv.screen_to_tile(e.pos))
224 elif self.selected_tool == constants.TOOL_SELL_EQUIPMENT:
225 self.sell_equipment(self.tv.screen_to_tile(e.pos))
223 elif self.selected_tool == constants.TOOL_LOGGING: 226 elif self.selected_tool == constants.TOOL_LOGGING:
224 self.logging_forest(self.tv.screen_to_tile(e.pos)) 227 self.logging_forest(self.tv.screen_to_tile(e.pos))
225 elif buildings.is_building(self.selected_tool): 228 elif buildings.is_building(self.selected_tool):
226 self.buy_building(self.tv.screen_to_tile(e.pos), self.selected_tool) 229 self.buy_building(self.tv.screen_to_tile(e.pos), self.selected_tool)
227 elif equipment.is_equipment(self.selected_tool): 230 elif equipment.is_equipment(self.selected_tool):
228 self.buy_equipment(self.tv.screen_to_tile(e.pos), self.selected_tool) 231 self.buy_equipment(self.tv.screen_to_tile(e.pos), self.selected_tool)
229 232
230 def get_chicken(self, tile_pos): 233 def get_chicken(self, tile_pos):
231 for chick in self.chickens: 234 for chick in self.chickens:
232 if chick.covers(tile_pos): 235 if chick.covers(tile_pos) and chick.outside():
233 return chick 236 return chick
234 return None 237 return None
235 238
236 def get_building(self, tile_pos): 239 def get_building(self, tile_pos):
237 for building in self.buildings: 240 for building in self.buildings:
255 258
256 This will either select an animal or 259 This will either select an animal or
257 place a selected animal in a building. 260 place a selected animal in a building.
258 """ 261 """
259 chicken = self.get_chicken(tile_pos) 262 chicken = self.get_chicken(tile_pos)
260 if chicken and chicken.abode is None: 263 if chicken:
261 if chicken is self.animal_to_place: 264 if chicken is self.animal_to_place:
262 self.animal_to_place = None 265 self.animal_to_place = None
263 pygame.mouse.set_cursor(*cursors.cursors['arrow']) 266 pygame.mouse.set_cursor(*cursors.cursors['arrow'])
264 else: 267 else:
265 self.animal_to_place = chicken 268 self.animal_to_place = chicken
332 chicken.set_pos(place.get_pos()) 335 chicken.set_pos(place.get_pos())
333 self.set_visibility(self.animal_to_place) 336 self.set_visibility(self.animal_to_place)
334 337
335 place_button_map = {} 338 place_button_map = {}
336 339
337 width, height = pygame.display.get_surface().get_size()
338 tbl = gui.Table() 340 tbl = gui.Table()
339 columns = building.max_floor_width() 341 columns = building.max_floor_width()
340 kwargs = { 'style': { 'padding_left': 10, 'padding_bottom': 10 }} 342 kwargs = { 'style': { 'padding_left': 10, 'padding_bottom': 10 }}
341 for floor in building.floors(): 343 for floor in building.floors():
342 tbl.tr() 344 tbl.tr()
408 if building is None: 410 if building is None:
409 return 411 return
410 self.add_cash(building.sell_price()) 412 self.add_cash(building.sell_price())
411 building.remove(self.tv) 413 building.remove(self.tv)
412 self.remove_building(building) 414 self.remove_building(building)
415
416 def sell_equipment(self, tile_pos):
417 chicken = self.get_chicken(tile_pos)
418 if chicken is None or not chicken.equipment:
419 return
420 if len(chicken.equipment) == 1:
421 item = chicken.equipment[0]
422 self.add_cash(item.sell_price())
423 chicken.unequip(item)
424 else:
425 self.open_equipment_dialog(chicken)
426
427 def open_equipment_dialog(self, chicken):
428 tbl = gui.Table()
429
430 def sell_item(item, button):
431 """Select item of equipment."""
432 self.add_cash(item.sell_price())
433 chicken.unequip(item)
434 self.disp.close(tbl)
435
436 kwargs = { 'style': { 'padding_left': 10, 'padding_bottom': 10 }}
437
438 tbl.tr()
439 tbl.td(gui.Button("Sell ... "), align=-1, **kwargs)
440
441 for item in chicken.equipment:
442 tbl.tr()
443 button = gui.Button(item.name().title())
444 button.connect(gui.CLICK, sell_item, item, button)
445 tbl.td(button, align=1, **kwargs)
446
447 self.open_dialog(tbl)
413 448
414 def event(self, e): 449 def event(self, e):
415 if e.type == KEYDOWN: 450 if e.type == KEYDOWN:
416 if e.key == K_UP: 451 if e.key == K_UP:
417 self.tvw.move_view(0, -self.TILE_DIMENSIONS[1]) 452 self.tvw.move_view(0, -self.TILE_DIMENSIONS[1])