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