2020-11-27 15:33:17 +00:00
|
|
|
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-11-27 19:42:19 +00:00
|
|
|
|
2020-11-27 17:09:08 +00:00
|
|
|
from json import JSONDecodeError
|
2020-11-10 20:41:54 +00:00
|
|
|
from random import randint
|
2020-11-18 13:56:59 +00:00
|
|
|
from typing import Any, Optional
|
2020-11-16 00:01:18 +00:00
|
|
|
import json
|
|
|
|
import os
|
2020-11-18 13:56:59 +00:00
|
|
|
import sys
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-11-06 16:48:47 +00:00
|
|
|
from .entities.player import Player
|
2020-11-11 22:48:46 +00:00
|
|
|
from .enums import GameMode, KeyValues, DisplayActions
|
2020-11-19 11:03:05 +00:00
|
|
|
from .interfaces import Map, Logs
|
2020-11-19 01:49:59 +00:00
|
|
|
from .resources import ResourceManager
|
2020-11-06 13:59:27 +00:00
|
|
|
from .settings import Settings
|
2020-11-06 17:06:28 +00:00
|
|
|
from . import menus
|
2020-11-27 19:53:24 +00:00
|
|
|
from .translations import gettext as _, setlocale
|
2020-11-10 17:08:06 +00:00
|
|
|
from typing import Callable
|
2020-11-06 16:24:20 +00:00
|
|
|
|
2020-11-06 17:11:59 +00:00
|
|
|
|
2020-10-23 12:53:08 +00:00
|
|
|
class Game:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
|
|
|
The game object controls all actions in the game.
|
|
|
|
"""
|
2020-11-08 22:26:54 +00:00
|
|
|
map: Map
|
|
|
|
player: Player
|
2020-11-13 17:08:48 +00:00
|
|
|
# display_actions is a display interface set by the bootstrapper
|
2020-11-11 22:48:46 +00:00
|
|
|
display_actions: Callable[[DisplayActions], None]
|
2020-11-08 22:26:54 +00:00
|
|
|
|
2020-11-06 17:39:55 +00:00
|
|
|
def __init__(self) -> None:
|
2020-11-08 22:31:17 +00:00
|
|
|
"""
|
|
|
|
Init the game.
|
|
|
|
"""
|
2020-11-10 18:40:59 +00:00
|
|
|
self.state = GameMode.MAINMENU
|
2020-11-06 17:06:28 +00:00
|
|
|
self.main_menu = menus.MainMenu()
|
2020-11-11 13:56:00 +00:00
|
|
|
self.settings_menu = menus.SettingsMenu()
|
2020-11-06 13:59:27 +00:00
|
|
|
self.settings = Settings()
|
|
|
|
self.settings.load_settings()
|
|
|
|
self.settings.write_settings()
|
2020-11-11 13:56:00 +00:00
|
|
|
self.settings_menu.update_values(self.settings)
|
2020-11-27 19:53:24 +00:00
|
|
|
setlocale(self.settings.LOCALE)
|
2020-11-19 11:03:05 +00:00
|
|
|
self.logs = Logs()
|
2020-11-27 17:09:08 +00:00
|
|
|
self.message = None
|
2020-10-23 12:53:08 +00:00
|
|
|
|
2020-11-10 17:08:06 +00:00
|
|
|
def new_game(self) -> None:
|
2020-11-08 22:31:17 +00:00
|
|
|
"""
|
|
|
|
Create a new game on the screen.
|
|
|
|
"""
|
2020-10-23 16:01:39 +00:00
|
|
|
# TODO generate a new map procedurally
|
2020-11-19 01:49:59 +00:00
|
|
|
self.map = Map.load(ResourceManager.get_asset_path("example_map_2.txt"))
|
2020-11-19 11:03:05 +00:00
|
|
|
self.map.logs = self.logs
|
2020-11-19 11:55:06 +00:00
|
|
|
self.logs.clear()
|
2020-10-23 16:01:39 +00:00
|
|
|
self.player = Player()
|
2020-11-08 22:26:54 +00:00
|
|
|
self.map.add_entity(self.player)
|
2020-11-11 15:58:20 +00:00
|
|
|
self.player.move(self.map.start_y, self.map.start_x)
|
2020-11-11 15:23:27 +00:00
|
|
|
self.map.spawn_random_entities(randint(3, 10))
|
2020-10-23 16:01:39 +00:00
|
|
|
|
2020-11-06 17:11:59 +00:00
|
|
|
def run(self, screen: Any) -> None:
|
2020-11-08 22:31:17 +00:00
|
|
|
"""
|
|
|
|
Main infinite loop.
|
2020-11-18 11:19:27 +00:00
|
|
|
We wait for the player's action, then we do what that should be done
|
|
|
|
when the given key gets pressed.
|
2020-11-08 22:31:17 +00:00
|
|
|
"""
|
2020-11-11 21:45:57 +00:00
|
|
|
while True: # pragma no cover
|
2020-11-26 21:20:14 +00:00
|
|
|
screen.erase()
|
2020-10-23 12:53:08 +00:00
|
|
|
screen.refresh()
|
2020-11-11 22:48:46 +00:00
|
|
|
self.display_actions(DisplayActions.REFRESH)
|
2020-10-23 12:53:08 +00:00
|
|
|
key = screen.getkey()
|
2020-11-11 21:36:42 +00:00
|
|
|
self.handle_key_pressed(
|
2020-11-11 21:47:51 +00:00
|
|
|
KeyValues.translate_key(key, self.settings), key)
|
2020-11-06 16:24:20 +00:00
|
|
|
|
2020-11-11 21:45:15 +00:00
|
|
|
def handle_key_pressed(self, key: Optional[KeyValues], raw_key: str = '')\
|
|
|
|
-> None:
|
2020-11-08 22:31:17 +00:00
|
|
|
"""
|
|
|
|
Indicates what should be done when the given key is pressed,
|
|
|
|
according to the current game state.
|
|
|
|
"""
|
2020-11-27 16:35:51 +00:00
|
|
|
if self.message:
|
|
|
|
self.message = None
|
2020-11-27 17:12:27 +00:00
|
|
|
self.display_actions(DisplayActions.REFRESH)
|
2020-11-27 16:35:51 +00:00
|
|
|
return
|
|
|
|
|
2020-11-06 17:06:28 +00:00
|
|
|
if self.state == GameMode.PLAY:
|
2020-11-08 22:26:54 +00:00
|
|
|
self.handle_key_pressed_play(key)
|
|
|
|
elif self.state == GameMode.MAINMENU:
|
2020-11-16 00:01:18 +00:00
|
|
|
self.handle_key_pressed_main_menu(key)
|
2020-11-08 22:26:54 +00:00
|
|
|
elif self.state == GameMode.SETTINGS:
|
2020-11-11 21:36:42 +00:00
|
|
|
self.settings_menu.handle_key_pressed(key, raw_key, self)
|
2020-11-11 22:48:46 +00:00
|
|
|
self.display_actions(DisplayActions.REFRESH)
|
2020-11-08 22:26:54 +00:00
|
|
|
|
|
|
|
def handle_key_pressed_play(self, key: KeyValues) -> None:
|
2020-11-08 22:31:17 +00:00
|
|
|
"""
|
2020-11-18 11:19:27 +00:00
|
|
|
In play mode, arrows or zqsd move the main character.
|
2020-11-08 22:31:17 +00:00
|
|
|
"""
|
2020-11-08 22:26:54 +00:00
|
|
|
if key == KeyValues.UP:
|
2020-11-10 23:38:02 +00:00
|
|
|
if self.player.move_up():
|
|
|
|
self.map.tick()
|
2020-11-08 22:26:54 +00:00
|
|
|
elif key == KeyValues.DOWN:
|
2020-11-10 23:38:02 +00:00
|
|
|
if self.player.move_down():
|
|
|
|
self.map.tick()
|
2020-11-08 22:26:54 +00:00
|
|
|
elif key == KeyValues.LEFT:
|
2020-11-10 23:38:02 +00:00
|
|
|
if self.player.move_left():
|
|
|
|
self.map.tick()
|
2020-11-08 22:26:54 +00:00
|
|
|
elif key == KeyValues.RIGHT:
|
2020-11-10 23:38:02 +00:00
|
|
|
if self.player.move_right():
|
|
|
|
self.map.tick()
|
2020-11-08 22:26:54 +00:00
|
|
|
elif key == KeyValues.SPACE:
|
|
|
|
self.state = GameMode.MAINMENU
|
2020-11-16 00:01:18 +00:00
|
|
|
|
|
|
|
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:
|
2020-11-19 00:32:52 +00:00
|
|
|
self.new_game()
|
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
|
|
|
self.state = GameMode.PLAY
|
|
|
|
if option == menus.MainMenuValues.RESUME:
|
2020-11-16 00:01:18 +00:00
|
|
|
self.state = GameMode.PLAY
|
|
|
|
elif option == menus.MainMenuValues.SAVE:
|
|
|
|
self.save_game()
|
|
|
|
elif option == menus.MainMenuValues.LOAD:
|
|
|
|
self.load_game()
|
|
|
|
elif option == menus.MainMenuValues.SETTINGS:
|
|
|
|
self.state = GameMode.SETTINGS
|
|
|
|
elif option == menus.MainMenuValues.EXIT:
|
|
|
|
sys.exit(0)
|
|
|
|
|
2020-11-18 13:56:59 +00:00
|
|
|
def save_state(self) -> dict:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-18 13:56:59 +00:00
|
|
|
Saves the game to a dictionary
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-16 00:01:18 +00:00
|
|
|
return self.map.save_state()
|
|
|
|
|
|
|
|
def load_state(self, d: dict) -> None:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-18 13:56:59 +00:00
|
|
|
Loads the game from a dictionary
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-11-27 17:16:54 +00:00
|
|
|
try:
|
|
|
|
self.map.load_state(d)
|
|
|
|
except KeyError:
|
2020-11-27 19:42:19 +00:00
|
|
|
self.message = _("Some keys are missing in your save file.\n"
|
|
|
|
"Your save seems to be corrupt. It got deleted.")
|
2020-11-27 17:16:54 +00:00
|
|
|
os.unlink(ResourceManager.get_config_path("save.json"))
|
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
|
|
|
return
|
|
|
|
|
2020-11-27 17:09:08 +00:00
|
|
|
players = self.map.find_entities(Player)
|
|
|
|
if not players:
|
2020-11-27 19:42:19 +00:00
|
|
|
self.message = _("No player was found on this map!\n"
|
|
|
|
"Maybe you died?")
|
2020-11-27 17:09:08 +00:00
|
|
|
self.player.health = 0
|
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.player = players[0]
|
2020-11-18 14:04:15 +00:00
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
2020-11-18 13:56:59 +00:00
|
|
|
|
2020-11-16 00:01:18 +00:00
|
|
|
def load_game(self) -> None:
|
|
|
|
"""
|
|
|
|
Loads the game from a file
|
|
|
|
"""
|
2020-11-19 02:13:01 +00:00
|
|
|
file_path = ResourceManager.get_config_path("save.json")
|
|
|
|
if os.path.isfile(file_path):
|
|
|
|
with open(file_path, "r") as f:
|
2020-11-27 17:09:08 +00:00
|
|
|
try:
|
|
|
|
state = json.loads(f.read())
|
|
|
|
self.load_state(state)
|
|
|
|
except JSONDecodeError:
|
2020-11-27 19:42:19 +00:00
|
|
|
self.message = _("The JSON file is not correct.\n"
|
|
|
|
"Your save seems corrupted."
|
|
|
|
"It got deleted.")
|
2020-11-27 17:09:08 +00:00
|
|
|
os.unlink(file_path)
|
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
2020-11-16 00:01:18 +00:00
|
|
|
|
|
|
|
def save_game(self) -> None:
|
|
|
|
"""
|
2020-11-18 11:19:27 +00:00
|
|
|
Saves the game to a file
|
2020-11-16 00:01:18 +00:00
|
|
|
"""
|
2020-11-19 02:13:01 +00:00
|
|
|
with open(ResourceManager.get_config_path("save.json"), "w") as f:
|
2020-11-16 00:01:18 +00:00
|
|
|
f.write(json.dumps(self.save_state()))
|