From 46ce7c33bf3dcabc5465c89c89490ed69bbc0056 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 18 Dec 2020 15:15:47 +0100 Subject: [PATCH] Merchant menu is updated through its update function, and does not access globally to the Game instance --- squirrelbattle/display/logsdisplay.py | 1 + squirrelbattle/display/mapdisplay.py | 1 + squirrelbattle/display/menudisplay.py | 17 ++++++++++++----- squirrelbattle/game.py | 10 ++++------ 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/squirrelbattle/display/logsdisplay.py b/squirrelbattle/display/logsdisplay.py index b21273d..bb53e48 100644 --- a/squirrelbattle/display/logsdisplay.py +++ b/squirrelbattle/display/logsdisplay.py @@ -7,6 +7,7 @@ from squirrelbattle.interfaces import Logs class LogsDisplay(Display): + logs: Logs def __init__(self, *args) -> None: super().__init__(*args) diff --git a/squirrelbattle/display/mapdisplay.py b/squirrelbattle/display/mapdisplay.py index 72e339c..b10619e 100644 --- a/squirrelbattle/display/mapdisplay.py +++ b/squirrelbattle/display/mapdisplay.py @@ -7,6 +7,7 @@ from ..game import Game class MapDisplay(Display): + map: Map def __init__(self, *args): super().__init__(*args) diff --git a/squirrelbattle/display/menudisplay.py b/squirrelbattle/display/menudisplay.py index ed10fec..c5dfda5 100644 --- a/squirrelbattle/display/menudisplay.py +++ b/squirrelbattle/display/menudisplay.py @@ -144,21 +144,25 @@ class MainMenuDisplay(Display): class PlayerInventoryDisplay(MenuDisplay): + selected: bool = True + store_mode: bool = False + def update(self, game: Game) -> None: 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: self.menubox.update_title(_("INVENTORY")) for i, item in enumerate(self.menu.values): rep = self.pack[item.name.upper()] selection = f"[{rep}]" if i == self.menu.position \ - and (Game.INSTANCE.state == GameMode.INVENTORY - or Game.INSTANCE.state == GameMode.STORE - and not Game.INSTANCE.is_in_store_menu) else f" {rep} " + and self.selected else f" {rep} " self.addstr(self.pad, i + 1, 0, selection + " " + item.translated_name.capitalize() + (": " + str(item.price) + " Hazels" - if Game.INSTANCE.state == GameMode.STORE else "")) + if self.store_mode else "")) @property def truewidth(self) -> int: @@ -178,15 +182,18 @@ class PlayerInventoryDisplay(MenuDisplay): class StoreInventoryDisplay(MenuDisplay): + selected: bool = False + def update(self, game: Game) -> None: self.update_menu(game.store_menu) + self.selected = game.is_in_store_menu def update_pad(self) -> None: self.menubox.update_title(_("STALL")) for i, item in enumerate(self.menu.values): rep = self.pack[item.name.upper()] 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 + " " + item.translated_name.capitalize() + ": " + str(item.price) + " Hazels") diff --git a/squirrelbattle/game.py b/squirrelbattle/game.py index 2204508..b0a65f4 100644 --- a/squirrelbattle/game.py +++ b/squirrelbattle/game.py @@ -22,9 +22,6 @@ class Game: """ The game object controls all actions in the game. """ - # Global instance of the game - INSTANCE: "Game" - map: Map player: Player screen: Any @@ -35,8 +32,6 @@ class Game: """ Init the game. """ - Game.INSTANCE = self - self.state = GameMode.MAINMENU self.waiting_for_friendly_key = False self.is_in_store_menu = True @@ -170,6 +165,7 @@ class Game: self.state = GameMode.STORE self.is_in_store_menu = True self.store_menu.update_merchant(entity) + self.display_actions(DisplayActions.UPDATE) def handle_key_pressed_inventory(self, key: KeyValues) -> None: """ @@ -208,8 +204,10 @@ class Game: menu.go_down() elif key == KeyValues.LEFT: self.is_in_store_menu = False + self.display_actions(DisplayActions.UPDATE) elif key == KeyValues.RIGHT: self.is_in_store_menu = True + self.display_actions(DisplayActions.UPDATE) if menu.values and not self.player.dead: if key == KeyValues.ENTER: item = menu.validate() @@ -220,7 +218,7 @@ class Game: flag = item.be_sold(buyer, owner) if not flag: self.message = _("The buyer does not have enough money") - self.display_actions(DisplayActions.UPDATE) + self.display_actions(DisplayActions.UPDATE) # Ensure that the cursor has a good position menu.position = min(menu.position, len(menu.values) - 1)