Very hacky bullets.
[koperkapel.git] / koperkapel / weapons.py
index 0050879b068b4b22d6a7903528d4f59cfb269f6a..7dcb41ed154250bcc009f0f5ce8fdf80261e2a56 100644 (file)
@@ -1,6 +1,8 @@
 """ Tools for dealing with weapons. """
 
-from pgzero.loaders import images
+from pygame.surface import Surface
+from pygame.draw import circle
+from pgzero.loaders import images, sounds
 from .actors.animsurf import AnimatedSurfActor
 from .util import safepath
 
@@ -14,7 +16,16 @@ class Weapon:
         self.damage = damage
         self.bullet_range = bullet_range
         self.can_tape = can_tape
-        self.sound = sound
+        self.sound = None
+        if sound:
+            self.sound = sounds.load(sound)
+
+    def play_sound(self):
+        if self.sound:
+            self.sound.play()
+
+    def assemble_bullet(self):
+        return BulletActor(self)
 
 
 WEAPONS = [
@@ -22,23 +33,39 @@ WEAPONS = [
            image_name="blank", frames=("",), sound="fire_spit"),
     Weapon("butter_knife", damage=2),
     Weapon("crowbar", damage=4),
-    Weapon("gun", damage=4, bullet_range=4),
+    Weapon("gun", damage=4, bullet_range=4, sound='gun_fire'),
 ]
 
 WEAPON_LOOKUP = {w.name: w for w in WEAPONS}
 
 
+def weapon_by_name(weapon_name):
+    return WEAPON_LOOKUP[weapon_name]
+
+
 class WeaponActor(AnimatedSurfActor):
     def __init__(self, weapon, **kw):
         super().__init__(**kw)
         self.weapon = weapon
 
 
+class BulletActor(AnimatedSurfActor):
+    def __init__(self, weapon, **kw):
+        radius = min(weapon.damage * 5, 32)
+        surf = Surface((radius * 2, radius * 2))
+        surf.convert_alpha()
+        surf.fill((255, 255, 255, 0))
+        circle(surf, (255, 0, 255, 32), (radius, radius), radius)
+        frames = [surf]
+        super().__init__(frames=frames, **kw)
+        self.weapon = weapon
+
+
 class WeaponFactory:
 
     def assemble_frame(self, suffix, weapon, tape):
         surf = images.load(safepath("weapons/%s%s")
-                            % (weapon.image_name, suffix))
+                           % (weapon.image_name, suffix))
         frame = surf.copy()
         if tape:
             tape_surf = images.load(safepath("weapons/tape"))
@@ -46,7 +73,7 @@ class WeaponFactory:
         return frame
 
     def assemble(self, weapon_name, tape=False):
-        weapon = WEAPON_LOOKUP[weapon_name]
+        weapon = weapon_by_name(weapon_name)
         tape = tape and weapon.can_tape
         frames = [
             self.assemble_frame(suffix, weapon, tape)