From 5728abc02c3b0b8533c95a1af8890d73b52031f2 Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Sun, 11 Oct 2020 15:24:51 +0200 Subject: [PATCH 1/8] 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/8] 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/8] 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) From 2e82849395833d950f1c62e8a50397c02c361ed6 Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Sun, 11 Oct 2020 15:24:51 +0200 Subject: [PATCH 4/8] 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 838cbec..2cef098 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -22,31 +22,22 @@ 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) + return Map(width, height, lines) 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 - - 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 42c9957cebaceabb4fc8d9a86613e56d2b00634a Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 16 Oct 2020 15:52:47 +0200 Subject: [PATCH 5/8] 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 2cef098..a28fe1a 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, []) def draw_string(self) -> str: return "\n".join("".join(tile.char for tile in line) for line in self.tiles) From 7d13a782358f1b59406d08aafab5bfd15da8a8ca Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 16 Oct 2020 15:54:33 +0200 Subject: [PATCH 6/8] 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) From 722ff07952e8e70efe71e2a8ab76ff97e8babd44 Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 16 Oct 2020 16:00:57 +0200 Subject: [PATCH 7/8] Fixed method display from mapdisplay.MapDisplay --- dungeonbattle/mapdisplay.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dungeonbattle/mapdisplay.py b/dungeonbattle/mapdisplay.py index 7ae7cc7..230ab1c 100644 --- a/dungeonbattle/mapdisplay.py +++ b/dungeonbattle/mapdisplay.py @@ -22,7 +22,7 @@ class MapDisplay: 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 = self.map.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)) From 14f93fd596cddc51d8e1775a879457c833617b24 Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 16 Oct 2020 18:14:47 +0200 Subject: [PATCH 8/8] Corrected equations in mapdisplay.MapDisplay.display. Closes #1 --- dungeonbattle/mapdisplay.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dungeonbattle/mapdisplay.py b/dungeonbattle/mapdisplay.py index 230ab1c..f482b37 100644 --- a/dungeonbattle/mapdisplay.py +++ b/dungeonbattle/mapdisplay.py @@ -6,25 +6,25 @@ class MapDisplay: def __init__(self, m: Map): self.map = m - self.pad = curses.newpad(m.height, m.width) + 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][:-1]) + 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): - 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 + 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 + 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)