this is totally a real rat
[koperkapel.git] / koperkapel / gamelib / enemy_roach.py
1 # Roach utilities
2
3 import random
4
5 from pgzero.clock import each_tick, unschedule
6 from functools import partial
7
8 from ..roaches import t32_roaches, WorldRoach, default_rats
9
10
11 def get_enemy_roach(level):
12     roach = t32_roaches.assemble(WorldRoach(), color=(255, 0, 0, 255))
13     roach.anchor = (-16, -16)  # this should center them on the tile
14     roach.game_pos = (0, 0)
15     roach.health = 5
16     roach.damage = 1
17     roach.angle = 0
18     roach.level = level
19     roach.level_layer = 'floor'  # always the case for now
20     roach.move = partial(move, roach)
21     roach.last_moved = 0
22     roach.last_attacked= 0
23     roach.start_pos = None
24     each_tick(roach.move)
25     roach.attack = partial(attack, roach)
26     return roach
27
28
29 def get_rat(level):
30     roach = default_rats.assemble()
31     roach.anchor = (0, 0)
32     roach.game_pos = (0, 0)
33     roach.health = 10
34     roach.damage = 2
35     roach.angle = 0
36     roach.level = level
37     roach.move = partial(move, roach)
38     roach.last_moved = 0
39     roach.last_attacked= 0
40     roach.start_pos = None
41     each_tick(roach.move)
42     roach.attack = partial(attack, roach)
43     return roach
44
45 def attack(roach, player_pos, player_layer, dt):
46     """Attack the player if close enough"""
47     roach.last_attacked += dt
48     if roach.last_attacked > 0.6:
49         roach.last_attacked = 0
50         if player_layer != 'floor':
51             return None
52         if abs(player_pos[0] - roach.game_pos[0]) > 1:
53             return None
54         if abs(player_pos[1] - roach.game_pos[1]) > 1:
55             return None
56         # Attacking, so turn towards the player
57         if player_pos[0] - roach.game_pos[0] < 0:
58             roach.angle = 270
59         elif player_pos[1] - roach.game_pos[1] < 0:
60             roach.angle = 0
61         elif player_pos[0] - roach.game_pos[0] > 0:
62             roach.angle = 90
63         else:
64             roach.angle = 270
65         # Do 1 damage
66         return roach.damage
67
68 def move(roach, dt):
69     """Enemy roach move method"""
70     roach.last_moved += dt
71     if not roach in roach.level.enemies:
72         unschedule(roach.move)
73         return
74     if roach.last_moved > 0.5:
75         if not roach.start_pos:
76             roach.start_pos = roach.game_pos
77         roach.last_moved = 0
78         attempt = 0
79         while attempt < 4:
80             attempt += 1
81             dx = random.randint(-1, 1)
82             dy = random.randint(-1, 1)
83             if abs(roach.game_pos[0] + dx - roach.start_pos[0]) > 2:
84                 continue
85             if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2:
86                 continue
87             if roach.level.can_walk(roach.game_pos[0] + dx, roach.game_pos[1] + dy, 'floor'):
88                 enemy = roach.level.get_enemy(roach.game_pos[0] + dx, roach.game_pos[1] + dy)
89                 if enemy and enemy is not roach:
90                     continue
91                 roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy)
92                 if dy == 1:
93                     roach.angle = 180
94                 elif dy == -1:
95                     roach.angle = 0
96                 elif dx == 1:
97                     roach.angle = 270
98                 else:
99                     roach.angle = 90
100                 break