Animate weapons.
authorSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 18:35:50 +0000 (20:35 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 18:35:50 +0000 (20:35 +0200)
koperkapel/weapons.py

index cc9f324b5a49a32b5a77b52c8c5be52b4f497216..d09c570b9a33d04e6b7ee3f44d00d3a8f6553730 100644 (file)
@@ -1,14 +1,15 @@
 """ Tools for dealing with weapons. """
 
 from pgzero.loaders import images
-from .actors.surf import SurfActor
+from .actors.animsurf import AnimatedSurfActor
 
 
 class Weapon:
     def __init__(self, name, damage, image_name=None, bullet_range=0,
-                 can_tape=True):
+                 can_tape=True, frames=("_1", "_2")):
         self.name = name
         self.image_name = image_name or name
+        self.frames = frames
         self.damage = damage
         self.bullet_range = bullet_range
         self.can_tape = can_tape
@@ -16,7 +17,7 @@ class Weapon:
 
 WEAPONS = [
     Weapon("spit", damage=1, bullet_range=2, can_tape=False,
-           image_name="blank"),
+           image_name="blank", frames=("",)),
     Weapon("butter_knife", damage=2),
     Weapon("crowbar", damage=4),
     Weapon("gun", damage=4, bullet_range=4),
@@ -25,7 +26,7 @@ WEAPONS = [
 WEAPON_LOOKUP = {w.name: w for w in WEAPONS}
 
 
-class WeaponActor(SurfActor):
+class WeaponActor(AnimatedSurfActor):
     def __init__(self, weapon, **kw):
         super().__init__(**kw)
         self.weapon = weapon
@@ -33,14 +34,21 @@ class WeaponActor(SurfActor):
 
 class WeaponFactory:
 
+    def assemble_frame(self, suffix, weapon, tape):
+        surf = images.load("weapons/%s%s" % (weapon.image_name, suffix))
+        frame = surf.copy()
+        if tape:
+            tape_surf = images.load("weapons/tape")
+            frame.blit(tape_surf, (0, 0))
+        return frame
+
     def assemble(self, weapon_name, tape=False):
         weapon = WEAPON_LOOKUP[weapon_name]
-        surf = images.load("weapons/%s" % (weapon.image_name,))
-        surf = surf.copy()
-        if tape and weapon.can_tape:
-            tape_surf = images.load("weapons/tape")
-            surf.blit(tape_surf, (0, 0))
-        return WeaponActor(weapon, surf=surf)
+        tape = tape and weapon.can_tape
+        frames = [
+            self.assemble_frame(suffix, weapon, tape)
+            for suffix in weapon.frames]
+        return WeaponActor(weapon, frames=frames)
 
 
 default_weapons = WeaponFactory()