Merchant menu is updated through its update function, and does not access globally to the Game instance

This commit is contained in:
Yohann D'ANELLO 2020-12-18 15:15:47 +01:00
parent 85870494a0
commit 46ce7c33bf
4 changed files with 18 additions and 11 deletions

View File

@ -7,6 +7,7 @@ from squirrelbattle.interfaces import Logs
class LogsDisplay(Display): class LogsDisplay(Display):
logs: Logs
def __init__(self, *args) -> None: def __init__(self, *args) -> None:
super().__init__(*args) super().__init__(*args)

View File

@ -7,6 +7,7 @@ from ..game import Game
class MapDisplay(Display): class MapDisplay(Display):
map: Map
def __init__(self, *args): def __init__(self, *args):
super().__init__(*args) super().__init__(*args)

View File

@ -144,21 +144,25 @@ class MainMenuDisplay(Display):
class PlayerInventoryDisplay(MenuDisplay): class PlayerInventoryDisplay(MenuDisplay):
selected: bool = True
store_mode: bool = False
def update(self, game: Game) -> None: def update(self, game: Game) -> None:
self.update_menu(game.inventory_menu) self.update_menu(game.inventory_menu)
self.store_mode = game.state == GameMode.STORE
self.selected = game.state == GameMode.INVENTORY \
or self.store_mode and not game.is_in_store_menu
def update_pad(self) -> None: def update_pad(self) -> None:
self.menubox.update_title(_("INVENTORY")) self.menubox.update_title(_("INVENTORY"))
for i, item in enumerate(self.menu.values): for i, item in enumerate(self.menu.values):
rep = self.pack[item.name.upper()] rep = self.pack[item.name.upper()]
selection = f"[{rep}]" if i == self.menu.position \ selection = f"[{rep}]" if i == self.menu.position \
and (Game.INSTANCE.state == GameMode.INVENTORY and self.selected else f" {rep} "
or Game.INSTANCE.state == GameMode.STORE
and not Game.INSTANCE.is_in_store_menu) else f" {rep} "
self.addstr(self.pad, i + 1, 0, selection self.addstr(self.pad, i + 1, 0, selection
+ " " + item.translated_name.capitalize() + " " + item.translated_name.capitalize()
+ (": " + str(item.price) + " Hazels" + (": " + str(item.price) + " Hazels"
if Game.INSTANCE.state == GameMode.STORE else "")) if self.store_mode else ""))
@property @property
def truewidth(self) -> int: def truewidth(self) -> int:
@ -178,15 +182,18 @@ class PlayerInventoryDisplay(MenuDisplay):
class StoreInventoryDisplay(MenuDisplay): class StoreInventoryDisplay(MenuDisplay):
selected: bool = False
def update(self, game: Game) -> None: def update(self, game: Game) -> None:
self.update_menu(game.store_menu) self.update_menu(game.store_menu)
self.selected = game.is_in_store_menu
def update_pad(self) -> None: def update_pad(self) -> None:
self.menubox.update_title(_("STALL")) self.menubox.update_title(_("STALL"))
for i, item in enumerate(self.menu.values): for i, item in enumerate(self.menu.values):
rep = self.pack[item.name.upper()] rep = self.pack[item.name.upper()]
selection = f"[{rep}]" if i == self.menu.position \ selection = f"[{rep}]" if i == self.menu.position \
and Game.INSTANCE.is_in_store_menu else f" {rep} " and self.selected else f" {rep} "
self.addstr(self.pad, i + 1, 0, selection self.addstr(self.pad, i + 1, 0, selection
+ " " + item.translated_name.capitalize() + " " + item.translated_name.capitalize()
+ ": " + str(item.price) + " Hazels") + ": " + str(item.price) + " Hazels")

View File

@ -22,9 +22,6 @@ class Game:
""" """
The game object controls all actions in the game. The game object controls all actions in the game.
""" """
# Global instance of the game
INSTANCE: "Game"
map: Map map: Map
player: Player player: Player
screen: Any screen: Any
@ -35,8 +32,6 @@ class Game:
""" """
Init the game. Init the game.
""" """
Game.INSTANCE = self
self.state = GameMode.MAINMENU self.state = GameMode.MAINMENU
self.waiting_for_friendly_key = False self.waiting_for_friendly_key = False
self.is_in_store_menu = True self.is_in_store_menu = True
@ -170,6 +165,7 @@ class Game:
self.state = GameMode.STORE self.state = GameMode.STORE
self.is_in_store_menu = True self.is_in_store_menu = True
self.store_menu.update_merchant(entity) self.store_menu.update_merchant(entity)
self.display_actions(DisplayActions.UPDATE)
def handle_key_pressed_inventory(self, key: KeyValues) -> None: def handle_key_pressed_inventory(self, key: KeyValues) -> None:
""" """
@ -208,8 +204,10 @@ class Game:
menu.go_down() menu.go_down()
elif key == KeyValues.LEFT: elif key == KeyValues.LEFT:
self.is_in_store_menu = False self.is_in_store_menu = False
self.display_actions(DisplayActions.UPDATE)
elif key == KeyValues.RIGHT: elif key == KeyValues.RIGHT:
self.is_in_store_menu = True self.is_in_store_menu = True
self.display_actions(DisplayActions.UPDATE)
if menu.values and not self.player.dead: if menu.values and not self.player.dead:
if key == KeyValues.ENTER: if key == KeyValues.ENTER:
item = menu.validate() item = menu.validate()