2020-11-10 23:50:47 +00:00
|
|
|
from random import randint
|
2020-11-11 14:25:50 +00:00
|
|
|
from typing import Dict, Tuple
|
2020-11-10 23:50:47 +00:00
|
|
|
|
2020-10-23 14:17:48 +00:00
|
|
|
from ..interfaces import FightingEntity
|
2020-10-16 15:58:00 +00:00
|
|
|
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-10-16 15:58:00 +00:00
|
|
|
class Player(FightingEntity):
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
|
|
|
The class of the player
|
|
|
|
"""
|
2020-11-06 20:23:17 +00:00
|
|
|
current_xp: int = 0
|
|
|
|
max_xp: int = 10
|
2020-11-11 22:41:06 +00:00
|
|
|
inventory: list
|
2020-11-11 14:25:50 +00:00
|
|
|
paths: Dict[Tuple[int, int], Tuple[int, int]]
|
2020-11-06 17:12:17 +00:00
|
|
|
|
2020-11-18 13:54:21 +00:00
|
|
|
def __init__(self, maxhealth: int = 20, strength: int = 5,
|
|
|
|
intelligence: int = 1, charisma: int = 1, dexterity: int = 1,
|
|
|
|
constitution: int = 1, level: int = 1, current_xp: int = 0,
|
|
|
|
max_xp: int = 10, *args, **kwargs) -> None:
|
|
|
|
super().__init__(name="player", maxhealth=maxhealth, strength=strength,
|
|
|
|
intelligence=intelligence, charisma=charisma,
|
|
|
|
dexterity=dexterity, constitution=constitution,
|
|
|
|
level=level, *args, **kwargs)
|
|
|
|
self.current_xp = current_xp
|
|
|
|
self.max_xp = max_xp
|
2020-11-11 22:41:06 +00:00
|
|
|
self.inventory = list()
|
2020-11-18 13:54:21 +00:00
|
|
|
self.paths = dict()
|
2020-11-11 22:41:06 +00:00
|
|
|
|
2020-11-10 21:02:41 +00:00
|
|
|
def move(self, y: int, x: int) -> None:
|
|
|
|
"""
|
|
|
|
When the player moves, move the camera of the map.
|
|
|
|
"""
|
|
|
|
super().move(y, x)
|
|
|
|
self.map.currenty = y
|
|
|
|
self.map.currentx = x
|
2020-11-11 14:25:50 +00:00
|
|
|
self.recalculate_paths()
|
2020-11-10 21:02:41 +00:00
|
|
|
|
2020-11-06 20:15:09 +00:00
|
|
|
def level_up(self) -> None:
|
2020-11-11 00:07:19 +00:00
|
|
|
"""
|
|
|
|
Add levels to the player as much as it is possible.
|
|
|
|
"""
|
2020-11-06 20:23:17 +00:00
|
|
|
while self.current_xp > self.max_xp:
|
2020-11-06 20:15:09 +00:00
|
|
|
self.level += 1
|
2020-11-06 20:23:17 +00:00
|
|
|
self.current_xp -= self.max_xp
|
2020-11-06 20:15:09 +00:00
|
|
|
self.max_xp = self.level * 10
|
2020-11-10 23:50:47 +00:00
|
|
|
self.health = self.maxhealth
|
2020-11-11 00:07:19 +00:00
|
|
|
# TODO Remove it, that's only fun
|
2020-11-11 15:23:27 +00:00
|
|
|
self.map.spawn_random_entities(randint(3 * self.level,
|
|
|
|
10 * self.level))
|
2020-11-06 20:15:09 +00:00
|
|
|
|
|
|
|
def add_xp(self, xp: int) -> None:
|
2020-11-11 00:07:19 +00:00
|
|
|
"""
|
|
|
|
Add some experience to the player.
|
|
|
|
If the required amount is reached, level up.
|
|
|
|
"""
|
2020-11-06 20:15:09 +00:00
|
|
|
self.current_xp += xp
|
2020-11-06 17:12:17 +00:00
|
|
|
self.level_up()
|
2020-11-10 23:50:47 +00:00
|
|
|
|
2020-11-11 15:47:19 +00:00
|
|
|
# noinspection PyTypeChecker,PyUnresolvedReferences
|
2020-11-11 00:04:30 +00:00
|
|
|
def check_move(self, y: int, x: int, move_if_possible: bool = False) \
|
|
|
|
-> bool:
|
2020-11-10 23:50:47 +00:00
|
|
|
"""
|
2020-11-11 00:04:30 +00:00
|
|
|
If the player tries to move but a fighting entity is there,
|
|
|
|
the player fights this entity.
|
|
|
|
It rewards some XP if it is dead.
|
2020-11-10 23:50:47 +00:00
|
|
|
"""
|
2020-11-11 00:17:00 +00:00
|
|
|
# Don't move if we are dead
|
|
|
|
if self.dead:
|
|
|
|
return False
|
2020-11-10 23:50:47 +00:00
|
|
|
for entity in self.map.entities:
|
2020-11-11 15:47:19 +00:00
|
|
|
if entity.y == y and entity.x == x:
|
|
|
|
if entity.is_fighting_entity():
|
|
|
|
self.hit(entity)
|
|
|
|
if entity.dead:
|
|
|
|
self.add_xp(randint(3, 7))
|
|
|
|
return True
|
|
|
|
elif entity.is_item():
|
|
|
|
entity.hold(self)
|
2020-11-11 00:04:30 +00:00
|
|
|
return super().check_move(y, x, move_if_possible)
|
2020-11-11 14:25:50 +00:00
|
|
|
|
2020-11-11 15:02:32 +00:00
|
|
|
def recalculate_paths(self, max_distance: int = 8) -> None:
|
2020-11-11 14:25:50 +00:00
|
|
|
"""
|
|
|
|
Use Dijkstra algorithm to calculate best paths
|
|
|
|
for monsters to go to the player.
|
|
|
|
"""
|
|
|
|
queue = [(self.y, self.x)]
|
|
|
|
visited = []
|
2020-11-11 15:02:32 +00:00
|
|
|
distances = {(self.y, self.x): 0}
|
2020-11-11 14:25:50 +00:00
|
|
|
predecessors = {}
|
|
|
|
while queue:
|
|
|
|
y, x = queue.pop(0)
|
|
|
|
visited.append((y, x))
|
2020-11-11 15:02:32 +00:00
|
|
|
if distances[(y, x)] >= max_distance:
|
|
|
|
continue
|
2020-11-11 14:25:50 +00:00
|
|
|
for diff_y, diff_x in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
|
|
|
|
new_y, new_x = y + diff_y, x + diff_x
|
|
|
|
if not 0 <= new_y < self.map.height or \
|
|
|
|
not 0 <= new_x < self.map.width or \
|
|
|
|
not self.map.tiles[y][x].can_walk() or \
|
|
|
|
(new_y, new_x) in visited or \
|
|
|
|
(new_y, new_x) in queue:
|
|
|
|
continue
|
|
|
|
predecessors[(new_y, new_x)] = (y, x)
|
2020-11-11 15:02:32 +00:00
|
|
|
distances[(new_y, new_x)] = distances[(y, x)] + 1
|
2020-11-11 14:25:50 +00:00
|
|
|
queue.append((new_y, new_x))
|
|
|
|
self.paths = predecessors
|