Made mobs check if they can see the player

This commit is contained in:
Nicolas Margulies 2021-01-08 17:26:56 +01:00
parent 4a80dc36ad
commit 46a5dc6931
2 changed files with 16 additions and 3 deletions

View File

@ -42,7 +42,9 @@ class Monster(FightingEntity):
# that targets the player. # that targets the player.
# If they can not move and are already close to the player, # If they can not move and are already close to the player,
# they hit. # 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 # Moves to target player by choosing the best available path
for next_y, next_x in target.paths[(self.y, self.x)]: for next_y, next_x in target.paths[(self.y, self.x)]:
moved = self.check_move(next_y, next_x, True) moved = self.check_move(next_y, next_x, True)

View File

@ -7,6 +7,7 @@ from random import choice, choices, randint
from typing import List, Optional, Any, Dict, Tuple from typing import List, Optional, Any, Dict, Tuple
from queue import PriorityQueue from queue import PriorityQueue
from functools import reduce from functools import reduce
from copy import deepcopy
from .display.texturepack import TexturePack from .display.texturepack import TexturePack
from .translations import gettext as _ from .translations import gettext as _
@ -193,6 +194,14 @@ class Map:
entity.move(y, x) entity.move(y, x)
self.add_entity(entity) 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: 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 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: if x + y > max_range:
continue continue
is_opaque = self.is_wall(y, x, octant, origin) 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\ 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 and (y != bottom_y
or bottom < Slope(y * 4 + 1, x * 4 - 1))) or bottom <= Slope(y, x)))
# is_visible = is_opaque\ # is_visible = is_opaque\
# or ((y != top_y or top >= Slope(y, x)) # or ((y != top_y or top >= Slope(y, x))
# and (y != bottom_y or bottom <= Slope(y, x))) # and (y != bottom_y or bottom <= Slope(y, x)))