squirrel-battle/dungeonbattle/display/mapdisplay.py

43 lines
1.6 KiB
Python
Raw Normal View History

2020-11-06 14:34:24 +00:00
#!/usr/bin/env python
import curses
2020-11-08 22:03:59 +00:00
from typing import Any
2020-11-06 19:24:19 +00:00
2020-11-08 22:03:59 +00:00
from dungeonbattle.display.display import Display
2020-11-06 19:24:19 +00:00
from dungeonbattle.display.texturepack import TexturePack
2020-11-08 22:03:59 +00:00
from dungeonbattle.entities.player import Player
2020-11-06 14:34:24 +00:00
from dungeonbattle.interfaces import Map
2020-11-06 16:43:30 +00:00
2020-11-06 14:34:24 +00:00
2020-11-08 22:03:59 +00:00
class MapDisplay(Display):
def __init__(self, screen: Any, m: Map, player: Player, pack: TexturePack):
super().__init__(screen)
self.height = self.rows
self.width = self.cols
2020-11-06 16:43:30 +00:00
self.pack = pack
2020-11-06 19:24:19 +00:00
self.map = m
2020-11-08 22:03:59 +00:00
self.player = player
2020-11-08 22:40:03 +00:00
self.pad = self.newpad(m.height, m.width + 1)
2020-11-06 14:34:24 +00:00
2020-11-06 20:15:09 +00:00
def update_pad(self) -> None:
2020-11-06 16:43:30 +00:00
self.pad.addstr(0, 0, self.map.draw_string(self.pack))
2020-11-06 14:34:24 +00:00
for e in self.map.entities:
2020-11-06 16:43:30 +00:00
self.pad.addstr(e.y, e.x, self.pack.PLAYER)
2020-11-06 14:34:24 +00:00
2020-11-06 20:15:09 +00:00
def display(self, y: int, x: int) -> None:
deltay, deltax = (self.height // 2) + 1, (self.width // 2) + 1
pminrow, pmincol = y - deltay, x - deltax
2020-11-06 14:34:24 +00:00
sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0)
deltay, deltax = self.height - deltay, self.width - deltax
2020-11-06 20:15:09 +00:00
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))
2020-11-06 14:34:24 +00:00
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-08 22:03:59 +00:00
def refresh(self) -> None:
return self.display(self.player.y, self.player.x)