Tiles are now an enumeration

This commit is contained in:
Nicolas Margulies 2020-10-16 15:41:25 +02:00 committed by Yohann D'ANELLO
parent c8de541eee
commit 1e7ca6026f
1 changed files with 14 additions and 15 deletions

View File

@ -1,4 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
from enum import Enum, auto
class Map: class Map:
width: int width: int
@ -22,28 +24,25 @@ class Map:
lines = [line for line in lines if line] lines = [line for line in lines if line]
height = len(lines) height = len(lines)
width = len(lines[0]) width = len(lines[0])
chars = [[Tile.from_char(c, x, y) tiles = [[Tile.from_char(c)
for x, c in enumerate(line)] for y, line in enumerate(lines)] for x, c in enumerate(line)] for y, line in enumerate(lines)]
return Map(width, height, chars) return Map(width, height, tiles)
class Tile: class Tile(Enum):
x: int EMPTY = auto()
y: int WALL = auto()
char: str FLOOR = auto()
@staticmethod @staticmethod
def from_char(c: str, x: int, y: int): def from_char(c: str):
t = Tile() return {'#': Tile.WALL, '.': Tile.FLOOR, ' ': Tile.EMPTY}[c]
t.x = x
t.y = y
t.char = c
return c
class Entity: class Entity:
tile: Tile x: int
y: int
def move(self, x: int, y: int) -> None: def move(self, x: int, y: int) -> None:
self.tile.x = x self.x = x
self.tile.y = y self.y = y