123 lines
2.9 KiB
Python
123 lines
2.9 KiB
Python
#!/usr/bin/env python
|
|
from enum import Enum
|
|
|
|
|
|
class Map:
|
|
"""
|
|
Object that represents a Map with its width, height
|
|
and the whole tiles, with their custom properties.
|
|
"""
|
|
width: int
|
|
height: int
|
|
tiles: list
|
|
|
|
def __init__(self, width: int, height: int, tiles: list):
|
|
self.width = width
|
|
self.height = height
|
|
self.tiles = tiles
|
|
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):
|
|
"""
|
|
Read a file that contains the content of a map, and build a Map object.
|
|
"""
|
|
with open(filename, "r") as f:
|
|
file = f.read()
|
|
return Map.load_from_string(file)
|
|
|
|
@staticmethod
|
|
def load_from_string(content: str):
|
|
"""
|
|
Load a map represented by its characters and build a Map object.
|
|
"""
|
|
lines = content.split("\n")
|
|
lines = [line for line in lines if line]
|
|
height = len(lines)
|
|
width = len(lines[0])
|
|
tiles = [[Tile(c)
|
|
for x, c in enumerate(line)] for y, line in enumerate(lines)]
|
|
|
|
return Map(width, height, tiles)
|
|
|
|
def draw_string(self) -> str:
|
|
"""
|
|
Draw the current map as a string object that can be rendered
|
|
in the window.
|
|
"""
|
|
return "\n".join("".join(tile.value for tile in line)
|
|
for line in self.tiles)
|
|
|
|
|
|
class Tile(Enum):
|
|
EMPTY = ' '
|
|
WALL = '█'
|
|
FLOOR = '.'
|
|
|
|
def is_wall(self) -> bool:
|
|
return self == Tile.WALL
|
|
|
|
def can_walk(self) -> bool:
|
|
"""
|
|
Check if an entity (player or not) can move in this tile.
|
|
"""
|
|
return not self.is_wall()
|
|
|
|
|
|
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
|
|
|
|
def act(self, m: Map) -> None:
|
|
"""
|
|
Define the action of the entity that is ran each tick.
|
|
By default, does nothing.
|
|
"""
|
|
pass
|
|
|
|
|
|
class FightingEntity(Entity):
|
|
maxhealth: int
|
|
health: int
|
|
strength: int
|
|
dead: bool
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.health = self.maxhealth
|
|
self.dead = False
|
|
|
|
def hit(self, opponent: "FightingEntity") -> None:
|
|
opponent.take_damage(self, self.strength)
|
|
|
|
def take_damage(self, attacker: "Entity", amount: int) -> None:
|
|
self.health -= amount
|
|
if self.health <= 0:
|
|
self.die()
|
|
|
|
def die(self) -> None:
|
|
self.dead = True
|