Mercurial > rinkhals
comparison gamelib/gameboard.py @ 105:7910b4e01dba
Add chicken moving tool and start of animal placement.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Wed, 02 Sep 2009 18:10:52 +0000 |
parents | 725b292ca07b |
children | 437cbd856a03 |
comparison
equal
deleted
inserted
replaced
104:d17375586866 | 105:7910b4e01dba |
---|---|
34 | 34 |
35 self.add_counter(None, self.cash_counter) | 35 self.add_counter(None, self.cash_counter) |
36 self.add_counter(icons.CHKN_ICON, self.chicken_counter) | 36 self.add_counter(icons.CHKN_ICON, self.chicken_counter) |
37 self.add_counter(icons.KILLED_FOX, self.killed_foxes) | 37 self.add_counter(icons.KILLED_FOX, self.killed_foxes) |
38 | 38 |
39 self.add_tool_button("Move Animals", constants.TOOL_PLACE_ANIMALS) | |
39 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN) | 40 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN) |
40 self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG) | 41 self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG) |
41 self.add_tool_button("Sell building", constants.TOOL_SELL_BUILDING) | 42 self.add_tool_button("Sell building", constants.TOOL_SELL_BUILDING) |
42 self.add_tool_button("Buy fence", constants.TOOL_BUY_FENCE) | 43 self.add_tool_button("Buy fence", constants.TOOL_BUY_FENCE) |
43 for building_cls in buildings.BUILDINGS: | 44 for building_cls in buildings.BUILDINGS: |
125 self.tv.png_folder_load_tiles(data.filepath('tiles')) | 126 self.tv.png_folder_load_tiles(data.filepath('tiles')) |
126 self.tv.tga_load_level(data.filepath('level1.tga')) | 127 self.tv.tga_load_level(data.filepath('level1.tga')) |
127 self.create_disp() | 128 self.create_disp() |
128 | 129 |
129 self.selected_tool = None | 130 self.selected_tool = None |
131 self.animal_to_place = None | |
130 self.chickens = [] | 132 self.chickens = [] |
131 self.foxes = [] | 133 self.foxes = [] |
132 self.buildings = [] | 134 self.buildings = [] |
133 self.cash = 0 | 135 self.cash = 0 |
134 self.killed_foxes = 0 | 136 self.killed_foxes = 0 |
157 def loop(self): | 159 def loop(self): |
158 self.tv.loop() | 160 self.tv.loop() |
159 | 161 |
160 def set_selected_tool(self, tool): | 162 def set_selected_tool(self, tool): |
161 self.selected_tool = tool | 163 self.selected_tool = tool |
164 self.animal_to_place = None | |
162 | 165 |
163 def in_bounds(self, pos): | 166 def in_bounds(self, pos): |
164 """Check if a position is within the game boundaries""" | 167 """Check if a position is within the game boundaries""" |
165 if pos.x < 0 or pos.y < 0: | 168 if pos.x < 0 or pos.y < 0: |
166 return False | 169 return False |
169 return False | 172 return False |
170 return True | 173 return True |
171 | 174 |
172 def use_tool(self, e): | 175 def use_tool(self, e): |
173 if self.selected_tool == constants.TOOL_SELL_CHICKEN: | 176 if self.selected_tool == constants.TOOL_SELL_CHICKEN: |
174 self.sell_chicken(e.pos) | 177 self.sell_chicken(self.tv.screen_to_tile(e.pos)) |
175 elif self.selected_tool == constants.TOOL_SELL_EGG: | 178 elif self.selected_tool == constants.TOOL_SELL_EGG: |
176 pass | 179 pass |
180 elif self.selected_tool == constants.TOOL_PLACE_ANIMALS: | |
181 self.place_animal(self.tv.screen_to_tile(e.pos)) | |
177 elif self.selected_tool == constants.TOOL_BUY_FENCE: | 182 elif self.selected_tool == constants.TOOL_BUY_FENCE: |
178 self.buy_fence(self.tv.screen_to_tile(e.pos)) | 183 self.buy_fence(self.tv.screen_to_tile(e.pos)) |
179 elif self.selected_tool == constants.TOOL_SELL_BUILDING: | 184 elif self.selected_tool == constants.TOOL_SELL_BUILDING: |
180 self.sell_building(self.tv.screen_to_tile(e.pos)) | 185 self.sell_building(self.tv.screen_to_tile(e.pos)) |
181 elif buildings.is_building(self.selected_tool): | 186 elif buildings.is_building(self.selected_tool): |
182 self.buy_building(self.tv.screen_to_tile(e.pos), self.selected_tool) | 187 self.buy_building(self.tv.screen_to_tile(e.pos), self.selected_tool) |
183 | 188 |
184 def get_chicken(self, pos): | 189 def get_chicken(self, tile_pos): |
185 for chick in self.chickens: | 190 for chick in self.chickens: |
186 if chick.rect.collidepoint(pos): | 191 if chick.covers(tile_pos): |
187 return chick | 192 return chick |
188 return None | 193 return None |
189 | 194 |
190 def sell_chicken(self, pos): | 195 def get_building(self, tile_pos): |
191 chick = self.get_chicken(pos) | 196 for building in self.buildings: |
197 if building.covers(tile_pos): | |
198 return building | |
199 return None | |
200 | |
201 def sell_chicken(self, tile_pos): | |
202 chick = self.get_chicken(tile_pos) | |
192 if chick is None: | 203 if chick is None: |
193 return | 204 return |
194 if len(self.chickens) == 1: | 205 if len(self.chickens) == 1: |
195 print "You can't sell your last chicken!" | 206 print "You can't sell your last chicken!" |
196 return | 207 return |
197 self.add_cash(constants.SELL_PRICE_CHICKEN) | 208 self.add_cash(constants.SELL_PRICE_CHICKEN) |
198 sound.play_sound("sell-chicken.ogg") | 209 sound.play_sound("sell-chicken.ogg") |
199 self.remove_chicken(chick) | 210 self.remove_chicken(chick) |
211 | |
212 def place_animal(self, tile_pos): | |
213 """Handle an TOOL_PLACE_ANIMALS click. | |
214 | |
215 This will either select an animal or | |
216 place a selected animal in a building. | |
217 """ | |
218 chicken = self.get_chicken(tile_pos) | |
219 if chicken: | |
220 self.animal_to_place = chicken | |
221 print "Selected animal %r" % (chicken,) | |
222 return | |
223 building = self.get_building(tile_pos) | |
224 if building: | |
225 if self.animal_to_place is not None: | |
226 self.put_animal_in_building(self.animal_to_place, building) | |
227 else: | |
228 self.select_animal_from_building(building) | |
229 | |
230 def put_animal_in_building(self, animal, building): | |
231 """Place animal in building.""" | |
232 # XXX: unimplemented | |
233 print "Placing %r in %r" % (animal, building) | |
234 | |
235 def select_animal_from_building(self, building): | |
236 """Create dialog for selecting an animal from a building.""" | |
237 # XXX: unimplemented | |
238 print "Selecting animal from building %r" % (building,) | |
200 | 239 |
201 def buy_fence(self, tile_pos): | 240 def buy_fence(self, tile_pos): |
202 this_tile = self.tv.get(tile_pos) | 241 this_tile = self.tv.get(tile_pos) |
203 if this_tile not in [self.GRASSLAND, self.BROKEN_FENCE]: | 242 if this_tile not in [self.GRASSLAND, self.BROKEN_FENCE]: |
204 return | 243 return |
227 self.add_building(building) | 266 self.add_building(building) |
228 | 267 |
229 def sell_building(self, tile_pos): | 268 def sell_building(self, tile_pos): |
230 if self.tv.get(tile_pos) == self.FENCE: | 269 if self.tv.get(tile_pos) == self.FENCE: |
231 return self.sell_fence(tile_pos) | 270 return self.sell_fence(tile_pos) |
232 for building in self.buildings: | 271 building = self.get_building(tile_pos) |
233 if building.covers(tile_pos): | 272 if building is None: |
234 self.add_cash(building.sell_price()) | 273 return |
235 building.remove(self.tv) | 274 self.add_cash(building.sell_price()) |
236 self.remove_building(building) | 275 building.remove(self.tv) |
237 break | 276 self.remove_building(building) |
238 | 277 |
239 def event(self, e): | 278 def event(self, e): |
240 if e.type == KEYDOWN: | 279 if e.type == KEYDOWN: |
241 if e.key == K_UP: | 280 if e.key == K_UP: |
242 self.tvw.move_view(0, -self.TILE_DIMENSIONS[1]) | 281 self.tvw.move_view(0, -self.TILE_DIMENSIONS[1]) |