b09c3aa0f98af86c03169af0824f05935ddfc259
[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.level_layer = 'floor'  # always the case for now
19     roach.move = partial(move, roach)
20     roach.last_moved = 0
21     roach.last_attacked= 0
22     roach.start_pos = None
23     each_tick(roach.move)
24     roach.attack = partial(attack, roach)
25     return roach
26
27
28 def attack(roach, player_pos, player_layer, dt):
29     """Attack the player if close enough"""
30     roach.last_attacked += dt
31     if roach.last_attacked > 0.6:
32         roach.last_attacked = 0
33         if player_layer != 'floor':
34             return None
35         if abs(player_pos[0] - roach.game_pos[0]) > 1:
36             return None
37         if abs(player_pos[1] - roach.game_pos[1]) > 1:
38             return None
39         # Attacking, so turn towards the player
40         if player_pos[0] - roach.game_pos[0] < 0:
41             roach.angle = 270
42         elif player_pos[1] - roach.game_pos[1] < 0:
43             roach.angle = 0
44         elif player_pos[0] - roach.game_pos[0] > 0:
45             roach.angle = 90
46         else:
47             roach.angle = 270
48         # Do 1 damage
49         return 1
50
51 def move(roach, dt):
52     """Enemy roach move method"""
53     roach.last_moved += dt
54     if not roach in roach.level.enemies:
55         unschedule(roach.move)
56         return
57     if roach.last_moved > 0.5:
58         if not roach.start_pos:
59             roach.start_pos = roach.game_pos
60         roach.last_moved = 0
61         attempt = 0
62         while attempt < 4:
63             attempt += 1
64             dx = random.randint(-1, 1)
65             dy = random.randint(-1, 1)
66             if abs(roach.game_pos[0] + dx - roach.start_pos[0]) > 2:
67                 continue
68             if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2:
69                 continue
70             if roach.level.can_walk(roach.game_pos[0] + dx, roach.game_pos[1] + dy, 'floor'):
71                 enemy = roach.level.get_enemy(roach.game_pos[0] + dx, roach.game_pos[1] + dy)
72                 if enemy and enemy is not roach:
73                     continue
74                 roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy)
75                 if dy == 1:
76                     roach.angle = 180
77                 elif dy == -1:
78                     roach.angle = 0
79                 elif dx == 1:
80                     roach.angle = 270
81                 else:
82                     roach.angle = 90
83                 break