Merchant menu is updated through its update function, and does not access globally to the Game instance
This commit is contained in:
		@@ -7,6 +7,7 @@ from squirrelbattle.interfaces import Logs
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class LogsDisplay(Display):
 | 
			
		||||
    logs: Logs
 | 
			
		||||
 | 
			
		||||
    def __init__(self, *args) -> None:
 | 
			
		||||
        super().__init__(*args)
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@ from ..game import Game
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class MapDisplay(Display):
 | 
			
		||||
    map: Map
 | 
			
		||||
 | 
			
		||||
    def __init__(self, *args):
 | 
			
		||||
        super().__init__(*args)
 | 
			
		||||
 
 | 
			
		||||
@@ -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")
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user