90695b3916f99e2594fd8e1833a251046b9404fa
[koperkapel.git] / koperkapel / gamelib / enemy_roach.py
1 # Roach utilities
2
3 import random
4
5 from pgzero.clock import each_tick, unschedule
6 from functools import partial
7
8 from ..roaches import t32_roaches, WorldRoach
9
10
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)
15     roach.health = 5
16     roach.level = level
17     roach.move = partial(move, roach)
18     roach.last_moved = 0
19     roach.start_pos = None
20     each_tick(roach.move)
21     return roach
22
23
24 def move(roach, dt):
25     """Enemy roach move method"""
26     roach.last_moved += dt
27     if not roach in roach.level.enemies:
28         unschedule(roach.move)
29         return
30     if roach.last_moved > 0.5:
31         if not roach.start_pos:
32             roach.start_pos = roach.game_pos
33         roach.last_moved = 0
34         attempt = 0
35         while attempt < 4:
36             attempt += 1
37             dx = random.randint(-1, 1)
38             dy = random.randint(-1, 1)
39             if abs(roach.game_pos[0] + dx - roach.start_pos[0]) > 2:
40                 continue
41             if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2:
42                 continue
43             if roach.level.can_walk(roach.game_pos[0] + dx, roach.game_pos[1] + dy, 'floor'):
44                 enemy = roach.level.get_enemy(roach.game_pos[0] + dx, roach.game_pos[1] + dy)
45                 if enemy and enemy is not roach:
46                     continue
47                 roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy)
48                 break