From c55a7451e78dca7b2e705500179e01d5d9abbc02 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 18 Dec 2020 01:50:11 +0100 Subject: [PATCH] Display more precisely where we are in the store menu --- squirrelbattle/display/menudisplay.py | 16 ++++++++++++---- squirrelbattle/display/texturepack.py | 2 +- squirrelbattle/game.py | 8 +++++++- squirrelbattle/tests/game_test.py | 4 ++++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/squirrelbattle/display/menudisplay.py b/squirrelbattle/display/menudisplay.py index a00d0fe..f9a5e09 100644 --- a/squirrelbattle/display/menudisplay.py +++ b/squirrelbattle/display/menudisplay.py @@ -7,7 +7,7 @@ from typing import List from squirrelbattle.menus import Menu, MainMenu from .display import Box, Display -from ..enums import KeyValues +from ..enums import KeyValues, GameMode from ..game import Game from ..resources import ResourceManager from ..translations import gettext as _ @@ -139,9 +139,14 @@ class PlayerInventoryDisplay(MenuDisplay): 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 else f" {rep} " + 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} " self.addstr(self.pad, i + 1, 0, selection - + " " + item.translated_name.capitalize()) + + " " + item.translated_name.capitalize() + + (": " + str(item.price) + " Hazels" + if Game.INSTANCE.state == GameMode.STORE else "")) @property def truewidth(self) -> int: @@ -156,6 +161,7 @@ class PlayerInventoryDisplay(MenuDisplay): We can select a menu item with the mouse. """ self.menu.position = max(0, min(len(self.menu.values) - 1, y - 2)) + game.is_in_store_menu = False game.handle_key_pressed(KeyValues.ENTER) @@ -164,7 +170,8 @@ class StoreInventoryDisplay(MenuDisplay): 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 else f" {rep} " + selection = f"[{rep}]" if i == self.menu.position \ + and Game.INSTANCE.is_in_store_menu else f" {rep} " self.addstr(self.pad, i + 1, 0, selection + " " + item.translated_name.capitalize() + ": " + str(item.price) + " Hazels") @@ -182,4 +189,5 @@ class StoreInventoryDisplay(MenuDisplay): We can select a menu item with the mouse. """ self.menu.position = max(0, min(len(self.menu.values) - 1, y - 2)) + game.is_in_store_menu = True game.handle_key_pressed(KeyValues.ENTER) diff --git a/squirrelbattle/display/texturepack.py b/squirrelbattle/display/texturepack.py index f72cd97..1f6dc76 100644 --- a/squirrelbattle/display/texturepack.py +++ b/squirrelbattle/display/texturepack.py @@ -97,7 +97,7 @@ TexturePack.SQUIRREL_PACK = TexturePack( MERCHANT='🦜', RABBIT='🐇', SUNFLOWER='🌻', - SWORD='🗡️', + SWORD='🗡️ ', TEDDY_BEAR='🧸', TIGER='🐅', WALL='🧱', diff --git a/squirrelbattle/game.py b/squirrelbattle/game.py index 5fc130b..2204508 100644 --- a/squirrelbattle/game.py +++ b/squirrelbattle/game.py @@ -22,6 +22,9 @@ class Game: """ The game object controls all actions in the game. """ + # Global instance of the game + INSTANCE: "Game" + map: Map player: Player screen: Any @@ -32,6 +35,8 @@ class Game: """ Init the game. """ + Game.INSTANCE = self + self.state = GameMode.MAINMENU self.waiting_for_friendly_key = False self.is_in_store_menu = True @@ -52,7 +57,7 @@ class Game: Create a new game on the screen. """ # TODO generate a new map procedurally - self.map = Map.load(ResourceManager.get_asset_path("example_map.txt")) + self.map = Map.load(ResourceManager.get_asset_path("example_map_2.txt")) self.map.logs = self.logs self.logs.clear() self.player = Player() @@ -163,6 +168,7 @@ class Game: self.logs.add_message(msg) if entity.is_merchant(): self.state = GameMode.STORE + self.is_in_store_menu = True self.store_menu.update_merchant(entity) def handle_key_pressed_inventory(self, key: KeyValues) -> None: diff --git a/squirrelbattle/tests/game_test.py b/squirrelbattle/tests/game_test.py index c70ca8f..2b604be 100644 --- a/squirrelbattle/tests/game_test.py +++ b/squirrelbattle/tests/game_test.py @@ -546,6 +546,10 @@ class TestGame(unittest.TestCase): # Navigate in the menu self.game.handle_key_pressed(KeyValues.DOWN) self.game.handle_key_pressed(KeyValues.DOWN) + self.game.handle_key_pressed(KeyValues.LEFT) + self.assertFalse(self.game.is_in_store_menu) + self.game.handle_key_pressed(KeyValues.RIGHT) + self.assertTrue(self.game.is_in_store_menu) self.game.handle_key_pressed(KeyValues.UP) self.assertEqual(self.game.store_menu.position, 1)