squirrel-battle/dungeonbattle/display/mapdisplay.py

42 lines
1.6 KiB
Python
Raw Normal View History

2020-11-06 14:34:24 +00:00
#!/usr/bin/env python
import curses
from dungeonbattle.interfaces import Map
2020-11-06 15:43:07 +00:00
from dungeonbattle.settings import Settings
2020-11-06 16:03:06 +00:00
import dungeonbattle.texturepack as tp
2020-11-06 14:34:24 +00:00
class MapDisplay:
2020-11-06 15:43:07 +00:00
def __init__(self, m: Map, settings : Settings, height: int, width: int):
2020-11-06 14:34:24 +00:00
self.width = width
self.height = height
self.map = m
self.pad = curses.newpad(m.height, m.width+1)
2020-11-06 16:09:39 +00:00
if self.settings.TEXTURE_PACK == 'ASCII' :
2020-11-06 16:03:06 +00:00
self.textures = tp.ascii_textures
2020-11-06 16:09:39 +00:00
if self.settings.TEXTURE_PACK == 'SQUIRREL' :
2020-11-06 16:03:06 +00:00
self.textures = tp.squirrel_textures
2020-11-06 14:34:24 +00:00
def update_pad(self):
self.pad.addstr(0, 0, self.map.draw_string())
for e in self.map.entities:
2020-11-06 16:03:06 +00:00
self.pad.addstr(e.y, e.x, self.textures[e.name])
2020-11-06 14:34:24 +00:00
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)
2020-11-06 15:43:07 +00:00
def refresh(self) :
self.display(self.map.currenty,self.map.currentx)