Raise exceptions for out-of-bounds points that are passed into Map helper methods

This commit is contained in:
Eryn Wells 2023-03-05 13:36:32 -08:00
parent 4b09d467d1
commit 42cfb78ba3

View file

@ -25,8 +25,9 @@ class Map:
self.configuration = config self.configuration = config
map_size = config.map_size map_size = config.map_size
shape = map_size.numpy_shape self._bounds = Rect(Point(), map_size)
shape = map_size.numpy_shape
self.tiles = np.full(shape, fill_value=Empty, order='F') self.tiles = np.full(shape, fill_value=Empty, order='F')
self.up_stairs = generator.up_stairs self.up_stairs = generator.up_stairs
@ -47,7 +48,7 @@ class Map:
@property @property
def bounds(self) -> Rect: def bounds(self) -> Rect:
'''The bounds of the map''' '''The bounds of the map'''
return Rect(Point(), self.size) return self._bounds
@property @property
def size(self) -> Size: def size(self) -> Size:
@ -87,11 +88,21 @@ class Map:
def tile_is_walkable(self, point: Point) -> bool: def tile_is_walkable(self, point: Point) -> bool:
'''Return True if the tile at the given point is walkable''' '''Return True if the tile at the given point is walkable'''
return self.tile_is_in_bounds(point) and self.tiles[point.x, point.y]['walkable'] if not self.tile_is_in_bounds(point):
raise ValueError(f'Point {point!s} is not in bounds')
return self.tiles[point.numpy_index]['walkable']
def point_is_visible(self, point: Point) -> bool:
'''Return True if the point is visible to the player'''
if not self.tile_is_in_bounds(point):
raise ValueError(f'Point {point!s} is not in bounds')
return self.visible[point.numpy_index]
def point_is_explored(self, point: Point) -> bool: def point_is_explored(self, point: Point) -> bool:
'''Return True if the tile at the given point has been explored by the player''' '''Return True if the tile at the given point has been explored by the player'''
return self.tile_is_in_bounds(point) and self.explored[point.x, point.y] if not self.tile_is_in_bounds(point):
raise ValueError(f'Point {point!s} is not in bounds')
return self.explored[point.numpy_index]
def highlight_points(self, points: Iterable[Point]): def highlight_points(self, points: Iterable[Point]):
'''Update the highlight graph with the list of points to highlight.''' '''Update the highlight graph with the list of points to highlight.'''