comparison gamelib/buildings.py @ 397:532f1ea476ff

Make foxes enter buildings, with a seperate count for them
author Neil Muller <drnlmuller@gmail.com>
date Fri, 13 Nov 2009 14:14:57 +0000
parents 2d0ff46118e2
children 9096c237928c
comparison
equal deleted inserted replaced
396:19e583e5cdc0 397:532f1ea476ff
18 def __init__(self, building, offset): 18 def __init__(self, building, offset):
19 self.occupant = None 19 self.occupant = None
20 self.building = building 20 self.building = building
21 self.offset = offset 21 self.offset = offset
22 22
23 def set_occupant(self, occupant, _update=True): 23 def set_occupant(self, occupant, _update=True, _predator=False):
24 self.clear_occupant(_update=False) 24 self.clear_occupant(_update=False)
25 self.occupant = occupant 25 self.occupant = occupant
26 self.occupant.abode = self 26 self.occupant.abode = self
27 if _update: 27 if _update:
28 self.building.update_occupant_count() 28 self.building.update_occupant_count()
81 self._sun_on = True 81 self._sun_on = True
82 self._font = pygame.font.SysFont('Vera', 30, bold=True) 82 self._font = pygame.font.SysFont('Vera', 30, bold=True)
83 self._font_image = pygame.Surface(self.images['fixed']['day'].get_size(), flags=SRCALPHA) 83 self._font_image = pygame.Surface(self.images['fixed']['day'].get_size(), flags=SRCALPHA)
84 self._font_image.fill((0, 0, 0, 0)) 84 self._font_image.fill((0, 0, 0, 0))
85 self._broken = False 85 self._broken = False
86 self._predators = []
86 87
87 self._floors = [] 88 self._floors = []
88 if self.FLOORS: 89 if self.FLOORS:
89 for f, z in enumerate(self.FLOORS): 90 for f, z in enumerate(self.FLOORS):
90 places = [] 91 places = []
236 self._sun_on = sun_on 237 self._sun_on = sun_on
237 self._set_main_image() 238 self._set_main_image()
238 239
239 def update_occupant_count(self): 240 def update_occupant_count(self):
240 count = len(list(self.occupants())) 241 count = len(list(self.occupants()))
241 if count == 0: 242 if count == 0 and not self._predators:
242 if "count" in self.draw_stack: 243 if "count" in self.draw_stack:
243 del self.draw_stack["count"] 244 del self.draw_stack["count"]
244 else: 245 else:
246 # Render chicken count
245 image = self._font_image.copy() 247 image = self._font_image.copy()
246 text = self._font.render(str(count), True, constants.FG_COLOR)
247 w, h = image.get_size() 248 w, h = image.get_size()
248 x, y = text.get_size() 249 if count:
249 image.blit(text, (w - x, h - y)) 250 text = self._font.render(str(count), True,
251 constants.FG_COLOR)
252 # Blit to the right
253 x, y = text.get_size()
254 image.blit(text, (w - x, h - y))
255 # Render predator count
256 if self._predators:
257 text = self._font.render(str(len(self._predators)), True,
258 constants.PREDATOR_COUNT_COLOR)
259 # Blit to the left
260 x, y = text.get_size()
261 image.blit(text, (0, h - y))
250 self.draw_stack["count"] = (1, image) 262 self.draw_stack["count"] = (1, image)
263
251 self._redraw() 264 self._redraw()
252 265
253 def floors(self): 266 def floors(self):
254 return self._floors 267 return self._floors
255 268
274 287
275 def occupants(self): 288 def occupants(self):
276 for place in self.places(): 289 for place in self.places():
277 if place.occupant is not None: 290 if place.occupant is not None:
278 yield place.occupant 291 yield place.occupant
292
293 def add_predator(self, animal):
294 animal.building = self
295 self._predators.append(animal)
296 self.update_occupant_count()
297
298 def remove_predator(self, animal):
299 if animal in self._predators:
300 self._predators.remove(animal)
301 animal.building = None
302 self.update_occupant_count()
279 303
280 class Abode(Building): 304 class Abode(Building):
281 ABODE = True 305 ABODE = True
282 306
283 class HenHouse(Abode): 307 class HenHouse(Abode):