From 8ab65eb96a622494a12d6577ca20f773b78272c9 Mon Sep 17 00:00:00 2001 From: kleph Date: Wed, 19 Jul 2017 03:46:17 +0200 Subject: [PATCH] 03 - first try of hallways - still buggy --- 03_map.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/03_map.py b/03_map.py index cd5d6b8..cd9a2d3 100644 --- a/03_map.py +++ b/03_map.py @@ -92,11 +92,15 @@ class Room: for x in range(0, self.width): tilemap[self.x + x][self.y + self.height - 1] = Level.TILE_WALL + def center(self): + return round(self.x + self.width/2), round(self.y + self.height/2) + class Level: TILE_WALL = 0 TILE_ROAD = 1 TILE_GROUND = 2 + TILE_HALLWAY = 3 def __init__(self, sizex, sizey, tile_file=None): self.sizex, self.sizey = sizex, sizey @@ -164,11 +168,13 @@ class Level: self.tree = self.generate_tree() # initialize tilemap with road/ground - self.tilemap = [[self.TILE_ROAD for y in range(0, self.sizey)] for x in range(0, self.sizex)] + self.tilemap = [[self.TILE_GROUND for y in range(0, self.sizey)] for x in range(0, self.sizex)] # create rooms from partitions self.rooms = self.generate_rooms(self.tree[0], self.tilemap, []) + self.generate_hallways() + def draw_map(self): for y in range(0, self.sizey): for x in range(0, self.sizex): @@ -180,6 +186,46 @@ class Level: print(self.tilemap[x][y], end="") print("") + def generate_hallways(self): + """ """ + if len(self.rooms) < 2: + # out of luck ? + return + + room_a = self.rooms[0] + for r in self.rooms[1:]: + room_b = r + self.generate_hallway(room_a, room_b) + room_a = room_b + + def generate_hallway(self, room_a, room_b): + """ """ + print("hallway between room" + str(room_a.id) + " and room " + str(room_b.id)) + (xa, ya) = room_a.center() + (xb, yb) = room_b.center() + + # horizontal part + if xa < xb: + start_x = xa + end_x = xb + else: + start_x = xb + end_x = xa + print("h_hw form " + str(start_x) + " and " + str(end_x)) + for x in range(start_x, end_x): + self.tilemap[x][ya] = self.TILE_ROAD + + # vertical part + if ya < yb: + start_y = ya + end_y = yb + else: + start_y = yb + end_y = ya + print("v_hw form " + str(start_y) + " and " + str(end_y)) + for y in range(start_y, end_y): + self.tilemap[end_x][y] = self.TILE_ROAD + ## # main