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