2021-01-10 09:46:17 +00:00
|
|
|
# Copyright (C) 2020-2021 by ÿnérant, eichhornchen, nicomarg, charlse
|
2020-11-27 15:33:17 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-11-27 19:42:19 +00:00
|
|
|
|
2020-12-11 15:56:22 +00:00
|
|
|
import curses
|
2020-11-16 00:01:18 +00:00
|
|
|
import json
|
2021-01-10 10:25:53 +00:00
|
|
|
from json import JSONDecodeError
|
2020-11-16 00:01:18 +00:00
|
|
|
import os
|
2021-01-10 10:25:53 +00:00
|
|
|
from random import randint
|
2020-11-18 13:56:59 +00:00
|
|
|
import sys
|
2021-01-10 10:25:53 +00:00
|
|
|
from typing import Any, List, Optional
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2021-01-10 10:25:53 +00:00
|
|
|
from . import menus
|
2020-11-06 16:48:47 +00:00
|
|
|
from .entities.player import Player
|
2021-01-10 10:25:53 +00:00
|
|
|
from .enums import DisplayActions, GameMode, KeyValues
|
|
|
|
from .interfaces import Logs, Map
|
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-28 00:59:52 +00:00
|
|
|
from .translations import gettext as _, Translator
|
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-12-25 23:45:17 +00:00
|
|
|
maps: List[Map]
|
|
|
|
map_index: int
|
2020-11-08 22:26:54 +00:00
|
|
|
player: Player
|
2020-12-03 23:27:25 +00:00
|
|
|
screen: Any
|
2020-11-13 17:08:48 +00:00
|
|
|
# display_actions is a display interface set by the bootstrapper
|
2020-12-11 15:56:22 +00:00
|
|
|
display_actions: callable
|
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
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Initiates the game.
|
2020-11-08 22:31:17 +00:00
|
|
|
"""
|
2020-11-10 18:40:59 +00:00
|
|
|
self.state = GameMode.MAINMENU
|
2020-12-07 20:29:57 +00:00
|
|
|
self.waiting_for_friendly_key = False
|
2021-01-08 17:06:26 +00:00
|
|
|
self.waiting_for_launch_key = False
|
2020-12-18 00:05:50 +00:00
|
|
|
self.is_in_store_menu = True
|
2021-01-08 22:15:48 +00:00
|
|
|
self.is_in_chest_menu = True
|
2020-11-06 13:59:27 +00:00
|
|
|
self.settings = Settings()
|
|
|
|
self.settings.load_settings()
|
|
|
|
self.settings.write_settings()
|
2020-11-28 00:59:52 +00:00
|
|
|
Translator.setlocale(self.settings.LOCALE)
|
2020-11-27 21:19:41 +00:00
|
|
|
self.main_menu = menus.MainMenu()
|
|
|
|
self.settings_menu = menus.SettingsMenu()
|
2020-11-27 20:51:54 +00:00
|
|
|
self.settings_menu.update_values(self.settings)
|
2020-12-04 13:41:59 +00:00
|
|
|
self.inventory_menu = menus.InventoryMenu()
|
2020-12-07 19:54:53 +00:00
|
|
|
self.store_menu = menus.StoreMenu()
|
2021-01-08 22:15:48 +00:00
|
|
|
self.chest_menu = menus.ChestMenu()
|
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
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Creates a new game on the screen.
|
2020-11-08 22:31:17 +00:00
|
|
|
"""
|
2020-10-23 16:01:39 +00:00
|
|
|
# TODO generate a new map procedurally
|
2020-12-25 23:45:17 +00:00
|
|
|
self.maps = []
|
|
|
|
self.map_index = 0
|
2020-12-26 00:08:43 +00:00
|
|
|
self.map = Map.load(ResourceManager.get_asset_path("example_map.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-12-04 15:28:37 +00:00
|
|
|
self.inventory_menu.update_player(self.player)
|
2020-10-23 16:01:39 +00:00
|
|
|
|
2020-12-25 23:45:17 +00:00
|
|
|
@property
|
|
|
|
def map(self) -> Map:
|
|
|
|
"""
|
|
|
|
Return the current map where the user is.
|
|
|
|
"""
|
|
|
|
return self.maps[self.map_index]
|
|
|
|
|
|
|
|
@map.setter
|
|
|
|
def map(self, m: Map) -> None:
|
|
|
|
"""
|
|
|
|
Redefine the current map.
|
|
|
|
"""
|
|
|
|
if len(self.maps) == self.map_index:
|
|
|
|
# Insert new map
|
|
|
|
self.maps.append(m)
|
|
|
|
# Redefine the current map
|
|
|
|
self.maps[self.map_index] = m
|
|
|
|
|
2020-12-18 15:40:52 +00:00
|
|
|
def run(self, screen: Any) -> None: # pragma no cover
|
2020-11-08 22:31:17 +00:00
|
|
|
"""
|
|
|
|
Main infinite loop.
|
2020-12-13 20:29:25 +00:00
|
|
|
We wait for the player's action, then we do what should be done
|
|
|
|
when a key gets pressed.
|
2020-11-08 22:31:17 +00:00
|
|
|
"""
|
2020-12-18 15:40:52 +00:00
|
|
|
screen.refresh()
|
|
|
|
while True:
|
2020-11-26 21:20:14 +00:00
|
|
|
screen.erase()
|
2020-12-18 15:40:52 +00:00
|
|
|
screen.noutrefresh()
|
2020-11-11 22:48:46 +00:00
|
|
|
self.display_actions(DisplayActions.REFRESH)
|
2020-12-18 15:40:52 +00:00
|
|
|
curses.doupdate()
|
2021-01-08 10:10:10 +00:00
|
|
|
try:
|
|
|
|
key = screen.getkey()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
exit(0)
|
|
|
|
return
|
2020-12-11 17:17:59 +00:00
|
|
|
if key == "KEY_MOUSE":
|
2021-01-08 10:21:40 +00:00
|
|
|
_ignored1, x, y, _ignored2, attr = curses.getmouse()
|
|
|
|
self.display_actions(DisplayActions.MOUSE, y, x, attr)
|
2020-12-11 17:17:59 +00:00
|
|
|
else:
|
|
|
|
self.handle_key_pressed(
|
|
|
|
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
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Indicates what should be done when a given key is pressed,
|
2020-11-08 22:31:17 +00:00
|
|
|
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-12-07 20:29:57 +00:00
|
|
|
if self.waiting_for_friendly_key:
|
|
|
|
# The player requested to talk with a friendly entity
|
|
|
|
self.handle_friendly_entity_chat(key)
|
2021-01-08 17:06:26 +00:00
|
|
|
elif self.waiting_for_launch_key:
|
|
|
|
# The player requested to launch
|
|
|
|
self.handle_launch(key)
|
2020-12-07 20:29:57 +00:00
|
|
|
else:
|
|
|
|
self.handle_key_pressed_play(key)
|
2020-12-04 13:57:53 +00:00
|
|
|
elif self.state == GameMode.INVENTORY:
|
|
|
|
self.handle_key_pressed_inventory(key)
|
2020-11-08 22:26:54 +00:00
|
|
|
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-12-07 19:54:53 +00:00
|
|
|
elif self.state == GameMode.STORE:
|
|
|
|
self.handle_key_pressed_store(key)
|
2021-01-08 22:15:48 +00:00
|
|
|
elif self.state == GameMode.CHEST:
|
|
|
|
self.handle_key_pressed_chest(key)
|
2020-12-18 21:24:41 +00:00
|
|
|
elif self.state == GameMode.CREDITS:
|
|
|
|
self.state = GameMode.MAINMENU
|
2020-11-11 22:48:46 +00:00
|
|
|
self.display_actions(DisplayActions.REFRESH)
|
2020-11-08 22:26:54 +00:00
|
|
|
|
2021-01-08 00:56:54 +00:00
|
|
|
def handle_key_pressed_play(self, key: KeyValues) -> None: # noqa: C901
|
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():
|
2020-12-18 16:29:59 +00:00
|
|
|
self.map.tick(self.player)
|
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():
|
2020-12-18 16:29:59 +00:00
|
|
|
self.map.tick(self.player)
|
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():
|
2020-12-18 16:29:59 +00:00
|
|
|
self.map.tick(self.player)
|
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():
|
2020-12-18 16:29:59 +00:00
|
|
|
self.map.tick(self.player)
|
2020-12-04 13:41:59 +00:00
|
|
|
elif key == KeyValues.INVENTORY:
|
|
|
|
self.state = GameMode.INVENTORY
|
2020-12-18 16:30:03 +00:00
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
2021-01-06 10:13:17 +00:00
|
|
|
elif key == KeyValues.USE and self.player.equipped_main:
|
2021-01-08 00:56:54 +00:00
|
|
|
if self.player.equipped_main:
|
|
|
|
self.player.equipped_main.use()
|
|
|
|
if self.player.equipped_secondary:
|
|
|
|
self.player.equipped_secondary.use()
|
2021-01-08 17:06:26 +00:00
|
|
|
elif key == KeyValues.LAUNCH:
|
|
|
|
# Wait for the direction to launch in
|
|
|
|
self.waiting_for_launch_key = True
|
2020-11-08 22:26:54 +00:00
|
|
|
elif key == KeyValues.SPACE:
|
|
|
|
self.state = GameMode.MAINMENU
|
2020-12-09 14:32:37 +00:00
|
|
|
elif key == KeyValues.CHAT:
|
2020-12-07 20:29:57 +00:00
|
|
|
# Wait for the direction of the friendly entity
|
|
|
|
self.waiting_for_friendly_key = True
|
2020-12-12 17:12:37 +00:00
|
|
|
elif key == KeyValues.WAIT:
|
2021-01-06 16:54:43 +00:00
|
|
|
self.map.tick(self.player)
|
2021-01-06 13:55:16 +00:00
|
|
|
elif key == KeyValues.LADDER:
|
2021-01-06 16:00:43 +00:00
|
|
|
self.handle_ladder()
|
2021-01-10 16:10:00 +00:00
|
|
|
elif key == KeyValues.DANCE:
|
|
|
|
self.player.dance()
|
|
|
|
self.map.tick(self.player)
|
2021-01-06 16:00:43 +00:00
|
|
|
|
|
|
|
def handle_ladder(self) -> None:
|
|
|
|
"""
|
|
|
|
The player pressed the ladder key to switch map
|
|
|
|
"""
|
|
|
|
# On a ladder, we switch level
|
|
|
|
y, x = self.player.y, self.player.x
|
|
|
|
if not self.map.tiles[y][x].is_ladder():
|
|
|
|
return
|
|
|
|
|
|
|
|
# We move up on the ladder of the beginning,
|
|
|
|
# down at the end of the stage
|
|
|
|
move_down = y != self.map.start_y and x != self.map.start_x
|
|
|
|
old_map = self.map
|
|
|
|
self.map_index += 1 if move_down else -1
|
|
|
|
if self.map_index == -1:
|
|
|
|
self.map_index = 0
|
|
|
|
return
|
|
|
|
while self.map_index >= len(self.maps):
|
|
|
|
# TODO: generate a new map
|
|
|
|
self.maps.append(Map.load(ResourceManager.get_asset_path(
|
|
|
|
"example_map_2.txt")))
|
|
|
|
new_map = self.map
|
|
|
|
new_map.floor = self.map_index
|
|
|
|
old_map.remove_entity(self.player)
|
|
|
|
new_map.add_entity(self.player)
|
|
|
|
if move_down:
|
|
|
|
self.player.move(self.map.start_y, self.map.start_x)
|
|
|
|
self.logs.add_message(
|
|
|
|
_("The player climbs down to the floor {floor}.")
|
|
|
|
.format(floor=-self.map_index))
|
|
|
|
else:
|
|
|
|
# Find the ladder of the end of the game
|
|
|
|
ladder_y, ladder_x = -1, -1
|
|
|
|
for y in range(self.map.height):
|
|
|
|
for x in range(self.map.width):
|
|
|
|
if (y, x) != (self.map.start_y, self.map.start_x) \
|
|
|
|
and self.map.tiles[y][x].is_ladder():
|
|
|
|
ladder_y, ladder_x = y, x
|
|
|
|
break
|
|
|
|
self.player.move(ladder_y, ladder_x)
|
|
|
|
self.logs.add_message(
|
|
|
|
_("The player climbs up the floor {floor}.")
|
|
|
|
.format(floor=-self.map_index))
|
|
|
|
|
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
2020-12-07 20:22:06 +00:00
|
|
|
|
2020-12-07 20:29:57 +00:00
|
|
|
def handle_friendly_entity_chat(self, key: KeyValues) -> None:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
If the player tries to talk to a friendly entity, the game waits for
|
|
|
|
a directional key to be pressed, verifies there is a friendly entity
|
|
|
|
in that direction and then lets the player interact with it.
|
2020-12-07 20:29:57 +00:00
|
|
|
"""
|
|
|
|
if not self.waiting_for_friendly_key:
|
|
|
|
return
|
|
|
|
self.waiting_for_friendly_key = False
|
|
|
|
|
|
|
|
if key == KeyValues.UP:
|
2020-12-07 20:22:06 +00:00
|
|
|
xp = self.player.x
|
|
|
|
yp = self.player.y - 1
|
2020-12-07 20:29:57 +00:00
|
|
|
elif key == KeyValues.DOWN:
|
2020-12-07 20:22:06 +00:00
|
|
|
xp = self.player.x
|
|
|
|
yp = self.player.y + 1
|
2020-12-07 20:29:57 +00:00
|
|
|
elif key == KeyValues.LEFT:
|
2020-12-07 20:22:06 +00:00
|
|
|
xp = self.player.x - 1
|
|
|
|
yp = self.player.y
|
2020-12-07 20:29:57 +00:00
|
|
|
elif key == KeyValues.RIGHT:
|
2020-12-07 20:22:06 +00:00
|
|
|
xp = self.player.x + 1
|
|
|
|
yp = self.player.y
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
if self.map.entity_is_present(yp, xp):
|
|
|
|
for entity in self.map.entities:
|
|
|
|
if entity.is_friendly() and entity.x == xp and \
|
|
|
|
entity.y == yp:
|
|
|
|
msg = entity.talk_to(self.player)
|
|
|
|
self.logs.add_message(msg)
|
|
|
|
if entity.is_merchant():
|
|
|
|
self.state = GameMode.STORE
|
2020-12-18 00:50:11 +00:00
|
|
|
self.is_in_store_menu = True
|
2020-12-07 20:22:06 +00:00
|
|
|
self.store_menu.update_merchant(entity)
|
2020-12-18 14:15:47 +00:00
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
2021-01-08 22:15:48 +00:00
|
|
|
elif entity.is_chest():
|
|
|
|
self.state = GameMode.CHEST
|
|
|
|
self.is_in_chest_menu = True
|
|
|
|
self.chest_menu.update_chest(entity)
|
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
2020-11-16 00:01:18 +00:00
|
|
|
|
2021-01-08 17:06:26 +00:00
|
|
|
def handle_launch(self, key: KeyValues) -> None:
|
|
|
|
"""
|
|
|
|
If the player tries to throw something in a direction, the game looks
|
|
|
|
for entities in that direction and within the range of the player's
|
|
|
|
weapon and adds damage
|
|
|
|
"""
|
|
|
|
if not self.waiting_for_launch_key:
|
|
|
|
return
|
|
|
|
self.waiting_for_launch_key = False
|
|
|
|
|
|
|
|
if key == KeyValues.UP:
|
|
|
|
direction = 0
|
|
|
|
elif key == KeyValues.DOWN:
|
|
|
|
direction = 2
|
|
|
|
elif key == KeyValues.LEFT:
|
|
|
|
direction = 3
|
|
|
|
elif key == KeyValues.RIGHT:
|
|
|
|
direction = 1
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.player.equipped_main:
|
2021-01-10 18:25:12 +00:00
|
|
|
if self.player.equipped_main.throw(direction):
|
|
|
|
self.map.tick(self.player)
|
2021-01-08 17:06:26 +00:00
|
|
|
|
2020-12-04 13:57:53 +00:00
|
|
|
def handle_key_pressed_inventory(self, key: KeyValues) -> None:
|
|
|
|
"""
|
|
|
|
In the inventory menu, we can interact with items or close the menu.
|
|
|
|
"""
|
|
|
|
if key == KeyValues.SPACE or key == KeyValues.INVENTORY:
|
|
|
|
self.state = GameMode.PLAY
|
2020-12-04 15:31:15 +00:00
|
|
|
elif key == KeyValues.UP:
|
|
|
|
self.inventory_menu.go_up()
|
|
|
|
elif key == KeyValues.DOWN:
|
|
|
|
self.inventory_menu.go_down()
|
2020-12-05 12:20:52 +00:00
|
|
|
if self.inventory_menu.values and not self.player.dead:
|
2020-12-04 16:01:00 +00:00
|
|
|
if key == KeyValues.USE:
|
|
|
|
self.inventory_menu.validate().use()
|
|
|
|
elif key == KeyValues.EQUIP:
|
|
|
|
self.inventory_menu.validate().equip()
|
|
|
|
elif key == KeyValues.DROP:
|
2020-12-04 16:19:06 +00:00
|
|
|
self.inventory_menu.validate().drop()
|
2020-12-04 16:01:00 +00:00
|
|
|
|
|
|
|
# Ensure that the cursor has a good position
|
|
|
|
self.inventory_menu.position = min(self.inventory_menu.position,
|
|
|
|
len(self.inventory_menu.values)
|
|
|
|
- 1)
|
2020-12-04 13:57:53 +00:00
|
|
|
|
2020-12-07 19:54:53 +00:00
|
|
|
def handle_key_pressed_store(self, key: KeyValues) -> None:
|
2020-12-06 10:43:48 +00:00
|
|
|
"""
|
2020-12-07 19:54:53 +00:00
|
|
|
In a store menu, we can buy items or close the menu.
|
2020-12-06 10:43:48 +00:00
|
|
|
"""
|
2020-12-18 00:05:50 +00:00
|
|
|
menu = self.store_menu if self.is_in_store_menu else self.inventory_menu
|
|
|
|
|
|
|
|
if key == KeyValues.SPACE or key == KeyValues.INVENTORY:
|
2020-12-06 10:43:48 +00:00
|
|
|
self.state = GameMode.PLAY
|
|
|
|
elif key == KeyValues.UP:
|
2020-12-18 00:05:50 +00:00
|
|
|
menu.go_up()
|
2020-12-06 10:43:48 +00:00
|
|
|
elif key == KeyValues.DOWN:
|
2020-12-18 00:05:50 +00:00
|
|
|
menu.go_down()
|
|
|
|
elif key == KeyValues.LEFT:
|
|
|
|
self.is_in_store_menu = False
|
2020-12-18 14:15:47 +00:00
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
2020-12-18 00:05:50 +00:00
|
|
|
elif key == KeyValues.RIGHT:
|
|
|
|
self.is_in_store_menu = True
|
2020-12-18 14:15:47 +00:00
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
2020-12-18 00:05:50 +00:00
|
|
|
if menu.values and not self.player.dead:
|
2020-12-07 19:54:53 +00:00
|
|
|
if key == KeyValues.ENTER:
|
2020-12-18 00:05:50 +00:00
|
|
|
item = menu.validate()
|
|
|
|
owner = self.store_menu.merchant if self.is_in_store_menu \
|
|
|
|
else self.player
|
|
|
|
buyer = self.player if self.is_in_store_menu \
|
|
|
|
else self.store_menu.merchant
|
|
|
|
flag = item.be_sold(buyer, owner)
|
2020-12-11 16:06:30 +00:00
|
|
|
if not flag:
|
2020-12-18 00:05:50 +00:00
|
|
|
self.message = _("The buyer does not have enough money")
|
2020-12-18 14:15:47 +00:00
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
2021-01-08 22:15:48 +00:00
|
|
|
# Ensure that the cursor has a good position
|
|
|
|
menu.position = min(menu.position, len(menu.values) - 1)
|
2021-01-08 22:32:47 +00:00
|
|
|
|
2021-01-08 22:15:48 +00:00
|
|
|
def handle_key_pressed_chest(self, key: KeyValues) -> None:
|
|
|
|
"""
|
|
|
|
In a chest menu, we can take or put items or close the menu.
|
|
|
|
"""
|
|
|
|
menu = self.chest_menu if self.is_in_chest_menu else self.inventory_menu
|
|
|
|
|
|
|
|
if key == KeyValues.SPACE or key == KeyValues.INVENTORY:
|
|
|
|
self.state = GameMode.PLAY
|
|
|
|
elif key == KeyValues.UP:
|
|
|
|
menu.go_up()
|
|
|
|
elif key == KeyValues.DOWN:
|
|
|
|
menu.go_down()
|
|
|
|
elif key == KeyValues.LEFT:
|
|
|
|
self.is_in_chest_menu = False
|
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
|
|
|
elif key == KeyValues.RIGHT:
|
|
|
|
self.is_in_chest_menu = True
|
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
|
|
|
if menu.values and not self.player.dead:
|
|
|
|
if key == KeyValues.ENTER:
|
|
|
|
item = menu.validate()
|
|
|
|
owner = self.chest_menu.chest if self.is_in_chest_menu \
|
|
|
|
else self.player
|
|
|
|
buyer = self.player if self.is_in_chest_menu \
|
|
|
|
else self.chest_menu.chest
|
2021-01-08 22:32:47 +00:00
|
|
|
item.be_sold(buyer, owner, for_free=True)
|
2021-01-08 22:15:48 +00:00
|
|
|
self.display_actions(DisplayActions.UPDATE)
|
2020-12-06 10:43:48 +00:00
|
|
|
# Ensure that the cursor has a good position
|
2020-12-18 00:05:50 +00:00
|
|
|
menu.position = min(menu.position, len(menu.values) - 1)
|
2020-12-06 10:43:48 +00:00
|
|
|
|
2020-11-16 00:01:18 +00:00
|
|
|
def handle_key_pressed_main_menu(self, key: KeyValues) -> None:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
In the main menu, we can navigate through different options.
|
2020-11-16 00:01:18 +00:00
|
|
|
"""
|
|
|
|
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-12-13 20:29:25 +00:00
|
|
|
Saves the game to a dictionary.
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2021-01-08 13:23:57 +00:00
|
|
|
return dict(map_index=self.map_index,
|
|
|
|
maps=[m.save_state() for m in self.maps])
|
2020-11-16 00:01:18 +00:00
|
|
|
|
|
|
|
def load_state(self, d: dict) -> None:
|
2020-11-18 11:19:27 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +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:
|
2021-01-08 13:23:57 +00:00
|
|
|
self.map_index = d["map_index"]
|
|
|
|
self.maps = [Map().load_state(map_dict) for map_dict in d["maps"]]
|
2021-01-10 19:20:12 +00:00
|
|
|
except KeyError as error:
|
2020-11-27 19:42:19 +00:00
|
|
|
self.message = _("Some keys are missing in your save file.\n"
|
2021-01-10 20:01:43 +00:00
|
|
|
"Your save seems to be corrupt. It got deleted.")\
|
|
|
|
+ f"\n{error}"
|
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]
|
2021-01-10 18:52:28 +00:00
|
|
|
self.inventory_menu.update_player(self.player)
|
2021-01-08 13:23:57 +00:00
|
|
|
self.map.compute_visibility(self.player.y, self.player.x,
|
|
|
|
self.player.vision)
|
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:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Loads the game from a file.
|
2020-11-16 00:01:18 +00:00
|
|
|
"""
|
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"
|
2020-11-27 21:21:16 +00:00
|
|
|
"Your save seems corrupted. "
|
2020-11-27 19:42:19 +00:00
|
|
|
"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-12-13 20:29:25 +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()))
|