annotate gamelib/buildings.py @ 114:4c2fbab20abe

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