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.move = partial(move, roach)
20 roach.last_attacked= 0
21 roach.start_pos = None
23 roach.attack = partial(attack, roach)
27 def attack(roach, player_pos, player_layer, dt):
28 """Attack the player if close enough"""
29 roach.last_attacked += dt
30 if roach.last_attacked > 0.6:
31 roach.last_attacked = 0
32 if player_layer != 'floor':
34 if abs(player_pos[0] - roach.game_pos[0]) > 1:
36 if abs(player_pos[1] - roach.game_pos[1]) > 1:
38 # Attacking, so turn towards the player
39 if player_pos[0] - roach.game_pos[0] < 0:
41 elif player_pos[1] - roach.game_pos[1] < 0:
43 elif player_pos[0] - roach.game_pos[0] > 0:
51 """Enemy roach move method"""
52 roach.last_moved += dt
53 if not roach in roach.level.enemies:
54 unschedule(roach.move)
56 if roach.last_moved > 0.5:
57 if not roach.start_pos:
58 roach.start_pos = roach.game_pos
63 dx = random.randint(-1, 1)
64 dy = random.randint(-1, 1)
65 if abs(roach.game_pos[0] + dx - roach.start_pos[0]) > 2:
67 if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2:
69 if roach.level.can_walk(roach.game_pos[0] + dx, roach.game_pos[1] + dy, 'floor'):
70 enemy = roach.level.get_enemy(roach.game_pos[0] + dx, roach.game_pos[1] + dy)
71 if enemy and enemy is not roach:
73 roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy)