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
@ -11,4 +12,4 @@ class Bootstrap:
game.new_game() game.new_game()
display = DisplayManager(term_manager.screen, game) display = DisplayManager(term_manager.screen, game)
game.display_refresh = display.refresh game.display_refresh = display.refresh
game.run(term_manager.screen) game.run(term_manager.screen)

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

@ -8,18 +8,20 @@ from dungeonbattle.game import Game, GameMode
class DisplayManager: class DisplayManager:
def __init__(self, screen: Any, g: Game): def __init__(self, screen: Any, g: Game):
self.game = g self.game = g
self.screen = screen self.screen = screen
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,11 +30,10 @@ 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:
""" """
@ -52,4 +53,4 @@ class DisplayManager:
@property @property
def cols(self) -> int: def cols(self) -> int:
return curses.COLS if self.screen else 42 return curses.COLS if self.screen else 42

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))
@ -36,4 +33,4 @@ class MapDisplay(Display):
pmincol = max(0, min(self.map.width, pmincol)) pmincol = max(0, min(self.map.width, pmincol))
self.pad.clear() self.pad.clear()
self.update_pad() self.update_pad()
self.pad.refresh(pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol) self.pad.refresh(pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol)

View File

@ -1,63 +1,63 @@
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
def __init__(self, *args) :
super().__init__(*args)
self.menubox = self.newpad(self.rows,self.cols)
def update_menu(self, menu: Menu): def __init__(self, *args):
super().__init__(*args)
self.menubox = self.newpad(self.rows, self.cols)
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)
self.truewidth = max([len(a) for a in self.values]) self.truewidth = max([len(a) for a in self.values])
#Menu values are printed in pad # Menu values are printed in pad
self.pad = self.newpad(self.trueheight,self.truewidth+2) self.pad = self.newpad(self.trueheight, self.truewidth + 2)
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:
cornery = 0 cornery = 0
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
self.menubox.addstr(0, 0, "" + "" * (self.width - 2) + "")
for i in range(1, self.height - 1):
self.menubox.addstr(i, 0, "" + " " * (self.width - 2) + "")
self.menubox.addstr(self.height - 1, 0,
"" + "" * (self.width - 2) + "")
#Menu box
self.menubox.addstr(0,0,""+""*(self.width-2)+"")
for i in range(1,self.height-1) :
self.menubox.addstr(i,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,
self.width + self.x) self.width + self.x)
self.update_pad() self.update_pad()
self.pad.refresh(cornery, 0, self.y+1, self.x+2, self.pad.refresh(cornery, 0, self.y + 1, self.x + 2,
self.height-1 + self.y, self.height - 1 + self.y,
self.width-1 + self.x) self.width - 1 + self.x)
@property @property
def preferred_width(self) -> int: def preferred_width(self) -> int:
return self.truewidth + 6 return self.truewidth + 6
@property @property
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)
self.menu = menu self.menu = menu
self.pad = self.newpad(self.rows, self.cols) self.pad = self.newpad(self.rows, self.cols)
@ -67,13 +67,14 @@ class MainMenuDisplay(Display):
self.menudisplay = MenuDisplay(self.screen, self.pack) self.menudisplay = MenuDisplay(self.screen, self.pack)
self.menudisplay.update_menu(self.menu) self.menudisplay.update_menu(self.menu)
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
self.pad.refresh(0,0,self.y,self.x,self.height,self.width) - len(self.title[0]) // 2 - 1, self.title[i])
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,18 +1,16 @@
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
class StatsDisplay(Display): class StatsDisplay(Display):
player: Player player: Player
def __init__(self, *args): def __init__(self, *args):
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