comparison gamelib/buildings.py @ 125:2e3a05b9594d

Chickens in buildings\!
author Simon Cross <hodgestar@gmail.com>
date Wed, 02 Sep 2009 21:28:04 +0000
parents 48019afde338
children b7dc405a28be
comparison
equal deleted inserted replaced
124:69fd96eafde8 125:2e3a05b9594d
2 2
3 from pgu.vid import Sprite 3 from pgu.vid import Sprite
4 4
5 import imagecache 5 import imagecache
6 import tiles 6 import tiles
7
8 class Place(object):
9 """Space within a building that can be occupied."""
10
11 def __init__(self, building, offset):
12 self.occupant = None
13 self.building = building
14 self.offset = offset
15
16 def set_occupant(self, occupant):
17 self.clear_occupant()
18 self.occupant = occupant
19 self.occupant.abode = self
20
21 def clear_occupant(self):
22 if self.occupant is not None:
23 self.occupant.abode = None
24 self.occupant = None
25
26 def get_pos(self):
27 bpos = self.building.pos
28 return (bpos[0] + self.offset[0], bpos[1] + self.offset[1])
29
30 class Floor(object):
31 """A set of places within a building. Places on a
32 floor are organised into rows and columns.
33 """
34
35 def __init__(self, title, places):
36 self.title = title # str
37 self.places = places # list of lists of places
38
39 def rows(self):
40 for row in self.places:
41 yield row
42
43 def width(self):
44 return max(len(row) for row in self.places)
45
46 class BuildingFullError(Exception):
47 pass
7 48
8 class Building(Sprite): 49 class Building(Sprite):
9 """Base class for buildings""" 50 """Base class for buildings"""
10 51
11 IS_BUILDING = True 52 IS_BUILDING = True
18 self.pos = pos 59 self.pos = pos
19 self.size = self.SIZE 60 self.size = self.SIZE
20 self.tile_no = self.TILE_NO 61 self.tile_no = self.TILE_NO
21 self._buy_price = self.BUY_PRICE 62 self._buy_price = self.BUY_PRICE
22 self._sell_price = self.SELL_PRICE 63 self._sell_price = self.SELL_PRICE
23 self._occupants = set() 64
65 self._floors = []
66 for f in range(self.FLOORS):
67 places = []
68 for i in range(self.size[0]):
69 row = []
70 for j in range(self.size[1]):
71 row.append(Place(self, (i, j)))
72 places.append(row)
73 floor = Floor("Floor %s" % (f+1,), places)
74 self._floors.append(floor)
24 75
25 # Create the building somewhere far off screen 76 # Create the building somewhere far off screen
26 Sprite.__init__(self, self.day_image, (-1000, -1000)) 77 Sprite.__init__(self, self.day_image, (-1000, -1000))
27 78
28 def tile_positions(self): 79 def tile_positions(self):
84 if sun_on: 135 if sun_on:
85 self.setimage(self.day_image) 136 self.setimage(self.day_image)
86 else: 137 else:
87 self.setimage(self.night_image) 138 self.setimage(self.night_image)
88 139
140 def floors(self):
141 return self._floors
142
143 def places(self):
144 for floor in self._floors:
145 for row in floor.rows():
146 for place in row:
147 yield place
148
149 def max_floor_width(self):
150 return max(floor.width() for floor in self._floors)
151
152 def first_empty_place(self):
153 for place in self.places():
154 if place.occupant is None:
155 return place
156 raise BuildingFullError()
157
158 def add_occupant(self, occupant):
159 place = self.first_empty_place()
160 place.set_occupant(occupant)
161
89 def occupants(self): 162 def occupants(self):
90 """Return list of buildings occupants.""" 163 for place in self.places():
91 return list(self._occupants) 164 if place.occupant is not None:
92 165 yield place.occupant
93 def add_occupant(self, occupant):
94 if occupant.abode is not None:
95 occupant.abode.remove_occupant(occupant)
96 occupant.abode = self
97 self._occupants.add(occupant)
98
99 def remove_occupant(self, occupant):
100 if occupant in self._occupants:
101 self._occupants.remove(occupant)
102 occupant.abode = None
103 166
104 class HenHouse(Building): 167 class HenHouse(Building):
105 """A HenHouse.""" 168 """A HenHouse."""
106 169
107 TILE_NO = tiles.REVERSE_TILE_MAP['henhouse'] 170 TILE_NO = tiles.REVERSE_TILE_MAP['henhouse']
108 BUY_PRICE = 100 171 BUY_PRICE = 100
109 SELL_PRICE = 90 172 SELL_PRICE = 90
110 SIZE = (3, 2) 173 SIZE = (3, 2)
111 IMAGE = 'sprites/henhouse.png' 174 IMAGE = 'sprites/henhouse.png'
112 NAME = 'Hen House' 175 NAME = 'Hen House'
113 176 FLOORS = 1
177
178 class DoubleStoryHenHouse(HenHouse):
179 """A double story hen house."""
180 BUY_PRICE = 300
181 SELL_PRICE = 150
182 NAME = 'Hendominium'
183 FLOORS = 2
114 184
115 class GuardTower(Building): 185 class GuardTower(Building):
116 """A GuardTower.""" 186 """A GuardTower."""
117 187
118 TILE_NO = tiles.REVERSE_TILE_MAP['guardtower'] 188 TILE_NO = tiles.REVERSE_TILE_MAP['guardtower']
119 BUY_PRICE = 200 189 BUY_PRICE = 200
120 SELL_PRICE = 150 190 SELL_PRICE = 150
121 SIZE = (2, 2) 191 SIZE = (2, 2)
122 IMAGE = 'sprites/watchtower.png' 192 IMAGE = 'sprites/watchtower.png'
123 NAME = 'Watch Tower' 193 NAME = 'Watch Tower'
194 FLOORS = 1
124 195
125 def is_building(obj): 196 def is_building(obj):
126 """Return true if obj is a build class.""" 197 """Return true if obj is a build class."""
127 return getattr(obj, "IS_BUILDING", False) and hasattr(obj, "NAME") 198 return getattr(obj, "IS_BUILDING", False) and hasattr(obj, "NAME")
128 199