2020-10-09 16:17:41 +00:00
|
|
|
#!/usr/bin/env python
|
2020-10-16 13:41:25 +00:00
|
|
|
from enum import Enum, auto
|
|
|
|
|
2020-10-09 16:17:41 +00:00
|
|
|
|
|
|
|
class Map:
|
|
|
|
width: int
|
|
|
|
height: int
|
|
|
|
tiles: list
|
|
|
|
|
|
|
|
def __init__(self, width: int, height: int, tiles: list):
|
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
self.tiles = tiles
|
|
|
|
|
|
|
|
@staticmethod
|
2020-10-09 16:24:13 +00:00
|
|
|
def load(filename: str):
|
2020-10-09 16:17:41 +00:00
|
|
|
with open(filename, "r") as f:
|
|
|
|
file = f.read()
|
|
|
|
return Map.load_from_string(file)
|
|
|
|
|
|
|
|
@staticmethod
|
2020-10-09 16:24:13 +00:00
|
|
|
def load_from_string(content: str):
|
2020-10-09 16:17:41 +00:00
|
|
|
lines = content.split("\n")
|
2020-10-09 16:24:13 +00:00
|
|
|
lines = [line for line in lines if line]
|
2020-10-09 16:17:41 +00:00
|
|
|
height = len(lines)
|
2020-10-09 16:24:13 +00:00
|
|
|
width = len(lines[0])
|
2020-10-16 13:41:25 +00:00
|
|
|
tiles = [[Tile.from_char(c)
|
2020-10-09 16:24:13 +00:00
|
|
|
for x, c in enumerate(line)] for y, line in enumerate(lines)]
|
2020-10-16 13:41:25 +00:00
|
|
|
return Map(width, height, tiles)
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2020-10-16 12:00:38 +00:00
|
|
|
def draw_string(self) -> str:
|
|
|
|
return "\n".join("".join(tile.char for tile in line) for line in self.tiles)
|
|
|
|
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2020-10-16 13:41:25 +00:00
|
|
|
class Tile(Enum):
|
|
|
|
EMPTY = auto()
|
|
|
|
WALL = auto()
|
|
|
|
FLOOR = auto()
|
2020-10-09 16:17:41 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2020-10-16 13:41:25 +00:00
|
|
|
def from_char(c: str):
|
|
|
|
return {'#': Tile.WALL, '.': Tile.FLOOR, ' ': Tile.EMPTY}[c]
|
2020-10-09 16:17:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Entity:
|
2020-10-16 13:41:25 +00:00
|
|
|
x: int
|
|
|
|
y: int
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2020-10-09 16:24:13 +00:00
|
|
|
def move(self, x: int, y: int) -> None:
|
2020-10-16 13:41:25 +00:00
|
|
|
self.x = x
|
|
|
|
self.y = y
|