2020-10-16 13:54:33 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import curses
|
|
|
|
from dungeonbattle.interfaces import Map
|
|
|
|
|
|
|
|
class MapDisplay:
|
|
|
|
|
|
|
|
def __init__(self, m: Map):
|
|
|
|
self.map = m
|
2020-10-16 16:14:47 +00:00
|
|
|
self.pad = curses.newpad(m.height, m.width+1)
|
2020-10-16 13:54:33 +00:00
|
|
|
|
|
|
|
def update_pad(self):
|
|
|
|
for i in range(self.map.height):
|
2020-10-16 16:14:47 +00:00
|
|
|
self.pad.addstr(i, 0, self.map.tiles[i])
|
2020-10-16 13:54:33 +00:00
|
|
|
for e in self.map.entities:
|
|
|
|
self.pad.addch(e.y, e.x, e.img)
|
|
|
|
|
|
|
|
def display(self, y, x):
|
|
|
|
deltay, deltax = (curses.LINES // 2) + 1, (curses.COLS //2) + 1
|
|
|
|
pminrow, pmincol = y-deltay, x-deltax
|
|
|
|
sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0)
|
2020-10-16 16:14:47 +00:00
|
|
|
deltay, deltax = curses.LINES - deltay, curses.COLS - deltax
|
|
|
|
smaxrow = self.map.height - (y + deltay) + curses.LINES -1
|
2020-10-16 13:54:33 +00:00
|
|
|
smaxrow = min(smaxrow, curses.LINES-1)
|
2020-10-16 16:14:47 +00:00
|
|
|
smaxcol = self.map.width - (x + deltax) + curses.COLS -1
|
2020-10-16 13:54:33 +00:00
|
|
|
smaxcol = min(smaxcol, curses.COLS-1)
|
|
|
|
pminrow = max(0, min(self.map.height, pminrow))
|
|
|
|
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)
|