annotate gamelib/buildings.py @ 108:437cbd856a03

Add occupants and abodes. Allowing moving chickens around.
author Simon Cross <hodgestar@gmail.com>
date Wed, 02 Sep 2009 18:42:00 +0000
parents 7e9c8ad06d32
children 48019afde338
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
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
8 class Building(Sprite):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
9 """Base class for buildings"""
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
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
108
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
22 self._occupants = set()
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
23
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
24 # 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
25 Sprite.__init__(self, self.day_image, (-1000, -1000))
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
26
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
27 def tile_positions(self):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
28 """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
29 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
30 """
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
31 xpos, ypos = self.pos
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
32 xsize, ysize = self.size
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
33
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
34 for dx in xrange(xsize):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
35 for dy in xrange(ysize):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
36 yield (xpos + dx, ypos + dy)
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
37
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
38 def loop(self, tv, _sprite):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
39 ppos = tv.tile_to_view(self.pos)
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
40 self.rect.x = ppos[0]
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
41 self.rect.y = ppos[1]
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
42
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
43 def move(self, state):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
44 """Given the game state, return a new position for the object"""
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
45 # Default is not to move
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
46 return self.pos
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
47
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
48 def place(self, tv):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
49 """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
50 and place it if possible.
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
51 """
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
52 # check that all spaces under the structure are grassland
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
53 for tile_pos in self.tile_positions():
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
54 if not tv.get(tile_pos) == self.GRASSLAND:
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
55 return False
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
56
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
57 # place tile
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
58 for tile_pos in self.tile_positions():
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
59 tv.set(tile_pos, self.tile_no)
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
60
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
61 return True
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
62
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
63 def covers(self, tile_pos):
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
64 """Return True if build covers tile_pos, False otherwise."""
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
65 xpos, ypos = self.pos
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
66 xsize, ysize = self.size
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
67 return (xpos <= tile_pos[0] < xpos + xsize) and \
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
68 (ypos <= tile_pos[1] < ypos + ysize)
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
69
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
70 def remove(self, tv):
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
71 """Remove the building from its current position."""
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
72 # remove tile
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
73 for tile_pos in self.tile_positions():
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
74 tv.set(tile_pos, self.GRASSLAND)
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
75
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
76 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
77 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
78
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
79 def sell_price(self):
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
80 return self._sell_price
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
81
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
82 def sun(self, sun_on):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
83 if sun_on:
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
84 self.setimage(self.day_image)
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
85 else:
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
86 self.setimage(self.night_image)
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
87
108
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
88 def occupants(self):
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
89 """Return list of buildings occupants."""
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
90 return list(self._occupants)
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
91
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
92 def add_occupant(self, occupant):
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
93 if occupant.abode is not None:
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
94 occupant.abode.remove_occupant(occupant)
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
95 occupant.abode = self
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
96 self._occupants.add(occupant)
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
97
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
98 def remove_occupant(self, occupant):
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
99 if occupant in self._occupants:
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
100 self._occupants.remove(occupant)
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
101 occupant.abode = None
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
43
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
103 class HenHouse(Building):
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
104 """A HenHouse."""
fb5be14ea930 Start of building sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
105
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
106 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
107 BUY_PRICE = 100
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
108 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
109 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
110 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
111 NAME = 'Hen House'
57
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
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
114 class GuardTower(Building):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
115 """A GuardTower."""
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
116
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
117 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
118 BUY_PRICE = 200
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
119 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
120 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
121 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
122 NAME = 'Watch Tower'
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 43
diff changeset
123
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
124 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
125 """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
126 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
127
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 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
129 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
130 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
131 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
132 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
133 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
134 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
135 pass