From 46a5dc6931d51d2ba15472541294f999c0debeb4 Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Fri, 8 Jan 2021 17:26:56 +0100 Subject: [PATCH] Made mobs check if they can see the player --- squirrelbattle/entities/monsters.py | 4 +++- squirrelbattle/interfaces.py | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/squirrelbattle/entities/monsters.py b/squirrelbattle/entities/monsters.py index e22aa51..374b1d6 100644 --- a/squirrelbattle/entities/monsters.py +++ b/squirrelbattle/entities/monsters.py @@ -42,7 +42,9 @@ class Monster(FightingEntity): # that targets the player. # If they can not move and are already close to the player, # they hit. - if target and (self.y, self.x) in target.paths: + if target and (self.y, self.x) in target.paths and \ + self.map.is_visible_from(self.y, self.x, + target.y, target.x, 5): # Moves to target player by choosing the best available path for next_y, next_x in target.paths[(self.y, self.x)]: moved = self.check_move(next_y, next_x, True) diff --git a/squirrelbattle/interfaces.py b/squirrelbattle/interfaces.py index 900067c..17e5cee 100644 --- a/squirrelbattle/interfaces.py +++ b/squirrelbattle/interfaces.py @@ -7,6 +7,7 @@ from random import choice, choices, randint from typing import List, Optional, Any, Dict, Tuple from queue import PriorityQueue from functools import reduce +from copy import deepcopy from .display.texturepack import TexturePack from .translations import gettext as _ @@ -193,6 +194,14 @@ class Map: entity.move(y, x) self.add_entity(entity) + def is_visible_from(self, starty: int, startx: int, desty: int, destx: int, + max_range: int) -> bool: + oldvisibility = deepcopy(self.visibility) + self.compute_visibility(starty, startx, max_range) + result = self.visibility[desty][destx] + self.visibility = oldvisibility + return result + def compute_visibility(self, y: int, x: int, max_range: int) -> None: """ Sets the visible tiles to be the ones visible by an entity at point @@ -245,10 +254,12 @@ class Map: if x + y > max_range: continue is_opaque = self.is_wall(y, x, octant, origin) + if y == top_y and octant == 7 and x == 4: + self.logs.add_message(f"{x}, {y}, {top.X}, {top.Y}") is_visible = is_opaque\ - or ((y != top_y or top > Slope(y * 4 - 1, x * 4 + 1)) + or ((y != top_y or top >= Slope(y, x)) and (y != bottom_y - or bottom < Slope(y * 4 + 1, x * 4 - 1))) + or bottom <= Slope(y, x))) # is_visible = is_opaque\ # or ((y != top_y or top >= Slope(y, x)) # and (y != bottom_y or bottom <= Slope(y, x)))