+def attack(roach, player_pos, player_layer, dt):
+ """Attack the player if close enough"""
+ roach.last_attacked += dt
+ if roach.last_attacked > 0.6:
+ roach.last_attacked = 0
+ if player_layer != 'floor':
+ return None
+ if abs(player_pos[0] - roach.game_pos[0]) > 1:
+ return None
+ if abs(player_pos[1] - roach.game_pos[1]) > 1:
+ return None
+ # Attacking, so turn towards the player
+ if player_pos[0] - roach.game_pos[0] < 0:
+ roach.angle = 270
+ elif player_pos[1] - roach.game_pos[1] < 0:
+ roach.angle = 0
+ elif player_pos[0] - roach.game_pos[0] > 0:
+ roach.angle = 90
+ else:
+ roach.angle = 270
+ # Do 1 damage
+ return 1
+