2020-10-09 16:17:41 +00:00
|
|
|
#!/usr/bin/env python
|
2020-11-06 16:43:30 +00:00
|
|
|
from enum import Enum, auto
|
2020-11-10 21:59:02 +00:00
|
|
|
from math import sqrt
|
2020-11-11 15:23:27 +00:00
|
|
|
from random import choice, randint
|
2020-11-11 15:00:40 +00:00
|
|
|
from typing import List
|
2020-11-06 16:43:30 +00:00
|
|
|
|
|
|
|
from dungeonbattle.display.texturepack import TexturePack
|
|
|
|
|
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
|
2020-11-18 11:19:27 +00:00
|
|
|
and tiles, that have their custom properties.
|
2020-10-16 16:05:49 +00:00
|
|
|
"""
|
2020-10-09 16:17:41 +00:00
|
|
|
width: int
|
|
|
|
height: int
|
2020-11-11 15:09:03 +00:00
|
|
|
start_y: int
|
|
|
|
start_x: int
|
2020-11-10 20:41:54 +00:00
|
|
|
tiles: List[List["Tile"]]
|
2020-11-10 20:47:36 +00:00
|
|
|
entities: List["Entity"]
|
2020-11-06 20:15:09 +00:00
|
|
|
# coordinates of the point that should be
|
|
|
|
# on the topleft corner of the screen
|
|
|
|
currentx: int
|
|
|
|
currenty: int
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2020-11-11 15:09:03 +00:00
|
|
|
def __init__(self, width: int, height: int, tiles: list,
|
|
|
|
start_y: int, start_x: int):
|
2020-10-09 16:17:41 +00:00
|
|
|
self.width = width
|
|
|
|
self.height = height
|
2020-11-11 15:09:03 +00:00
|
|
|
self.start_y = start_y
|
|
|
|
self.start_x = start_x
|
2020-10-09 16:17:41 +00:00
|
|
|
self.tiles = tiles
|
2020-11-06 16:59:19 +00:00
|
|
|
self.entities = []
|
|
|
|
|
|
|
|
def add_entity(self, entity: "Entity") -> None:
|
|
|
|
"""
|
|
|
|
Register a new entity in the map.
|
|
|
|
"""
|
|
|
|
self.entities.append(entity)
|
|
|
|
entity.map = self
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2020-11-10 21:44:53 +00:00
|
|
|
def remove_entity(self, entity: "Entity") -> None:
|
|
|
|
"""
|
|
|
|
Unregister an entity from the map.
|
|
|
|
"""
|
|
|
|
self.entities.remove(entity)
|
|
|
|
|
2020-11-10 20:41:54 +00:00
|
|
|
def is_free(self, y: int, x: int) -> bool:
|
|
|
|
"""
|
|
|
|
Indicates that the case at the coordinates (y, x) is empty.
|
|
|
|
"""
|
|
|
|
return 0 <= y < self.height and 0 <= x < self.width and \
|
|
|
|
self.tiles[y][x].can_walk() and \
|
|
|
|
not any(entity.x == x and entity.y == y for entity in self.entities)
|
|
|
|
|
2020-10-09 16:17:41 +00:00
|
|
|
@staticmethod
|
2020-11-10 20:41:54 +00:00
|
|
|
def load(filename: str) -> "Map":
|
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-11-10 20:41:54 +00:00
|
|
|
def load_from_string(content: str) -> "Map":
|
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-11-11 15:09:03 +00:00
|
|
|
first_line = lines[0]
|
|
|
|
start_y, start_x = map(int, first_line.split(" "))
|
|
|
|
lines = [line for line in lines[1:] 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-11-06 19:18:27 +00:00
|
|
|
tiles = [[Tile.from_ascii_char(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-11-11 15:09:03 +00:00
|
|
|
return Map(width, height, tiles, start_y, start_x)
|
2020-10-16 13:41:25 +00:00
|
|
|
|
2020-11-16 00:01:18 +00:00
|
|
|
@staticmethod
|
|
|
|
def load_dungeon_from_string(content: str) -> "Map":
|
|
|
|
"""
|
|
|
|
Transforms a string into the list of corresponding tiles
|
|
|
|
"""
|
|
|
|
lines = content.split("\n")
|
|
|
|
lines = [line for line in lines[1:] if line]
|
|
|
|
tiles = [[Tile.from_ascii_char(c)
|
|
|
|
for x, c in enumerate(line)] for y, line in enumerate(lines)]
|
|
|
|
return tiles
|
|
|
|
|
2020-11-06 16:43:30 +00:00
|
|
|
def draw_string(self, pack: TexturePack) -> 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-11-06 16:43:30 +00:00
|
|
|
return "\n".join("".join(tile.char(pack) for tile in line)
|
2020-10-16 14:41:38 +00:00
|
|
|
for line in self.tiles)
|
2020-10-16 12:00:38 +00:00
|
|
|
|
2020-11-10 20:41:54 +00:00
|
|
|
def spawn_random_entities(self, count: int) -> None:
|
|
|
|
"""
|
2020-11-10 20:47:36 +00:00
|
|
|
Put randomly {count} hedgehogs on the map, where it is available.
|
2020-11-10 20:41:54 +00:00
|
|
|
"""
|
|
|
|
for _ in range(count):
|
|
|
|
y, x = 0, 0
|
|
|
|
while True:
|
|
|
|
y, x = randint(0, self.height - 1), randint(0, self.width - 1)
|
|
|
|
tile = self.tiles[y][x]
|
|
|
|
if tile.can_walk():
|
|
|
|
break
|
2020-11-11 15:23:27 +00:00
|
|
|
entity = choice(Entity.get_all_entity_classes())()
|
|
|
|
entity.move(y, x)
|
|
|
|
self.add_entity(entity)
|
2020-11-10 20:41:54 +00:00
|
|
|
|
2020-11-10 21:34:12 +00:00
|
|
|
def tick(self) -> None:
|
|
|
|
"""
|
|
|
|
Trigger all entity events.
|
|
|
|
"""
|
|
|
|
for entity in self.entities:
|
|
|
|
entity.act(self)
|
|
|
|
|
2020-11-16 00:01:18 +00:00
|
|
|
def save_state(self) -> dict:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Saves the map's attributes to a dictionnary
|
|
|
|
"""
|
2020-11-16 00:01:18 +00:00
|
|
|
d = dict()
|
|
|
|
d["width"] = self.width
|
|
|
|
d["height"] = self.height
|
|
|
|
d["start_y"] = self.start_y
|
|
|
|
d["start_x"] = self.start_x
|
|
|
|
d["currentx"] = self.currentx
|
|
|
|
d["currenty"] = self.currenty
|
|
|
|
for enti in self.entities:
|
|
|
|
d.update(enti.save_state())
|
|
|
|
d["map"] = self.draw_string(TexturePack.ASCII_PACK)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def load_state(self, d: dict) -> None:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Loads the map's attributes from a dictionnary
|
|
|
|
"""
|
2020-11-16 00:01:18 +00:00
|
|
|
self.width = d["width"]
|
|
|
|
self.height = d["height"]
|
|
|
|
self.start_y = d["start_y"]
|
|
|
|
self.start_x = d["start_x"]
|
|
|
|
self.currentx = d["currentx"]
|
|
|
|
self.currenty = d["currenty"]
|
|
|
|
self.map = self.load_dungeon_from_string(d["map"])
|
|
|
|
#add entities
|
2020-10-16 13:41:25 +00:00
|
|
|
|
|
|
|
class Tile(Enum):
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
The internal representation of the tiles of the map
|
|
|
|
"""
|
2020-11-06 16:43:30 +00:00
|
|
|
EMPTY = auto()
|
|
|
|
WALL = auto()
|
|
|
|
FLOOR = auto()
|
|
|
|
|
2020-11-16 00:01:18 +00:00
|
|
|
@staticmethod
|
|
|
|
def from_ascii_char(ch: str) -> "Tile":
|
|
|
|
"""
|
|
|
|
Maps an ascii character to its equivalent in the texture pack
|
|
|
|
"""
|
2020-11-06 19:18:27 +00:00
|
|
|
for tile in Tile:
|
|
|
|
if tile.char(TexturePack.ASCII_PACK) == ch:
|
|
|
|
return tile
|
|
|
|
raise ValueError(ch)
|
|
|
|
|
2020-11-06 16:43:30 +00:00
|
|
|
def char(self, pack: TexturePack) -> str:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Translates a Tile to the corresponding character according to the texture pack
|
|
|
|
"""
|
2020-11-06 16:43:30 +00:00
|
|
|
return getattr(pack, self.name)
|
2020-10-16 13:41:25 +00:00
|
|
|
|
2020-10-16 15:47:52 +00:00
|
|
|
def is_wall(self) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Is this Tile a wall?
|
|
|
|
"""
|
2020-10-16 15:47:52 +00:00
|
|
|
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-11-10 20:41:54 +00:00
|
|
|
return not self.is_wall() and self != Tile.EMPTY
|
2020-10-16 15:47:52 +00:00
|
|
|
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2020-10-11 13:24:51 +00:00
|
|
|
class Entity:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
An Entity object represents any entity present on the map
|
|
|
|
"""
|
2020-10-16 13:41:25 +00:00
|
|
|
y: int
|
2020-11-06 15:13:28 +00:00
|
|
|
x: int
|
2020-11-06 20:15:09 +00:00
|
|
|
name: str
|
2020-11-18 11:19:27 +00:00
|
|
|
map: Map
|
2020-10-09 16:17:41 +00:00
|
|
|
|
2020-11-06 15:13:28 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.y = 0
|
|
|
|
self.x = 0
|
|
|
|
|
2020-11-06 16:59:19 +00:00
|
|
|
def check_move(self, y: int, x: int, move_if_possible: bool = False)\
|
|
|
|
-> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Checks if moving to (y,x) is authorized
|
|
|
|
"""
|
2020-11-10 20:41:54 +00:00
|
|
|
free = self.map.is_free(y, x)
|
|
|
|
if free and move_if_possible:
|
2020-11-06 16:59:19 +00:00
|
|
|
self.move(y, x)
|
2020-11-10 20:41:54 +00:00
|
|
|
return free
|
2020-11-06 16:59:19 +00:00
|
|
|
|
2020-11-10 21:44:53 +00:00
|
|
|
def move(self, y: int, x: int) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Moves an entity to (y,x) coordinates
|
|
|
|
"""
|
2020-10-11 13:24:51 +00:00
|
|
|
self.y = y
|
2020-11-06 15:13:28 +00:00
|
|
|
self.x = x
|
2020-11-10 21:44:53 +00:00
|
|
|
return True
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-11-10 21:10:28 +00:00
|
|
|
def move_up(self, force: bool = False) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Moves the entity up one tile, if possible
|
|
|
|
"""
|
2020-11-10 21:10:28 +00:00
|
|
|
return self.move(self.y - 1, self.x) if force else \
|
|
|
|
self.check_move(self.y - 1, self.x, True)
|
|
|
|
|
|
|
|
def move_down(self, force: bool = False) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Moves the entity down one tile, if possible
|
|
|
|
"""
|
2020-11-10 21:10:28 +00:00
|
|
|
return self.move(self.y + 1, self.x) if force else \
|
|
|
|
self.check_move(self.y + 1, self.x, True)
|
|
|
|
|
|
|
|
def move_left(self, force: bool = False) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Moves the entity left one tile, if possible
|
|
|
|
"""
|
2020-11-10 21:10:28 +00:00
|
|
|
return self.move(self.y, self.x - 1) if force else \
|
|
|
|
self.check_move(self.y, self.x - 1, True)
|
|
|
|
|
|
|
|
def move_right(self, force: bool = False) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Moves the entity right one tile, if possible
|
|
|
|
"""
|
2020-11-10 21:10:28 +00:00
|
|
|
return self.move(self.y, self.x + 1) if force else \
|
|
|
|
self.check_move(self.y, self.x + 1, True)
|
|
|
|
|
2020-11-06 14:33:26 +00:00
|
|
|
def act(self, m: Map) -> None:
|
|
|
|
"""
|
|
|
|
Define the action of the entity that is ran each tick.
|
|
|
|
By default, does nothing.
|
|
|
|
"""
|
2020-10-23 16:02:57 +00:00
|
|
|
pass
|
2020-10-16 15:58:00 +00:00
|
|
|
|
2020-11-10 21:59:02 +00:00
|
|
|
def distance_squared(self, other: "Entity") -> int:
|
|
|
|
"""
|
|
|
|
Get the square of the distance to another entity.
|
|
|
|
Useful to check distances since square root takes time.
|
|
|
|
"""
|
|
|
|
return (self.y - other.y) ** 2 + (self.x - other.x) ** 2
|
|
|
|
|
|
|
|
def distance(self, other: "Entity") -> float:
|
|
|
|
"""
|
|
|
|
Get the cartesian distance to another entity.
|
|
|
|
"""
|
|
|
|
return sqrt(self.distance_squared(other))
|
|
|
|
|
2020-11-11 15:47:19 +00:00
|
|
|
def is_fighting_entity(self) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Is this entity a fighting entity?
|
|
|
|
"""
|
2020-11-11 15:47:19 +00:00
|
|
|
return isinstance(self, FightingEntity)
|
|
|
|
|
|
|
|
def is_item(self) -> bool:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Is this entity an item?
|
|
|
|
"""
|
2020-11-11 15:47:19 +00:00
|
|
|
from dungeonbattle.entities.items import Item
|
|
|
|
return isinstance(self, Item)
|
|
|
|
|
2020-11-11 15:23:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_all_entity_classes():
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Returns all entities subclasses
|
|
|
|
"""
|
2020-11-11 15:23:27 +00:00
|
|
|
from dungeonbattle.entities.items import Heart, Bomb
|
2020-11-11 16:39:48 +00:00
|
|
|
from dungeonbattle.entities.monsters import Beaver, Hedgehog, \
|
|
|
|
Rabbit, TeddyBear
|
|
|
|
return [Beaver, Bomb, Heart, Hedgehog, Rabbit, TeddyBear]
|
2020-11-11 15:23:27 +00:00
|
|
|
|
2020-11-16 00:01:18 +00:00
|
|
|
def save_state(self) -> dict:
|
|
|
|
"""
|
|
|
|
Saves the coordinates of the entity
|
|
|
|
"""
|
|
|
|
d = dict()
|
|
|
|
d["x"] = self.x
|
|
|
|
d["y"] = self.y
|
|
|
|
return d
|
|
|
|
|
|
|
|
def recover_state(self, d : dict) -> None:
|
|
|
|
"""
|
|
|
|
Loads the coordinates of the entity from a dictionnary
|
|
|
|
"""
|
|
|
|
self.x = d["x"]
|
|
|
|
self.y = d["y"]
|
|
|
|
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-10-16 15:58:00 +00:00
|
|
|
class FightingEntity(Entity):
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
A FightingEntity is an entity that can fight, and thus has a health,
|
|
|
|
level and stats
|
|
|
|
"""
|
2020-10-16 15:58:00 +00:00
|
|
|
maxhealth: int
|
|
|
|
health: int
|
|
|
|
strength: int
|
2020-11-06 15:13:28 +00:00
|
|
|
dead: bool
|
2020-11-06 17:08:10 +00:00
|
|
|
intelligence: int
|
|
|
|
charisma: int
|
|
|
|
dexterity: int
|
|
|
|
constitution: int
|
|
|
|
level: int
|
2020-10-16 15:58:00 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
2020-11-06 15:13:28 +00:00
|
|
|
super().__init__()
|
2020-10-16 15:58:00 +00:00
|
|
|
self.health = self.maxhealth
|
2020-11-06 15:13:28 +00:00
|
|
|
self.dead = False
|
2020-11-16 00:01:18 +00:00
|
|
|
self.health = 0
|
|
|
|
self.strength = 0
|
|
|
|
self.dead = False
|
|
|
|
self.intelligence = 0
|
|
|
|
self.charisma = 0
|
|
|
|
self.dexterity = 0
|
|
|
|
self.constitution = 0
|
|
|
|
self.level = 1
|
|
|
|
|
2020-10-16 15:58:00 +00:00
|
|
|
|
2020-11-06 14:33:26 +00:00
|
|
|
def hit(self, opponent: "FightingEntity") -> None:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Deals damage to the opponent, based on the stats
|
|
|
|
"""
|
2020-10-16 15:58:00 +00:00
|
|
|
opponent.take_damage(self, self.strength)
|
2020-11-06 14:33:26 +00:00
|
|
|
|
|
|
|
def take_damage(self, attacker: "Entity", amount: int) -> None:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Take damage from the attacker, based on the stats
|
|
|
|
"""
|
2020-10-16 15:58:00 +00:00
|
|
|
self.health -= amount
|
|
|
|
if self.health <= 0:
|
|
|
|
self.die()
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-10-16 15:58:00 +00:00
|
|
|
def die(self) -> None:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
If a fighting entity has no more health, it dies and is removed
|
|
|
|
"""
|
2020-11-06 15:13:28 +00:00
|
|
|
self.dead = True
|
2020-11-10 21:44:53 +00:00
|
|
|
self.map.remove_entity(self)
|
2020-11-16 00:01:18 +00:00
|
|
|
|
|
|
|
def keys(self) -> list:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Returns a fighting entities specific attributes
|
|
|
|
"""
|
2020-11-16 00:01:18 +00:00
|
|
|
return ["maxhealth", "health", "level", "dead", "strength", "intelligence", "charisma", "dexterity", "constitution"]
|
|
|
|
|
|
|
|
def save_state(self) -> dict:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Saves the state of the entity into a dictionnary
|
|
|
|
"""
|
2020-11-16 00:01:18 +00:00
|
|
|
d = super().save_state()
|
|
|
|
for name in self.keys():
|
|
|
|
d[name] = self.__getattribute__(name)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def recover_state(self, d : dict) -> None:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
Loads the state of an entity from a dictionnary
|
|
|
|
"""
|
2020-11-16 00:01:18 +00:00
|
|
|
super().recover_state(d)
|
|
|
|
for name in d.keys():
|
|
|
|
self.__setattribute__(name, d[name])
|