Mercurial > rinkhals
annotate gamelib/buildings.py @ 94:fa8d8fc1bf5b
Added some sounds
author | David Fraser <davidf@sjsoft.com> |
---|---|
date | Wed, 02 Sep 2009 11:25:42 +0000 |
parents | 7e9c8ad06d32 |
children | 437cbd856a03 |
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 |
8 class Building(Sprite): | |
9 """Base class for buildings""" | |
10 | |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
11 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland'] |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
12 |
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
|
13 def __init__(self, pos): |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
14 """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
|
15 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
|
16 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
|
17 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
|
18 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
|
19 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
|
20 self._buy_price = self.BUY_PRICE |
65
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
21 self._sell_price = self.SELL_PRICE |
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
|
22 |
43 | 23 # 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
|
24 Sprite.__init__(self, self.day_image, (-1000, -1000)) |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
25 |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
26 def tile_positions(self): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
27 """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
|
28 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
|
29 """ |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
30 xpos, ypos = self.pos |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
31 xsize, ysize = self.size |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
32 |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
33 for dx in xrange(xsize): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
34 for dy in xrange(ysize): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
35 yield (xpos + dx, ypos + dy) |
43 | 36 |
37 def loop(self, tv, _sprite): | |
38 ppos = tv.tile_to_view(self.pos) | |
39 self.rect.x = ppos[0] | |
40 self.rect.y = ppos[1] | |
41 | |
42 def move(self, state): | |
43 """Given the game state, return a new position for the object""" | |
44 # Default is not to move | |
45 return self.pos | |
46 | |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
47 def place(self, tv): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
48 """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
|
49 and place it if possible. |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
50 """ |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
51 # check that all spaces under the structure are grassland |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
52 for tile_pos in self.tile_positions(): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
53 if not tv.get(tile_pos) == self.GRASSLAND: |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
54 return False |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
55 |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
56 # place tile |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
57 for tile_pos in self.tile_positions(): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
58 tv.set(tile_pos, self.tile_no) |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
59 |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
60 return True |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
61 |
65
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
62 def covers(self, tile_pos): |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
63 """Return True if build covers tile_pos, False otherwise.""" |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
64 xpos, ypos = self.pos |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
65 xsize, ysize = self.size |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
66 return (xpos <= tile_pos[0] < xpos + xsize) and \ |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
67 (ypos <= tile_pos[1] < ypos + ysize) |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
68 |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
69 def remove(self, tv): |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
70 """Remove the building from its current position.""" |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
71 # remove tile |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
72 for tile_pos in self.tile_positions(): |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
73 tv.set(tile_pos, self.GRASSLAND) |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
74 |
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 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
|
76 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
|
77 |
65
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
78 def sell_price(self): |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
79 return self._sell_price |
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
80 |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
81 def sun(self, sun_on): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
82 if sun_on: |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
83 self.setimage(self.day_image) |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
84 else: |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
85 self.setimage(self.night_image) |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
86 |
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
|
87 |
43 | 88 class HenHouse(Building): |
89 """A HenHouse.""" | |
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
|
91 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
|
92 BUY_PRICE = 100 |
65
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
93 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
|
94 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
|
95 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
|
96 NAME = 'Hen House' |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
97 |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
98 |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
99 class GuardTower(Building): |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
100 """A GuardTower.""" |
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
101 |
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
|
102 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
|
103 BUY_PRICE = 200 |
65
7e9c8ad06d32
Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents:
64
diff
changeset
|
104 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
|
105 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
|
106 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
|
107 NAME = 'Watch Tower' |
57
08665fa60345
Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
108 |
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
|
109 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
|
110 """Return true if obj is a build class.""" |
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
|
111 return hasattr(obj, "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
|
112 |
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
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 pass |