Mercurial > rinkhals
annotate gamelib/buildings.py @ 136:9acf8e3d9596
Fix orientation of places in buildings.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Wed, 02 Sep 2009 22:14:55 +0000 |
parents | b7dc405a28be |
children | a9b800b4175e |
rev | line source |
---|---|
43 | 1 """Classes for various buildings in the game.""" |
2 | |
3 from pgu.vid import Sprite | |
4 | |
5 import imagecache | |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
6 import tiles |
43 | 7 |
125 | 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 | |
48 | |
43 | 49 class Building(Sprite): |
50 """Base class for buildings""" | |
51 | |
109
48019afde338
Equipment purchasing and some toolbar tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
108
diff
changeset
|
52 IS_BUILDING = True |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
53 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland'] |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
54 |
64
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
55 def __init__(self, pos): |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
56 """Initial image, tile vid position, size and tile number for building.""" |
64
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
57 self.day_image = imagecache.load_image(self.IMAGE) |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
58 self.night_image = imagecache.load_image(self.IMAGE, ('night',)) |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
59 self.pos = pos |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
60 self.size = self.SIZE |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
61 self.tile_no = self.TILE_NO |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
62 self._buy_price = self.BUY_PRICE |
65
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
63 self._sell_price = self.SELL_PRICE |
125 | 64 |
65 self._floors = [] | |
66 for f in range(self.FLOORS): | |
67 places = [] | |
136
9acf8e3d9596
Fix orientation of places in buildings.
Simon Cross <hodgestar@gmail.com>
parents:
133
diff
changeset
|
68 for j in range(self.size[1]): |
125 | 69 row = [] |
136
9acf8e3d9596
Fix orientation of places in buildings.
Simon Cross <hodgestar@gmail.com>
parents:
133
diff
changeset
|
70 for i in range(self.size[0]): |
125 | 71 row.append(Place(self, (i, j))) |
72 places.append(row) | |
73 floor = Floor("Floor %s" % (f+1,), places) | |
74 self._floors.append(floor) | |
64
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
75 |
43 | 76 # Create the building somewhere far off screen |
64
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
77 Sprite.__init__(self, self.day_image, (-1000, -1000)) |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
78 |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
79 def tile_positions(self): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
80 """Return pairs of (x, y) tile positions for each of the tile positions |
64
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
81 occupied by the building. |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
82 """ |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
83 xpos, ypos = self.pos |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
84 xsize, ysize = self.size |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
85 |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
86 for dx in xrange(xsize): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
87 for dy in xrange(ysize): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
88 yield (xpos + dx, ypos + dy) |
43 | 89 |
90 def loop(self, tv, _sprite): | |
91 ppos = tv.tile_to_view(self.pos) | |
92 self.rect.x = ppos[0] | |
93 self.rect.y = ppos[1] | |
94 | |
95 def move(self, state): | |
96 """Given the game state, return a new position for the object""" | |
97 # Default is not to move | |
98 return self.pos | |
99 | |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
100 def place(self, tv): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
101 """Check that the building can be placed at its current position |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
102 and place it if possible. |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
103 """ |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
104 # check that all spaces under the structure are grassland |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
105 for tile_pos in self.tile_positions(): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
106 if not tv.get(tile_pos) == self.GRASSLAND: |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
107 return False |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
108 |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
109 # place tile |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
110 for tile_pos in self.tile_positions(): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
111 tv.set(tile_pos, self.tile_no) |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
112 |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
113 return True |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
114 |
65
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
115 def covers(self, tile_pos): |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
116 """Return True if build covers tile_pos, False otherwise.""" |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
117 xpos, ypos = self.pos |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
118 xsize, ysize = self.size |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
119 return (xpos <= tile_pos[0] < xpos + xsize) and \ |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
120 (ypos <= tile_pos[1] < ypos + ysize) |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
121 |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
122 def remove(self, tv): |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
123 """Remove the building from its current position.""" |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
124 # remove tile |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
125 for tile_pos in self.tile_positions(): |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
126 tv.set(tile_pos, self.GRASSLAND) |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
127 |
64
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
128 def buy_price(self): |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
129 return self._buy_price |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
130 |
65
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
131 def sell_price(self): |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
132 return self._sell_price |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
133 |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
134 def sun(self, sun_on): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
135 if sun_on: |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
136 self.setimage(self.day_image) |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
137 else: |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
138 self.setimage(self.night_image) |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
139 |
125 | 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() | |
108
437cbd856a03
Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents:
65
diff
changeset
|
157 |
437cbd856a03
Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents:
65
diff
changeset
|
158 def add_occupant(self, occupant): |
125 | 159 place = self.first_empty_place() |
160 place.set_occupant(occupant) | |
108
437cbd856a03
Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents:
65
diff
changeset
|
161 |
125 | 162 def occupants(self): |
163 for place in self.places(): | |
164 if place.occupant is not None: | |
165 yield place.occupant | |
64
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
166 |
43 | 167 class HenHouse(Building): |
168 """A HenHouse.""" | |
169 | |
64
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
170 TILE_NO = tiles.REVERSE_TILE_MAP['henhouse'] |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
171 BUY_PRICE = 100 |
65
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
172 SELL_PRICE = 90 |
64
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
173 SIZE = (3, 2) |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
174 IMAGE = 'sprites/henhouse.png' |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
175 NAME = 'Hen House' |
125 | 176 FLOORS = 1 |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
177 |
125 | 178 class DoubleStoryHenHouse(HenHouse): |
179 """A double story hen house.""" | |
133
b7dc405a28be
Add Hendominium image support.
Simon Cross <hodgestar@gmail.com>
parents:
125
diff
changeset
|
180 TILE_NO = tiles.REVERSE_TILE_MAP['hendominium'] |
125 | 181 BUY_PRICE = 300 |
182 SELL_PRICE = 150 | |
133
b7dc405a28be
Add Hendominium image support.
Simon Cross <hodgestar@gmail.com>
parents:
125
diff
changeset
|
183 SIZE = (2, 3) |
b7dc405a28be
Add Hendominium image support.
Simon Cross <hodgestar@gmail.com>
parents:
125
diff
changeset
|
184 IMAGE = 'sprites/hendominium.png' |
125 | 185 NAME = 'Hendominium' |
186 FLOORS = 2 | |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
187 |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
188 class GuardTower(Building): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
189 """A GuardTower.""" |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
190 |
64
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
191 TILE_NO = tiles.REVERSE_TILE_MAP['guardtower'] |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
192 BUY_PRICE = 200 |
65
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
193 SELL_PRICE = 150 |
64
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
194 SIZE = (2, 2) |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
195 IMAGE = 'sprites/watchtower.png' |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
196 NAME = 'Watch Tower' |
125 | 197 FLOORS = 1 |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
198 |
64
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
199 def is_building(obj): |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
200 """Return true if obj is a build class.""" |
109
48019afde338
Equipment purchasing and some toolbar tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
108
diff
changeset
|
201 return getattr(obj, "IS_BUILDING", False) and hasattr(obj, "NAME") |
64
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
202 |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
203 BUILDINGS = [] |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
204 for name in dir(): |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
205 obj = eval(name) |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
206 try: |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
207 if is_building(obj): |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
208 BUILDINGS.append(obj) |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
209 except TypeError: |
99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents:
63
diff
changeset
|
210 pass |