Merge branch 'master' into 'equipment'
# Conflicts: # squirrelbattle/display/statsdisplay.py # squirrelbattle/entities/items.py # squirrelbattle/entities/player.py # squirrelbattle/interfaces.py # squirrelbattle/locale/de/LC_MESSAGES/squirrelbattle.po # squirrelbattle/locale/es/LC_MESSAGES/squirrelbattle.po # squirrelbattle/locale/fr/LC_MESSAGES/squirrelbattle.po # squirrelbattle/tests/game_test.py
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
|
||||
from json import JSONDecodeError
|
||||
from random import randint
|
||||
from typing import Any, Optional
|
||||
from typing import Any, Optional, List
|
||||
import curses
|
||||
import json
|
||||
import os
|
||||
@ -22,7 +22,8 @@ class Game:
|
||||
"""
|
||||
The game object controls all actions in the game.
|
||||
"""
|
||||
map: Map
|
||||
maps: List[Map]
|
||||
map_index: int
|
||||
player: Player
|
||||
screen: Any
|
||||
# display_actions is a display interface set by the bootstrapper
|
||||
@ -30,7 +31,7 @@ class Game:
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""
|
||||
Init the game.
|
||||
Initiates the game.
|
||||
"""
|
||||
self.state = GameMode.MAINMENU
|
||||
self.waiting_for_friendly_key = False
|
||||
@ -49,10 +50,12 @@ class Game:
|
||||
|
||||
def new_game(self) -> None:
|
||||
"""
|
||||
Create a new game on the screen.
|
||||
Creates a new game on the screen.
|
||||
"""
|
||||
# TODO generate a new map procedurally
|
||||
self.map = Map.load(ResourceManager.get_asset_path("example_map_2.txt"))
|
||||
self.maps = []
|
||||
self.map_index = 0
|
||||
self.map = Map.load(ResourceManager.get_asset_path("example_map.txt"))
|
||||
self.map.logs = self.logs
|
||||
self.logs.clear()
|
||||
self.player = Player()
|
||||
@ -61,11 +64,29 @@ class Game:
|
||||
self.map.spawn_random_entities(randint(3, 10))
|
||||
self.inventory_menu.update_player(self.player)
|
||||
|
||||
@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
|
||||
|
||||
def run(self, screen: Any) -> None: # pragma no cover
|
||||
"""
|
||||
Main infinite loop.
|
||||
We wait for the player's action, then we do what that should be done
|
||||
when the given key gets pressed.
|
||||
We wait for the player's action, then we do what should be done
|
||||
when a key gets pressed.
|
||||
"""
|
||||
screen.refresh()
|
||||
while True:
|
||||
@ -84,7 +105,7 @@ class Game:
|
||||
def handle_key_pressed(self, key: Optional[KeyValues], raw_key: str = '')\
|
||||
-> None:
|
||||
"""
|
||||
Indicates what should be done when the given key is pressed,
|
||||
Indicates what should be done when a given key is pressed,
|
||||
according to the current game state.
|
||||
"""
|
||||
if self.message:
|
||||
@ -106,6 +127,8 @@ class Game:
|
||||
self.settings_menu.handle_key_pressed(key, raw_key, self)
|
||||
elif self.state == GameMode.STORE:
|
||||
self.handle_key_pressed_store(key)
|
||||
elif self.state == GameMode.CREDITS:
|
||||
self.state = GameMode.MAINMENU
|
||||
self.display_actions(DisplayActions.REFRESH)
|
||||
|
||||
def handle_key_pressed_play(self, key: KeyValues) -> None: # noqa: C901
|
||||
@ -114,16 +137,16 @@ class Game:
|
||||
"""
|
||||
if key == KeyValues.UP:
|
||||
if self.player.move_up():
|
||||
self.map.tick()
|
||||
self.map.tick(self.player)
|
||||
elif key == KeyValues.DOWN:
|
||||
if self.player.move_down():
|
||||
self.map.tick()
|
||||
self.map.tick(self.player)
|
||||
elif key == KeyValues.LEFT:
|
||||
if self.player.move_left():
|
||||
self.map.tick()
|
||||
self.map.tick(self.player)
|
||||
elif key == KeyValues.RIGHT:
|
||||
if self.player.move_right():
|
||||
self.map.tick()
|
||||
self.map.tick(self.player)
|
||||
elif key == KeyValues.INVENTORY:
|
||||
self.state = GameMode.INVENTORY
|
||||
self.display_actions(DisplayActions.UPDATE)
|
||||
@ -138,12 +161,61 @@ class Game:
|
||||
# Wait for the direction of the friendly entity
|
||||
self.waiting_for_friendly_key = True
|
||||
elif key == KeyValues.WAIT:
|
||||
self.map.tick()
|
||||
self.map.tick(self.player)
|
||||
elif key == KeyValues.LADDER:
|
||||
self.handle_ladder()
|
||||
|
||||
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)
|
||||
|
||||
def handle_friendly_entity_chat(self, key: KeyValues) -> None:
|
||||
"""
|
||||
If the player is talking to a friendly entity, we get the direction
|
||||
where the entity is, then we interact with it.
|
||||
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.
|
||||
"""
|
||||
if not self.waiting_for_friendly_key:
|
||||
return
|
||||
@ -232,7 +304,7 @@ class Game:
|
||||
|
||||
def handle_key_pressed_main_menu(self, key: KeyValues) -> None:
|
||||
"""
|
||||
In the main menu, we can navigate through options.
|
||||
In the main menu, we can navigate through different options.
|
||||
"""
|
||||
if key == KeyValues.DOWN:
|
||||
self.main_menu.go_down()
|
||||
@ -257,13 +329,13 @@ class Game:
|
||||
|
||||
def save_state(self) -> dict:
|
||||
"""
|
||||
Saves the game to a dictionary
|
||||
Saves the game to a dictionary.
|
||||
"""
|
||||
return self.map.save_state()
|
||||
|
||||
def load_state(self, d: dict) -> None:
|
||||
"""
|
||||
Loads the game from a dictionary
|
||||
Loads the game from a dictionary.
|
||||
"""
|
||||
try:
|
||||
self.map.load_state(d)
|
||||
@ -287,7 +359,7 @@ class Game:
|
||||
|
||||
def load_game(self) -> None:
|
||||
"""
|
||||
Loads the game from a file
|
||||
Loads the game from a file.
|
||||
"""
|
||||
file_path = ResourceManager.get_config_path("save.json")
|
||||
if os.path.isfile(file_path):
|
||||
@ -304,7 +376,7 @@ class Game:
|
||||
|
||||
def save_game(self) -> None:
|
||||
"""
|
||||
Saves the game to a file
|
||||
Saves the game to a file.
|
||||
"""
|
||||
with open(ResourceManager.get_config_path("save.json"), "w") as f:
|
||||
f.write(json.dumps(self.save_state()))
|
||||
|
Reference in New Issue
Block a user