think of the poor windows users
[koperkapel.git] / koperkapel / weapons.py
1 """ Tools for dealing with weapons. """
2
3 from pgzero.loaders import images
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",)):
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
18
19 WEAPONS = [
20     Weapon("spit", damage=1, bullet_range=2, can_tape=False,
21            image_name="blank", frames=("",)),
22     Weapon("butter_knife", damage=2),
23     Weapon("crowbar", damage=4),
24     Weapon("gun", damage=4, bullet_range=4),
25 ]
26
27 WEAPON_LOOKUP = {w.name: w for w in WEAPONS}
28
29
30 class WeaponActor(AnimatedSurfActor):
31     def __init__(self, weapon, **kw):
32         super().__init__(**kw)
33         self.weapon = weapon
34
35
36 class WeaponFactory:
37
38     def assemble_frame(self, suffix, weapon, tape):
39         surf = images.load(safepath("weapons/%s%s")
40                             % (weapon.image_name, suffix))
41         frame = surf.copy()
42         if tape:
43             tape_surf = images.load(safepath("weapons/tape"))
44             frame.blit(tape_surf, (0, 0))
45         return frame
46
47     def assemble(self, weapon_name, tape=False):
48         weapon = WEAPON_LOOKUP[weapon_name]
49         tape = tape and weapon.can_tape
50         frames = [
51             self.assemble_frame(suffix, weapon, tape)
52             for suffix in weapon.frames]
53         return WeaponActor(weapon, frames=frames)
54
55
56 default_weapons = WeaponFactory()