5 from pgzero.clock import each_tick, unschedule
6 from functools import partial
8 from ..roaches import t32_roaches, WorldRoach, default_rats, default_robots
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.level_layer = 'floor' # always the case for now
38 roach.move = partial(move, roach)
40 roach.last_attacked= 0
41 roach.start_pos = None
43 roach.attack = partial(attack, roach)
48 roach = default_robots.assemble()
50 roach.game_pos = (0, 0)
55 roach.level_layer = 'floor' # always the case for now
56 roach.move = partial(move, roach)
58 roach.last_attacked= 0
59 roach.start_pos = None
61 roach.attack = partial(attack, roach)
65 def attack(roach, player_pos, player_layer, dt):
66 """Attack the player if close enough"""
67 roach.last_attacked += dt
68 if roach.last_attacked > 0.6:
69 roach.last_attacked = 0
70 if player_layer != 'floor':
72 if abs(player_pos[0] - roach.game_pos[0]) > 1:
74 if abs(player_pos[1] - roach.game_pos[1]) > 1:
76 # Attacking, so turn towards the player
77 if player_pos[0] - roach.game_pos[0] < 0:
79 elif player_pos[1] - roach.game_pos[1] < 0:
81 elif player_pos[0] - roach.game_pos[0] > 0:
89 """Enemy roach move method"""
90 roach.last_moved += dt
91 if not roach in roach.level.enemies:
92 unschedule(roach.move)
94 if roach.last_moved > 0.5:
95 if not roach.start_pos:
96 roach.start_pos = roach.game_pos
101 dx = random.randint(-1, 1)
102 dy = random.randint(-1, 1)
103 if abs(roach.game_pos[0] + dx - roach.start_pos[0]) > 2:
105 if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2:
107 if roach.level.can_walk(roach.game_pos[0] + dx, roach.game_pos[1] + dy, 'floor'):
108 enemy = roach.level.get_enemy(roach.game_pos[0] + dx, roach.game_pos[1] + dy)
109 if enemy and enemy is not roach:
111 roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy)