Merge branch 'inventory' into entities

This commit is contained in:
Yohann D'ANELLO 2020-11-11 20:34:30 +01:00
commit f33d8b8170
4 changed files with 35 additions and 8 deletions

View File

@ -1,7 +1,7 @@
import curses import curses
from dungeonbattle.display.mapdisplay import MapDisplay from dungeonbattle.display.mapdisplay import MapDisplay
from dungeonbattle.display.statsdisplay import StatsDisplay from dungeonbattle.display.statsdisplay import StatsDisplay
from dungeonbattle.display.menudisplay import MainMenuDisplay from dungeonbattle.display.menudisplay import MenuDisplay, MainMenuDisplay
from dungeonbattle.display.texturepack import TexturePack from dungeonbattle.display.texturepack import TexturePack
from typing import Any from typing import Any
from dungeonbattle.game import Game, GameMode from dungeonbattle.game import Game, GameMode
@ -17,9 +17,11 @@ class DisplayManager:
self.statsdisplay = StatsDisplay(screen, pack) self.statsdisplay = StatsDisplay(screen, pack)
self.mainmenudisplay = MainMenuDisplay(self.game.main_menu, self.mainmenudisplay = MainMenuDisplay(self.game.main_menu,
screen, pack) screen, pack)
self.settingsmenudisplay = MenuDisplay(screen, pack)
self.displays = [self.statsdisplay, self.mapdisplay, self.displays = [self.statsdisplay, self.mapdisplay,
self.mainmenudisplay] self.mainmenudisplay, self.settingsmenudisplay]
self.update_game_components() self.update_game_components()
self.settingsmenudisplay.update_menu(self.game.settings_menu)
def update_game_components(self) -> None: def update_game_components(self) -> None:
for d in self.displays: for d in self.displays:
@ -34,6 +36,8 @@ class DisplayManager:
self.rows // 5, self.cols) self.rows // 5, self.cols)
if self.game.state == GameMode.MAINMENU: if self.game.state == GameMode.MAINMENU:
self.mainmenudisplay.refresh(0, 0, self.rows, self.cols) self.mainmenudisplay.refresh(0, 0, self.rows, self.cols)
if self.game.state == GameMode.SETTINGS:
self.settingsmenudisplay.refresh(0, 0, self.rows, self.cols-1)
self.resize_window() self.resize_window()
def resize_window(self) -> bool: def resize_window(self) -> bool:

View File

@ -11,7 +11,6 @@ class MenuDisplay(Display):
def update_menu(self, menu: Menu) -> None: def update_menu(self, menu: Menu) -> None:
self.menu = menu self.menu = menu
self.values = [str(a) for a in menu.values]
self.trueheight = len(self.values) self.trueheight = len(self.values)
self.truewidth = max([len(a) for a in self.values]) self.truewidth = max([len(a) for a in self.values])
@ -22,7 +21,7 @@ class MenuDisplay(Display):
def update_pad(self) -> None: def update_pad(self) -> None:
for i in range(self.trueheight): for i in range(self.trueheight):
self.pad.addstr(i, 0, " ") self.pad.addstr(i, 0, " " + self.values[i])
# set a marker on the selected line # set a marker on the selected line
self.pad.addstr(self.menu.position, 0, ">") self.pad.addstr(self.menu.position, 0, ">")
@ -54,6 +53,10 @@ class MenuDisplay(Display):
def preferred_height(self) -> int: def preferred_height(self) -> int:
return self.trueheight + 2 return self.trueheight + 2
@property
def values(self):
return [str(a) for a in self.menu.values]
class MainMenuDisplay(Display): class MainMenuDisplay(Display):
def __init__(self, menu: MainMenu, *args): def __init__(self, menu: MainMenu, *args):

View File

@ -37,9 +37,11 @@ class Game:
""" """
self.state = GameMode.MAINMENU self.state = GameMode.MAINMENU
self.main_menu = menus.MainMenu() self.main_menu = menus.MainMenu()
self.settings_menu = menus.SettingsMenu()
self.settings = Settings() self.settings = Settings()
self.settings.load_settings() self.settings.load_settings()
self.settings.write_settings() self.settings.write_settings()
self.settings_menu.update_values(self.settings)
def new_game(self) -> None: def new_game(self) -> None:
""" """
@ -68,7 +70,7 @@ class Game:
screen.refresh() screen.refresh()
self.display_refresh() self.display_refresh()
key = screen.getkey() key = screen.getkey()
self.handle_key_pressed(self.translate_key(key)) self.handle_key_pressed(self.translate_key(key), screen)
def translate_key(self, key: str) -> KeyValues: def translate_key(self, key: str) -> KeyValues:
""" """
@ -91,7 +93,7 @@ class Game:
elif key == ' ': elif key == ' ':
return KeyValues.SPACE return KeyValues.SPACE
def handle_key_pressed(self, key: KeyValues) -> None: def handle_key_pressed(self, key: KeyValues, screen) -> None:
""" """
Indicates what should be done when the given key is pressed, Indicates what should be done when the given key is pressed,
according to the current game state. according to the current game state.
@ -101,7 +103,7 @@ class Game:
elif self.state == GameMode.MAINMENU: elif self.state == GameMode.MAINMENU:
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.handle_key_pressed_settings(key) self.handle_key_pressed_settings(key,screen)
self.display_refresh() self.display_refresh()
def handle_key_pressed_play(self, key: KeyValues) -> None: def handle_key_pressed_play(self, key: KeyValues) -> None:
@ -140,9 +142,20 @@ class Game:
elif option == menus.MainMenuValues.EXIT: elif option == menus.MainMenuValues.EXIT:
sys.exit(0) sys.exit(0)
def handle_key_pressed_settings(self, key: KeyValues) -> None: def handle_key_pressed_settings(self, key: KeyValues, screen : Any) -> None:
""" """
For now, in the settings mode, we can only go backwards. For now, in the settings mode, we can only go backwards.
""" """
if key == KeyValues.SPACE: if key == KeyValues.SPACE:
self.state = GameMode.MAINMENU self.state = GameMode.MAINMENU
if key == KeyValues.DOWN:
self.settings_menu.go_down()
if key == KeyValues.UP:
self.settings_menu.go_up()
if key == KeyValues.ENTER:
option = self.settings_menu.validate().split(": ")[0]
if option != "TEXTURE_PACK":
newkey = screen.getkey()
self.settings.__setattr__(option, newkey)
self.settings.write_settings()
self.settings_menu.update_values(self.settings)

View File

@ -1,5 +1,6 @@
from enum import Enum from enum import Enum
from typing import Any from typing import Any
from .settings import Settings
class Menu: class Menu:
@ -30,6 +31,12 @@ class MainMenuValues(Enum):
class MainMenu(Menu): class MainMenu(Menu):
values = [e for e in MainMenuValues] values = [e for e in MainMenuValues]
class SettingsMenu(Menu) :
def __init__(self):
super().__init__()
def update_values(self, settings : Settings):
s = settings.dumps_to_string()
self.values = s[6:-2].replace("\"","").split(",\n ")
class ArbitraryMenu(Menu): class ArbitraryMenu(Menu):
def __init__(self, values: list): def __init__(self, values: list):