Player now pays for what he buys and buying a heart does not put it in the inventory. Solves #38 and #36

This commit is contained in:
eichhornchen 2020-12-11 16:49:17 +01:00
parent 7ba49277a9
commit b9b776b7ad
4 changed files with 75 additions and 12 deletions

View File

@ -3,6 +3,7 @@ from ..translations import gettext as _
from .player import Player
from .items import Item
from random import choice
from typing import Dict, Tuple, Any
class Merchant(FriendlyEntity):
@ -51,6 +52,24 @@ class Merchant(FriendlyEntity):
d["inventory"] = [item.save_state() for item in self.inventory]
return d
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 change_hazel_balance(self, hz: int) -> None:
"""
Change the number of hazel the merchant has by hz.
"""
self.hazel += hz
class Sunflower(FriendlyEntity):
"""

View File

@ -2,7 +2,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from random import choice, randint
from typing import Optional
from typing import Optional, Any
from .player import Player
from ..interfaces import Entity, FightingEntity, Map
@ -15,12 +15,14 @@ class Item(Entity):
"""
held: bool
held_by: Optional[Player]
price: int
def __init__(self, held: bool = False, held_by: Optional[Player] = None,
def __init__(self, held: bool = False, held_by: Optional[Player] = None, price : int=2,
*args, **kwargs):
super().__init__(*args, **kwargs)
self.held = held
self.held_by = held_by
self.price = price
def drop(self) -> None:
"""
@ -64,6 +66,19 @@ class Item(Entity):
def get_all_items() -> list:
return [BodySnatchPotion, Bomb, Heart, Sword]
def be_sold(self, buyer: Entity, seller: Entity, 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 inventory
"""
if buyer.hazel>=self.price :
buyer.add_to_inventory(self)
seller.remove_from_inventory(self)
buyer.change_hazel_balance(-self.price)
seller.change_hazel_balance(self.price)
return True
else :
return False
class Heart(Item):
"""
@ -71,8 +86,8 @@ class Heart(Item):
"""
healing: int
def __init__(self, name: str = "heart", healing: int = 5, *args, **kwargs):
super().__init__(name=name, *args, **kwargs)
def __init__(self, name: str = "heart", healing: int = 5, price: int = 3, *args, **kwargs):
super().__init__(name=name, price=price, *args, **kwargs)
self.healing = healing
def hold(self, player: "Player") -> None:
@ -90,6 +105,20 @@ class Heart(Item):
d["healing"] = self.healing
return d
def be_sold(self, buyer: Entity, seller: Entity, game: Any) -> str:
"""
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 :
self.hold(buyer)
seller.remove_from_inventory(self)
buyer.change_hazel_balance(-self.price)
seller.change_hazel_balance(self.price)
return True
else :
return False
class Bomb(Item):
"""
@ -101,8 +130,8 @@ class Bomb(Item):
tick: int
def __init__(self, name: str = "bomb", damage: int = 5,
exploding: bool = False, *args, **kwargs):
super().__init__(name=name, *args, **kwargs)
exploding: bool = False, price: int = 4, *args, **kwargs):
super().__init__(name=name, price=price, *args, **kwargs)
self.damage = damage
self.exploding = exploding
self.tick = 4
@ -172,8 +201,8 @@ class Sword(Weapon):
"""
A basic weapon
"""
def __init__(self, name: str = "sword", *args, **kwargs):
super().__init__(name=name, *args, **kwargs)
def __init__(self, name: str = "sword", price: int = 20, *args, **kwargs):
super().__init__(name=name, price=price, *args, **kwargs)
self.name = name
@ -183,8 +212,8 @@ class BodySnatchPotion(Item):
other entity.
"""
def __init__(self, name: str = "body_snatch_potion", *args, **kwargs):
super().__init__(name=name, *args, **kwargs)
def __init__(self, name: str = "body_snatch_potion", price: int = 14, *args, **kwargs):
super().__init__(name=name, price=price, *args, **kwargs)
def use(self) -> None:
"""

View File

@ -70,6 +70,13 @@ 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:
@ -125,6 +132,12 @@ class Player(FightingEntity):
"""
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

View File

@ -194,8 +194,10 @@ class Game:
if self.store_menu.values and not self.player.dead:
if key == KeyValues.ENTER:
item = self.store_menu.validate()
item.hold(self.player)
self.store_menu.merchant.inventory.remove(item)
flag = item.be_sold(self.player, self.store_menu.merchant, self)
if not flag :
self.message = _("You do not have enough money")
self.display_actions(DisplayActions.UPDATE)
# Ensure that the cursor has a good position
self.store_menu.position = min(self.store_menu.position,
len(self.store_menu.values) - 1)