squirrel-battle/dungeonbattle/mapdisplay.py

31 lines
1.1 KiB
Python

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