level fix and robots
[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, default_rats, default_robots
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.damage = 1
17     roach.angle = 0
18     roach.level = level
19     roach.level_layer = 'floor'  # always the case for now
20     roach.move = partial(move, roach)
21     roach.last_moved = 0
22     roach.last_attacked= 0
23     roach.start_pos = None
24     each_tick(roach.move)
25     roach.attack = partial(attack, roach)
26     return roach
27
28
29 def get_rat(level):
30     roach = default_rats.assemble()
31     roach.anchor = (0, 0)
32     roach.game_pos = (0, 0)
33     roach.health = 10
34     roach.damage = 2
35     roach.angle = 0
36     roach.level = level
37     roach.level_layer = 'floor'  # always the case for now
38     roach.move = partial(move, roach)
39     roach.last_moved = 0
40     roach.last_attacked= 0
41     roach.start_pos = None
42     each_tick(roach.move)
43     roach.attack = partial(attack, roach)
44     return roach
45
46
47 def get_robot(level):
48     roach = default_robots.assemble()
49     roach.anchor = (0, 0)
50     roach.game_pos = (0, 0)
51     roach.health = 10
52     roach.damage = 5
53     roach.angle = 0
54     roach.level = level
55     roach.level_layer = 'floor'  # always the case for now
56     roach.move = partial(move, roach)
57     roach.last_moved = 0
58     roach.last_attacked= 0
59     roach.start_pos = None
60     each_tick(roach.move)
61     roach.attack = partial(attack, roach)
62     return roach
63
64
65 def attack(roach, player_pos, player_layer, dt):
66     """Attack the player if close enough"""
67     roach.last_attacked += dt
68     if roach.last_attacked > 0.6:
69         roach.last_attacked = 0
70         if player_layer != 'floor':
71             return None
72         if abs(player_pos[0] - roach.game_pos[0]) > 1:
73             return None
74         if abs(player_pos[1] - roach.game_pos[1]) > 1:
75             return None
76         # Attacking, so turn towards the player
77         if player_pos[0] - roach.game_pos[0] < 0:
78             roach.angle = 270
79         elif player_pos[1] - roach.game_pos[1] < 0:
80             roach.angle = 0
81         elif player_pos[0] - roach.game_pos[0] > 0:
82             roach.angle = 90
83         else:
84             roach.angle = 270
85         # Do 1 damage
86         return roach.damage
87
88 def move(roach, dt):
89     """Enemy roach move method"""
90     roach.last_moved += dt
91     if not roach in roach.level.enemies:
92         unschedule(roach.move)
93         return
94     if roach.last_moved > 0.5:
95         if not roach.start_pos:
96             roach.start_pos = roach.game_pos
97         roach.last_moved = 0
98         attempt = 0
99         while attempt < 4:
100             attempt += 1
101             dx = random.randint(-1, 1)
102             dy = random.randint(-1, 1)
103             if abs(roach.game_pos[0] + dx - roach.start_pos[0]) > 2:
104                 continue
105             if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2:
106                 continue
107             if roach.level.can_walk(roach.game_pos[0] + dx, roach.game_pos[1] + dy, 'floor'):
108                 enemy = roach.level.get_enemy(roach.game_pos[0] + dx, roach.game_pos[1] + dy)
109                 if enemy and enemy is not roach:
110                     continue
111                 roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy)
112                 if dy == 1:
113                     roach.angle = 180
114                 elif dy == -1:
115                     roach.angle = 0
116                 elif dx == 1:
117                     roach.angle = 270
118                 else:
119                     roach.angle = 90
120                 break