Display more precisely where we are in the store menu

This commit is contained in:
Yohann D'ANELLO 2020-12-18 01:50:11 +01:00
parent 9a556ba669
commit c55a7451e7
4 changed files with 24 additions and 6 deletions

View File

@ -7,7 +7,7 @@ from typing import List
from squirrelbattle.menus import Menu, MainMenu from squirrelbattle.menus import Menu, MainMenu
from .display import Box, Display from .display import Box, Display
from ..enums import KeyValues from ..enums import KeyValues, GameMode
from ..game import Game from ..game import Game
from ..resources import ResourceManager from ..resources import ResourceManager
from ..translations import gettext as _ from ..translations import gettext as _
@ -139,9 +139,14 @@ class PlayerInventoryDisplay(MenuDisplay):
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 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 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 @property
def truewidth(self) -> int: def truewidth(self) -> int:
@ -156,6 +161,7 @@ class PlayerInventoryDisplay(MenuDisplay):
We can select a menu item with the mouse. We can select a menu item with the mouse.
""" """
self.menu.position = max(0, min(len(self.menu.values) - 1, y - 2)) 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) game.handle_key_pressed(KeyValues.ENTER)
@ -164,7 +170,8 @@ class StoreInventoryDisplay(MenuDisplay):
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 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 self.addstr(self.pad, i + 1, 0, selection
+ " " + item.translated_name.capitalize() + " " + item.translated_name.capitalize()
+ ": " + str(item.price) + " Hazels") + ": " + str(item.price) + " Hazels")
@ -182,4 +189,5 @@ class StoreInventoryDisplay(MenuDisplay):
We can select a menu item with the mouse. We can select a menu item with the mouse.
""" """
self.menu.position = max(0, min(len(self.menu.values) - 1, y - 2)) 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) game.handle_key_pressed(KeyValues.ENTER)

View File

@ -97,7 +97,7 @@ TexturePack.SQUIRREL_PACK = TexturePack(
MERCHANT='🦜', MERCHANT='🦜',
RABBIT='🐇', RABBIT='🐇',
SUNFLOWER='🌻', SUNFLOWER='🌻',
SWORD='🗡️', SWORD='🗡️ ',
TEDDY_BEAR='🧸', TEDDY_BEAR='🧸',
TIGER='🐅', TIGER='🐅',
WALL='🧱', WALL='🧱',

View File

@ -22,6 +22,9 @@ 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
@ -32,6 +35,8 @@ 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
@ -52,7 +57,7 @@ class Game:
Create a new game on the screen. Create a new game on the screen.
""" """
# TODO generate a new map procedurally # 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.map.logs = self.logs
self.logs.clear() self.logs.clear()
self.player = Player() self.player = Player()
@ -163,6 +168,7 @@ class Game:
self.logs.add_message(msg) self.logs.add_message(msg)
if entity.is_merchant(): if entity.is_merchant():
self.state = GameMode.STORE self.state = GameMode.STORE
self.is_in_store_menu = True
self.store_menu.update_merchant(entity) self.store_menu.update_merchant(entity)
def handle_key_pressed_inventory(self, key: KeyValues) -> None: def handle_key_pressed_inventory(self, key: KeyValues) -> None:

View File

@ -546,6 +546,10 @@ class TestGame(unittest.TestCase):
# Navigate in the menu # Navigate in the menu
self.game.handle_key_pressed(KeyValues.DOWN) self.game.handle_key_pressed(KeyValues.DOWN)
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.game.handle_key_pressed(KeyValues.UP)
self.assertEqual(self.game.store_menu.position, 1) self.assertEqual(self.game.store_menu.position, 1)