Merge branch 'map_display' into 'master'
Implemented basic map display Closes #1 See merge request ynerant/dungeon-battle!1
This commit is contained in:
commit
6f64646a7a
@ -1,14 +1,16 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
|
||||
class Map:
|
||||
width: int
|
||||
height: int
|
||||
tiles: list
|
||||
|
||||
def __init__(self, width: int, height: int, tiles: list):
|
||||
def __init__(self, width: int, height: int, tiles: list, entities: list):
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.tiles = tiles
|
||||
self.entities = entities
|
||||
|
||||
@staticmethod
|
||||
def load(filename: str):
|
||||
@ -22,31 +24,19 @@ class Map:
|
||||
lines = [line for line in lines if line]
|
||||
height = len(lines)
|
||||
width = len(lines[0])
|
||||
chars = [[Tile.from_char(c, x, y)
|
||||
for x, c in enumerate(line)] for y, line in enumerate(lines)]
|
||||
return Map(width, height, chars)
|
||||
|
||||
def draw_string(self) -> str:
|
||||
return "\n".join("".join(tile.char for tile in line) for line in self.tiles)
|
||||
|
||||
|
||||
class Tile:
|
||||
x: int
|
||||
y: int
|
||||
char: str
|
||||
|
||||
@staticmethod
|
||||
def from_char(c: str, x: int, y: int):
|
||||
t = Tile()
|
||||
t.x = x
|
||||
t.y = y
|
||||
t.char = c
|
||||
return c
|
||||
return Map(width, height, lines, [])
|
||||
|
||||
|
||||
class Entity:
|
||||
tile: Tile
|
||||
y: int
|
||||
x: int
|
||||
img: str
|
||||
|
||||
def __init__(self, y: int, x: int, img: str):
|
||||
self.y = y
|
||||
self.x = x
|
||||
self.img = img
|
||||
|
||||
def move(self, x: int, y: int) -> None:
|
||||
self.tile.x = x
|
||||
self.tile.y = y
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
30
dungeonbattle/mapdisplay.py
Normal file
30
dungeonbattle/mapdisplay.py
Normal file
@ -0,0 +1,30 @@
|
||||
#!/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+1)
|
||||
|
||||
def update_pad(self):
|
||||
for i in range(self.map.height):
|
||||
self.pad.addstr(i, 0, self.map.tiles[i])
|
||||
for e in self.map.entities:
|
||||
self.pad.addch(e.y, e.x, e.img)
|
||||
|
||||
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)
|
Loading…
Reference in New Issue
Block a user