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:
parent
7ba49277a9
commit
b9b776b7ad
|
@ -3,6 +3,7 @@ from ..translations import gettext as _
|
||||||
from .player import Player
|
from .player import Player
|
||||||
from .items import Item
|
from .items import Item
|
||||||
from random import choice
|
from random import choice
|
||||||
|
from typing import Dict, Tuple, Any
|
||||||
|
|
||||||
|
|
||||||
class Merchant(FriendlyEntity):
|
class Merchant(FriendlyEntity):
|
||||||
|
@ -51,6 +52,24 @@ class Merchant(FriendlyEntity):
|
||||||
d["inventory"] = [item.save_state() for item in self.inventory]
|
d["inventory"] = [item.save_state() for item in self.inventory]
|
||||||
return d
|
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):
|
class Sunflower(FriendlyEntity):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
from random import choice, randint
|
from random import choice, randint
|
||||||
from typing import Optional
|
from typing import Optional, Any
|
||||||
|
|
||||||
from .player import Player
|
from .player import Player
|
||||||
from ..interfaces import Entity, FightingEntity, Map
|
from ..interfaces import Entity, FightingEntity, Map
|
||||||
|
@ -15,12 +15,14 @@ class Item(Entity):
|
||||||
"""
|
"""
|
||||||
held: bool
|
held: bool
|
||||||
held_by: Optional[Player]
|
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):
|
*args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.held = held
|
self.held = held
|
||||||
self.held_by = held_by
|
self.held_by = held_by
|
||||||
|
self.price = price
|
||||||
|
|
||||||
def drop(self) -> None:
|
def drop(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -64,6 +66,19 @@ class Item(Entity):
|
||||||
def get_all_items() -> list:
|
def get_all_items() -> list:
|
||||||
return [BodySnatchPotion, Bomb, Heart, Sword]
|
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):
|
class Heart(Item):
|
||||||
"""
|
"""
|
||||||
|
@ -71,8 +86,8 @@ class Heart(Item):
|
||||||
"""
|
"""
|
||||||
healing: int
|
healing: int
|
||||||
|
|
||||||
def __init__(self, name: str = "heart", healing: int = 5, *args, **kwargs):
|
def __init__(self, name: str = "heart", healing: int = 5, price: int = 3, *args, **kwargs):
|
||||||
super().__init__(name=name, *args, **kwargs)
|
super().__init__(name=name, price=price, *args, **kwargs)
|
||||||
self.healing = healing
|
self.healing = healing
|
||||||
|
|
||||||
def hold(self, player: "Player") -> None:
|
def hold(self, player: "Player") -> None:
|
||||||
|
@ -90,6 +105,20 @@ class Heart(Item):
|
||||||
d["healing"] = self.healing
|
d["healing"] = self.healing
|
||||||
return d
|
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):
|
class Bomb(Item):
|
||||||
"""
|
"""
|
||||||
|
@ -101,8 +130,8 @@ class Bomb(Item):
|
||||||
tick: int
|
tick: int
|
||||||
|
|
||||||
def __init__(self, name: str = "bomb", damage: int = 5,
|
def __init__(self, name: str = "bomb", damage: int = 5,
|
||||||
exploding: bool = False, *args, **kwargs):
|
exploding: bool = False, price: int = 4, *args, **kwargs):
|
||||||
super().__init__(name=name, *args, **kwargs)
|
super().__init__(name=name, price=price, *args, **kwargs)
|
||||||
self.damage = damage
|
self.damage = damage
|
||||||
self.exploding = exploding
|
self.exploding = exploding
|
||||||
self.tick = 4
|
self.tick = 4
|
||||||
|
@ -172,8 +201,8 @@ class Sword(Weapon):
|
||||||
"""
|
"""
|
||||||
A basic weapon
|
A basic weapon
|
||||||
"""
|
"""
|
||||||
def __init__(self, name: str = "sword", *args, **kwargs):
|
def __init__(self, name: str = "sword", price: int = 20, *args, **kwargs):
|
||||||
super().__init__(name=name, *args, **kwargs)
|
super().__init__(name=name, price=price, *args, **kwargs)
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
|
|
||||||
|
@ -183,8 +212,8 @@ class BodySnatchPotion(Item):
|
||||||
other entity.
|
other entity.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, name: str = "body_snatch_potion", *args, **kwargs):
|
def __init__(self, name: str = "body_snatch_potion", price: int = 14, *args, **kwargs):
|
||||||
super().__init__(name=name, *args, **kwargs)
|
super().__init__(name=name, price=price, *args, **kwargs)
|
||||||
|
|
||||||
def use(self) -> None:
|
def use(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -70,6 +70,13 @@ class Player(FightingEntity):
|
||||||
self.current_xp += xp
|
self.current_xp += xp
|
||||||
self.level_up()
|
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
|
# noinspection PyTypeChecker,PyUnresolvedReferences
|
||||||
def check_move(self, y: int, x: int, move_if_possible: bool = False) \
|
def check_move(self, y: int, x: int, move_if_possible: bool = False) \
|
||||||
-> bool:
|
-> bool:
|
||||||
|
@ -125,6 +132,12 @@ class Player(FightingEntity):
|
||||||
"""
|
"""
|
||||||
self.inventory.append(obj)
|
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:
|
def save_state(self) -> dict:
|
||||||
"""
|
"""
|
||||||
Saves the state of the entity into a dictionary
|
Saves the state of the entity into a dictionary
|
||||||
|
|
|
@ -194,8 +194,10 @@ class Game:
|
||||||
if self.store_menu.values and not self.player.dead:
|
if self.store_menu.values and not self.player.dead:
|
||||||
if key == KeyValues.ENTER:
|
if key == KeyValues.ENTER:
|
||||||
item = self.store_menu.validate()
|
item = self.store_menu.validate()
|
||||||
item.hold(self.player)
|
flag = item.be_sold(self.player, self.store_menu.merchant, self)
|
||||||
self.store_menu.merchant.inventory.remove(item)
|
if not flag :
|
||||||
|
self.message = _("You do not have enough money")
|
||||||
|
self.display_actions(DisplayActions.UPDATE)
|
||||||
# Ensure that the cursor has a good position
|
# Ensure that the cursor has a good position
|
||||||
self.store_menu.position = min(self.store_menu.position,
|
self.store_menu.position = min(self.store_menu.position,
|
||||||
len(self.store_menu.values) - 1)
|
len(self.store_menu.values) - 1)
|
||||||
|
|
Loading…
Reference in New Issue