Random roaches
[koperkapel.git] / koperkapel / gamelib / enemy_roach.py
1 # Roach utilities
2
3 import random
4
5 from pgzero.clock import each_tick
6 from functools import partial
7
8 from ..roaches import t32_roaches, WorldRoach
9
10
11 def get_enemy_roach(level):
12     roach = t32_roaches.assemble(WorldRoach(), color=(255, 0, 0, 255))
13     roach.anchor = (0, 0)
14     roach.game_pos = (0, 0)
15     roach.health = 5
16     roach.level = level
17     roach.move = partial(move, roach)
18     roach.last_moved = 0
19     roach.start_pos = None
20     each_tick(roach.move)
21     return roach
22
23
24 def move(roach, dt):
25     """Enemy roach move method"""
26     roach.last_moved += dt
27     if roach.last_moved > 0.5:
28         if not roach.start_pos:
29             roach.start_pos = roach.game_pos
30         roach.last_moved = 0
31         attempt = 0
32         while attempt < 4:
33             attempt += 1
34             dx = random.randint(-1, 1)
35             dy = random.randint(-1, 1)
36             if abs(roach.game_pos[0] + dx - roach.start_pos[0]) > 2:
37                 continue
38             if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2:
39                 continue
40             if roach.level.can_walk(roach.game_pos[0] + dx, roach.game_pos[1] + dy, 'floor'):
41                 enemy = roach.level.get_enemy(roach.game_pos[0] + dx, roach.game_pos[1] + dy)
42                 if enemy and enemy is not roach:
43                     continue
44                 roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy)
45                 break