Work in progress on printing a store menu. Its buggy though

This commit is contained in:
eichhornchen 2020-12-07 20:54:53 +01:00
parent d39427f978
commit 8540a8f354
7 changed files with 68 additions and 34 deletions

View File

@ -7,7 +7,7 @@ from squirrelbattle.display.mapdisplay import MapDisplay
from squirrelbattle.display.messagedisplay import MessageDisplay from squirrelbattle.display.messagedisplay import MessageDisplay
from squirrelbattle.display.statsdisplay import StatsDisplay from squirrelbattle.display.statsdisplay import StatsDisplay
from squirrelbattle.display.menudisplay import MainMenuDisplay, \ from squirrelbattle.display.menudisplay import MainMenuDisplay, \
InventoryDisplay, SettingsMenuDisplay PlayerInventoryDisplay, StoreInventoryDisplay, SettingsMenuDisplay
from squirrelbattle.display.logsdisplay import LogsDisplay from squirrelbattle.display.logsdisplay import LogsDisplay
from squirrelbattle.display.texturepack import TexturePack from squirrelbattle.display.texturepack import TexturePack
from typing import Any from typing import Any
@ -24,7 +24,8 @@ class DisplayManager:
self.mapdisplay = MapDisplay(screen, pack) self.mapdisplay = MapDisplay(screen, pack)
self.statsdisplay = StatsDisplay(screen, pack) self.statsdisplay = StatsDisplay(screen, pack)
self.logsdisplay = LogsDisplay(screen, pack) self.logsdisplay = LogsDisplay(screen, pack)
self.inventorydisplay = InventoryDisplay(screen, pack) self.playerinventorydisplay = PlayerInventoryDisplay(screen, pack)
self.storeinventorydisplay = StoreInventoryDisplay(screen, pack)
self.mainmenudisplay = MainMenuDisplay(self.game.main_menu, self.mainmenudisplay = MainMenuDisplay(self.game.main_menu,
screen, pack) screen, pack)
self.settingsmenudisplay = SettingsMenuDisplay(screen, pack) self.settingsmenudisplay = SettingsMenuDisplay(screen, pack)
@ -47,14 +48,18 @@ class DisplayManager:
d.pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK) d.pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
self.mapdisplay.update_map(self.game.map) self.mapdisplay.update_map(self.game.map)
self.statsdisplay.update_player(self.game.player) self.statsdisplay.update_player(self.game.player)
self.inventorydisplay.update_menu(self.game.inventory_menu) self.game.inventory_menu.update_player(self.game.player)
self.game.store_menu.update_merchant(self.game.player)
self.playerinventorydisplay.update_menu(self.game.inventory_menu)
self.storeinventorydisplay.update_menu(self.game.store_menu)
self.settingsmenudisplay.update_menu(self.game.settings_menu) self.settingsmenudisplay.update_menu(self.game.settings_menu)
self.logsdisplay.update_logs(self.game.logs) self.logsdisplay.update_logs(self.game.logs)
self.messagedisplay.update_message(self.game.message) self.messagedisplay.update_message(self.game.message)
def refresh(self) -> None: def refresh(self) -> None:
if self.game.state == GameMode.PLAY \ if self.game.state == GameMode.PLAY \
or self.game.state == GameMode.INVENTORY: or self.game.state == GameMode.INVENTORY \
or self.game.state == GameMode.STORE :
# The map pad has already the good size # The map pad has already the good size
self.mapdisplay.refresh(0, 0, self.rows * 4 // 5, self.mapdisplay.refresh(0, 0, self.rows * 4 // 5,
self.mapdisplay.pack.tile_width self.mapdisplay.pack.tile_width
@ -68,7 +73,12 @@ class DisplayManager:
self.hbar.refresh(self.rows * 4 // 5, 0, 1, self.cols * 4 // 5) self.hbar.refresh(self.rows * 4 // 5, 0, 1, self.cols * 4 // 5)
self.vbar.refresh(0, self.cols * 4 // 5, self.rows, 1) self.vbar.refresh(0, self.cols * 4 // 5, self.rows, 1)
if self.game.state == GameMode.INVENTORY: if self.game.state == GameMode.INVENTORY:
self.inventorydisplay.refresh(self.rows // 10, self.playerinventorydisplay.refresh(self.rows // 10,
self.cols // 2,
8 * self.rows // 10,
2 * self.cols // 5)
elif self.game.state == GameMode.STORE:
self.storeinventorydisplay.refresh(self.rows // 10,
self.cols // 2, self.cols // 2,
8 * self.rows // 10, 8 * self.rows // 10,
2 * self.cols // 5) 2 * self.cols // 5)

View File

@ -110,8 +110,8 @@ class MainMenuDisplay(Display):
class InventoryDisplay(MenuDisplay): class InventoryDisplay(MenuDisplay):
message : str
def update_pad(self) -> None: def update_pad(self) -> None:
message = _("== INVENTORY ==")
self.addstr(self.pad, 0, (self.width - len(message)) // 2, message, self.addstr(self.pad, 0, (self.width - len(message)) // 2, message,
curses.A_BOLD | curses.A_ITALIC) curses.A_BOLD | curses.A_ITALIC)
for i, item in enumerate(self.menu.values): for i, item in enumerate(self.menu.values):
@ -127,3 +127,9 @@ class InventoryDisplay(MenuDisplay):
@property @property
def trueheight(self) -> int: def trueheight(self) -> int:
return 2 + super().trueheight return 2 + super().trueheight
class PlayerInventoryDisplay(InventoryDisplay):
message = _("== INVENTORY ==")
class StoreInventoryDisplay(InventoryDisplay):
message = _("== STALL ==")

View File

@ -1,13 +1,14 @@
from ..interfaces import FriendlyEntity from ..interfaces import FriendlyEntity
from ..translations import gettext as _ from ..translations import gettext as _
from .player import Player from .player import Player
from .items import Item
from random import choice
class Merchant(FriendlyEntity) : class Merchant(FriendlyEntity) :
""" """
The class for merchants in the dungeon The class for merchants in the dungeon
""" """
name = "Merchant"
inventory = list inventory = list
hazel = int hazel = int
@ -17,10 +18,13 @@ class Merchant(FriendlyEntity) :
""" """
return ["maxhealth", "health", "inventory", "hazel"] return ["maxhealth", "health", "inventory", "hazel"]
def __init__(self, inventory : list, hazel : int = 75): def __init__(self, name : str = "merchant", hazel : int = 75):
super().__init__() super().__init__(name = name)
self.inventory = inventory
self.hazel = hazel self.hazel = hazel
self.name = name
self.inventory = []
for i in range(5) :
self.inventory.append(choice(Item.get_all_items())())
def talk_to(self, player : Player) -> str: def talk_to(self, player : Player) -> str:
""" """

View File

@ -59,6 +59,9 @@ class Item(Entity):
d = super().save_state() d = super().save_state()
d["held"] = self.held d["held"] = self.held
return d return d
def get_all_items() -> list:
return [BodySnatchPotion, Bomb, Heart, Weapon, Sword]
class Heart(Item): class Heart(Item):
@ -166,9 +169,9 @@ class Sword(Weapon) :
""" """
A basic weapon A basic weapon
""" """
def __init__(self, name: int, *args, **kwargs): def __init__(self, name: str = "sword", *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(name = name, *args, **kwargs)
self.name = "sword" self.name = name
class BodySnatchPotion(Item): class BodySnatchPotion(Item):

View File

@ -41,6 +41,7 @@ class Game:
self.settings_menu = menus.SettingsMenu() self.settings_menu = menus.SettingsMenu()
self.settings_menu.update_values(self.settings) self.settings_menu.update_values(self.settings)
self.inventory_menu = menus.InventoryMenu() self.inventory_menu = menus.InventoryMenu()
self.store_menu = menus.StoreMenu()
self.logs = Logs() self.logs = Logs()
self.message = None self.message = None
@ -92,6 +93,8 @@ class Game:
self.handle_key_pressed_main_menu(key) self.handle_key_pressed_main_menu(key)
elif self.state == GameMode.SETTINGS: elif self.state == GameMode.SETTINGS:
self.settings_menu.handle_key_pressed(key, raw_key, self) self.settings_menu.handle_key_pressed(key, raw_key, self)
elif self.state == GameMode.STORE:
self.handle_key_pressed_store(key)
self.display_actions(DisplayActions.REFRESH) self.display_actions(DisplayActions.REFRESH)
def handle_key_pressed_play(self, key: KeyValues) -> None: def handle_key_pressed_play(self, key: KeyValues) -> None:
@ -138,6 +141,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.store_menu.update_merchant(entity)
def handle_key_pressed_inventory(self, key: KeyValues) -> None: def handle_key_pressed_inventory(self, key: KeyValues) -> None:
@ -162,28 +166,23 @@ class Game:
self.inventory_menu.position = min(self.inventory_menu.position, self.inventory_menu.position = min(self.inventory_menu.position,
len(self.inventory_menu.values) len(self.inventory_menu.values)
- 1) - 1)
def handle_key_pressed_inventory(self, key: KeyValues) -> None: def handle_key_pressed_store(self, key: KeyValues) -> None:
""" """
In the inventory menu, we can interact with items or close the menu. In a store menu, we can buy items or close the menu.
""" """
if key == KeyValues.SPACE or key == KeyValues.INVENTORY: if key == KeyValues.SPACE :
self.state = GameMode.PLAY self.state = GameMode.PLAY
elif key == KeyValues.UP: elif key == KeyValues.UP:
self.inventory_menu.go_up() self.store_menu.go_up()
elif key == KeyValues.DOWN: elif key == KeyValues.DOWN:
self.inventory_menu.go_down() self.store_menu.go_down()
if self.inventory_menu.values and not self.player.dead: if self.store_menu.values and not self.player.dead:
if key == KeyValues.USE: if key == KeyValues.ENTER:
self.inventory_menu.validate().use() self.player.add_to_inventory(self.store_menu.validate())
elif key == KeyValues.EQUIP:
self.inventory_menu.validate().equip()
elif key == KeyValues.DROP:
self.inventory_menu.validate().drop()
# Ensure that the cursor has a good position # Ensure that the cursor has a good position
self.inventory_menu.position = min(self.inventory_menu.position, self.store_menu.position = min(self.store_menu.position,
len(self.inventory_menu.values) len(self.store_menu.values)
- 1) - 1)
def handle_key_pressed_main_menu(self, key: KeyValues) -> None: def handle_key_pressed_main_menu(self, key: KeyValues) -> None:

View File

@ -135,7 +135,7 @@ class Map:
def spawn_random_entities(self, count: int) -> None: def spawn_random_entities(self, count: int) -> None:
""" """
Put randomly {count} hedgehogs on the map, where it is available. Put randomly {count} entities on the map, where it is available.
""" """
for ignored in range(count): for ignored in range(count):
y, x = 0, 0 y, x = 0, 0
@ -333,6 +333,7 @@ class Entity:
""" """
Is this entity a merchant? Is this entity a merchant?
""" """
from squirrelbattle.entities.friendly import Merchant
return isinstance(self, Merchant) return isinstance(self, Merchant)
@property @property
@ -340,7 +341,7 @@ class Entity:
return _(self.name.replace("_", " ")) return _(self.name.replace("_", " "))
@staticmethod @staticmethod
def get_all_entity_classes(): def get_all_entity_classes() -> list:
""" """
Returns all entities subclasses Returns all entities subclasses
""" """
@ -348,9 +349,8 @@ class Entity:
from squirrelbattle.entities.monsters import Tiger, Hedgehog, \ from squirrelbattle.entities.monsters import Tiger, Hedgehog, \
Rabbit, TeddyBear Rabbit, TeddyBear
from squirrelbattle.entities.friendly import Merchant,Sunflower from squirrelbattle.entities.friendly import Merchant,Sunflower
return [BodySnatchPotion, Bomb, Heart, Hedgehog, Rabbit, TeddyBear,Sunflower,Tiger] return [BodySnatchPotion, Bomb, Heart, Hedgehog, Rabbit, TeddyBear, \
return [BodySnatchPotion, Bomb, Heart, Hedgehog, Sunflower,Tiger,Merchant]
Rabbit, TeddyBear, Tiger]
@staticmethod @staticmethod
def get_all_entity_classes_in_a_dict() -> dict: def get_all_entity_classes_in_a_dict() -> dict:

View File

@ -6,6 +6,7 @@ from typing import Any, Optional
from .display.texturepack import TexturePack from .display.texturepack import TexturePack
from .entities.player import Player from .entities.player import Player
from .entities.friendly import Merchant
from .enums import GameMode, KeyValues, DisplayActions from .enums import GameMode, KeyValues, DisplayActions
from .settings import Settings from .settings import Settings
from .translations import gettext as _, Translator from .translations import gettext as _, Translator
@ -127,3 +128,14 @@ class InventoryMenu(Menu):
@property @property
def values(self) -> list: def values(self) -> list:
return self.player.inventory return self.player.inventory
class StoreMenu(Menu) :
merchant: Merchant
def update_merchant(self, merchant: Merchant) -> None:
self.merchant = merchant
@property
def values(self) -> list:
return self.merchant.inventory