35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
#!/usr/bin/env python
|
|
import curses
|
|
|
|
from dungeonbattle.entities.player import Player
|
|
from dungeonbattle.interfaces import Map
|
|
|
|
|
|
class MapDisplay:
|
|
def __init__(self, m: Map, player: Player, init_pad: bool = True):
|
|
self.map = m
|
|
self.player = player
|
|
if init_pad:
|
|
self.pad = curses.newpad(m.height, m.width + 1)
|
|
|
|
def update_pad(self) -> None:
|
|
self.pad.addstr(0, 0, self.map.draw_string())
|
|
# TODO Not all entities should be a player
|
|
for e in self.map.entities:
|
|
self.pad.addstr(e.y, e.x, '🐿')
|
|
|
|
def display(self, y: int, x: int) -> None:
|
|
deltay, deltax = (curses.LINES // 2) + 1, (curses.COLS // 2) + 1
|
|
pminrow, pmincol = y - deltay, x - deltax
|
|
sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0)
|
|
deltay, deltax = curses.LINES - deltay, curses.COLS - deltax
|
|
smaxrow = self.map.height - (y + deltay) + curses.LINES - 1
|
|
smaxrow = min(smaxrow, curses.LINES - 1)
|
|
smaxcol = self.map.width - (x + deltax) + curses.COLS - 1
|
|
smaxcol = min(smaxcol, curses.COLS - 1)
|
|
pminrow = max(0, min(self.map.height, pminrow))
|
|
pmincol = max(0, min(self.map.width, pmincol))
|
|
self.pad.clear()
|
|
self.update_pad()
|
|
self.pad.refresh(pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol)
|