More separation on menu code
This commit is contained in:
parent
17edb6a645
commit
748561e87d
|
@ -0,0 +1,41 @@
|
|||
from enum import Enum, auto
|
||||
|
||||
from dungeonbattle.settings import Settings
|
||||
|
||||
|
||||
class GameMode(Enum):
|
||||
MAINMENU = auto()
|
||||
PLAY = auto()
|
||||
SETTINGS = auto()
|
||||
INVENTORY = auto()
|
||||
|
||||
|
||||
class KeyValues(Enum):
|
||||
UP = auto()
|
||||
DOWN = auto()
|
||||
LEFT = auto()
|
||||
RIGHT = auto()
|
||||
ENTER = auto()
|
||||
SPACE = auto()
|
||||
|
||||
@staticmethod
|
||||
def translate_key(key: str, settings: Settings) -> "KeyValues":
|
||||
"""
|
||||
Translate the raw string key into an enum value that we can use.
|
||||
"""
|
||||
if key in (settings.KEY_DOWN_SECONDARY,
|
||||
settings.KEY_DOWN_PRIMARY):
|
||||
return KeyValues.DOWN
|
||||
elif key in (settings.KEY_LEFT_PRIMARY,
|
||||
settings.KEY_LEFT_SECONDARY):
|
||||
return KeyValues.LEFT
|
||||
elif key in (settings.KEY_RIGHT_PRIMARY,
|
||||
settings.KEY_RIGHT_SECONDARY):
|
||||
return KeyValues.RIGHT
|
||||
elif key in (settings.KEY_UP_PRIMARY,
|
||||
settings.KEY_UP_SECONDARY):
|
||||
return KeyValues.UP
|
||||
elif key == settings.KEY_ENTER:
|
||||
return KeyValues.ENTER
|
||||
elif key == ' ':
|
||||
return KeyValues.SPACE
|
|
@ -1,31 +1,14 @@
|
|||
import sys
|
||||
from random import randint
|
||||
from typing import Any
|
||||
|
||||
from .entities.player import Player
|
||||
from .enums import GameMode, KeyValues
|
||||
from .interfaces import Map
|
||||
from .settings import Settings
|
||||
from enum import Enum, auto
|
||||
from . import menus
|
||||
from typing import Callable
|
||||
|
||||
|
||||
class GameMode(Enum):
|
||||
MAINMENU = auto()
|
||||
PLAY = auto()
|
||||
SETTINGS = auto()
|
||||
INVENTORY = auto()
|
||||
|
||||
|
||||
class KeyValues(Enum):
|
||||
UP = auto()
|
||||
DOWN = auto()
|
||||
LEFT = auto()
|
||||
RIGHT = auto()
|
||||
ENTER = auto()
|
||||
SPACE = auto()
|
||||
|
||||
|
||||
class Game:
|
||||
map: Map
|
||||
player: Player
|
||||
|
@ -70,30 +53,9 @@ class Game:
|
|||
screen.refresh()
|
||||
self.display_refresh()
|
||||
key = screen.getkey()
|
||||
self.handle_key_pressed(self.translate_key(key), screen)
|
||||
self.handle_key_pressed(KeyValues.translate_key(key, self.settings))
|
||||
|
||||
def translate_key(self, key: str) -> KeyValues:
|
||||
"""
|
||||
Translate the raw string key into an enum value that we can use.
|
||||
"""
|
||||
if key in (self.settings.KEY_DOWN_SECONDARY,
|
||||
self.settings.KEY_DOWN_PRIMARY):
|
||||
return KeyValues.DOWN
|
||||
elif key in (self.settings.KEY_LEFT_PRIMARY,
|
||||
self.settings.KEY_LEFT_SECONDARY):
|
||||
return KeyValues.LEFT
|
||||
elif key in (self.settings.KEY_RIGHT_PRIMARY,
|
||||
self.settings.KEY_RIGHT_SECONDARY):
|
||||
return KeyValues.RIGHT
|
||||
elif key in (self.settings.KEY_UP_PRIMARY,
|
||||
self.settings.KEY_UP_SECONDARY):
|
||||
return KeyValues.UP
|
||||
elif key == self.settings.KEY_ENTER:
|
||||
return KeyValues.ENTER
|
||||
elif key == ' ':
|
||||
return KeyValues.SPACE
|
||||
|
||||
def handle_key_pressed(self, key: KeyValues, screen: Any) -> None:
|
||||
def handle_key_pressed(self, key: KeyValues) -> None:
|
||||
"""
|
||||
Indicates what should be done when the given key is pressed,
|
||||
according to the current game state.
|
||||
|
@ -101,9 +63,9 @@ class Game:
|
|||
if self.state == GameMode.PLAY:
|
||||
self.handle_key_pressed_play(key)
|
||||
elif self.state == GameMode.MAINMENU:
|
||||
self.handle_key_pressed_main_menu(key)
|
||||
self.main_menu.handle_key_pressed(key, self)
|
||||
elif self.state == GameMode.SETTINGS:
|
||||
self.handle_key_pressed_settings(key, screen)
|
||||
self.settings_menu.handle_key_pressed(key, self.settings)
|
||||
self.display_refresh()
|
||||
|
||||
def handle_key_pressed_play(self, key: KeyValues) -> None:
|
||||
|
@ -124,38 +86,3 @@ class Game:
|
|||
self.map.tick()
|
||||
elif key == KeyValues.SPACE:
|
||||
self.state = GameMode.MAINMENU
|
||||
|
||||
def handle_key_pressed_main_menu(self, key: KeyValues) -> None:
|
||||
"""
|
||||
In the main menu, we can navigate through options.
|
||||
"""
|
||||
if key == KeyValues.DOWN:
|
||||
self.main_menu.go_down()
|
||||
if key == KeyValues.UP:
|
||||
self.main_menu.go_up()
|
||||
if key == KeyValues.ENTER:
|
||||
option = self.main_menu.validate()
|
||||
if option == menus.MainMenuValues.START:
|
||||
self.state = GameMode.PLAY
|
||||
elif option == menus.MainMenuValues.SETTINGS:
|
||||
self.state = GameMode.SETTINGS
|
||||
elif option == menus.MainMenuValues.EXIT:
|
||||
sys.exit(0)
|
||||
|
||||
def handle_key_pressed_settings(self, key: KeyValues, screen: Any) -> None:
|
||||
"""
|
||||
For now, in the settings mode, we can only go backwards.
|
||||
"""
|
||||
if key == KeyValues.SPACE:
|
||||
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)
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
import sys
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
from .enums import GameMode, KeyValues
|
||||
from .settings import Settings
|
||||
|
||||
|
||||
|
@ -31,6 +34,23 @@ class MainMenuValues(Enum):
|
|||
class MainMenu(Menu):
|
||||
values = [e for e in MainMenuValues]
|
||||
|
||||
def handle_key_pressed(self, key: KeyValues, game: Any) -> None:
|
||||
"""
|
||||
In the main menu, we can navigate through options.
|
||||
"""
|
||||
if key == KeyValues.DOWN:
|
||||
self.go_down()
|
||||
if key == KeyValues.UP:
|
||||
self.go_up()
|
||||
if key == KeyValues.ENTER:
|
||||
option = self.validate()
|
||||
if option == MainMenuValues.START:
|
||||
game.state = GameMode.PLAY
|
||||
elif option == MainMenuValues.SETTINGS:
|
||||
game.state = GameMode.SETTINGS
|
||||
elif option == MainMenuValues.EXIT:
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
class SettingsMenu(Menu):
|
||||
def __init__(self):
|
||||
|
@ -40,6 +60,24 @@ class SettingsMenu(Menu):
|
|||
s = settings.dumps_to_string()
|
||||
self.values = s[6:-2].replace("\"", "").split(",\n ")
|
||||
|
||||
def handle_key_pressed(self, key: KeyValues, settings: Settings) -> None:
|
||||
"""
|
||||
For now, in the settings mode, we can only go backwards.
|
||||
"""
|
||||
if key == KeyValues.SPACE:
|
||||
self.state = GameMode.MAINMENU
|
||||
if key == KeyValues.DOWN:
|
||||
self.go_down()
|
||||
if key == KeyValues.UP:
|
||||
self.go_up()
|
||||
if key == KeyValues.ENTER:
|
||||
option = self.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)
|
||||
|
||||
|
||||
class ArbitraryMenu(Menu):
|
||||
def __init__(self, values: list):
|
||||
|
|
Loading…
Reference in New Issue