2020-10-16 13:54:33 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import curses
|
2020-11-06 14:33:26 +00:00
|
|
|
|
2020-11-06 16:48:47 +00:00
|
|
|
from dungeonbattle.entities.player import Player
|
2020-10-16 13:54:33 +00:00
|
|
|
from dungeonbattle.interfaces import Map
|
|
|
|
|
|
|
|
|
2020-11-06 14:33:26 +00:00
|
|
|
class MapDisplay:
|
2020-11-06 18:12:56 +00:00
|
|
|
def __init__(self, m: Map, player: Player, init_pad: bool = True):
|
2020-10-16 13:54:33 +00:00
|
|
|
self.map = m
|
2020-10-23 13:14:19 +00:00
|
|
|
self.player = player
|
2020-11-06 18:12:56 +00:00
|
|
|
if init_pad:
|
|
|
|
self.pad = curses.newpad(m.height, m.width + 1)
|
2020-10-16 13:54:33 +00:00
|
|
|
|
2020-11-06 14:33:26 +00:00
|
|
|
def update_pad(self) -> None:
|
2020-10-23 13:00:59 +00:00
|
|
|
self.pad.addstr(0, 0, self.map.draw_string())
|
2020-11-06 16:48:47 +00:00
|
|
|
# TODO Not all entities should be a player
|
2020-10-16 13:54:33 +00:00
|
|
|
for e in self.map.entities:
|
2020-11-06 16:48:47 +00:00
|
|
|
self.pad.addstr(e.y, e.x, '🐿')
|
2020-10-16 13:54:33 +00:00
|
|
|
|
2020-11-06 14:33:26 +00:00
|
|
|
def display(self, y: int, x: int) -> None:
|
|
|
|
deltay, deltax = (curses.LINES // 2) + 1, (curses.COLS // 2) + 1
|
|
|
|
pminrow, pmincol = y - deltay, x - deltax
|
2020-10-16 13:54:33 +00:00
|
|
|
sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0)
|
2020-10-16 16:14:47 +00:00
|
|
|
deltay, deltax = curses.LINES - deltay, curses.COLS - deltax
|
2020-11-06 14:33:26 +00:00
|
|
|
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))
|
2020-10-16 13:54:33 +00:00
|
|
|
pmincol = max(0, min(self.map.width, pmincol))
|
2020-10-16 16:14:47 +00:00
|
|
|
self.pad.clear()
|
2020-10-16 13:54:33 +00:00
|
|
|
self.update_pad()
|
|
|
|
self.pad.refresh(pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol)
|