Mercurial > rinkhals
comparison gamelib/buildings.py @ 64:99fbb652ce8d
Refactor buildings so that new ones can be added just by adding a class to buildings.py.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Mon, 31 Aug 2009 20:25:11 +0000 |
parents | 1047ccd22dac |
children | 7e9c8ad06d32 |
comparison
equal
deleted
inserted
replaced
63:1047ccd22dac | 64:99fbb652ce8d |
---|---|
8 class Building(Sprite): | 8 class Building(Sprite): |
9 """Base class for buildings""" | 9 """Base class for buildings""" |
10 | 10 |
11 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland'] | 11 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland'] |
12 | 12 |
13 def __init__(self, day_image, night_image, pos, size, tile_no): | 13 def __init__(self, pos): |
14 """Initial image, tile vid position, size and tile number for building.""" | 14 """Initial image, tile vid position, size and tile number for building.""" |
15 self.day_image = imagecache.load_image(self.IMAGE) | |
16 self.night_image = imagecache.load_image(self.IMAGE, ('night',)) | |
17 self.pos = pos | |
18 self.size = self.SIZE | |
19 self.tile_no = self.TILE_NO | |
20 self._buy_price = self.BUY_PRICE | |
21 | |
15 # Create the building somewhere far off screen | 22 # Create the building somewhere far off screen |
16 Sprite.__init__(self, day_image, (-1000, -1000)) | 23 Sprite.__init__(self, self.day_image, (-1000, -1000)) |
17 self.day_image = day_image | |
18 self.night_image = night_image | |
19 self.pos = pos | |
20 self.size = size | |
21 self.tile_no = tile_no | |
22 | 24 |
23 def tile_positions(self): | 25 def tile_positions(self): |
24 """Return pairs of (x, y) tile positions for each of the tile positions | 26 """Return pairs of (x, y) tile positions for each of the tile positions |
25 occupied by the building.""" | 27 occupied by the building. |
28 """ | |
26 xpos, ypos = self.pos | 29 xpos, ypos = self.pos |
27 xsize, ysize = self.size | 30 xsize, ysize = self.size |
28 | 31 |
29 for dx in xrange(xsize): | 32 for dx in xrange(xsize): |
30 for dy in xrange(ysize): | 33 for dy in xrange(ysize): |
56 for tile_pos in self.tile_positions(): | 59 for tile_pos in self.tile_positions(): |
57 tv.set(tile_pos, self.tile_no) | 60 tv.set(tile_pos, self.tile_no) |
58 | 61 |
59 return True | 62 return True |
60 | 63 |
64 def buy_price(self): | |
65 return self._buy_price | |
66 | |
61 def sun(self, sun_on): | 67 def sun(self, sun_on): |
62 if sun_on: | 68 if sun_on: |
63 self.setimage(self.day_image) | 69 self.setimage(self.day_image) |
64 else: | 70 else: |
65 self.setimage(self.night_image) | 71 self.setimage(self.night_image) |
66 | 72 |
73 | |
67 class HenHouse(Building): | 74 class HenHouse(Building): |
68 """A HenHouse.""" | 75 """A HenHouse.""" |
69 | 76 |
70 HENHOUSE = tiles.REVERSE_TILE_MAP['henhouse'] | 77 TILE_NO = tiles.REVERSE_TILE_MAP['henhouse'] |
71 | 78 BUY_PRICE = 100 |
72 def __init__(self, pos): | 79 SIZE = (3, 2) |
73 day_image = imagecache.load_image('sprites/henhouse.png') | 80 IMAGE = 'sprites/henhouse.png' |
74 night_image = imagecache.load_image('sprites/henhouse.png', ('night',)) | 81 NAME = 'Hen House' |
75 size = (3, 2) | |
76 Building.__init__(self, day_image, night_image, pos, size, self.HENHOUSE) | |
77 | 82 |
78 | 83 |
79 class GuardTower(Building): | 84 class GuardTower(Building): |
80 """A GuardTower.""" | 85 """A GuardTower.""" |
81 | 86 |
82 GUARDTOWER = tiles.REVERSE_TILE_MAP['guardtower'] | 87 TILE_NO = tiles.REVERSE_TILE_MAP['guardtower'] |
88 BUY_PRICE = 200 | |
89 SIZE = (2, 2) | |
90 IMAGE = 'sprites/watchtower.png' | |
91 NAME = 'Watch Tower' | |
83 | 92 |
84 def __init__(self, pos): | 93 def is_building(obj): |
85 day_image = imagecache.load_image('sprites/watchtower.png') | 94 """Return true if obj is a build class.""" |
86 night_image = imagecache.load_image('sprites/watchtower.png', ('night',)) | 95 return hasattr(obj, "NAME") |
87 size = (2, 2) | 96 |
88 Building.__init__(self, day_image, night_image, pos, size, self.GUARDTOWER) | 97 BUILDINGS = [] |
98 for name in dir(): | |
99 obj = eval(name) | |
100 try: | |
101 if is_building(obj): | |
102 BUILDINGS.append(obj) | |
103 except TypeError: | |
104 pass |