Melee weapons now do damage.
authorSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 20:45:16 +0000 (22:45 +0200)
committerSimon Cross <hodgestar@gmail.com>
Sat, 5 Mar 2016 20:45:16 +0000 (22:45 +0200)
koperkapel/gamelib/enemy_roach.py
koperkapel/gamelib/level.py
koperkapel/scenes/level.py
koperkapel/weapons.py

index 95b3e19912eb6312f9718df1a3eccb1c56ab658b..e36b1765802c4e953f022cdd50a6984824b7122d 100644 (file)
@@ -8,4 +8,5 @@ def get_enemy_roach():
     roach = t32_roaches.assemble(WorldRoach(), color=(255, 0, 0, 255))
     roach.anchor = (0, 0)
     roach.game_pos = (0, 0)
+    roach.health = 5
     return roach
index 23701b947c387797c7e1288e88ce367e739c8735..aed4cccb880b88b48cd33ba33e6afe0abcba9663 100644 (file)
@@ -99,6 +99,9 @@ class Level(object):
                 return enemy
         return None
 
+    def remove_enemy(self, enemy):
+        self.enemies.remove(enemy)
+
     def add_enemy(self, enemy, x, y):
         """Add an enemy to an empty floor space near x, y"""
         added = False
@@ -112,4 +115,3 @@ class Level(object):
                         added = True
             x += random.randint(-1, 1)
             y += random.randint(-1, 1)
-
index 7978a73546cdcc5f2a1d2f5e66eb33d211fa15c2..50ec80864287ebb325b47439d973520ec1c8f164 100644 (file)
@@ -10,6 +10,7 @@ from ..constants import TILE_SIZE, WIDTH, HEIGHT
 from ..gamelib.items import clone_old_item
 from ..roaches import build_roach
 from ..vehicles.base import Vehicle
+from ..weapons import weapon_by_name
 
 
 class BaseLevelScene(Scene):
@@ -99,7 +100,8 @@ class GameLevelScene(BaseLevelScene):
         self._enemies = self.actors.add_layer("enemies", level=7)
         self._vehicle = Vehicle.current(world)
         self._mode = 'walk'
-        self._angle = 0
+        self._angle = 0  # up
+        self._angle_dp = (0, -1)  # up
         self._init_items()
         self._init_friendly_roaches()
         self._init_generators()
@@ -154,8 +156,9 @@ class GameLevelScene(BaseLevelScene):
         elif self._mode == 'crawl':
             return self._level.can_crawl(x, y, self._level_layer)
 
-    def _set_angle(self, angle):
+    def _set_angle(self, angle, dp):
         self._angle = angle
+        self._angle_dp = dp
         self._avatar.angle = angle
 
     @defer_to_update
@@ -165,7 +168,7 @@ class GameLevelScene(BaseLevelScene):
         self._avatar = self._vehicle.get_avatar(world)
         self._avatar.pos = (WIDTH // 2, HEIGHT // 2)
         self._roaches.add(self._avatar)
-        self._set_angle(self._angle)
+        self._set_angle(self._angle, self._angle_dp)
 
     @defer_to_update
     def _add_roach(self, world):
@@ -185,6 +188,26 @@ class GameLevelScene(BaseLevelScene):
                 self._items.add(clone)
         self._vehicle_changed()
 
+    def _hit_enemy(self, enemy, weapon):
+        enemy.health -= weapon.damage
+        if enemy.health <= 0:
+            self._level.remove_enemy(enemy)
+            self._enemies.remove(enemy)
+
+    @defer_to_update
+    def _fire_weapon(self, world):
+        weapon = weapon_by_name(world.weapons.current)
+        if weapon.bullet_range > 0:
+            # ranged, fire bullet
+            print("Pew.")
+        else:
+            # melee
+            pos, dp = self._player_pos, self._angle_dp
+            pos = (pos[0] + dp[0], pos[1] + dp[1])
+            enemy = self._level.get_enemy(pos[0], pos[1])
+            if enemy:
+                self._hit_enemy(enemy, weapon)
+
     @defer_to_update
     def _change_vehicle(self, world):
         vehicle = Vehicle.random()
@@ -239,12 +262,12 @@ class GameLevelScene(BaseLevelScene):
                     if self._can_move(nx, ny):
                         self._set_pos(nx, ny)
                         offset = (TILE_SIZE * dp[0], TILE_SIZE * dp[1])
-                        self._set_angle(angle)
+                        self._set_angle(angle, dp)
                         self._last_key_down = 0
                         return [MoveViewportEvent(offset)]
                 else:
                     # just turn
-                    self._set_angle(angle)
+                    self._set_angle(angle, dp)
 
     def _activate_key(self):
         x, y = self._player_pos
@@ -279,7 +302,7 @@ class GameLevelScene(BaseLevelScene):
     def _fire_key(self, dt):
         if self._last_key_down > self._key_rate:
             self._last_key_down = 0
-            print('Boom')
+            self._fire_weapon()
 
     def _vehicle_management_key(self):
         from .roach_management import RoachesScene
index 3962ecc96917cd14a3e7908af3dbe03485d40ea1..3ef987919c9820b4bef15792b759376fc6f20914 100644 (file)
@@ -28,6 +28,10 @@ WEAPONS = [
 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)
@@ -38,7 +42,7 @@ 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 +50,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)