squirrel-battle/dungeonbattle/mapdisplay.py

32 lines
1.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python
import curses
from dungeonbattle.interfaces import Map
class MapDisplay:
2020-10-23 13:14:19 +00:00
def __init__(self, m: Map, player):
self.map = m
self.pad = curses.newpad(m.height, m.width+1)
2020-10-23 13:14:19 +00:00
self.player = player
def update_pad(self):
2020-10-23 13:00:59 +00:00
self.pad.addstr(0, 0, self.map.draw_string())
for e in self.map.entities:
self.pad.addch(e.y, e.x, e.img)
2020-10-23 13:14:19 +00:00
self.pad.addstr(self.player.getPosY(), self.player.getPosX(), '🐿')
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)
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)