Added chests, they are immortal and contain objects the player can take for free.

This commit is contained in:
eichhornchen
2021-01-08 23:15:48 +01:00
parent 175706b1e4
commit bdbf214d8d
9 changed files with 178 additions and 13 deletions

View File

@ -38,6 +38,39 @@ class Merchant(InventoryHolder, FriendlyEntity):
"""
self.hazel += hz
class Chest(InventoryHolder, FriendlyEntity):
"""
A class of chest inanimate entities which contain objects.
"""
def __init__(self, name: str = "chest", inventory: list = None,
hazel: int = 0, *args, **kwargs):
super().__init__(name=name, *args, **kwargs)
self.hazel = hazel
self.inventory = self.translate_inventory(inventory or [])
if not self.inventory:
for i in range(3):
self.inventory.append(choice(Item.get_all_items())())
def talk_to(self, player: Player) -> str:
"""
This function is used to open the chest's inventory in a menu,
and allows the player to take objects.
"""
return _("You have opened the chest")
def take_damage(self, attacker: "Entity", amount: int) -> str:
"""
A chest is not living, it can not take damage
"""
return _("It's not really effective")
@property
def dead(self) -> bool:
"""
Chest can not die
"""
return False
class Sunflower(FriendlyEntity):
"""

View File

@ -88,13 +88,18 @@ class Item(Entity):
Chestplate, Helmet, RingCritical, RingXP,
ScrollofDamage, ScrollofWeakening, Ruler, Bow, FireBallStaff]
def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder) -> bool:
def be_sold(self, buyer: InventoryHolder, seller: InventoryHolder,\
for_free: bool = False) -> bool:
"""
Does all necessary actions when an object is to be sold.
Is overwritten by some classes that cannot exist in the player's
inventory.
"""
if buyer.hazel >= self.price:
if for_free:
self.hold(buyer)
seller.remove_from_inventory(self)
return True
elif buyer.hazel >= self.price:
self.hold(buyer)
seller.remove_from_inventory(self)
buyer.change_hazel_balance(-self.price)