5 from pgzero.clock import each_tick, unschedule
6 from functools import partial
8 from ..roaches import t32_roaches, WorldRoach
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)
18 roach.level_layer = 'floor' # always the case for now
19 roach.move = partial(move, roach)
21 roach.last_attacked= 0
22 roach.start_pos = None
24 roach.attack = partial(attack, roach)
28 def attack(roach, player_pos, player_layer, dt):
29 """Attack the player if close enough"""
30 roach.last_attacked += dt
31 if roach.last_attacked > 0.6:
32 roach.last_attacked = 0
33 if player_layer != 'floor':
35 if abs(player_pos[0] - roach.game_pos[0]) > 1:
37 if abs(player_pos[1] - roach.game_pos[1]) > 1:
39 # Attacking, so turn towards the player
40 if player_pos[0] - roach.game_pos[0] < 0:
42 elif player_pos[1] - roach.game_pos[1] < 0:
44 elif player_pos[0] - roach.game_pos[0] > 0:
52 """Enemy roach move method"""
53 roach.last_moved += dt
54 if not roach in roach.level.enemies:
55 unschedule(roach.move)
57 if roach.last_moved > 0.5:
58 if not roach.start_pos:
59 roach.start_pos = roach.game_pos
64 dx = random.randint(-1, 1)
65 dy = random.randint(-1, 1)
66 if abs(roach.game_pos[0] + dx - roach.start_pos[0]) > 2:
68 if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2:
70 if roach.level.can_walk(roach.game_pos[0] + dx, roach.game_pos[1] + dy, 'floor'):
71 enemy = roach.level.get_enemy(roach.game_pos[0] + dx, roach.game_pos[1] + dy)
72 if enemy and enemy is not roach:
74 roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy)