5 from pgzero.clock import each_tick, unschedule
6 from functools import partial
8 from ..roaches import t32_roaches, WorldRoach, default_rats
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)
19 roach.level_layer = 'floor' # always the case for now
20 roach.move = partial(move, roach)
22 roach.last_attacked= 0
23 roach.start_pos = None
25 roach.attack = partial(attack, roach)
30 roach = default_rats.assemble()
32 roach.game_pos = (0, 0)
37 roach.move = partial(move, roach)
39 roach.last_attacked= 0
40 roach.start_pos = None
42 roach.attack = partial(attack, roach)
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':
52 if abs(player_pos[0] - roach.game_pos[0]) > 1:
54 if abs(player_pos[1] - roach.game_pos[1]) > 1:
56 # Attacking, so turn towards the player
57 if player_pos[0] - roach.game_pos[0] < 0:
59 elif player_pos[1] - roach.game_pos[1] < 0:
61 elif player_pos[0] - roach.game_pos[0] > 0:
69 """Enemy roach move method"""
70 roach.last_moved += dt
71 if not roach in roach.level.enemies:
72 unschedule(roach.move)
74 if roach.last_moved > 0.5:
75 if not roach.start_pos:
76 roach.start_pos = roach.game_pos
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:
85 if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2:
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:
91 roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy)