comparison gamelib/gameboard.py @ 422:ab4fc3fe0f96

chickens scatter; chop wood
author Adrianna Pińska <adrianna.pinska@gmail.com>
date Sat, 21 Nov 2009 15:35:23 +0000
parents e65536ca215b
children fbef17cab632
comparison
equal deleted inserted replaced
421:e65536ca215b 422:ab4fc3fe0f96
61 gui.Table.__init__(self, **params) 61 gui.Table.__init__(self, **params)
62 self.group = gui.Group(name='toolbar', value=None) 62 self.group = gui.Group(name='toolbar', value=None)
63 self._next_tool_value = 0 63 self._next_tool_value = 0
64 self.gameboard = gameboard 64 self.gameboard = gameboard
65 self.cash_counter = mklabel(align=1) 65 self.cash_counter = mklabel(align=1)
66 self.wood_counter = mklabel(align=1)
66 self.chicken_counter = mklabel(align=1) 67 self.chicken_counter = mklabel(align=1)
67 self.egg_counter = mklabel(align=1) 68 self.egg_counter = mklabel(align=1)
68 self.day_counter = mklabel(align=1) 69 self.day_counter = mklabel(align=1)
69 self.killed_foxes = mklabel(align=1) 70 self.killed_foxes = mklabel(align=1)
70 71
71 self.tr() 72 self.tr()
72 self.td(gui.Spacer(self.rect.w/2, 0)) 73 self.td(gui.Spacer(self.rect.w/2, 0))
73 self.td(gui.Spacer(self.rect.w/2, 0)) 74 self.td(gui.Spacer(self.rect.w/2, 0))
74 self.add_counter(mklabel("Day:"), self.day_counter) 75 self.add_counter(mklabel("Day:"), self.day_counter)
75 self.add_counter(mklabel("Groats:"), self.cash_counter) 76 self.add_counter(mklabel("Groats:"), self.cash_counter)
77 self.add_counter(mklabel("Planks:"), self.wood_counter)
76 self.add_counter(mklabel("Eggs:"), self.egg_counter) 78 self.add_counter(mklabel("Eggs:"), self.egg_counter)
77 self.add_counter(icons.CHKN_ICON, self.chicken_counter) 79 self.add_counter(icons.CHKN_ICON, self.chicken_counter)
78 self.add_counter(icons.KILLED_FOX, self.killed_foxes) 80 self.add_counter(icons.KILLED_FOX, self.killed_foxes)
79 self.add_spacer(5) 81 self.add_spacer(5)
80 82
81 self.add_tool_button("Move Hen", constants.TOOL_PLACE_ANIMALS, 83 self.add_tool_button("Move Hen", constants.TOOL_PLACE_ANIMALS,
82 None, cursors.cursors['select']) 84 None, cursors.cursors['select'])
83 self.add_tool_button("Cut Trees", constants.TOOL_LOGGING,
84 constants.LOGGING_PRICE, cursors.cursors['ball'])
85 self.add_spacer(5) 85 self.add_spacer(5)
86 86
87 self.add_heading("Sell ...") 87 self.add_heading("Sell ...")
88 self.add_tool_button("Chicken", constants.TOOL_SELL_CHICKEN, 88 self.add_tool_button("Chicken", constants.TOOL_SELL_CHICKEN,
89 level.sell_price_chicken, cursors.cursors['sell']) 89 level.sell_price_chicken, cursors.cursors['sell'])
110 self.add_spacer(5) 110 self.add_spacer(5)
111 self.add_tool_button("Repair", constants.TOOL_REPAIR_BUILDING, None, cursors.cursors['repair']) 111 self.add_tool_button("Repair", constants.TOOL_REPAIR_BUILDING, None, cursors.cursors['repair'])
112 112
113 self.add_spacer(5) 113 self.add_spacer(5)
114 self.add_tool("Price Reference", self.show_prices) 114 self.add_tool("Price Reference", self.show_prices)
115 self.add_spacer(10)
116 115
117 self.fin_tool = self.add_tool("Finished Day", self.day_done) 116 self.fin_tool = self.add_tool("Finished Day", self.day_done)
118 117
119 self.anim_clear_tool = False # Flag to clear the tool on an anim loop 118 self.anim_clear_tool = False # Flag to clear the tool on an anim loop
120 # pgu's tool widget fiddling happens after the tool action, so calling 119 # pgu's tool widget fiddling happens after the tool action, so calling
195 close_button.connect(gui.CLICK, dialog.close) 194 close_button.connect(gui.CLICK, dialog.close)
196 dialog.open() 195 dialog.open()
197 self.anim_clear_tool = True 196 self.anim_clear_tool = True
198 197
199 update_cash_counter = mkcountupdate('cash_counter') 198 update_cash_counter = mkcountupdate('cash_counter')
199 update_wood_counter = mkcountupdate('wood_counter')
200 update_fox_counter = mkcountupdate('killed_foxes') 200 update_fox_counter = mkcountupdate('killed_foxes')
201 update_chicken_counter = mkcountupdate('chicken_counter') 201 update_chicken_counter = mkcountupdate('chicken_counter')
202 update_egg_counter = mkcountupdate('egg_counter') 202 update_egg_counter = mkcountupdate('egg_counter')
203 update_day_counter = mkcountupdate('day_counter') 203 update_day_counter = mkcountupdate('day_counter')
204 204
291 self.chickens = set() 291 self.chickens = set()
292 self.foxes = set() 292 self.foxes = set()
293 self.buildings = [] 293 self.buildings = []
294 self._pos_cache = { 'fox' : [], 'chicken' : []} 294 self._pos_cache = { 'fox' : [], 'chicken' : []}
295 self.cash = 0 295 self.cash = 0
296 self.wood = 0
296 self.eggs = 0 297 self.eggs = 0
297 self.days = 0 298 self.days = 0
298 self.killed_foxes = 0 299 self.killed_foxes = 0
299 self.add_cash(level.starting_cash) 300 self.add_cash(level.starting_cash)
300 self.day, self.night = True, False 301 self.day, self.night = True, False
412 self.sell_building(self.tv.screen_to_tile(e.pos)) 413 self.sell_building(self.tv.screen_to_tile(e.pos))
413 elif self.selected_tool == constants.TOOL_SELL_EQUIPMENT: 414 elif self.selected_tool == constants.TOOL_SELL_EQUIPMENT:
414 self.sell_equipment(self.tv.screen_to_tile(e.pos)) 415 self.sell_equipment(self.tv.screen_to_tile(e.pos))
415 elif self.selected_tool == constants.TOOL_REPAIR_BUILDING: 416 elif self.selected_tool == constants.TOOL_REPAIR_BUILDING:
416 self.repair_building(self.tv.screen_to_tile(e.pos)) 417 self.repair_building(self.tv.screen_to_tile(e.pos))
417 elif self.selected_tool == constants.TOOL_LOGGING:
418 self.logging_forest(self.tv.screen_to_tile(e.pos))
419 elif buildings.is_building(self.selected_tool): 418 elif buildings.is_building(self.selected_tool):
420 self.buy_building(self.tv.screen_to_tile(e.pos), self.selected_tool) 419 self.buy_building(self.tv.screen_to_tile(e.pos), self.selected_tool)
421 elif equipment.is_equipment(self.selected_tool): 420 elif equipment.is_equipment(self.selected_tool):
422 self.buy_equipment(self.tv.screen_to_tile(e.pos), self.selected_tool) 421 self.buy_equipment(self.tv.screen_to_tile(e.pos), self.selected_tool)
423 422
651 button.connect(gui.CLICK, evict_callback) 650 button.connect(gui.CLICK, evict_callback)
652 tbl.td(button, colspan=2, **kwargs) 651 tbl.td(button, colspan=2, **kwargs)
653 652
654 self.open_dialog(tbl, close_callback=close_callback) 653 self.open_dialog(tbl, close_callback=close_callback)
655 654
656 def logging_forest(self, tile_pos):
657 if self.tv.get(tile_pos) != self.WOODLAND:
658 return
659 if self.cash < constants.LOGGING_PRICE:
660 return
661 self.add_cash(-constants.LOGGING_PRICE)
662 self.tv.set(tile_pos, self.GRASSLAND)
663
664 def buy_building(self, tile_pos, building_cls): 655 def buy_building(self, tile_pos, building_cls):
665 building = building_cls(tile_pos) 656 building = building_cls(tile_pos)
666 if self.cash < building.buy_price(): 657 if self.cash < building.buy_price():
667 return 658 return
668 if any(building.covers((chicken.pos.x, chicken.pos.y)) for chicken in self.chickens): 659 if any(building.covers((chicken.pos.x, chicken.pos.y)) for chicken in self.chickens):
841 if not self._pos_cache[cache_type]: 832 if not self._pos_cache[cache_type]:
842 return None # We don't maintain the cache during the day 833 return None # We don't maintain the cache during the day
843 if self.in_bounds(pos): 834 if self.in_bounds(pos):
844 return self._pos_cache[cache_type][pos.x][pos.y][pos.z] 835 return self._pos_cache[cache_type][pos.x][pos.y][pos.z]
845 return None 836 return None
837
838 def chickens_scatter(self):
839 """Chickens outside move around randomly a bit"""
840 for chicken in [chick for chick in self.chickens if chick.outside()]:
841 old_pos = chicken.pos
842 chicken.move(self)
843 if chicken.pos != old_pos:
844 self._update_pos_cache(old_pos, chicken, 'chicken')
845
846 def chickens_chop_wood(self):
847 """Chickens with axes chop down trees near them"""
848 for chicken in [chick for chick in self.chickens if chick.outside()]:
849 chicken.chop(self)
846 850
847 def foxes_move(self): 851 def foxes_move(self):
848 over = True 852 over = True
849 for fox in self.foxes: 853 for fox in self.foxes:
850 old_pos = fox.pos 854 old_pos = fox.pos
928 self.tv.sprites.remove(building, layer='buildings') 932 self.tv.sprites.remove(building, layer='buildings')
929 933
930 def add_cash(self, amount): 934 def add_cash(self, amount):
931 self.cash += amount 935 self.cash += amount
932 self.toolbar.update_cash_counter(self.cash) 936 self.toolbar.update_cash_counter(self.cash)
937
938 def add_wood(self, planks):
939 self.wood += planks
940 self.toolbar.update_wood_counter(self.wood)
933 941
934 def add_start_chickens(self, _map, tile, value): 942 def add_start_chickens(self, _map, tile, value):
935 """Add chickens as specified by the code layer""" 943 """Add chickens as specified by the code layer"""
936 chick = animal.Chicken((tile.tx, tile.ty)) 944 chick = animal.Chicken((tile.tx, tile.ty))
937 for equip_cls in equipment.EQUIP_MAP[value]: 945 for equip_cls in equipment.EQUIP_MAP[value]: