2020-10-09 16:17:41 +00:00
|
|
|
#!/usr/bin/env python
|
2020-10-16 14:41:38 +00:00
|
|
|
from enum import Enum
|
2020-11-06 16:04:42 +00:00
|
|
|
from dungeonbattle.settings import Settings
|
|
|
|
import dungeonbattle.texturepack as tp
|
2020-10-09 16:17:41 +00:00
|
|
|
|
|
|
|
class Map:
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
|
|
|
Object that represents a Map with its width, height
|
|
|
|
and the whole tiles, with their custom properties.
|
|
|
|
"""
|
2020-10-09 16:17:41 +00:00
|
|
|
width: int
|
|
|
|
height: int
|
|
|
|
tiles: list
|
2020-11-06 15:38:32 +00:00
|
|
|
currentx : int #coordinates of the point that should be on the topleft corner of the screen
|
|
|
|
currenty : int
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2020-10-23 14:17:48 +00:00
|
|
|
def __init__(self, width: int, height: int, tiles: list, entities = []):
|
2020-10-09 16:17:41 +00:00
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
self.tiles = tiles
|
2020-10-16 13:52:47 +00:00
|
|
|
self.entities = entities
|
2020-10-09 16:17:41 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2020-10-09 16:24:13 +00:00
|
|
|
def load(filename: str):
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
|
|
|
Read a file that contains the content of a map, and build a Map object.
|
|
|
|
"""
|
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-16 16:05:49 +00:00
|
|
|
"""
|
|
|
|
Load a map represented by its characters and build a Map object.
|
|
|
|
"""
|
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 15:41:37 +00:00
|
|
|
tiles = [[Tile(c)
|
2020-10-16 13:41:25 +00:00
|
|
|
for x, c in enumerate(line)] for y, line in enumerate(lines)]
|
2020-10-23 13:55:30 +00:00
|
|
|
|
2020-10-16 16:29:55 +00:00
|
|
|
return Map(width, height, tiles, [])
|
2020-10-23 13:55:30 +00:00
|
|
|
|
2020-10-16 13:41:25 +00:00
|
|
|
|
2020-10-16 12:00:38 +00:00
|
|
|
def draw_string(self) -> str:
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
|
|
|
Draw the current map as a string object that can be rendered
|
|
|
|
in the window.
|
|
|
|
"""
|
2020-10-16 14:41:38 +00:00
|
|
|
return "\n".join("".join(tile.value for tile in line)
|
|
|
|
for line in self.tiles)
|
2020-10-16 12:00:38 +00:00
|
|
|
|
2020-10-16 13:41:25 +00:00
|
|
|
|
|
|
|
class Tile(Enum):
|
2020-11-06 16:10:38 +00:00
|
|
|
if self.settings.TEXTURE_PACK == 'ASCII' :
|
2020-11-06 16:04:42 +00:00
|
|
|
self.textures = tp.ascii_textures
|
2020-11-06 16:10:38 +00:00
|
|
|
if self.settings.TEXTURE_PACK == 'SQUIRREL' :
|
2020-11-06 16:04:42 +00:00
|
|
|
self.textures = tp.squirrel_textures
|
|
|
|
|
|
|
|
EMPTY = self.textures["EMPTY"]
|
|
|
|
WALL = self.textures["WALL"]
|
|
|
|
FLOOR = self.textures["FLOOR"]
|
2020-10-16 13:41:25 +00:00
|
|
|
|
2020-10-16 15:47:52 +00:00
|
|
|
def is_wall(self) -> bool:
|
|
|
|
return self == Tile.WALL
|
|
|
|
|
|
|
|
def can_walk(self) -> bool:
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
|
|
|
Check if an entity (player or not) can move in this tile.
|
|
|
|
"""
|
2020-10-16 15:47:52 +00:00
|
|
|
return not self.is_wall()
|
|
|
|
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2020-10-11 13:24:51 +00:00
|
|
|
class Entity:
|
|
|
|
x: int
|
2020-10-16 13:41:25 +00:00
|
|
|
y: int
|
2020-11-06 15:05:01 +00:00
|
|
|
name : str
|
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-11 13:24:51 +00:00
|
|
|
self.x = x
|
|
|
|
self.y = y
|
2020-10-23 16:02:57 +00:00
|
|
|
|
|
|
|
def act(self, m:Map):
|
|
|
|
pass
|
2020-10-16 15:58:00 +00:00
|
|
|
|
|
|
|
class FightingEntity(Entity):
|
|
|
|
maxhealth: int
|
|
|
|
health: int
|
|
|
|
strength: int
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.health = self.maxhealth
|
|
|
|
|
|
|
|
def hit(self, opponent) -> None:
|
|
|
|
opponent.take_damage(self, self.strength)
|
|
|
|
|
|
|
|
def take_damage(self, attacker, amount:int) -> None:
|
|
|
|
self.health -= amount
|
|
|
|
if self.health <= 0:
|
|
|
|
self.die()
|
|
|
|
|
|
|
|
def die(self) -> None:
|
|
|
|
pass
|