X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=koperkapel%2Fgamelib%2Fenemy_roach.py;h=2201c586f389e51193bc9c6851c9f5ea6b2a116e;hb=f1955c8e7345b183c12682caa9daac0741d52af1;hp=95b3e19912eb6312f9718df1a3eccb1c56ab658b;hpb=ec18a4dde4d14bd4486fb936476266e8511b169b;p=koperkapel.git diff --git a/koperkapel/gamelib/enemy_roach.py b/koperkapel/gamelib/enemy_roach.py index 95b3e19..2201c58 100644 --- a/koperkapel/gamelib/enemy_roach.py +++ b/koperkapel/gamelib/enemy_roach.py @@ -1,11 +1,82 @@ # Roach utilities +import random + +from pgzero.clock import each_tick, unschedule +from functools import partial + from ..roaches import t32_roaches, WorldRoach -def get_enemy_roach(): - # red +def get_enemy_roach(level): roach = t32_roaches.assemble(WorldRoach(), color=(255, 0, 0, 255)) - roach.anchor = (0, 0) + roach.anchor = (-16, -16) # this should center them on the tile roach.game_pos = (0, 0) + roach.health = 5 + roach.angle = 0 + roach.level = level + roach.move = partial(move, roach) + roach.last_moved = 0 + roach.last_attacked= 0 + roach.start_pos = None + each_tick(roach.move) + roach.attack = partial(attack, roach) return roach + + +def attack(roach, player_pos, player_layer, dt): + """Attack the player if close enough""" + roach.last_attacked += dt + if roach.last_attacked > 0.3: + roach.last_attacked = 0 + if player_layer != 'floor': + return None + if abs(player_pos[0] - roach.game_pos[0]) > 1: + return None + if abs(player_pos[1] - roach.game_pos[1]) > 1: + return None + # Attacking, so turn towards the player + if player_pos[0] - roach.game_pos[0] < 0: + roach.angle = 270 + elif player_pos[1] - roach.game_pos[1] < 0: + roach.angle = 0 + elif player_pos[0] - roach.game_pos[0] > 0: + roach.angle = 90 + else: + roach.angle = 270 + # Do 1 damage + return 1 + +def move(roach, dt): + """Enemy roach move method""" + roach.last_moved += dt + if not roach in roach.level.enemies: + unschedule(roach.move) + return + if roach.last_moved > 0.5: + if not roach.start_pos: + roach.start_pos = roach.game_pos + roach.last_moved = 0 + attempt = 0 + while attempt < 4: + attempt += 1 + dx = random.randint(-1, 1) + dy = random.randint(-1, 1) + if abs(roach.game_pos[0] + dx - roach.start_pos[0]) > 2: + continue + if abs(roach.game_pos[1] + dy - roach.start_pos[1]) > 2: + continue + if roach.level.can_walk(roach.game_pos[0] + dx, roach.game_pos[1] + dy, 'floor'): + enemy = roach.level.get_enemy(roach.game_pos[0] + dx, roach.game_pos[1] + dy) + if enemy and enemy is not roach: + continue + roach.game_pos = (roach.game_pos[0] + dx, roach.game_pos[1] + dy) + if dy == 1: + roach.angle = 180 + elif dy == -1: + roach.angle = 0 + elif dx == 1: + roach.angle = 270 + else: + roach.angle = 90 + break