Collisions are working
This commit is contained in:
@ -11,12 +11,18 @@ class Map:
|
||||
height: int
|
||||
tiles: list
|
||||
|
||||
def __init__(self, width: int, height: int, tiles: list,
|
||||
entities: list = None):
|
||||
def __init__(self, width: int, height: int, tiles: list):
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.tiles = tiles
|
||||
self.entities = entities or []
|
||||
self.entities = []
|
||||
|
||||
def add_entity(self, entity: "Entity") -> None:
|
||||
"""
|
||||
Register a new entity in the map.
|
||||
"""
|
||||
self.entities.append(entity)
|
||||
entity.map = self
|
||||
|
||||
@staticmethod
|
||||
def load(filename: str):
|
||||
@ -39,7 +45,7 @@ class Map:
|
||||
tiles = [[Tile(c)
|
||||
for x, c in enumerate(line)] for y, line in enumerate(lines)]
|
||||
|
||||
return Map(width, height, tiles, [])
|
||||
return Map(width, height, tiles)
|
||||
|
||||
def draw_string(self) -> str:
|
||||
"""
|
||||
@ -68,11 +74,19 @@ class Tile(Enum):
|
||||
class Entity:
|
||||
y: int
|
||||
x: int
|
||||
map: Map
|
||||
|
||||
def __init__(self):
|
||||
self.y = 0
|
||||
self.x = 0
|
||||
|
||||
def check_move(self, y: int, x: int, move_if_possible: bool = False)\
|
||||
-> bool:
|
||||
tile = self.map.tiles[y][x]
|
||||
if tile.can_walk() and move_if_possible:
|
||||
self.move(y, x)
|
||||
return tile.can_walk()
|
||||
|
||||
def move(self, y: int, x: int) -> None:
|
||||
self.y = y
|
||||
self.x = x
|
||||
|
Reference in New Issue
Block a user