2020-11-27 15:33:17 +00:00
|
|
|
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-11-10 23:50:47 +00:00
|
|
|
from random import randint
|
2020-12-18 16:30:03 +00:00
|
|
|
from typing import Dict, Optional, Tuple
|
2020-11-10 23:50:47 +00:00
|
|
|
|
2020-12-18 16:30:03 +00:00
|
|
|
from .items import Item
|
2020-12-11 16:20:50 +00:00
|
|
|
from ..interfaces import FightingEntity, InventoryHolder
|
2020-10-16 15:58:00 +00:00
|
|
|
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-12-11 16:20:50 +00:00
|
|
|
class Player(InventoryHolder, FightingEntity):
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
The class of the player.
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
2020-11-06 20:23:17 +00:00
|
|
|
current_xp: int = 0
|
|
|
|
max_xp: int = 10
|
2021-01-06 10:44:52 +00:00
|
|
|
xp_buff: float = 1
|
2020-11-11 14:25:50 +00:00
|
|
|
paths: Dict[Tuple[int, int], Tuple[int, int]]
|
2021-01-06 09:46:36 +00:00
|
|
|
equipped_main: Optional[Item]
|
|
|
|
equipped_secondary: Optional[Item]
|
|
|
|
equipped_helmet: Optional[Item]
|
2020-12-18 16:30:03 +00:00
|
|
|
equipped_armor: Optional[Item]
|
2020-11-06 17:12:17 +00:00
|
|
|
|
2020-12-05 13:25:22 +00:00
|
|
|
def __init__(self, name: str = "player", maxhealth: int = 20,
|
|
|
|
strength: int = 5, intelligence: int = 1, charisma: int = 1,
|
|
|
|
dexterity: int = 1, constitution: int = 1, level: int = 1,
|
2020-12-05 13:35:59 +00:00
|
|
|
current_xp: int = 0, max_xp: int = 10, inventory: list = None,
|
2021-01-06 09:46:36 +00:00
|
|
|
hazel: int = 42, equipped_main: Optional[Item] = None,
|
2021-01-05 18:07:15 +00:00
|
|
|
equipped_armor: Optional[Item] = None, critical: int = 5,\
|
2021-01-06 09:46:36 +00:00
|
|
|
equipped_secondary: Optional[Item] = None, \
|
2021-01-06 10:44:52 +00:00
|
|
|
equipped_helmet: Optional[Item] = None, xp_buff: float = 1,\
|
2021-01-05 18:07:15 +00:00
|
|
|
*args, **kwargs) -> None:
|
2020-12-05 13:25:22 +00:00
|
|
|
super().__init__(name=name, maxhealth=maxhealth, strength=strength,
|
2020-11-18 23:10:37 +00:00
|
|
|
intelligence=intelligence, charisma=charisma,
|
|
|
|
dexterity=dexterity, constitution=constitution,
|
2021-01-05 18:07:15 +00:00
|
|
|
level=level, critical=critical, *args, **kwargs)
|
2020-11-18 23:10:37 +00:00
|
|
|
self.current_xp = current_xp
|
|
|
|
self.max_xp = max_xp
|
2021-01-06 10:44:52 +00:00
|
|
|
self.xp_buff = xp_buff
|
2020-12-11 16:20:50 +00:00
|
|
|
self.inventory = self.translate_inventory(inventory or [])
|
2020-11-18 13:54:21 +00:00
|
|
|
self.paths = dict()
|
2020-11-27 16:11:59 +00:00
|
|
|
self.hazel = hazel
|
2021-01-06 09:46:36 +00:00
|
|
|
if isinstance(equipped_main, dict):
|
|
|
|
equipped_main = self.dict_to_item(equipped_main)
|
2020-12-18 16:30:03 +00:00
|
|
|
if isinstance(equipped_armor, dict):
|
|
|
|
equipped_armor = self.dict_to_item(equipped_armor)
|
2021-01-06 09:46:36 +00:00
|
|
|
if isinstance(equipped_secondary, dict):
|
|
|
|
equipped_secondary = self.dict_to_item(equipped_secondary)
|
|
|
|
if isinstance(equipped_helmet, dict):
|
|
|
|
equipped_helmet = self.dict_to_item(equipped_helmet)
|
|
|
|
self.equipped_main = equipped_main
|
2020-12-18 16:30:03 +00:00
|
|
|
self.equipped_armor = equipped_armor
|
2021-01-06 09:46:36 +00:00
|
|
|
self.equipped_secondary = equipped_secondary
|
|
|
|
self.equipped_helmet = equipped_helmet
|
2020-11-11 22:41:06 +00:00
|
|
|
|
2020-11-10 21:02:41 +00:00
|
|
|
def move(self, y: int, x: int) -> None:
|
|
|
|
"""
|
2020-11-19 15:17:02 +00:00
|
|
|
Moves the view of the map (the point on which the camera is centered)
|
|
|
|
according to the moves of the player.
|
2020-11-10 21:02:41 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Add as many levels as possible to the player.
|
2020-11-11 00:07:19 +00:00
|
|
|
"""
|
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-19 15:17:02 +00:00
|
|
|
self.strength = self.strength + 1
|
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
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Adds some experience to the player.
|
|
|
|
If the required amount is reached, the player levels up.
|
2020-11-11 00:07:19 +00:00
|
|
|
"""
|
2021-01-06 10:44:52 +00:00
|
|
|
self.current_xp += int(xp*self.xp_buff)
|
2020-11-06 17:12:17 +00:00
|
|
|
self.level_up()
|
2020-11-10 23:50:47 +00:00
|
|
|
|
2020-12-18 16:57:42 +00:00
|
|
|
def remove_from_inventory(self, obj: Item) -> None:
|
|
|
|
"""
|
|
|
|
Remove the given item from the inventory, even if the item is equipped.
|
|
|
|
"""
|
2021-01-06 09:46:36 +00:00
|
|
|
if obj == self.equipped_main:
|
|
|
|
self.equipped_main = None
|
2020-12-18 16:57:42 +00:00
|
|
|
elif obj == self.equipped_armor:
|
|
|
|
self.equipped_armor = None
|
2021-01-06 09:46:36 +00:00
|
|
|
elif obj == self.equipped_secondary:
|
|
|
|
self.equipped_secondary = None
|
|
|
|
elif obj == self.equipped_helmet:
|
|
|
|
self.equipped_helmet = None
|
2020-12-18 16:57:42 +00:00
|
|
|
else:
|
|
|
|
return super().remove_from_inventory(obj)
|
|
|
|
|
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.
|
2020-11-19 15:17:02 +00:00
|
|
|
If the entity dies, the player is rewarded with some XP
|
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():
|
2020-11-19 11:03:05 +00:00
|
|
|
self.map.logs.add_message(self.hit(entity))
|
2020-11-11 15:47:19 +00:00
|
|
|
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-18 21:42:46 +00:00
|
|
|
def save_state(self) -> dict:
|
|
|
|
"""
|
|
|
|
Saves the state of the entity into a dictionary
|
|
|
|
"""
|
|
|
|
d = super().save_state()
|
|
|
|
d["current_xp"] = self.current_xp
|
|
|
|
d["max_xp"] = self.max_xp
|
2021-01-06 09:46:36 +00:00
|
|
|
d["equipped_main"] = self.equipped_main.save_state()\
|
|
|
|
if self.equipped_main else None
|
2020-12-18 16:50:26 +00:00
|
|
|
d["equipped_armor"] = self.equipped_armor.save_state()\
|
|
|
|
if self.equipped_armor else None
|
2021-01-06 09:46:36 +00:00
|
|
|
d["equipped_secondary"] = self.equipped_secondary.save_state()\
|
|
|
|
if self.equipped_secondary else None
|
|
|
|
d["equipped_helmet"] = self.equipped_helmet.save_state()\
|
|
|
|
if self.equipped_helmet else None
|
2020-11-18 21:42:46 +00:00
|
|
|
return d
|