this is totally a real rat
[koperkapel.git] / koperkapel / gamelib / enemy_roach.py
index 2a8438241f3bdc8ac3fa0380a57b28bc1b6e6834..2dd2f2bc8276cde542e7bdf7f373fef8e254983f 100644 (file)
@@ -5,7 +5,7 @@ import random
 from pgzero.clock import each_tick, unschedule
 from functools import partial
 
-from ..roaches import t32_roaches, WorldRoach
+from ..roaches import t32_roaches, WorldRoach, default_rats
 
 
 def get_enemy_roach(level):
@@ -13,15 +13,58 @@ def get_enemy_roach(level):
     roach.anchor = (-16, -16)  # this should center them on the tile
     roach.game_pos = (0, 0)
     roach.health = 5
+    roach.damage = 1
     roach.angle = 0
     roach.level = level
+    roach.level_layer = 'floor'  # always the case for now
     roach.move = partial(move, roach)
     roach.last_moved = 0
+    roach.last_attacked= 0
     roach.start_pos = None
     each_tick(roach.move)
+    roach.attack = partial(attack, roach)
     return roach
 
 
+def get_rat(level):
+    roach = default_rats.assemble()
+    roach.anchor = (0, 0)
+    roach.game_pos = (0, 0)
+    roach.health = 10
+    roach.damage = 2
+    roach.angle = 0
+    roach.level = level
+    roach.move = partial(move, roach)
+    roach.last_moved = 0
+    roach.last_attacked= 0
+    roach.start_pos = None
+    each_tick(roach.move)
+    roach.attack = partial(attack, roach)
+    return roach
+
+def attack(roach, player_pos, player_layer, dt):
+    """Attack the player if close enough"""
+    roach.last_attacked += dt
+    if roach.last_attacked > 0.6:
+        roach.last_attacked = 0
+        if player_layer != 'floor':
+            return None
+        if abs(player_pos[0] - roach.game_pos[0]) > 1:
+            return None
+        if abs(player_pos[1] - roach.game_pos[1]) > 1:
+            return None
+        # Attacking, so turn towards the player
+        if player_pos[0] - roach.game_pos[0] < 0:
+            roach.angle = 270
+        elif player_pos[1] - roach.game_pos[1] < 0:
+            roach.angle = 0
+        elif player_pos[0] - roach.game_pos[0] > 0:
+            roach.angle = 90
+        else:
+            roach.angle = 270
+        # Do 1 damage
+        return roach.damage
+
 def move(roach, dt):
     """Enemy roach move method"""
     roach.last_moved += dt