annotate gamelib/buildings.py @ 198:355eaae40b1f

Buildings now affect weapon range and accuracy.
author Jeremy Thurgood <firxen@gmail.com>
date Fri, 04 Sep 2009 19:34:01 +0000
parents b400991ccce1
children 696936621a93
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']
198
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
54 MODIFY_KNIFE_RANGE = lambda s, x: 0
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
55 MODIFY_GUN_RANGE = lambda s, x: -1
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
56
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 def __init__(self, pos):
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
58 """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
59 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
60 self.night_image = imagecache.load_image(self.IMAGE, ('night',))
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
61 self.selected_image = imagecache.load_image(self.SELECTED_IMAGE)
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
62 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
63 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
64 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
65 self._buy_price = self.BUY_PRICE
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
66 self._sell_price = self.SELL_PRICE
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
67 self._sun_on = True
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
68
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
69 self._floors = []
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
70 for f in range(self.FLOORS):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
71 places = []
136
9acf8e3d9596 Fix orientation of places in buildings.
Simon Cross <hodgestar@gmail.com>
parents: 133
diff changeset
72 for j in range(self.size[1]):
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
73 row = []
136
9acf8e3d9596 Fix orientation of places in buildings.
Simon Cross <hodgestar@gmail.com>
parents: 133
diff changeset
74 for i in range(self.size[0]):
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
75 row.append(Place(self, (i, j)))
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
76 places.append(row)
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
77 floor = Floor("Floor %s" % (f+1,), places)
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
78 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
79
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
80 # 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
81 Sprite.__init__(self, self.day_image, (-1000, -1000))
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
82
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
83 def tile_positions(self):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
84 """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
85 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
86 """
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
87 xpos, ypos = self.pos
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
88 xsize, ysize = self.size
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
89
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
90 for dx in xrange(xsize):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
91 for dy in xrange(ysize):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
92 yield (xpos + dx, ypos + dy)
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
93
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
94 def loop(self, tv, _sprite):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
95 ppos = tv.tile_to_view(self.pos)
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
96 self.rect.x = ppos[0]
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
97 self.rect.y = ppos[1]
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
98
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
99 def move(self, state):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
100 """Given the game state, return a new position for the object"""
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
101 # Default is not to move
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
102 return self.pos
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
103
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
104 def place(self, tv):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
105 """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
106 and place it if possible.
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
107 """
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
108 # check that all spaces under the structure are grassland
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
109 for tile_pos in self.tile_positions():
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
110 if not tv.get(tile_pos) == self.GRASSLAND:
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
111 return False
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 # place tile
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
114 for tile_pos in self.tile_positions():
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
115 tv.set(tile_pos, self.tile_no)
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
116
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
117 return True
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
118
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
119 def covers(self, tile_pos):
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
120 """Return True if build covers tile_pos, False otherwise."""
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
121 xpos, ypos = self.pos
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
122 xsize, ysize = self.size
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
123 return (xpos <= tile_pos[0] < xpos + xsize) and \
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
124 (ypos <= tile_pos[1] < ypos + ysize)
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
125
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
126 def remove(self, tv):
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
127 """Remove the building from its current position."""
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
128 # remove tile
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
129 for tile_pos in self.tile_positions():
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
130 tv.set(tile_pos, self.GRASSLAND)
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
131
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
132 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
133 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
134
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
135 def sell_price(self):
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
136 return self._sell_price
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
137
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
138 def selected(self, selected):
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
139 if selected:
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
140 self.setimage(self.selected_image)
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
141 else:
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
142 self.sun(self._sun_on)
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
143
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
144 def sun(self, sun_on):
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
145 self._sun_on = sun_on
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
146 if sun_on:
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
147 self.setimage(self.day_image)
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
148 else:
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
149 self.setimage(self.night_image)
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
150
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
151 def floors(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
152 return self._floors
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
153
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
154 def places(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
155 for floor in self._floors:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
156 for row in floor.rows():
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
157 for place in row:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
158 yield place
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
159
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
160 def max_floor_width(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
161 return max(floor.width() for floor in self._floors)
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
162
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
163 def first_empty_place(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
164 for place in self.places():
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
165 if place.occupant is None:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
166 return place
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
167 raise BuildingFullError()
108
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
168
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
169 def add_occupant(self, occupant):
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
170 place = self.first_empty_place()
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
171 place.set_occupant(occupant)
108
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
172
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
173 def occupants(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
174 for place in self.places():
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
175 if place.occupant is not None:
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
176 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
177
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
178 class HenHouse(Building):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
179 """A HenHouse."""
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
180
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
181 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
182 BUY_PRICE = 100
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
183 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
184 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
185 IMAGE = 'sprites/henhouse.png'
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
186 SELECTED_IMAGE = 'sprites/select_henhouse.png'
177
b400991ccce1 pedantic building renaming
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 170
diff changeset
187 NAME = 'Henhouse'
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
188 FLOORS = 1
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
189
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
190 class DoubleStoryHenHouse(HenHouse):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
191 """A double story hen house."""
198
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
192
133
b7dc405a28be Add Hendominium image support.
Simon Cross <hodgestar@gmail.com>
parents: 125
diff changeset
193 TILE_NO = tiles.REVERSE_TILE_MAP['hendominium']
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
194 BUY_PRICE = 300
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
195 SELL_PRICE = 150
133
b7dc405a28be Add Hendominium image support.
Simon Cross <hodgestar@gmail.com>
parents: 125
diff changeset
196 SIZE = (2, 3)
b7dc405a28be Add Hendominium image support.
Simon Cross <hodgestar@gmail.com>
parents: 125
diff changeset
197 IMAGE = 'sprites/hendominium.png'
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
198 SELECTED_IMAGE = 'sprites/select_hendominium.png'
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
199 NAME = 'Hendominium'
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
200 FLOORS = 2
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
201
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
202 class GuardTower(Building):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
203 """A GuardTower."""
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
204
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
205 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
206 BUY_PRICE = 200
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
207 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
208 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
209 IMAGE = 'sprites/watchtower.png'
170
92d11e0544bc Switch building to selected image when building is selected.
Simon Cross <hodgestar@gmail.com>
parents: 144
diff changeset
210 SELECTED_IMAGE = 'sprites/select_watchtower.png'
177
b400991ccce1 pedantic building renaming
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 170
diff changeset
211 NAME = 'Watchtower'
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 109
diff changeset
212 FLOORS = 1
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
213
198
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
214 MODIFY_GUN_RANGE = lambda s, x: (3*x)/2
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
215 MODIFY_GUN_BASE_HIT = lambda s, x: x-5
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
216 MODIFY_GUN_RANGE_PENALTY = lambda s, x: x-1
355eaae40b1f Buildings now affect weapon range and accuracy.
Jeremy Thurgood <firxen@gmail.com>
parents: 177
diff changeset
217
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
218 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
219 """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
220 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
221
144
a9b800b4175e Add define for henhouses & egg laying.
Neil Muller <drnlmuller@gmail.com>
parents: 136
diff changeset
222 # Building hens can lay eggs in
a9b800b4175e Add define for henhouses & egg laying.
Neil Muller <drnlmuller@gmail.com>
parents: 136
diff changeset
223 HENHOUSES = [HenHouse.NAME, DoubleStoryHenHouse.NAME]
a9b800b4175e Add define for henhouses & egg laying.
Neil Muller <drnlmuller@gmail.com>
parents: 136
diff changeset
224
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
225 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
226 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
227 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
228 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
229 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
230 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
231 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
232 pass