Enemy roaches are orientable
[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.angle = 0
17     roach.level = level
18     roach.move = partial(move, roach)
19     roach.last_moved = 0
20     roach.start_pos = None
21     each_tick(roach.move)
22     return roach
23
24
25 def move(roach, dt):
26     """Enemy roach move method"""
27     roach.last_moved += dt
28     if not roach in roach.level.enemies:
29         unschedule(roach.move)
30         return
31     if roach.last_moved > 0.5:
32         if not roach.start_pos:
33             roach.start_pos = roach.game_pos
34         roach.last_moved = 0
35         attempt = 0
36         while attempt < 4:
37             attempt += 1
38             dx = random.randint(-1, 1)
39             dy = random.randint(-1, 1)
40             if abs(roach.game_pos[0] + dx - roach.start_pos[0]) > 2:
41                 continue
42             if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2:
43                 continue
44             if roach.level.can_walk(roach.game_pos[0] + dx, roach.game_pos[1] + dy, 'floor'):
45                 enemy = roach.level.get_enemy(roach.game_pos[0] + dx, roach.game_pos[1] + dy)
46                 if enemy and enemy is not roach:
47                     continue
48                 roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy)
49                 if dy == 1:
50                     roach.angle = 180
51                 elif dy == -1:
52                     roach.angle = 0
53                 elif dx == 1:
54                     roach.angle = 270
55                 else:
56                     roach.angle = 90
57                 break