From 85870494a077190d49529d061f55b3aae5f3378b Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 18 Dec 2020 15:07:09 +0100 Subject: [PATCH] More generic display update --- squirrelbattle/display/display.py | 10 ++++++++++ squirrelbattle/display/display_manager.py | 13 +++++-------- squirrelbattle/display/logsdisplay.py | 5 +++-- squirrelbattle/display/mapdisplay.py | 8 +++++--- squirrelbattle/display/menudisplay.py | 17 ++++++++++++++++- squirrelbattle/display/messagedisplay.py | 5 +++-- squirrelbattle/display/statsdisplay.py | 5 +++-- squirrelbattle/tests/game_test.py | 1 + 8 files changed, 46 insertions(+), 18 deletions(-) diff --git a/squirrelbattle/display/display.py b/squirrelbattle/display/display.py index 29295de..023ad6e 100644 --- a/squirrelbattle/display/display.py +++ b/squirrelbattle/display/display.py @@ -162,6 +162,16 @@ class Display: pad.refresh(top_y, top_x, window_y, window_x, last_y, last_x) 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 def handle_click(self, y: int, x: int, game: Game) -> None: diff --git a/squirrelbattle/display/display_manager.py b/squirrelbattle/display/display_manager.py index 83df543..3481348 100644 --- a/squirrelbattle/display/display_manager.py +++ b/squirrelbattle/display/display_manager.py @@ -49,16 +49,13 @@ class DisplayManager: self.handle_mouse_click(*params) def update_game_components(self) -> None: + """ + The game state was updated. + Trigger all displays of these modifications. + """ for d in self.displays: d.pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK) - self.mapdisplay.update_map(self.game.map) - self.statsdisplay.update_player(self.game.player) - self.game.inventory_menu.update_player(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) + d.update(self.game) def handle_mouse_click(self, y: int, x: int) -> None: displays = self.refresh() diff --git a/squirrelbattle/display/logsdisplay.py b/squirrelbattle/display/logsdisplay.py index b768a0e..b21273d 100644 --- a/squirrelbattle/display/logsdisplay.py +++ b/squirrelbattle/display/logsdisplay.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later from squirrelbattle.display.display import Display +from squirrelbattle.game import Game from squirrelbattle.interfaces import Logs @@ -11,8 +12,8 @@ class LogsDisplay(Display): super().__init__(*args) self.pad = self.newpad(self.rows, self.cols) - def update_logs(self, logs: Logs) -> None: - self.logs = logs + def update(self, game: Game) -> None: + self.logs = game.logs def display(self) -> None: messages = self.logs.messages[-self.height:] diff --git a/squirrelbattle/display/mapdisplay.py b/squirrelbattle/display/mapdisplay.py index 54d9432..72e339c 100644 --- a/squirrelbattle/display/mapdisplay.py +++ b/squirrelbattle/display/mapdisplay.py @@ -3,6 +3,7 @@ from squirrelbattle.interfaces import Map from .display import Display +from ..game import Game class MapDisplay(Display): @@ -10,9 +11,10 @@ class MapDisplay(Display): def __init__(self, *args): super().__init__(*args) - def update_map(self, m: Map) -> None: - self.map = m - self.pad = self.newpad(m.height, self.pack.tile_width * m.width + 1) + def update(self, game: Game) -> None: + self.map = game.map + self.pad = self.newpad(self.map.height, + self.pack.tile_width * self.map.width + 1) def update_pad(self) -> None: self.addstr(self.pad, 0, 0, self.map.draw_string(self.pack), diff --git a/squirrelbattle/display/menudisplay.py b/squirrelbattle/display/menudisplay.py index f9a5e09..ed10fec 100644 --- a/squirrelbattle/display/menudisplay.py +++ b/squirrelbattle/display/menudisplay.py @@ -5,7 +5,7 @@ import curses from random import randint from typing import List -from squirrelbattle.menus import Menu, MainMenu +from squirrelbattle.menus import Menu, MainMenu, SettingsMenu from .display import Box, Display from ..enums import KeyValues, GameMode from ..game import Game @@ -17,6 +17,7 @@ class MenuDisplay(Display): """ A class to display the menu objects """ + menu: Menu position: int def __init__(self, *args, **kwargs): @@ -80,6 +81,11 @@ class SettingsMenuDisplay(MenuDisplay): """ A class to display specifically a settingsmenu object """ + menu: SettingsMenu + + def update(self, game: Game) -> None: + self.update_menu(game.settings_menu) + @property def values(self) -> List[str]: return [_(a[1][1]) + (" : " @@ -122,6 +128,9 @@ class MainMenuDisplay(Display): menuy, menux, min(self.menudisplay.preferred_height, 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: menuwidth = min(self.menudisplay.preferred_width, self.width) menuy, menux = len(self.title) + 8, self.width // 2 - menuwidth // 2 - 1 @@ -135,6 +144,9 @@ class MainMenuDisplay(Display): class PlayerInventoryDisplay(MenuDisplay): + def update(self, game: Game) -> None: + self.update_menu(game.inventory_menu) + def update_pad(self) -> None: self.menubox.update_title(_("INVENTORY")) for i, item in enumerate(self.menu.values): @@ -166,6 +178,9 @@ class PlayerInventoryDisplay(MenuDisplay): class StoreInventoryDisplay(MenuDisplay): + def update(self, game: Game) -> None: + self.update_menu(game.store_menu) + def update_pad(self) -> None: self.menubox.update_title(_("STALL")) for i, item in enumerate(self.menu.values): diff --git a/squirrelbattle/display/messagedisplay.py b/squirrelbattle/display/messagedisplay.py index 32f7139..271055a 100644 --- a/squirrelbattle/display/messagedisplay.py +++ b/squirrelbattle/display/messagedisplay.py @@ -3,6 +3,7 @@ import curses from squirrelbattle.display.display import Box, Display +from squirrelbattle.game import Game class MessageDisplay(Display): @@ -17,8 +18,8 @@ class MessageDisplay(Display): self.message = "" self.pad = self.newpad(1, 1) - def update_message(self, msg: str) -> None: - self.message = msg + def update(self, game: Game) -> None: + self.message = game.message def display(self) -> None: self.box.refresh(self.y - 1, self.x - 2, diff --git a/squirrelbattle/display/statsdisplay.py b/squirrelbattle/display/statsdisplay.py index 9937c3e..ef358bb 100644 --- a/squirrelbattle/display/statsdisplay.py +++ b/squirrelbattle/display/statsdisplay.py @@ -4,6 +4,7 @@ import curses from ..entities.player import Player +from ..game import Game from ..translations import gettext as _ from .display import Display @@ -15,8 +16,8 @@ class StatsDisplay(Display): super().__init__(*args, **kwargs) self.pad = self.newpad(self.rows, self.cols) - def update_player(self, p: Player) -> None: - self.player = p + def update(self, game: Game) -> None: + self.player = game.player def update_pad(self) -> None: string2 = "Player -- LVL {}\nEXP {}/{}\nHP {}/{}"\ diff --git a/squirrelbattle/tests/game_test.py b/squirrelbattle/tests/game_test.py index 89d6ed6..6751016 100644 --- a/squirrelbattle/tests/game_test.py +++ b/squirrelbattle/tests/game_test.py @@ -404,6 +404,7 @@ class TestGame(unittest.TestCase): Check that some functions are not implemented, only for coverage. """ self.assertRaises(NotImplementedError, Display.display, None) + self.assertRaises(NotImplementedError, Display.update, None, self.game) def test_messages(self) -> None: """