X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=koperkapel%2Fgamelib%2Fenemy_roach.py;h=c419af53282fca8967d8477376df6dd5ee5cf18a;hb=HEAD;hp=2645efa8778cca7dd1939465bfa341d1eea58eb0;hpb=af3fcb580e7a3acf730f5da2b8e7f51ad1314e83;p=koperkapel.git diff --git a/koperkapel/gamelib/enemy_roach.py b/koperkapel/gamelib/enemy_roach.py index 2645efa..c419af5 100644 --- a/koperkapel/gamelib/enemy_roach.py +++ b/koperkapel/gamelib/enemy_roach.py @@ -2,28 +2,99 @@ import random -from pgzero.clock import each_tick +from pgzero.clock import each_tick, unschedule +from pgzero.loaders import sounds from functools import partial -from ..roaches import t32_roaches, WorldRoach +from ..roaches import t32_roaches, WorldRoach, default_rats, default_robots 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.damage = 1 + roach.angle = 0 + roach.hit_sound = sounds.load('enemy_hit') + roach.level = level + roach.level_layer = 'floor' # always the case for now + 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 get_rat(level): + roach = default_rats.assemble() + roach.anchor = (0, 0) + roach.game_pos = (0, 0) + roach.health = 10 + roach.damage = 2 + roach.angle = 0 + roach.hit_sound = sounds.load('enemy_hit') roach.level = level + roach.level_layer = 'floor' # always the case for now 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 get_robot(level): + roach = default_robots.assemble() + roach.anchor = (0, 0) + roach.game_pos = (0, 0) + roach.health = 10 + roach.damage = 5 + roach.angle = 0 + roach.level = level + roach.hit_sound = sounds.load('enemy_hit') + roach.level_layer = 'floor' # always the case for now + 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.6: + 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 roach.damage + 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 @@ -42,4 +113,12 @@ def move(roach, dt): 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