This commit is contained in:
Yohann D'ANELLO 2020-11-10 20:34:22 +01:00
parent 1ff2e26cd4
commit 5c95bf11e7
7 changed files with 72 additions and 71 deletions

View File

@ -2,6 +2,7 @@ from dungeonbattle.game import Game
from dungeonbattle.display.display_manager import DisplayManager from dungeonbattle.display.display_manager import DisplayManager
from dungeonbattle.term_manager import TermManager from dungeonbattle.term_manager import TermManager
class Bootstrap: class Bootstrap:
@staticmethod @staticmethod

View File

@ -1,11 +1,15 @@
import curses import curses
from typing import Any, Union from typing import Any, Union
from typing import Any
from dungeonbattle.tests.screen import FakePad from dungeonbattle.tests.screen import FakePad
class Display: class Display:
x: int
y: int
width: int
height: int
def __init__(self, screen: Any, pack: Any): def __init__(self, screen: Any, pack: Any):
self.screen = screen self.screen = screen
self.pack = pack self.pack = pack
@ -13,19 +17,19 @@ class Display:
def newpad(self, height: int, width: int) -> Union[FakePad, Any]: def newpad(self, height: int, width: int) -> Union[FakePad, Any]:
return curses.newpad(height, width) if self.screen else FakePad() return curses.newpad(height, width) if self.screen else FakePad()
def resize(self, y, x, height, width): def resize(self, y: int, x: int, height: int, width: int) -> None:
self.x = x self.x = x
self.y = y self.y = y
self.width = width self.width = width
self.height = height self.height = height
def refresh(self, *args): def refresh(self, *args) -> None:
if len(args) == 4: if len(args) == 4:
self.resize(*args) self.resize(*args)
self.display() self.display()
def display(self): def display(self) -> None:
pass raise NotImplementedError
@property @property
def rows(self) -> int: def rows(self) -> int:

View File

