Skip to content

Commit

Permalink
text - New function to display shopkeeper balance, modified STR_THIS_…
Browse files Browse the repository at this point in the history
…ITEM_CANT_BE_SOLD to better fit context

shop - Added new variable shop_balance; initialized to 500 for testing, buy and sell functions adjusted to update vendor amount
menu_creator_manager - create_shop_menu updated to show vendor gold in UI
load xml, load tmx, level_scene - added shop_balance parameter to constructors

Additional Notes: Shop balance does not show on sell interface. Shop balance value is not updated when saved, may be due to the fact that they were set to 500 in the constructor. The load xml, load tmx, and level_scene files need to be reviewed for confirmation. The basic functionality is there, but tweaks still need to be made.
  • Loading branch information
Sonny-Cao committed Feb 11, 2024
1 parent 3e764fc commit d6d7c79
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
7 changes: 5 additions & 2 deletions data/en/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def f_LEVEL_NUMBER_AND_NAME(number: int, name: str):
STR_END_TURN = "End turn"
STR_DEFAULT_DIARY_BODY_CONTENT = "No event has been recorded yet"


# Reward menu
STR_REWARD_CONGRATULATIONS = "Congratulations! Objective has been completed!"

Expand Down Expand Up @@ -90,6 +89,10 @@ def f_UR_GOLD(gold):
return f"Your gold: {gold}"


def f_SHOP_GOLD(shop_balance):
return f"Shopkeeper gold: {shop_balance}"


# Trade menu
STR_50G_TO_RIGHT = "50G ->"
STR_200G_TO_RIGHT = "200G ->"
Expand Down Expand Up @@ -256,7 +259,7 @@ def f_YOU_RECEIVED_ITEM(item):
)
STR_NOT_ENOUGH_GOLD_TO_BY_THIS_ITEM = "Not enough gold to buy this item."
STR_THE_ITEM_HAS_BEEN_SOLD = "The item has been sold."
STR_THIS_ITEM_CANT_BE_SOLD = "This item can't be sold !"
STR_THIS_ITEM_CANT_BE_SOLD = "The vendor lacks the funds to buy from you !"
STR_THIS_HOUSE_SEEMS_CLOSED = "This house seems closed..."


Expand Down
12 changes: 8 additions & 4 deletions src/game_entities/shop.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Shop(Building):
stock -- the data structure containing all the available items to be bought with their associated quantity
menu -- the shop menu displaying all the items that could be bought
gold_sfx -- the sound that should be started when an item is sold or bought
shop_balance -- the amount of gold the shop has
"""

interaction_callback = None
Expand All @@ -50,16 +51,18 @@ def __init__(
name: str,
position: Position,
sprite_link: str,
shop_balance: int,
stock: list[dict[str, any]],
interaction: Optional[dict[str, any]] = None,
sprite: Optional[pygame.Surface] = None,
) -> None:
super().__init__(name, position, sprite_link, interaction, sprite)
self.shop_balance = 500
self.current_visitor: Optional[Character] = None
self.stock: list[dict[str, any]] = stock
self.interaction: dict[str, any] = interaction
self.menu: InfoBox = menu_creator_manager.create_shop_menu(
Shop.interaction_callback, self.stock, 0
Shop.interaction_callback, self.stock, 0, shop_balance
)
self.gold_sfx: pygame.mixer.Sound = pygame.mixer.Sound(
os.path.join("sound_fx", "trade.ogg")
Expand All @@ -85,7 +88,7 @@ def update_shop_menu(self, gold: int) -> None:
gold -- the new gold amount for the player that should be displayed
"""
self.menu = menu_creator_manager.create_shop_menu(
Shop.interaction_callback, self.stock, gold
Shop.interaction_callback, self.stock, gold, self.shop_balance
)

def interact(self, actor: Character) -> list[list[BoxElement]]:
Expand Down Expand Up @@ -127,9 +130,9 @@ def buy(self, item: Item) -> str:
if self.current_visitor.gold >= item.price:
if len(self.current_visitor.items) < self.current_visitor.nb_items_max:
pygame.mixer.Sound.play(self.gold_sfx)

