32 lines
1.2 KiB
Python
32 lines
1.2 KiB
Python
|
#!/usr/bin/env python
|
||
|
import curses
|
||
|
from dungeonbattle.interfaces import Map
|
||
|
|
||
|
class MapDisplay:
|
||
|
|
||
|
def __init__(self, m: Map, height: int, width: int):
|
||
|
self.width = width
|
||
|
self.height = height
|
||
|
self.map = m
|
||
|
self.pad = curses.newpad(m.height, m.width+1)
|
||
|
|
||
|
def update_pad(self):
|
||
|
self.pad.addstr(0, 0, self.map.draw_string())
|
||
|
for e in self.map.entities:
|
||
|
self.pad.addch(e.y, e.x, e.img)
|
||
|
|
||
|
def display(self, y, x):
|
||
|
deltay, deltax = (self.height // 2) + 1, (self.width //2) + 1
|
||
|
pminrow, pmincol = y-deltay, x-deltax
|
||
|
sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0)
|
||
|
deltay, deltax = self.height - deltay, self.width - deltax
|
||
|
smaxrow = self.map.height - (y + deltay) + self.height -1
|
||
|
smaxrow = min(smaxrow, self.height-1)
|
||
|
smaxcol = self.map.width - (x + deltax) + self.width -1
|
||
|
smaxcol = min(smaxcol, self.width-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)
|