@ -15,11 +15,13 @@ class DisplayManager:
pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK) pack = TexturePack.get_pack(self.game.settings.TEXTURE_PACK)
self.mapdisplay = MapDisplay(screen, pack) self.mapdisplay = MapDisplay(screen, pack)
self.statsdisplay = StatsDisplay(screen, pack) self.statsdisplay = StatsDisplay(screen, pack)
self.mainmenudisplay = MainMenuDisplay(self.game.main_menu, screen, pack) self.mainmenudisplay = MainMenuDisplay(self.game.main_menu,
self.displays = [self.statsdisplay, self.mapdisplay, self.mainmenudisplay] screen, pack)
self.displays = [self.statsdisplay, self.mapdisplay,
self.mainmenudisplay]
self.update_game_components() self.update_game_components()
def update_game_components(self): def update_game_components(self) -> None:
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) self.mapdisplay.update_map(self.game.map)
@ -28,12 +30,11 @@ class DisplayManager:
def refresh(self) -> None: def refresh(self) -> None:
if self.game.state == GameMode.PLAY: if self.game.state == GameMode.PLAY:
self.mapdisplay.refresh(0, 0, self.rows * 4 // 5, self.cols) self.mapdisplay.refresh(0, 0, self.rows * 4 // 5, self.cols)
self.statsdisplay.refresh(self.rows*4//5, 0, self.rows//5, self.cols) self.statsdisplay.refresh(self.rows * 4 // 5, 0,
self.rows // 5, self.cols)
if self.game.state == GameMode.MAINMENU: if self.game.state == GameMode.MAINMENU:
self.mainmenudisplay.refresh(0, 0, self.rows, self.cols) self.mainmenudisplay.refresh(0, 0, self.rows, self.cols)
# self.menudisplay.refresh(self.position)
def ensure_resized(self, *pads) -> bool: def ensure_resized(self, *pads) -> bool:
""" """
If the window got resized, ensure that the pads are also resized. If the window got resized, ensure that the pads are also resized.

View File

@ -1,21 +1,18 @@
#!/usr/bin/env python #!/usr/bin/env python
from typing import Any
import curses
from dungeonbattle.display.texturepack import TexturePack
from dungeonbattle.entities.player import Player from dungeonbattle.entities.player import Player
from dungeonbattle.interfaces import Map from dungeonbattle.interfaces import Map
from .display import Display from .display import Display
class MapDisplay(Display): class MapDisplay(Display):
player: Player player: Player
def __init__(self, *args): def __init__(self, *args):
super().__init__(*args) super().__init__(*args)
def update_map(self, m: Map): def update_map(self, m: Map) -> None:
self.map = m self.map = m
self.pad = curses.newpad(m.height, m.width + 1) self.pad = self.newpad(m.height, m.width + 1)
def update_pad(self) -> None: def update_pad(self) -> None:
self.pad.addstr(0, 0, self.map.draw_string(self.pack)) self.pad.addstr(0, 0, self.map.draw_string(self.pack))

View File

@ -1,7 +1,6 @@
from dungeonbattle.menus import Menu, MainMenu from dungeonbattle.menus import Menu, MainMenu
from typing import Any
from .display import Display from .display import Display
import curses
class MenuDisplay(Display): class MenuDisplay(Display):
position: int position: int
@ -10,7 +9,7 @@ class MenuDisplay(Display):
super().__init__(*args) super().__init__(*args)
self.menubox = self.newpad(self.rows, self.cols) self.menubox = self.newpad(self.rows, self.cols)
def update_menu(self, menu: Menu): def update_menu(self, menu: Menu) -> None:
self.menu = menu self.menu = menu
self.values = [str(a) for a in menu.values] self.values = [str(a) for a in menu.values]
self.trueheight = len(self.values) self.trueheight = len(self.values)
@ -21,11 +20,11 @@ class MenuDisplay(Display):
for i in range(self.trueheight): for i in range(self.trueheight):
self.pad.addstr(i, 0, " " + self.values[i]) self.pad.addstr(i, 0, " " + self.values[i])
def update_pad(self) -> None: def update_pad(self) -> None:
for i in range(self.trueheight): for i in range(self.trueheight):
self.pad.addstr(i, 0, " ") self.pad.addstr(i, 0, " ")
self.pad.addstr(self.menu.position,0,">") #set a marker on the selected line # set a marker on the selected line
self.pad.addstr(self.menu.position, 0, ">")
def display(self) -> None: def display(self) -> None:
if self.height - 2 >= self.menu.position - 1: if self.height - 2 >= self.menu.position - 1:
@ -33,12 +32,12 @@ class MenuDisplay(Display):
elif self.height - 2 >= self.trueheight - self.menu.position: elif self.height - 2 >= self.trueheight - self.menu.position:
cornery = self.trueheight - self.height + 2 cornery = self.trueheight - self.height + 2
# Menu box # Menu box
self.menubox.addstr(0, 0, "" + "" * (self.width - 2) + "") self.menubox.addstr(0, 0, "" + "" * (self.width - 2) + "")
for i in range(1, self.height - 1): for i in range(1, self.height - 1):
self.menubox.addstr(i, 0, "" + " " * (self.width - 2) + "") self.menubox.addstr(i, 0, "" + " " * (self.width - 2) + "")
self.menubox.addstr(self.height-1, 0, ""+""*(self.width-2)+"") self.menubox.addstr(self.height - 1, 0,
"" + "" * (self.width - 2) + "")
self.menubox.refresh(0, 0, self.y, self.x, self.menubox.refresh(0, 0, self.y, self.x,
self.height + self.y, self.height + self.y,
@ -56,6 +55,7 @@ class MenuDisplay(Display):
def preferred_height(self) -> int: def preferred_height(self) -> int:
return self.trueheight + 2 return self.trueheight + 2
class MainMenuDisplay(Display): class MainMenuDisplay(Display):
def __init__(self, menu: MainMenu, *args): def __init__(self, menu: MainMenu, *args):
super().__init__(*args) super().__init__(*args)
@ -70,10 +70,11 @@ class MainMenuDisplay(Display):
def display(self) -> None: def display(self) -> None:
for i in range(len(self.title)): for i in range(len(self.title)):
self.pad.addstr(4+i,self.width//2-len(self.title[0])//2-1,self.title[i]) self.pad.addstr(4 + i, self.width // 2
- len(self.title[0]) // 2 - 1, self.title[i])
self.pad.refresh(0, 0, self.y, self.x, self.height, self.width) self.pad.refresh(0, 0, self.y, self.x, self.height, self.width)
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
self.menudisplay.refresh(menuy, menux, min(self.menudisplay.preferred_height, self.height-menuy), menuwidth) self.menudisplay.refresh(
menuy, menux, min(self.menudisplay.preferred_height,
self.height - menuy), menuwidth)

View File

@ -1,6 +1,4 @@
from typing import Any
from .display import Display from .display import Display
import curses
from dungeonbattle.entities.player import Player from dungeonbattle.entities.player import Player
@ -12,7 +10,7 @@ class StatsDisplay(Display):
super().__init__(*args) super().__init__(*args)
self.pad = self.newpad(self.rows, self.cols) self.pad = self.newpad(self.rows, self.cols)
def update_player(self, p: Player): def update_player(self, p: Player) -> None:
self.player = p self.player = p
def update_pad(self) -> None: def update_pad(self) -> None:

View File

@ -1,7 +1,6 @@
import sys import sys
from typing import Any from typing import Any
from .display.display import Display
from .entities.player import Player from .entities.player import Player
from .interfaces import Map from .interfaces import Map
from .settings import Settings from .settings import Settings