squirrel-battle/dungeonbattle/interfaces.py

53 lines
1.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python
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):
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):
lines = content.split("\n")
2020-10-09 16:24:13 +00:00
lines = [line for line in lines if line]
height = len(lines)
2020-10-09 16:24:13 +00:00
width = len(lines[0])
chars = [[Tile.from_char(c, x, y)
for x, c in enumerate(line)] for y, line in enumerate(lines)]
return Map(width, height, chars)
def draw_string(self) -> str:
return "\n".join("".join(tile.char for tile in line) for line in self.tiles)
class Tile:
x: int
y: int
char: str
@staticmethod
def from_char(c: str, x: int, y: int):
t = Tile()
t.x = x
t.y = y
t.char = c
return c
class Entity:
tile: Tile
2020-10-09 16:24:13 +00:00
def move(self, x: int, y: int) -> None:
self.tile.x = x
self.tile.y = y