f07caedb2404ae802bc94f280ea839fc9a38f621
[koperkapel.git] / koperkapel / weapons.py
1 """ Tools for dealing with weapons. """
2
3 from pgzero.loaders import images, sounds
4 from .actors.animsurf import AnimatedSurfActor
5 from .util import safepath
6
7
8 class Weapon:
9     def __init__(self, name, damage, image_name=None, bullet_range=0,
10                  can_tape=True, frames=("_1",), sound=None):
11         self.name = name
12         self.image_name = image_name or name
13         self.frames = frames
14         self.damage = damage
15         self.bullet_range = bullet_range
16         self.can_tape = can_tape
17         self.sound = None
18         if sound:
19             self.sound = sounds.load(sound)
20
21
22 WEAPONS = [
23     Weapon("spit", damage=1, bullet_range=2, can_tape=False,
24            image_name="blank", frames=("",), sound="fire_spit"),
25     Weapon("butter_knife", damage=2),
26     Weapon("crowbar", damage=4),
27     Weapon("gun", damage=4, bullet_range=4, sound='gun_fire'),
28 ]
29
30 WEAPON_LOOKUP = {w.name: w for w in WEAPONS}
31
32
33 def weapon_by_name(weapon_name):
34     return WEAPON_LOOKUP[weapon_name]
35
36
37 class WeaponActor(AnimatedSurfActor):
38     def __init__(self, weapon, **kw):
39         super().__init__(**kw)
40         self.weapon = weapon
41
42
43 class WeaponFactory:
44
45     def assemble_frame(self, suffix, weapon, tape):
46         surf = images.load(safepath("weapons/%s%s")
47                            % (weapon.image_name, suffix))
48         frame = surf.copy()
49         if tape:
50             tape_surf = images.load(safepath("weapons/tape"))
51             frame.blit(tape_surf, (0, 0))
52         return frame
53
54     def assemble(self, weapon_name, tape=False):
55         weapon = weapon_by_name(weapon_name)
56         tape = tape and weapon.can_tape
57         frames = [
58             self.assemble_frame(suffix, weapon, tape)
59             for suffix in weapon.frames]
60         return WeaponActor(weapon, frames=frames)
61
62
63 default_weapons = WeaponFactory()