source: gamelib/equipment.py @ 210:00d609883252

Last change on this file since 210:00d609883252 was 210:00d609883252, checked in by Adrianna Pińska <adrianna.pinska@…>, 11 years ago

egg equipment

File size: 4.7 KB
Line 
1"""Stuff for animals to use."""
2
3import random
4import sound
5import imagecache
6import animations
7
8class Equipment(object):
9    IS_EQUIPMENT = True
10    DRAW_LAYER = 0
11
12    def __init__(self):
13        self._buy_price = self.BUY_PRICE
14        self._sell_price = self.SELL_PRICE
15        self._name = self.NAME
16
17    def buy_price(self):
18        return self._buy_price
19
20    def sell_price(self):
21        return self._sell_price
22
23    def name(self):
24        return self._name
25
26    def images(self, eq_image_attr):
27        eq_image_file = getattr(self, eq_image_attr, None)
28        if not eq_image_file:
29            return None
30        eq_image_left = imagecache.load_image(eq_image_file)
31        eq_image_right = imagecache.load_image(eq_image_file, ("right_facing",))
32        return eq_image_left, eq_image_right, self.DRAW_LAYER
33
34class Weapon(Equipment):
35    IS_WEAPON = True
36    DRAW_LAYER = 10
37
38    def _get_parameter(self, parameter, wielder):
39        param = getattr(self, parameter)
40        if wielder.abode:
41            mod_attr = 'MODIFY_%s_%s' % (self.TYPE, parameter)
42            modifier = getattr(wielder.abode.building, mod_attr, lambda r: r)
43            param = modifier(param)
44        return param
45
46    def in_range(self, gameboard, wielder, target):
47        """Can the lucky wielder hit the potentially unlucky target with this?"""
48        return wielder.pos.dist(target.pos) <= self._get_parameter('RANGE', wielder)
49
50    def hit(self, gameboard, wielder, target):
51        """Is the potentially unlucky target actually unlucky?"""
52        if hasattr(self, 'HIT_SOUND'):
53            sound.play_sound(self.HIT_SOUND)
54        if hasattr(self, 'ANIMATION'):
55            gameboard.animations.append(self.ANIMATION(wielder))
56        roll = random.randint(1, 100)
57        base_hit = self._get_parameter('BASE_HIT', wielder)
58        range_penalty = self._get_parameter('RANGE_PENALTY', wielder)
59        return roll > (100-base_hit) + range_penalty*wielder.pos.dist(target.pos)
60
61    def place(self, animal):
62        for eq in animal.equipment:
63            if is_weapon(eq):
64                return False
65        return True
66
67class Rifle(Weapon):
68    TYPE = "GUN"
69    NAME = "rifle"
70    BUY_PRICE = 100
71    SELL_PRICE = 75
72
73    RANGE = 3
74    BASE_HIT = 55
75    RANGE_PENALTY = 15
76    HIT_SOUND = "fire-rifle.ogg"
77
78    CHICKEN_IMAGE_FILE = 'sprites/equip_rifle.png'
79
80    ANIMATION = animations.MuzzleFlash
81
82class Knife(Weapon):
83    TYPE = "KNIFE"
84    NAME = "knife"
85    BUY_PRICE = 25
86    SELL_PRICE = 15
87
88    RANGE = 1
89    BASE_HIT = 70
90    RANGE_PENALTY = 0
91
92    CHICKEN_IMAGE_FILE = 'sprites/equip_knife.png'
93
94class Armour(Equipment):
95    IS_ARMOUR = True
96    DRAW_LAYER = 5
97
98    def __init__(self):
99        super(Armour, self).__init__()
100        self.hitpoints = self.STARTING_HITPOINTS
101
102    def place(self, animal):
103        for eq in animal.equipment:
104            if eq.NAME == self.NAME:
105                return False
106        return True
107
108    def survive_damage(self):
109        self.hitpoints -= 1
110        if self.hitpoints > 0:
111            self._sell_price = int(self._sell_price*self.hitpoints/float(self.hitpoints+1))
112            return True
113        return False
114
115class Helmet(Armour):
116    NAME = "helmet"
117    BUY_PRICE = 25
118    SELL_PRICE = 15
119
120    STARTING_HITPOINTS = 1
121
122    CHICKEN_IMAGE_FILE = 'sprites/helmet.png'
123
124class Kevlar(Armour):
125    NAME = "kevlar"
126    BUY_PRICE = 100
127    SELL_PRICE = 75
128
129    STARTING_HITPOINTS = 2
130
131    CHICKEN_IMAGE_FILE = 'sprites/kevlar.png'
132
133class Accoutrement(Equipment):
134    """Things which are not equipment, but are displayed in the same way"""
135    IS_EQUIPMENT = False
136    IS_ACCOUTREMENT = True
137    BUY_PRICE = 0
138    SELL_PRICE = 0
139
140    def place(self, animal):
141        for eq in animal.accoutrements:
142            if eq.NAME == self.NAME:
143                return False
144        return True
145
146class Spotlight(Accoutrement):
147    NAME = "spotlight"
148    CHICKEN_IMAGE_FILE = 'sprites/select_chkn.png'
149    DRAW_LAYER = -5
150
151class Nest(Accoutrement):
152    NAME = "nest"
153    CHICKEN_IMAGE_FILE = 'sprites/nest.png'
154    DRAW_LAYER = 15
155
156class NestEgg(Accoutrement):
157    NAME = "nestegg"
158    CHICKEN_IMAGE_FILE = 'sprites/equip_egg.png'
159    DRAW_LAYER = 14
160
161def is_equipment(obj):
162    """Return true if obj is a build class."""
163    return getattr(obj, "IS_EQUIPMENT", False) and hasattr(obj, "NAME")
164
165def is_weapon(obj):
166    return is_equipment(obj) and getattr(obj, 'IS_WEAPON', False)
167
168def is_armour(obj):
169    return is_equipment(obj) and getattr(obj, 'IS_ARMOUR', False)
170
171def is_accoutrement(obj):
172    return not getattr(obj, "IS_EQUIPMENT", False) and hasattr(obj, "NAME") and getattr(obj, 'IS_ACCOUTREMENT', False)
173
174EQUIPMENT = []
175for name in dir():
176    obj = eval(name)
177    try:
178        if is_equipment(obj):
179            EQUIPMENT.append(obj)
180    except TypeError:
181        pass
Note: See TracBrowser for help on using the repository browser.