annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
1 """Classes for various buildings in the game."""
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
2
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
3 from pgu.vid import Sprite
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
4
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
5 import imagecache
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
6 import tiles
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
7
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
8 class Place(object):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
9 """Space within a building that can be occupied."""
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
10
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
11 def __init__(self, building, offset):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
12 self.occupant = None
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
13 self.building = building
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
14 self.offset = offset
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
15
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
16 def set_occupant(self, occupant):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
17 self.clear_occupant()
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
18 self.occupant = occupant
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
19 self.occupant.abode = self
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
20
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
21 def clear_occupant(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
22 if self.occupant is not None:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
23 self.occupant.abode = None
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
24 self.occupant = None
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
25
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
26 def get_pos(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
27 bpos = self.building.pos
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
28 return (bpos[0] + self.offset[0], bpos[1] + self.offset[1])
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
29
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
30 class Floor(object):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
31 """A set of places within a building. Places on a
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
32 floor are organised into rows and columns.
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
33 """
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
34
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
35 def __init__(self, title, places):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
36 self.title = title # str
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
37 self.places = places # list of lists of places
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
38
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
39 def rows(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
40 for row in self.places:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
41 yield row
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
42
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
43 def width(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
44 return max(len(row) for row in self.places)
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
45
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
46 class BuildingFullError(Exception):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
47 pass
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
48
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
49 class Building(Sprite):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
50 """Base class for buildings"""
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
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
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
64
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
65 self._floors = []
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
66 for f in range(self.FLOORS):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
67 places = []
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
68 for i in range(self.size[0]):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
69 row = []
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
70 for j in range(self.size[1]):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
71 row.append(Place(self, (i, j)))
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
72 places.append(row)
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
73 floor = Floor("Floor %s" % (f+1,), places)
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
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
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
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
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
89
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
90 def loop(self, tv, _sprite):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
91 ppos = tv.tile_to_view(self.pos)
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
92 self.rect.x = ppos[0]
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
93 self.rect.y = ppos[1]
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
94
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
95 def move(self, state):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
96 """Given the game state, return a new position for the object"""
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
97 # Default is not to move
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
98 return self.pos
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
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
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
140 def floors(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
141 return self._floors
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
142
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
143 def places(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
144 for floor in self._floors:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
145 for row in floor.rows():
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
146 for place in row:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
147 yield place
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
148
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
149 def max_floor_width(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
150 return max(floor.width() for floor in self._floors)
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
151
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
152 def first_empty_place(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
153 for place in self.places():
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
154 if place.occupant is None:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
155 return place
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
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
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
159 place = self.first_empty_place()
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
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
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
162 def occupants(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
163 for place in self.places():
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
164 if place.occupant is not None:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
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
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
167 class HenHouse(Building):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
168 """A HenHouse."""
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
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
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
176 FLOORS = 1
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
177
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
178 class DoubleStoryHenHouse(HenHouse):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
179 """A double story hen house."""
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
180 BUY_PRICE = 300
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
181 SELL_PRICE = 150
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
182 NAME = 'Hendominium'
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
183 FLOORS = 2
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
184
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
185 class GuardTower(Building):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
186 """A GuardTower."""
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
187
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
188 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
189 BUY_PRICE = 200
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
190 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
191 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
192 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
193 NAME = 'Watch Tower'
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
194 FLOORS = 1
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
195
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
196 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
197 """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
198 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
199
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 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
201 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
202 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
203 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
204 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
205 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
206 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
207 pass