self.current_visitor.gold -= item.price
self.current_visitor.set_item(copy(item))
self.shop_balance += item.price

entry: Optional[dict[str, any]] = self.get_item_entry(item)
entry["quantity"] -= 1
Expand All @@ -153,9 +156,10 @@ def sell(self, item: Item) -> tuple[bool, str]:
actor -- the actor selling the item
item -- the item that is being sold
"""
if item.resell_price > 0:
if 0 < item.resell_price < self.shop_balance:
self.current_visitor.remove_item(item)
self.current_visitor.gold += item.resell_price
self.shop_balance -= item.resell_price

# Update shop screen content (gold total amount has been augmented)
self.update_shop_menu(self.current_visitor.gold)
Expand Down
2 changes: 1 addition & 1 deletion src/scenes/level_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def load_level_content(self) -> None:
self.entities.chests = tmx_loader.load_chests(self.tmx_data, gap_x, gap_y)
self.entities.allies = tmx_loader.load_allies(self.tmx_data, gap_x, gap_y)
self.entities.buildings = tmx_loader.load_buildings(
self.tmx_data, DATA_PATH + self.directory, gap_x, gap_y
self.tmx_data, DATA_PATH + self.directory, gap_x, gap_y, self.active_shop.shop_balance
)
self.entities.breakables = tmx_loader.load_breakables(
self.tmx_data, gap_x, gap_y
Expand Down
3 changes: 2 additions & 1 deletion src/services/load_from_tmx_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def load_events(


def load_buildings(
tmx_data: pytmx.TiledMap, directory: str, horizontal_gap: int, vertical_gap: int
tmx_data: pytmx.TiledMap, directory: str, horizontal_gap: int, vertical_gap: int, shop_balance: int
) -> list[Building]:
buildings = []
for tile_object in tmx_data.get_layer_by_name("dynamic_data"):
Expand Down Expand Up @@ -364,6 +364,7 @@ def load_buildings(
tile_object.name,
position,
tile_object.properties["sprite_link"],
shop_balance,
stock,
interaction,
image,
Expand Down
5 changes: 3 additions & 2 deletions src/services/load_from_xml_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,12 +728,13 @@ def load_door_from_save(door, gap_x, gap_y):
return loaded_door


def load_building_from_save(building, gap_x, gap_y):
def load_building_from_save(building, gap_x, gap_y, shop_balance=None):
"""
:param building:
:param gap_x:
:param gap_y:
:shop_balance:
:return:
"""
# Static data
Expand Down Expand Up @@ -774,7 +775,7 @@ def load_building_from_save(building, gap_x, gap_y):
"quantity": int(item.find("quantity").text.strip()),
}
stock.append(entry)
loaded_building = Shop(name, position, sprite, stock, interaction_element)
loaded_building = Shop(name, position, sprite, shop_balance, stock, interaction_element)
else:
print("Error : building type isn't recognized : ", type)
raise SystemError
Expand Down
5 changes: 4 additions & 1 deletion src/services/menu_creator_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ def create_shop_menu(
interaction_callback: Callable,
stock: Sequence[dict[str, Union[Item, int]]],
gold: int,
) -> InfoBox:
shop_balance: int) -> InfoBox:
"""
Return the interface of a shop menu with user as the buyer.
Keyword arguments:
stock -- the collection of items that are available in the shop, with the quantity of each one
gold -- the amount of gold that should be displayed at the bottom
shop_balance -- the amount of gold that shopkeeper has
"""
element_grid = []
row = []
Expand Down Expand Up @@ -96,6 +97,8 @@ def create_shop_menu(
# Gold at end
player_gold_line = [TextElement(f_UR_GOLD(gold), font=fonts["ITEM_DESC_FONT"])]
element_grid.append(player_gold_line)
shop_gold_line = [TextElement(f_SHOP_GOLD(shop_balance), font=fonts["ITEM_DESC_FONT"])]
element_grid.append(shop_gold_line)

return InfoBox(
STR_SHOP_BUYING,
Expand Down

0 comments on commit d6d7c79

Please sign in to comment.