Mercurial > rinkhals
comparison gamelib/buildings.py @ 378:71f5897ac5ef
Fences are now buildings, with appropriate (but ugly) UI changes.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 24 Oct 2009 19:08:54 +0000 |
parents | 47c411d20b00 |
children | 2d0ff46118e2 |
comparison
equal
deleted
inserted
replaced
377:0dc9d17c689e | 378:71f5897ac5ef |
---|---|
62 | 62 |
63 IS_BUILDING = True | 63 IS_BUILDING = True |
64 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland'] | 64 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland'] |
65 MODIFY_KNIFE_RANGE = lambda s, x: 0 | 65 MODIFY_KNIFE_RANGE = lambda s, x: 0 |
66 MODIFY_GUN_RANGE = lambda s, x: -1 | 66 MODIFY_GUN_RANGE = lambda s, x: -1 |
67 BREAKABLE = False | |
68 ABODE = False | |
67 | 69 |
68 def __init__(self, pos): | 70 def __init__(self, pos): |
69 """Initial image, tile vid position, size and tile number for building.""" | 71 """Initial image, tile vid position, size and tile number for building.""" |
70 self.day_image = imagecache.load_image(self.IMAGE) | 72 self._set_images() |
71 self.night_image = imagecache.load_image(self.IMAGE, ('night',)) | |
72 self.selected_image = imagecache.load_image(self.SELECTED_IMAGE) | |
73 self.pos = pos | 73 self.pos = pos |
74 self.size = self.SIZE | 74 self.size = self.SIZE |
75 self.tile_no = self.TILE_NO | 75 self.tile_no = self.TILE_NO |
76 self._buy_price = self.BUY_PRICE | 76 self._buy_price = self.BUY_PRICE |
77 self._sell_price = self.SELL_PRICE | 77 self._sell_price = self.SELL_PRICE |
78 self._repair_price = getattr(self, 'REPAIR_PRICE', None) | |
78 self._sun_on = True | 79 self._sun_on = True |
79 self._font = pygame.font.SysFont('Vera', 30, bold=True) | 80 self._font = pygame.font.SysFont('Vera', 30, bold=True) |
80 self._font_image = pygame.Surface(self.day_image.get_size(), flags=SRCALPHA) | 81 self._font_image = pygame.Surface(self.images['fixed']['day'].get_size(), flags=SRCALPHA) |
81 self._font_image.fill((0, 0, 0, 0)) | 82 self._font_image.fill((0, 0, 0, 0)) |
83 self._broken = False | |
82 | 84 |
83 self._floors = [] | 85 self._floors = [] |
84 for f in range(self.FLOORS): | 86 if self.FLOORS: |
85 places = [] | 87 for f in range(self.FLOORS): |
86 for j in range(self.size[1]): | 88 places = [] |
87 row = [] | 89 for j in range(self.size[1]): |
88 for i in range(self.size[0]): | 90 row = [] |
89 row.append(Place(self, (i, j))) | 91 for i in range(self.size[0]): |
90 places.append(row) | 92 row.append(Place(self, (i, j))) |
91 floor = Floor("Floor %s" % (f+1,), places) | 93 places.append(row) |
92 self._floors.append(floor) | 94 floor = Floor("Floor %s" % (f+1,), places) |
93 | 95 self._floors.append(floor) |
94 # 0: the main iamge | 96 |
97 # 0: the main image | |
95 # 1: above, -1: below | 98 # 1: above, -1: below |
96 self.draw_stack = {"main": (0, self.day_image)} | 99 self.draw_stack = {"main": (0, self.images['fixed']['day'])} |
97 | 100 |
98 # Create the building somewhere far off screen | 101 # Create the building somewhere far off screen |
99 Sprite.__init__(self, self.day_image, (-1000, -1000)) | 102 Sprite.__init__(self, self.images['fixed']['day'], (-1000, -1000)) |
103 | |
104 def _set_images(self): | |
105 self.images = {'fixed': { | |
106 'day': imagecache.load_image(self.IMAGE), | |
107 'night': imagecache.load_image(self.IMAGE, ('night',)), | |
108 'selected': imagecache.load_image(self.SELECTED_IMAGE), | |
109 }} | |
110 if self.BREAKABLE: | |
111 self.images['broken'] = { | |
112 'day': imagecache.load_image(self.IMAGE_BROKEN), | |
113 'night': imagecache.load_image(self.IMAGE_BROKEN, ('night',)), | |
114 'selected': imagecache.load_image(self.SELECTED_IMAGE_BROKEN), | |
115 } | |
116 | |
117 def _set_main_image(self): | |
118 image_set = self.images[{True: 'broken',False: 'fixed'}[self._broken]] | |
119 self._replace_main(image_set[{True: 'day', False: 'night'}[self._sun_on]]) | |
100 | 120 |
101 def _redraw(self): | 121 def _redraw(self): |
102 items = self.draw_stack.values() | 122 items = self.draw_stack.values() |
103 items.sort(key=lambda x: x[0]) | 123 items.sort(key=lambda x: x[0]) |
104 image = items.pop(0)[1].copy() | 124 image = items.pop(0)[1].copy() |
166 xpos, ypos = self.pos | 186 xpos, ypos = self.pos |
167 xsize, ysize = self.size | 187 xsize, ysize = self.size |
168 return (xpos <= tile_pos[0] < xpos + xsize) and \ | 188 return (xpos <= tile_pos[0] < xpos + xsize) and \ |
169 (ypos <= tile_pos[1] < ypos + ysize) | 189 (ypos <= tile_pos[1] < ypos + ysize) |
170 | 190 |
191 def broken(self): | |
192 return self._broken | |
193 | |
194 def damage(self, tv): | |
195 if not self.BREAKABLE: | |
196 return False | |
197 self._broken = True | |
198 self._sell_price = self.SELL_PRICE_BROKEN | |
199 self.tile_no = self.TILE_NO_BROKEN | |
200 for tile_pos in self.tile_positions(): | |
201 tv.set(tile_pos, self.tile_no) | |
202 self._set_main_image() | |
203 | |
204 def repair(self, tv): | |
205 self._broken = False | |
206 self._sell_price = self.SELL_PRICE | |
207 self.tile_no = self.TILE_NO | |
208 for tile_pos in self.tile_positions(): | |
209 tv.set(tile_pos, self.tile_no) | |
210 self._set_main_image() | |
211 | |
171 def remove(self, tv): | 212 def remove(self, tv): |
172 """Remove the building from its current position.""" | 213 """Remove the building from its current position.""" |
173 # remove tile | 214 # remove tile |
174 for tile_pos in self.tile_positions(): | 215 for tile_pos in self.tile_positions(): |
175 tv.set(tile_pos, self.GRASSLAND) | 216 tv.set(tile_pos, self.GRASSLAND) |
178 return self._buy_price | 219 return self._buy_price |
179 | 220 |
180 def sell_price(self): | 221 def sell_price(self): |
181 return self._sell_price | 222 return self._sell_price |
182 | 223 |
224 def repair_price(self): | |
225 return self._repair_price | |
226 | |
183 def selected(self, selected): | 227 def selected(self, selected): |
184 if selected: | 228 if selected: |
185 self._replace_main(self.selected_image) | 229 self._replace_main(self.images[{True: 'broken',False: 'fixed'}[self._broken]]['selected']) |
186 else: | 230 else: |
187 self.sun(self._sun_on) | 231 self._set_main_image() |
188 | 232 |
189 def sun(self, sun_on): | 233 def sun(self, sun_on): |
190 self._sun_on = sun_on | 234 self._sun_on = sun_on |
191 if sun_on: | 235 self._set_main_image() |
192 self._replace_main(self.day_image) | |
193 else: | |
194 self._replace_main(self.night_image) | |
195 | 236 |
196 def update_occupant_count(self): | 237 def update_occupant_count(self): |
197 count = len(list(self.occupants())) | 238 count = len(list(self.occupants())) |
198 if count == 0: | 239 if count == 0: |
199 if "count" in self.draw_stack: | 240 if "count" in self.draw_stack: |
232 def occupants(self): | 273 def occupants(self): |
233 for place in self.places(): | 274 for place in self.places(): |
234 if place.occupant is not None: | 275 if place.occupant is not None: |
235 yield place.occupant | 276 yield place.occupant |
236 | 277 |
237 class HenHouse(Building): | 278 class Abode(Building): |
279 ABODE = True | |
280 | |
281 class HenHouse(Abode): | |
238 """A HenHouse.""" | 282 """A HenHouse.""" |
239 | 283 |
240 TILE_NO = tiles.REVERSE_TILE_MAP['henhouse'] | 284 TILE_NO = tiles.REVERSE_TILE_MAP['henhouse'] |
241 BUY_PRICE = 100 | 285 BUY_PRICE = 100 |
242 SELL_PRICE = 90 | 286 SELL_PRICE = 90 |
256 IMAGE = 'sprites/hendominium.png' | 300 IMAGE = 'sprites/hendominium.png' |
257 SELECTED_IMAGE = 'sprites/select_hendominium.png' | 301 SELECTED_IMAGE = 'sprites/select_hendominium.png' |
258 NAME = 'Hendominium' | 302 NAME = 'Hendominium' |
259 FLOORS = 2 | 303 FLOORS = 2 |
260 | 304 |
261 class GuardTower(Building): | 305 class GuardTower(Abode): |
262 """A GuardTower.""" | 306 """A GuardTower.""" |
263 | 307 |
264 TILE_NO = tiles.REVERSE_TILE_MAP['guardtower'] | 308 TILE_NO = tiles.REVERSE_TILE_MAP['guardtower'] |
265 BUY_PRICE = 200 | 309 BUY_PRICE = 200 |
266 SELL_PRICE = 150 | 310 SELL_PRICE = 150 |
274 MODIFY_GUN_BASE_HIT = lambda s, x: x-5 | 318 MODIFY_GUN_BASE_HIT = lambda s, x: x-5 |
275 MODIFY_GUN_RANGE_PENALTY = lambda s, x: x-1 | 319 MODIFY_GUN_RANGE_PENALTY = lambda s, x: x-1 |
276 MODIFY_VISION_BONUS = lambda s, x: x+10 | 320 MODIFY_VISION_BONUS = lambda s, x: x+10 |
277 MODIFY_VISION_RANGE_PENALTY = lambda s, x: x-2 | 321 MODIFY_VISION_RANGE_PENALTY = lambda s, x: x-2 |
278 | 322 |
323 class Fence(Building): | |
324 """A fence.""" | |
325 | |
326 TILE_NO = tiles.REVERSE_TILE_MAP['fence'] | |
327 TILE_NO_BROKEN = tiles.REVERSE_TILE_MAP['broken fence'] | |
328 BREAKABLE = True | |
329 BUY_PRICE = 50 | |
330 SELL_PRICE = 25 | |
331 REPAIR_PRICE = 25 | |
332 SELL_PRICE_BROKEN = 5 | |
333 SIZE = (1, 1) | |
334 IMAGE = 'tiles/fence.png' | |
335 SELECTED_IMAGE = 'tiles/fence.png' | |
336 IMAGE_BROKEN = 'tiles/broken_fence.png' | |
337 SELECTED_IMAGE_BROKEN = 'tiles/broken_fence.png' | |
338 NAME = 'Fence' | |
339 FLOORS = 0 | |
340 | |
341 | |
279 def is_building(obj): | 342 def is_building(obj): |
280 """Return true if obj is a build class.""" | 343 """Return true if obj is a build class.""" |
281 return getattr(obj, "IS_BUILDING", False) and hasattr(obj, "NAME") | 344 return getattr(obj, "IS_BUILDING", False) and hasattr(obj, "NAME") |
282 | 345 |
283 # Building hens can lay eggs in | 346 # Building hens can lay eggs in |