5 from pgzero.clock import each_tick, unschedule
6 from pgzero.loaders import sounds
7 from functools import partial
9 from ..roaches import t32_roaches, WorldRoach, default_rats, default_robots
12 def get_enemy_roach(level):
13 roach = t32_roaches.assemble(WorldRoach(), color=(255, 0, 0, 255))
14 roach.anchor = (-16, -16) # this should center them on the tile
15 roach.game_pos = (0, 0)
19 roach.hit_sound = sounds.load('enemy_hit')
21 roach.level_layer = 'floor' # always the case for now
22 roach.move = partial(move, roach)
24 roach.last_attacked= 0
25 roach.start_pos = None
27 roach.attack = partial(attack, roach)
32 roach = default_rats.assemble()
34 roach.game_pos = (0, 0)
38 roach.hit_sound = sounds.load('enemy_hit')
40 roach.level_layer = 'floor' # always the case for now
41 roach.move = partial(move, roach)
43 roach.last_attacked= 0
44 roach.start_pos = None
46 roach.attack = partial(attack, roach)
51 roach = default_robots.assemble()
53 roach.game_pos = (0, 0)
58 roach.hit_sound = sounds.load('enemy_hit')
59 roach.level_layer = 'floor' # always the case for now
60 roach.move = partial(move, roach)
62 roach.last_attacked= 0
63 roach.start_pos = None
65 roach.attack = partial(attack, roach)
69 def attack(roach, player_pos, player_layer, dt):
70 """Attack the player if close enough"""
71 roach.last_attacked += dt
72 if roach.last_attacked > 0.6:
73 roach.last_attacked = 0
74 if player_layer != 'floor':
76 if abs(player_pos[0] - roach.game_pos[0]) > 1:
78 if abs(player_pos[1] - roach.game_pos[1]) > 1:
80 # Attacking, so turn towards the player
81 if player_pos[0] - roach.game_pos[0] < 0:
83 elif player_pos[1] - roach.game_pos[1] < 0:
85 elif player_pos[0] - roach.game_pos[0] > 0:
93 """Enemy roach move method"""
94 roach.last_moved += dt
95 if not roach in roach.level.enemies:
96 unschedule(roach.move)
98 if roach.last_moved > 0.5:
99 if not roach.start_pos:
100 roach.start_pos = roach.game_pos
105 dx = random.randint(-1, 1)
106 dy = random.randint(-1, 1)
107 if abs(roach.game_pos[0] + dx - roach.start_pos[0]) > 2:
109 if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2:
111 if roach.level.can_walk(roach.game_pos[0] + dx, roach.game_pos[1] + dy, 'floor'):
112 enemy = roach.level.get_enemy(roach.game_pos[0] + dx, roach.game_pos[1] + dy)
113 if enemy and enemy is not roach:
115 roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy)