2018-12-13 11:17:35 +01:00
|
|
|
"""
|
|
|
|
This module contains the Map class.
|
|
|
|
"""
|
|
|
|
|
2019-03-13 17:52:37 +01:00
|
|
|
import numpy as np
|
|
|
|
|
2019-03-13 17:27:56 +01:00
|
|
|
def is_on_border(i, j, w ,h):
|
|
|
|
return i == 0 or i == w - 1 or j == 0 or j == h - 1
|
|
|
|
|
2018-10-23 16:27:27 +02:00
|
|
|
class Map:
|
2018-12-13 11:17:35 +01:00
|
|
|
"""
|
|
|
|
The map of the game.
|
|
|
|
|
|
|
|
It basically consists of a matrix with a certain width and height. You can
|
2019-03-13 17:27:56 +01:00
|
|
|
access its items by using the [] operator.
|
2018-12-13 11:17:35 +01:00
|
|
|
"""
|
2019-03-13 17:27:56 +01:00
|
|
|
def __init__(self, w, h, empty, wall):
|
2018-12-13 11:17:35 +01:00
|
|
|
"""
|
|
|
|
Creates a new map from its width and its height.
|
|
|
|
|
2019-03-13 20:45:36 +01:00
|
|
|
The map will contain (height + 2) * (width + 2) tiles, by having a
|
|
|
|
border. The matrix is initialized with `empty` in the inside and
|
|
|
|
`wall` on the borders.
|
2018-12-13 11:17:35 +01:00
|
|
|
"""
|
2019-03-13 17:27:56 +01:00
|
|
|
self.width = w
|
|
|
|
self.height = h
|
2019-03-13 17:52:37 +01:00
|
|
|
self._data = np.array([[wall if is_on_border(i, j, w + 2, h + 2) else empty for i in range(h + 2)] for j in range(w + 2)])
|
2019-03-13 17:27:56 +01:00
|
|
|
|
|
|
|
def clone(self):
|
|
|
|
"""
|
|
|
|
Creates a clone of the map.
|
|
|
|
"""
|
2019-03-13 20:45:36 +01:00
|
|
|
clone = Map(self.width, self.height, 0, 0)
|
|
|
|
clone._data = np.copy(self._data)
|
|
|
|
return clone
|
2018-10-23 16:27:27 +02:00
|
|
|
|
2019-03-13 21:01:54 +01:00
|
|
|
def apply(self, converter):
|
2019-03-13 20:45:36 +01:00
|
|
|
"""
|
|
|
|
Converts a map by applying a function to each element.
|
|
|
|
"""
|
|
|
|
converted = Map(self.width, self.height, 0, 0)
|
|
|
|
converted._data = np.array([[converter(self._data[i][j]) for i in range(self.height + 2)] for j in range(self.width + 2)])
|
|
|
|
return converted
|
|
|
|
|
|
|
|
def array(self):
|
|
|
|
"""
|
|
|
|
Returns the inner array of the map.
|
|
|
|
"""
|
|
|
|
return self._data
|
2019-03-13 17:52:37 +01:00
|
|
|
|
2019-03-13 21:01:54 +01:00
|
|
|
def clone_array(self):
|
|
|
|
"""
|
|
|
|
Returns a copy of the inner array of the map.
|
|
|
|
"""
|
2019-03-22 09:57:45 +01:00
|
|
|
#return np.array([[converter(self._data[i][j]) for i in range(self.height + 2)] for j in range(self.width + 2)])
|
|
|
|
clone_map = self.clone()
|
|
|
|
return clone_map._data
|
2019-03-13 21:01:54 +01:00
|
|
|
|
2018-10-23 16:27:27 +02:00
|
|
|
def __getitem__(self, index):
|
|
|
|
(i, j) = index
|
2019-03-13 17:27:56 +01:00
|
|
|
return self._data[i+1][j+1]
|
2018-10-23 16:27:27 +02:00
|
|
|
|
|
|
|
def __setitem__(self, position, other):
|
|
|
|
(i, j) = position
|
2019-03-13 17:27:56 +01:00
|
|
|
self._data[i+1][j+1] = other
|
2019-03-13 17:52:37 +01:00
|
|
|
|