squirrel-battle/dungeonbattle/game.py
2020-11-06 17:24:20 +01:00

94 lines
2.4 KiB
Python

from typing import Any
from .interfaces import Map
from .mapdisplay import MapDisplay
from .settings import Settings
from .term_manager import TermManager
from enum import Enum, auto
class GameMode(Enum):
MAINMENU = auto()
PLAY = auto()
SETTINGS = auto()
INVENTORY = auto()
class KeyValues(Enum):
UP = auto()
DOWN = auto()
LEFT = auto()
RIGHT = auto()
ENTER = auto()
class Game:
INSTANCE = None
def init(self) -> None:
Game.INSTANCE = self
self.state = GameMode.MAINMENU
self.settings = Settings()
self.settings.load_settings()
self.settings.write_settings()
def new_game(self):
# TODO generate a new map procedurally
self.m = Map.load("example_map.txt")
self.player = Player()
self.player.y = 1
self.player.x = 6
self.d = MapDisplay(self.m, self.player)
@classmethod
def load_game(filename):
# TODO loading map from a file
raise NotImplementedError()
def run(self, screen):
while True:
screen.clear()
screen.refresh()
self.d.display(self.player.y, self.player.x)
key = screen.getkey()
self.handle_key_pressed(self.translate_key(key))
def translate_key(self, key:str) -> KeyValues:
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
def handle_key_pressed(self, key: KeyValues) -> None:
if key == KeyValues.UP:
self.player.move_up()
if key == KeyValues.DOWN:
self.player.move_down()
if key == KeyValues.LEFT:
self.player.move_left()
if key == KeyValues.RIGHT:
self.player.move_right()
class Player:
# FIXME Should be elsewhere, only useful to don't break the previous code
y: int = 0
x: int = 0
def move_up(self) -> None:
self.y -= 1
def move_down(self) -> None:
self.y += 1
def move_left(self) -> None:
self.x -= 1
def move_right(self) -> None:
self.x += 1