Add weapons.
authorSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 17:51:23 +0000 (19:51 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 17:51:23 +0000 (19:51 +0200)
koperkapel/vehicles/base.py
koperkapel/weapons.py [new file with mode: 0644]

index 9eb3eb0cfe714f74f9517d934da6f8e73ba5c7d6..27e1b24d4f0633575ac9519260de5c05d8c1a002 100644 (file)
@@ -7,6 +7,7 @@ from pygame.constants import BLEND_RGBA_MULT
 from pgzero.loaders import images
 from ..actors.orientatedsurf import OrientatedSurfActor
 from ..actors.animsurf import AnimatedSurfActor
+from ..weapons import default_weapons
 
 
 class Vehicle:
@@ -15,6 +16,7 @@ class Vehicle:
     vehicle_type = None
     overlay_frame_no = None
     approximate_radius = 200
+    weapons_taped_on = True
     selected_seat_overlay_color = (255, 0, 0, 255)
 
     def __init__(self):
@@ -89,14 +91,17 @@ class Vehicle:
         cls.register(Robot)
         cls.register(Roomba)
 
-    def _avatar_frame(self, i, suffix="_tiles"):
+    def _avatar_frame(self, i, weapon, suffix="_tiles"):
         vehicle = images.load("vehicle%s/%s_%d" % (
             suffix, self.vehicle_type, i + 1))
         frame = vehicle.copy()
+        frame.blit(weapon.surf, (0, 0))
         return frame
 
     def get_avatar(self, world):
-        frames = [self._avatar_frame(i) for i in range(4)]
+        weapon = default_weapons.assemble(
+            world.weapons.current, tape=self.weapons_taped_on)
+        frames = [self._avatar_frame(i, weapon) for i in range(4)]
         return AnimatedSurfActor(frames, anchor=(0, 0))
 
 
diff --git a/koperkapel/weapons.py b/koperkapel/weapons.py
new file mode 100644 (file)
index 0000000..cc9f324
--- /dev/null
@@ -0,0 +1,46 @@
+""" Tools for dealing with weapons. """
+
+from pgzero.loaders import images
+from .actors.surf import SurfActor
+
+
+class Weapon:
+    def __init__(self, name, damage, image_name=None, bullet_range=0,
+                 can_tape=True):
+        self.name = name
+        self.image_name = image_name or name
+        self.damage = damage
+        self.bullet_range = bullet_range
+        self.can_tape = can_tape
+
+
+WEAPONS = [
+    Weapon("spit", damage=1, bullet_range=2, can_tape=False,
+           image_name="blank"),
+    Weapon("butter_knife", damage=2),
+    Weapon("crowbar", damage=4),
+    Weapon("gun", damage=4, bullet_range=4),
+]
+
+WEAPON_LOOKUP = {w.name: w for w in WEAPONS}
+
+
+class WeaponActor(SurfActor):
+    def __init__(self, weapon, **kw):
+        super().__init__(**kw)
+        self.weapon = weapon
+
+
+class WeaponFactory:
+
+    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)
+
+
+default_weapons = WeaponFactory()