From 5728abc02c3b0b8533c95a1af8890d73b52031f2 Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Sun, 11 Oct 2020 15:24:51 +0200 Subject: [PATCH 1/3] Sanitizing data structure --- dungeonbattle/interfaces.py | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 1674427..4a2339c 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -22,28 +22,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) - - -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 From ba4bb78166598a63f9c7fd6703b551a2203f8b8e Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 16 Oct 2020 15:52:47 +0200 Subject: [PATCH 2/3] Added entities management to class Map --- dungeonbattle/interfaces.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 4a2339c..7824134 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -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,7 +24,7 @@ class Map: lines = [line for line in lines if line] height = len(lines) width = len(lines[0]) - return Map(width, height, lines) + return Map(width, height, lines, []) class Entity: From 1b635502c48ad22bccd0756bd5e853062f17d816 Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 16 Oct 2020 15:54:33 +0200 Subject: [PATCH 3/3] New class MapDisplay implements displaying maps to terminal. See #1 --- dungeonbattle/mapdisplay.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 dungeonbattle/mapdisplay.py diff --git a/dungeonbattle/mapdisplay.py b/dungeonbattle/mapdisplay.py new file mode 100644 index 0000000..7ae7cc7 --- /dev/null +++ b/dungeonbattle/mapdisplay.py @@ -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) + + 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)