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

@ -5,7 +5,7 @@ from random import choice, randint
from typing import Optional, Any
from .player import Player
from ..interfaces import Entity, FightingEntity, Map
from ..interfaces import Entity, FightingEntity, Map, InventoryHolder
from ..translations import gettext as _
@ -66,7 +66,8 @@ class Item(Entity):
def get_all_items() -> list:
return [BodySnatchPotion, Bomb, Heart, Sword]
def be_sold(self, buyer: Entity, seller: Entity, game: Any) -> bool:
def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder,
game: Any) -> bool:
"""
Does all necessary actions when an object is to be sold.
Is overwritten by some classes that cannot exist in the player's
@ -93,12 +94,12 @@ class Heart(Item):
super().__init__(name=name, price=price, *args, **kwargs)
self.healing = healing
def hold(self, player: "Player") -> None:
def hold(self, entity: InventoryHolder) -> None:
"""
When holding a heart, heal the player and don't put item in inventory.
"""
player.health = min(player.maxhealth, player.health + self.healing)
player.map.remove_entity(self)
entity.health = min(entity.maxhealth, entity.health + self.healing)
entity.map.remove_entity(self)
def save_state(self) -> dict:
"""
@ -108,7 +109,8 @@ class Heart(Item):
d["healing"] = self.healing
return d
def be_sold(self, buyer: Entity, seller: Entity, game: Any) -> bool:
def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder,
game: Any) -> bool:
"""
Does all necessary actions when an object is to be sold.
Is overwritten by some classes that cannot exist in the player's