Merge branch 'capitalism' into 'master'
Sell items Closes #50, #49 et #47 See merge request ynerant/squirrel-battle!51
This commit is contained in:
commit
330d78702a
|
@ -159,9 +159,19 @@ class Display:
|
||||||
|
|
||||||
if last_y >= window_y and last_x >= window_x:
|
if last_y >= window_y and last_x >= window_x:
|
||||||
# Refresh the pad only if coordinates are valid
|
# Refresh the pad only if coordinates are valid
|
||||||
pad.refresh(top_y, top_x, window_y, window_x, last_y, last_x)
|
pad.noutrefresh(top_y, top_x, window_y, window_x, last_y, last_x)
|
||||||
|
|
||||||
def display(self) -> None:
|
def display(self) -> None:
|
||||||
|
"""
|
||||||
|
Draw the content of the display and refresh pads.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def update(self, game: Game) -> None:
|
||||||
|
"""
|
||||||
|
The game state was updated.
|
||||||
|
Indicate what to do with the new state.
|
||||||
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def handle_click(self, y: int, x: int, game: Game) -> None:
|
def handle_click(self, y: int, x: int, game: Game) -> None:
|
||||||
|
|
|
@ -49,17 +49,13 @@ class DisplayManager:
|
||||||
self.handle_mouse_click(*params)
|
self.handle_mouse_click(*params)
|
||||||
|
|
||||||
def update_game_components(self) -> None:
|
def update_game_components(self) -> None:
|
||||||
|
"""
|
||||||
|
The game state was updated.
|
||||||
|
Trigger all displays of these modifications.
|
||||||
|
"""
|
||||||
for d in self.displays:
|
for d in self.displays:
|
||||||
d.pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
|
d.pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
|
||||||
self.mapdisplay.update_map(self.game.map)
|
d.update(self.game)
|
||||||
self.statsdisplay.update_player(self.game.player)
|
|
||||||
self.game.inventory_menu.update_player(self.game.player)
|
|
||||||
self.game.store_menu.update_merchant(self.game.player)
|
|
||||||
self.playerinventorydisplay.update_menu(self.game.inventory_menu)
|
|
||||||
self.storeinventorydisplay.update_menu(self.game.store_menu)
|
|
||||||
self.settingsmenudisplay.update_menu(self.game.settings_menu)
|
|
||||||
self.logsdisplay.update_logs(self.game.logs)
|
|
||||||
self.messagedisplay.update_message(self.game.message)
|
|
||||||
|
|
||||||
def handle_mouse_click(self, y: int, x: int) -> None:
|
def handle_mouse_click(self, y: int, x: int) -> None:
|
||||||
displays = self.refresh()
|
displays = self.refresh()
|
||||||
|
@ -75,6 +71,7 @@ class DisplayManager:
|
||||||
|
|
||||||
def refresh(self) -> List[Display]:
|
def refresh(self) -> List[Display]:
|
||||||
displays = []
|
displays = []
|
||||||
|
pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
|
||||||
|
|
||||||
if self.game.state == GameMode.PLAY \
|
if self.game.state == GameMode.PLAY \
|
||||||
or self.game.state == GameMode.INVENTORY \
|
or self.game.state == GameMode.INVENTORY \
|
||||||
|
@ -97,14 +94,24 @@ class DisplayManager:
|
||||||
|
|
||||||
if self.game.state == GameMode.INVENTORY:
|
if self.game.state == GameMode.INVENTORY:
|
||||||
self.playerinventorydisplay.refresh(
|
self.playerinventorydisplay.refresh(
|
||||||
self.rows // 10, self.cols // 2,
|
self.rows // 10,
|
||||||
8 * self.rows // 10, 2 * self.cols // 5)
|
pack.tile_width * (self.cols // (2 * pack.tile_width)),
|
||||||
|
8 * self.rows // 10,
|
||||||
|
pack.tile_width * (2 * self.cols // (5 * pack.tile_width)))
|
||||||
displays.append(self.playerinventorydisplay)
|
displays.append(self.playerinventorydisplay)
|
||||||
elif self.game.state == GameMode.STORE:
|
elif self.game.state == GameMode.STORE:
|
||||||
self.storeinventorydisplay.refresh(
|
self.storeinventorydisplay.refresh(
|
||||||
self.rows // 10, self.cols // 2,
|
self.rows // 10,
|
||||||
8 * self.rows // 10, 2 * self.cols // 5)
|
pack.tile_width * (self.cols // (2 * pack.tile_width)),
|
||||||
|
8 * self.rows // 10,
|
||||||
|
pack.tile_width * (2 * self.cols // (5 * pack.tile_width)))
|
||||||
|
self.playerinventorydisplay.refresh(
|
||||||
|
self.rows // 10,
|
||||||
|
pack.tile_width * (self.cols // (10 * pack.tile_width)),
|
||||||
|
8 * self.rows // 10,
|
||||||
|
pack.tile_width * (2 * self.cols // (5 * pack.tile_width)))
|
||||||
displays.append(self.storeinventorydisplay)
|
displays.append(self.storeinventorydisplay)
|
||||||
|
displays.append(self.playerinventorydisplay)
|
||||||
elif self.game.state == GameMode.MAINMENU:
|
elif self.game.state == GameMode.MAINMENU:
|
||||||
self.mainmenudisplay.refresh(0, 0, self.rows, self.cols)
|
self.mainmenudisplay.refresh(0, 0, self.rows, self.cols)
|
||||||
displays.append(self.mainmenudisplay)
|
displays.append(self.mainmenudisplay)
|
||||||
|
@ -117,7 +124,8 @@ class DisplayManager:
|
||||||
for line in self.game.message.split("\n"):
|
for line in self.game.message.split("\n"):
|
||||||
height += 1
|
height += 1
|
||||||
width = max(width, len(line))
|
width = max(width, len(line))
|
||||||
y, x = (self.rows - height) // 2, (self.cols - width) // 2
|
y = pack.tile_width * (self.rows - height) // (2 * pack.tile_width)
|
||||||
|
x = pack.tile_width * ((self.cols - width) // (2 * pack.tile_width))
|
||||||
self.messagedisplay.refresh(y, x, height, width)
|
self.messagedisplay.refresh(y, x, height, width)
|
||||||
displays.append(self.messagedisplay)
|
displays.append(self.messagedisplay)
|
||||||
|
|
||||||
|
|
|
@ -2,17 +2,19 @@
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
from squirrelbattle.display.display import Display
|
from squirrelbattle.display.display import Display
|
||||||
|
from squirrelbattle.game import Game
|
||||||
from squirrelbattle.interfaces import Logs
|
from squirrelbattle.interfaces import Logs
|
||||||
|
|
||||||
|
|
||||||
class LogsDisplay(Display):
|
class LogsDisplay(Display):
|
||||||
|
logs: Logs
|
||||||
|
|
||||||
def __init__(self, *args) -> None:
|
def __init__(self, *args) -> None:
|
||||||
super().__init__(*args)
|
super().__init__(*args)
|
||||||
self.pad = self.newpad(self.rows, self.cols)
|
self.pad = self.newpad(self.rows, self.cols)
|
||||||
|
|
||||||
def update_logs(self, logs: Logs) -> None:
|
def update(self, game: Game) -> None:
|
||||||
self.logs = logs
|
self.logs = game.logs
|
||||||
|
|
||||||
def display(self) -> None:
|
def display(self) -> None:
|
||||||
messages = self.logs.messages[-self.height:]
|
messages = self.logs.messages[-self.height:]
|
||||||
|
|
|
@ -3,18 +3,22 @@
|
||||||
|
|
||||||
from squirrelbattle.interfaces import Map
|
from squirrelbattle.interfaces import Map
|
||||||
from .display import Display
|
from .display import Display
|
||||||
|
from ..game import Game
|
||||||
|
|
||||||
|
|
||||||
class MapDisplay(Display):
|
class MapDisplay(Display):
|
||||||
|
map: Map
|
||||||
|
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
super().__init__(*args)
|
super().__init__(*args)
|
||||||
|
|
||||||
def update_map(self, m: Map) -> None:
|
def update(self, game: Game) -> None:
|
||||||
self.map = m
|
self.map = game.map
|
||||||
self.pad = self.newpad(m.height, self.pack.tile_width * m.width + 1)
|
self.pad = self.newpad(self.map.height,
|
||||||
|
self.pack.tile_width * self.map.width + 1)
|
||||||
|
|
||||||
def update_pad(self) -> None:
|
def update_pad(self) -> None:
|
||||||
|
self.pad.resize(500, 500)
|
||||||
self.addstr(self.pad, 0, 0, self.map.draw_string(self.pack),
|
self.addstr(self.pad, 0, 0, self.map.draw_string(self.pack),
|
||||||
self.pack.tile_fg_color, self.pack.tile_bg_color)
|
self.pack.tile_fg_color, self.pack.tile_bg_color)
|
||||||
for e in self.map.entities:
|
for e in self.map.entities:
|
||||||
|
|
|
@ -5,9 +5,10 @@ import curses
|
||||||
from random import randint
|
from random import randint
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from squirrelbattle.menus import Menu, MainMenu
|
from squirrelbattle.menus import Menu, MainMenu, SettingsMenu, StoreMenu
|
||||||
from .display import Box, Display
|
from .display import Box, Display
|
||||||
from ..enums import KeyValues
|
from ..entities.player import Player
|
||||||
|
from ..enums import KeyValues, GameMode
|
||||||
from ..game import Game
|
from ..game import Game
|
||||||
from ..resources import ResourceManager
|
from ..resources import ResourceManager
|
||||||
from ..translations import gettext as _
|
from ..translations import gettext as _
|
||||||
|
@ -17,6 +18,7 @@ class MenuDisplay(Display):
|
||||||
"""
|
"""
|
||||||
A class to display the menu objects
|
A class to display the menu objects
|
||||||
"""
|
"""
|
||||||
|
menu: Menu
|
||||||
position: int
|
position: int
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
@ -80,6 +82,11 @@ class SettingsMenuDisplay(MenuDisplay):
|
||||||
"""
|
"""
|
||||||
A class to display specifically a settingsmenu object
|
A class to display specifically a settingsmenu object
|
||||||
"""
|
"""
|
||||||
|
menu: SettingsMenu
|
||||||
|
|
||||||
|
def update(self, game: Game) -> None:
|
||||||
|
self.update_menu(game.settings_menu)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def values(self) -> List[str]:
|
def values(self) -> List[str]:
|
||||||
return [_(a[1][1]) + (" : "
|
return [_(a[1][1]) + (" : "
|
||||||
|
@ -122,6 +129,9 @@ class MainMenuDisplay(Display):
|
||||||
menuy, menux, min(self.menudisplay.preferred_height,
|
menuy, menux, min(self.menudisplay.preferred_height,
|
||||||
self.height - menuy), menuwidth)
|
self.height - menuy), menuwidth)
|
||||||
|
|
||||||
|
def update(self, game: Game) -> None:
|
||||||
|
self.menudisplay.update_menu(game.main_menu)
|
||||||
|
|
||||||
def handle_click(self, y: int, x: int, game: Game) -> None:
|
def handle_click(self, y: int, x: int, game: Game) -> None:
|
||||||
menuwidth = min(self.menudisplay.preferred_width, self.width)
|
menuwidth = min(self.menudisplay.preferred_width, self.width)
|
||||||
menuy, menux = len(self.title) + 8, self.width // 2 - menuwidth // 2 - 1
|
menuy, menux = len(self.title) + 8, self.width // 2 - menuwidth // 2 - 1
|
||||||
|
@ -135,13 +145,33 @@ class MainMenuDisplay(Display):
|
||||||
|
|
||||||
|
|
||||||
class PlayerInventoryDisplay(MenuDisplay):
|
class PlayerInventoryDisplay(MenuDisplay):
|
||||||
|
player: Player = None
|
||||||
|
selected: bool = True
|
||||||
|
store_mode: bool = False
|
||||||
|
|
||||||
|
def update(self, game: Game) -> None:
|
||||||
|
self.player = game.player
|
||||||
|
self.update_menu(game.inventory_menu)
|
||||||
|
self.store_mode = game.state == GameMode.STORE
|
||||||
|
self.selected = game.state == GameMode.INVENTORY \
|
||||||
|
or (self.store_mode and not game.is_in_store_menu)
|
||||||
|
|
||||||
def update_pad(self) -> None:
|
def update_pad(self) -> None:
|
||||||
self.menubox.update_title(_("INVENTORY"))
|
self.menubox.update_title(_("INVENTORY"))
|
||||||
for i, item in enumerate(self.menu.values):
|
for i, item in enumerate(self.menu.values):
|
||||||
rep = self.pack[item.name.upper()]
|
rep = self.pack[item.name.upper()]
|
||||||
selection = f"[{rep}]" if i == self.menu.position else f" {rep} "
|
selection = f"[{rep}]" if i == self.menu.position \
|
||||||
|
and self.selected else f" {rep} "
|
||||||
self.addstr(self.pad, i + 1, 0, selection
|
self.addstr(self.pad, i + 1, 0, selection
|
||||||
+ " " + item.translated_name.capitalize())
|
+ " " + item.translated_name.capitalize()
|
||||||
|
+ (": " + str(item.price) + " Hazels"
|
||||||
|
if self.store_mode else ""))
|
||||||
|
|
||||||
|
if self.store_mode:
|
||||||
|
price = f"{self.pack.HAZELNUT} {self.player.hazel} Hazels"
|
||||||
|
width = len(price) + (self.pack.tile_width - 1)
|
||||||
|
self.addstr(self.pad, self.height - 3, self.width - width - 2,
|
||||||
|
price, italic=True)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def truewidth(self) -> int:
|
def truewidth(self) -> int:
|
||||||
|
@ -156,19 +186,33 @@ class PlayerInventoryDisplay(MenuDisplay):
|
||||||
We can select a menu item with the mouse.
|
We can select a menu item with the mouse.
|
||||||
"""
|
"""
|
||||||
self.menu.position = max(0, min(len(self.menu.values) - 1, y - 2))
|
self.menu.position = max(0, min(len(self.menu.values) - 1, y - 2))
|
||||||
|
game.is_in_store_menu = False
|
||||||
game.handle_key_pressed(KeyValues.ENTER)
|
game.handle_key_pressed(KeyValues.ENTER)
|
||||||
|
|
||||||
|
|
||||||
class StoreInventoryDisplay(MenuDisplay):
|
class StoreInventoryDisplay(MenuDisplay):
|
||||||
|
menu: StoreMenu
|
||||||
|
selected: bool = False
|
||||||
|
|
||||||
|
def update(self, game: Game) -> None:
|
||||||
|
self.update_menu(game.store_menu)
|
||||||
|
self.selected = game.is_in_store_menu
|
||||||
|
|
||||||
def update_pad(self) -> None:
|
def update_pad(self) -> None:
|
||||||
self.menubox.update_title(_("STALL"))
|
self.menubox.update_title(_("STALL"))
|
||||||
for i, item in enumerate(self.menu.values):
|
for i, item in enumerate(self.menu.values):
|
||||||
rep = self.pack[item.name.upper()]
|
rep = self.pack[item.name.upper()]
|
||||||
selection = f"[{rep}]" if i == self.menu.position else f" {rep} "
|
selection = f"[{rep}]" if i == self.menu.position \
|
||||||
|
and self.selected else f" {rep} "
|
||||||
self.addstr(self.pad, i + 1, 0, selection
|
self.addstr(self.pad, i + 1, 0, selection
|
||||||
+ " " + item.translated_name.capitalize()
|
+ " " + item.translated_name.capitalize()
|
||||||
+ ": " + str(item.price) + " Hazels")
|
+ ": " + str(item.price) + " Hazels")
|
||||||
|
|
||||||
|
price = f"{self.pack.HAZELNUT} {self.menu.merchant.hazel} Hazels"
|
||||||
|
width = len(price) + (self.pack.tile_width - 1)
|
||||||
|
self.addstr(self.pad, self.height - 3, self.width - width - 2, price,
|
||||||
|
italic=True)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def truewidth(self) -> int:
|
def truewidth(self) -> int:
|
||||||
return max(1, self.height if hasattr(self, "height") else 10)
|
return max(1, self.height if hasattr(self, "height") else 10)
|
||||||
|
@ -182,4 +226,5 @@ class StoreInventoryDisplay(MenuDisplay):
|
||||||
We can select a menu item with the mouse.
|
We can select a menu item with the mouse.
|
||||||
"""
|
"""
|
||||||
self.menu.position = max(0, min(len(self.menu.values) - 1, y - 2))
|
self.menu.position = max(0, min(len(self.menu.values) - 1, y - 2))
|
||||||
|
game.is_in_store_menu = True
|
||||||
game.handle_key_pressed(KeyValues.ENTER)
|
game.handle_key_pressed(KeyValues.ENTER)
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
import curses
|
import curses
|
||||||
|
|
||||||
from squirrelbattle.display.display import Box, Display
|
from squirrelbattle.display.display import Box, Display
|
||||||
|
from squirrelbattle.game import Game
|
||||||
|
|
||||||
|
|
||||||
class MessageDisplay(Display):
|
class MessageDisplay(Display):
|
||||||
|
@ -17,8 +18,8 @@ class MessageDisplay(Display):
|
||||||
self.message = ""
|
self.message = ""
|
||||||
self.pad = self.newpad(1, 1)
|
self.pad = self.newpad(1, 1)
|
||||||
|
|
||||||
def update_message(self, msg: str) -> None:
|
def update(self, game: Game) -> None:
|
||||||
self.message = msg
|
self.message = game.message
|
||||||
|
|
||||||
def display(self) -> None:
|
def display(self) -> None:
|
||||||
self.box.refresh(self.y - 1, self.x - 2,
|
self.box.refresh(self.y - 1, self.x - 2,
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
import curses
|
import curses
|
||||||
|
|
||||||
from ..entities.player import Player
|
from ..entities.player import Player
|
||||||
|
from ..game import Game
|
||||||
from ..translations import gettext as _
|
from ..translations import gettext as _
|
||||||
from .display import Display
|
from .display import Display
|
||||||
|
|
||||||
|
@ -15,8 +16,8 @@ class StatsDisplay(Display):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.pad = self.newpad(self.rows, self.cols)
|
self.pad = self.newpad(self.rows, self.cols)
|
||||||
|
|
||||||
def update_player(self, p: Player) -> None:
|
def update(self, game: Game) -> None:
|
||||||
self.player = p
|
self.player = game.player
|
||||||
|
|
||||||
def update_pad(self) -> None:
|
def update_pad(self) -> None:
|
||||||
string2 = "Player -- LVL {}\nEXP {}/{}\nHP {}/{}"\
|
string2 = "Player -- LVL {}\nEXP {}/{}\nHP {}/{}"\
|
||||||
|
|
|
@ -97,7 +97,7 @@ TexturePack.SQUIRREL_PACK = TexturePack(
|
||||||
MERCHANT='🦜',
|
MERCHANT='🦜',
|
||||||
RABBIT='🐇',
|
RABBIT='🐇',
|
||||||
SUNFLOWER='🌻',
|
SUNFLOWER='🌻',
|
||||||
SWORD='🗡️',
|
SWORD='🗡️ ',
|
||||||
TEDDY_BEAR='🧸',
|
TEDDY_BEAR='🧸',
|
||||||
TIGER='🐅',
|
TIGER='🐅',
|
||||||
WALL='🧱',
|
WALL='🧱',
|
||||||
|
|
|
@ -34,6 +34,7 @@ class Game:
|
||||||
"""
|
"""
|
||||||
self.state = GameMode.MAINMENU
|
self.state = GameMode.MAINMENU
|
||||||
self.waiting_for_friendly_key = False
|
self.waiting_for_friendly_key = False
|
||||||
|
self.is_in_store_menu = True
|
||||||
self.settings = Settings()
|
self.settings = Settings()
|
||||||
self.settings.load_settings()
|
self.settings.load_settings()
|
||||||
self.settings.write_settings()
|
self.settings.write_settings()
|
||||||
|
@ -51,7 +52,7 @@ class Game:
|
||||||
Create a new game on the screen.
|
Create a new game on the screen.
|
||||||
"""
|
"""
|
||||||
# TODO generate a new map procedurally
|
# TODO generate a new map procedurally
|
||||||
self.map = Map.load(ResourceManager.get_asset_path("example_map.txt"))
|
self.map = Map.load(ResourceManager.get_asset_path("example_map_2.txt"))
|
||||||
self.map.logs = self.logs
|
self.map.logs = self.logs
|
||||||
self.logs.clear()
|
self.logs.clear()
|
||||||
self.player = Player()
|
self.player = Player()
|
||||||
|
@ -60,16 +61,18 @@ class Game:
|
||||||
self.map.spawn_random_entities(randint(3, 10))
|
self.map.spawn_random_entities(randint(3, 10))
|
||||||
self.inventory_menu.update_player(self.player)
|
self.inventory_menu.update_player(self.player)
|
||||||
|
|
||||||
def run(self, screen: Any) -> None:
|
def run(self, screen: Any) -> None: # pragma no cover
|
||||||
"""
|
"""
|
||||||
Main infinite loop.
|
Main infinite loop.
|
||||||
We wait for the player's action, then we do what that should be done
|
We wait for the player's action, then we do what that should be done
|
||||||
when the given key gets pressed.
|
when the given key gets pressed.
|
||||||
"""
|
"""
|
||||||
while True: # pragma no cover
|
screen.refresh()
|
||||||
|
while True:
|
||||||
screen.erase()
|
screen.erase()
|
||||||
screen.refresh()
|
screen.noutrefresh()
|
||||||
self.display_actions(DisplayActions.REFRESH)
|
self.display_actions(DisplayActions.REFRESH)
|
||||||
|
curses.doupdate()
|
||||||
key = screen.getkey()
|
key = screen.getkey()
|
||||||
if key == "KEY_MOUSE":
|
if key == "KEY_MOUSE":
|
||||||
_ignored1, x, y, _ignored2, _ignored3 = curses.getmouse()
|
_ignored1, x, y, _ignored2, _ignored3 = curses.getmouse()
|
||||||
|
@ -162,7 +165,9 @@ class Game:
|
||||||
self.logs.add_message(msg)
|
self.logs.add_message(msg)
|
||||||
if entity.is_merchant():
|
if entity.is_merchant():
|
||||||
self.state = GameMode.STORE
|
self.state = GameMode.STORE
|
||||||
|
self.is_in_store_menu = True
|
||||||
self.store_menu.update_merchant(entity)
|
self.store_menu.update_merchant(entity)
|
||||||
|
self.display_actions(DisplayActions.UPDATE)
|
||||||
|
|
||||||
def handle_key_pressed_inventory(self, key: KeyValues) -> None:
|
def handle_key_pressed_inventory(self, key: KeyValues) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -191,22 +196,33 @@ class Game:
|
||||||
"""
|
"""
|
||||||
In a store menu, we can buy items or close the menu.
|
In a store menu, we can buy items or close the menu.
|
||||||
"""
|
"""
|
||||||
if key == KeyValues.SPACE:
|
menu = self.store_menu if self.is_in_store_menu else self.inventory_menu
|
||||||
|
|
||||||
|
if key == KeyValues.SPACE or key == KeyValues.INVENTORY:
|
||||||
self.state = GameMode.PLAY
|
self.state = GameMode.PLAY
|
||||||
elif key == KeyValues.UP:
|
elif key == KeyValues.UP:
|
||||||
self.store_menu.go_up()
|
menu.go_up()
|
||||||
elif key == KeyValues.DOWN:
|
elif key == KeyValues.DOWN:
|
||||||
self.store_menu.go_down()
|
menu.go_down()
|
||||||
if self.store_menu.values and not self.player.dead:
|
elif key == KeyValues.LEFT:
|
||||||
|
self.is_in_store_menu = False
|
||||||
|
self.display_actions(DisplayActions.UPDATE)
|
||||||
|
elif key == KeyValues.RIGHT:
|
||||||
|
self.is_in_store_menu = True
|
||||||
|
self.display_actions(DisplayActions.UPDATE)
|
||||||
|
if menu.values and not self.player.dead:
|
||||||
if key == KeyValues.ENTER:
|
if key == KeyValues.ENTER:
|
||||||
item = self.store_menu.validate()
|
item = menu.validate()
|
||||||
flag = item.be_sold(self.player, self.store_menu.merchant)
|
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)
|
||||||
if not flag:
|
if not flag:
|
||||||
self.message = _("You do not have enough money")
|
self.message = _("The buyer does not have enough money")
|
||||||
self.display_actions(DisplayActions.UPDATE)
|
self.display_actions(DisplayActions.UPDATE)
|
||||||
# Ensure that the cursor has a good position
|
# Ensure that the cursor has a good position
|
||||||
self.store_menu.position = min(self.store_menu.position,
|
menu.position = min(menu.position, len(menu.values) - 1)
|
||||||
len(self.store_menu.values) - 1)
|
|
||||||
|
|
||||||
def handle_key_pressed_main_menu(self, key: KeyValues) -> None:
|
def handle_key_pressed_main_menu(self, key: KeyValues) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -59,8 +59,8 @@ msgid "{player} exchanged its body with {entity}."
|
||||||
msgstr "{player} täuscht seinem Körper mit {entity} aus."
|
msgstr "{player} täuscht seinem Körper mit {entity} aus."
|
||||||
|
|
||||||
#: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573
|
#: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573
|
||||||
msgid "You do not have enough money"
|
msgid "The buyer does not have enough money"
|
||||||
msgstr "Sie haben nicht genug Geld"
|
msgstr "Der Kaufer hat nicht genug Geld"
|
||||||
|
|
||||||
#: squirrelbattle/game.py:249
|
#: squirrelbattle/game.py:249
|
||||||
msgid ""
|
msgid ""
|
||||||
|
|
|
@ -58,8 +58,8 @@ msgid "{player} exchanged its body with {entity}."
|
||||||
msgstr "{player} intercambió su cuerpo con {entity}."
|
msgstr "{player} intercambió su cuerpo con {entity}."
|
||||||
|
|
||||||
#: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573
|
#: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573
|
||||||
msgid "You do not have enough money"
|
msgid "The buyer does not have enough money"
|
||||||
msgstr "No tienes suficiente dinero"
|
msgstr "El comprador no tiene suficiente dinero"
|
||||||
|
|
||||||
#: squirrelbattle/game.py:249
|
#: squirrelbattle/game.py:249
|
||||||
msgid ""
|
msgid ""
|
||||||
|
|
|
@ -59,8 +59,8 @@ msgid "{player} exchanged its body with {entity}."
|
||||||
msgstr "{player} a échangé son corps avec {entity}."
|
msgstr "{player} a échangé son corps avec {entity}."
|
||||||
|
|
||||||
#: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573
|
#: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573
|
||||||
msgid "You do not have enough money"
|
msgid "The buyer does not have enough money"
|
||||||
msgstr "Vous n'avez pas assez d'argent"
|
msgstr "L'acheteur n'a pas assez d'argent"
|
||||||
|
|
||||||
#: squirrelbattle/game.py:249
|
#: squirrelbattle/game.py:249
|
||||||
msgid ""
|
msgid ""
|
||||||
|
|
|
@ -132,11 +132,11 @@ class InventoryMenu(Menu):
|
||||||
|
|
||||||
|
|
||||||
class StoreMenu(Menu):
|
class StoreMenu(Menu):
|
||||||
merchant: Merchant
|
merchant: Merchant = None
|
||||||
|
|
||||||
def update_merchant(self, merchant: Merchant) -> None:
|
def update_merchant(self, merchant: Merchant) -> None:
|
||||||
self.merchant = merchant
|
self.merchant = merchant
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def values(self) -> list:
|
def values(self) -> list:
|
||||||
return self.merchant.inventory
|
return self.merchant.inventory if self.merchant else []
|
||||||
|
|
|
@ -12,6 +12,7 @@ from ..entities.items import Bomb, Heart, Sword, Explosion
|
||||||
from ..entities.player import Player
|
from ..entities.player import Player
|
||||||
from ..enums import DisplayActions
|
from ..enums import DisplayActions
|
||||||
from ..game import Game, KeyValues, GameMode
|
from ..game import Game, KeyValues, GameMode
|
||||||
|
from ..interfaces import Map
|
||||||
from ..menus import MainMenuValues
|
from ..menus import MainMenuValues
|
||||||
from ..resources import ResourceManager
|
from ..resources import ResourceManager
|
||||||
from ..settings import Settings
|
from ..settings import Settings
|
||||||
|
@ -25,6 +26,10 @@ class TestGame(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
self.game = Game()
|
self.game = Game()
|
||||||
self.game.new_game()
|
self.game.new_game()
|
||||||
|
self.game.map = Map.load(
|
||||||
|
ResourceManager.get_asset_path("example_map.txt"))
|
||||||
|
self.game.map.add_entity(self.game.player)
|
||||||
|
self.game.player.move(self.game.map.start_y, self.game.map.start_x)
|
||||||
self.game.logs.add_message("Hello World !")
|
self.game.logs.add_message("Hello World !")
|
||||||
display = DisplayManager(None, self.game)
|
display = DisplayManager(None, self.game)
|
||||||
self.game.display_actions = display.handle_display_action
|
self.game.display_actions = display.handle_display_action
|
||||||
|
@ -399,6 +404,7 @@ class TestGame(unittest.TestCase):
|
||||||
Check that some functions are not implemented, only for coverage.
|
Check that some functions are not implemented, only for coverage.
|
||||||
"""
|
"""
|
||||||
self.assertRaises(NotImplementedError, Display.display, None)
|
self.assertRaises(NotImplementedError, Display.display, None)
|
||||||
|
self.assertRaises(NotImplementedError, Display.update, None, self.game)
|
||||||
|
|
||||||
def test_messages(self) -> None:
|
def test_messages(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -546,18 +552,21 @@ class TestGame(unittest.TestCase):
|
||||||
# Navigate in the menu
|
# Navigate in the menu
|
||||||
self.game.handle_key_pressed(KeyValues.DOWN)
|
self.game.handle_key_pressed(KeyValues.DOWN)
|
||||||
self.game.handle_key_pressed(KeyValues.DOWN)
|
self.game.handle_key_pressed(KeyValues.DOWN)
|
||||||
|
self.game.handle_key_pressed(KeyValues.LEFT)
|
||||||
|
self.assertFalse(self.game.is_in_store_menu)
|
||||||
|
self.game.handle_key_pressed(KeyValues.RIGHT)
|
||||||
|
self.assertTrue(self.game.is_in_store_menu)
|
||||||
self.game.handle_key_pressed(KeyValues.UP)
|
self.game.handle_key_pressed(KeyValues.UP)
|
||||||
self.assertEqual(self.game.store_menu.position, 1)
|
self.assertEqual(self.game.store_menu.position, 1)
|
||||||
|
|
||||||
self.game.player.hazel = 0x7ffff42ff
|
self.game.player.hazel = 0x7ffff42ff
|
||||||
|
|
||||||
# The second item is not a heart
|
# The second item is not a heart
|
||||||
merchant.inventory[1] = Sword()
|
merchant.inventory[1] = sword = Sword()
|
||||||
# Buy the second item by clicking on it
|
# Buy the second item by clicking on it
|
||||||
item = self.game.store_menu.validate()
|
item = self.game.store_menu.validate()
|
||||||
self.assertIn(item, merchant.inventory)
|
self.assertIn(item, merchant.inventory)
|
||||||
self.game.display_actions(DisplayActions.MOUSE, 7, 25)
|
self.game.display_actions(DisplayActions.MOUSE, 7, 25)
|
||||||
self.game.handle_key_pressed(KeyValues.ENTER)
|
|
||||||
self.assertIn(item, self.game.player.inventory)
|
self.assertIn(item, self.game.player.inventory)
|
||||||
self.assertNotIn(item, merchant.inventory)
|
self.assertNotIn(item, merchant.inventory)
|
||||||
|
|
||||||
|
@ -579,9 +588,24 @@ class TestGame(unittest.TestCase):
|
||||||
self.game.handle_key_pressed(KeyValues.ENTER)
|
self.game.handle_key_pressed(KeyValues.ENTER)
|
||||||
self.assertNotIn(item, self.game.player.inventory)
|
self.assertNotIn(item, self.game.player.inventory)
|
||||||
self.assertIn(item, merchant.inventory)
|
self.assertIn(item, merchant.inventory)
|
||||||
self.assertEqual(self.game.message, _("You do not have enough money"))
|
self.assertEqual(self.game.message,
|
||||||
|
_("The buyer does not have enough money"))
|
||||||
self.game.handle_key_pressed(KeyValues.ENTER)
|
self.game.handle_key_pressed(KeyValues.ENTER)
|
||||||
|
|
||||||
|
# Sell an item
|
||||||
|
self.game.inventory_menu.position = len(self.game.player.inventory) - 1
|
||||||
|
self.game.handle_key_pressed(KeyValues.LEFT)
|
||||||
|
self.assertFalse(self.game.is_in_store_menu)
|
||||||
|
self.assertIn(sword, self.game.player.inventory)
|
||||||
|
self.assertEqual(self.game.inventory_menu.validate(), sword)
|
||||||
|
old_player_money, old_merchant_money = self.game.player.hazel,\
|
||||||
|
merchant.hazel
|
||||||
|
self.game.handle_key_pressed(KeyValues.ENTER)
|
||||||
|
self.assertNotIn(sword, self.game.player.inventory)
|
||||||
|
self.assertIn(sword, merchant.inventory)
|
||||||
|
self.assertEqual(self.game.player.hazel, old_player_money + sword.price)
|
||||||
|
self.assertEqual(merchant.hazel, old_merchant_money - sword.price)
|
||||||
|
|
||||||
# Exit the menu
|
# Exit the menu
|
||||||
self.game.handle_key_pressed(KeyValues.SPACE)
|
self.game.handle_key_pressed(KeyValues.SPACE)
|
||||||
self.assertEqual(self.game.state, GameMode.PLAY)
|
self.assertEqual(self.game.state, GameMode.PLAY)
|
||||||
|
|
|
@ -12,8 +12,8 @@ class FakePad:
|
||||||
def addstr(self, y: int, x: int, message: str, color: int = 0) -> None:
|
def addstr(self, y: int, x: int, message: str, color: int = 0) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def refresh(self, pminrow: int, pmincol: int, sminrow: int,
|
def noutrefresh(self, pminrow: int, pmincol: int, sminrow: int,
|
||||||
smincol: int, smaxrow: int, smaxcol: int) -> None:
|
smincol: int, smaxrow: int, smaxcol: int) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def erase(self) -> None:
|
def erase(self) -> None:
|
||||||
|
|
Loading…
Reference in New Issue