Add a InventoryHolder superclass for player and merchants

This commit is contained in:
Yohann D'ANELLO
2020-12-11 17:20:50 +01:00
parent 98b5dd64a8
commit 7179346e2b
4 changed files with 68 additions and 73 deletions

View File

@ -2,20 +2,18 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from random import randint
from typing import Dict, Tuple, Any
from typing import Dict, Tuple
from ..interfaces import FightingEntity
from ..interfaces import FightingEntity, InventoryHolder
class Player(FightingEntity):
class Player(InventoryHolder, FightingEntity):
"""
The class of the player
"""
current_xp: int = 0
max_xp: int = 10
inventory: list
paths: Dict[Tuple[int, int], Tuple[int, int]]
hazel: int # It is the currency of this game
def __init__(self, name: str = "player", maxhealth: int = 20,
strength: int = 5, intelligence: int = 1, charisma: int = 1,
@ -29,12 +27,7 @@ class Player(FightingEntity):
level=level, *args, **kwargs)
self.current_xp = current_xp
self.max_xp = max_xp
self.inventory = inventory if inventory else list()
entity_classes = self.get_all_entity_classes_in_a_dict()
for i in range(len(self.inventory)):
if isinstance(self.inventory[i], dict):
item_class = entity_classes[self.inventory[i]["type"]]
self.inventory[i] = item_class(**self.inventory[i])
self.inventory = self.translate_inventory(inventory or [])
self.paths = dict()
self.hazel = hazel
@ -70,13 +63,6 @@ class Player(FightingEntity):
self.current_xp += xp
self.level_up()
def change_hazel_balance(self, hz: int) -> None:
"""
Change the number of hazel the player has by hz. hz is negative
when the player loses money and positive when he gains money
"""
self.hazel += hz
# noinspection PyTypeChecker,PyUnresolvedReferences
def check_move(self, y: int, x: int, move_if_possible: bool = False) \
-> bool:
@ -126,18 +112,6 @@ class Player(FightingEntity):
queue.append((new_y, new_x))
self.paths = predecessors
def add_to_inventory(self, obj: Any) -> None:
"""
Adds an object to inventory
"""
self.inventory.append(obj)
def remove_from_inventory(self, obj: Any) -> None:
"""
Removes an object from the inventory
"""
self.inventory.remove(obj)
def save_state(self) -> dict:
"""
Saves the state of the entity into a dictionary
@ -145,6 +119,4 @@ class Player(FightingEntity):
d = super().save_state()
d["current_xp"] = self.current_xp
d["max_xp"] = self.max_xp
d["inventory"] = [item.save_state() for item in self.inventory]
d["hazel"] = self.hazel
return d