From 14cfb15d17000e5e13f848155e03bd74c79de309 Mon Sep 17 00:00:00 2001 From: Stan_Lewry Date: Sat, 17 Jul 2021 17:00:14 +0100 Subject: [PATCH] world gen first implementation --- Debug.py | 3 +- InputHandler.py | 6 ++- Main.py | 18 ++++---- Renderer.py | 15 +++---- World.py | 4 +- WorldGen.py | 109 +++++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 136 insertions(+), 19 deletions(-) diff --git a/Debug.py b/Debug.py index 30f6e4a..e2883a4 100644 --- a/Debug.py +++ b/Debug.py @@ -1 +1,2 @@ -debugRects = [] \ No newline at end of file +debugRects = [] +worldGenDebugRects = [] \ No newline at end of file diff --git a/InputHandler.py b/InputHandler.py index 7526bb2..9a4340a 100644 --- a/InputHandler.py +++ b/InputHandler.py @@ -6,10 +6,11 @@ from dataclasses import dataclass class InputState: xy: Tuple = (0, 0) quit: bool = False - zoomLevel: float = 0.5 + zoomLevel: float = 0.125 showDebug: bool = False debugLevel: int = 0 inputMade: bool = False + debugRegenWorld: bool = False maxZoom = 3.0 minZoom = 1.0 @@ -21,6 +22,7 @@ def handleInputs(): global globalInputState newInputState = globalInputState newInputState.xy = (0, 0) + newInputState.debugRegenWorld = False for event in pygame.event.get(): if event.type == pygame.QUIT: newInputState.exit = True @@ -43,6 +45,8 @@ def handleInputs(): if newInputState.debugLevel < maxDebugLevel: newInputState.debugLevel += 1 else: newInputState.debugLevel = 0 + elif event.key == pygame.K_F4: + newInputState.debugRegenWorld = True newInputState.inputMade = not newInputState == globalInputState # now replace the global one with our new one diff --git a/Main.py b/Main.py index a664620..90d02f2 100644 --- a/Main.py +++ b/Main.py @@ -7,15 +7,16 @@ import InputHandler import Renderer import Algorithm import Debug +import random def main(): + random.seed(None) renderer = Renderer.Renderer(1920, 1080) - World.world = WorldGen.generateWorld() - - player = Player.Player(2, 2) + World.world = WorldGen.generateWorldGrowingRectangles() + player = Player.Player(World.worldWidth / 2, World.worldHeight / 2) # begin game loop getTicksLastFrame = 0 @@ -33,9 +34,8 @@ def main(): player.update() - Algorithm.computeVisibility(player) - - + #Algorithm.computeVisibility(player) + cam = (player.worldX, player.worldY) renderer.renderWorld(cam, InputHandler.globalInputState.zoomLevel, player) @@ -43,7 +43,11 @@ def main(): renderer.renderDebugUI(player, deltaTime) if InputHandler.globalInputState.debugLevel > 1: renderer.renderDebugRects(cam, InputHandler.globalInputState.zoomLevel) - + + if InputHandler.globalInputState.debugRegenWorld: + Debug.worldGenDebugRects.clear() + World.world = WorldGen.generateWorldGrowingRectangles() + pygame.display.flip() pygame.quit() diff --git a/Renderer.py b/Renderer.py index 68e2444..87ccbf3 100644 --- a/Renderer.py +++ b/Renderer.py @@ -69,11 +69,12 @@ class Renderer: screenTopX = (cam[0] * actualWidth) - (self.screenWidth / 2) screenTopY = (cam[1] * actualHeight) - (self.screenHeight / 2) - for r in Debug.debugRects: - worldX = (r[0] * actualWidth) - (actualWidth / 2) - worldY = (r[1] * actualHeight) - (actualHeight / 2) + for seq in (Debug.debugRects, Debug.worldGenDebugRects): + for r in seq: + worldX = (r[0] * actualWidth) - (actualWidth / 2) + worldY = (r[1] * actualHeight) - (actualHeight / 2) - s = pygame.Surface((actualWidth, actualHeight)) - s.set_alpha(128) - s.fill(r[2]) - self.screen.blit(s, (worldX - screenTopX, worldY - screenTopY)) \ No newline at end of file + s = pygame.Surface((actualWidth, actualHeight)) + s.set_alpha(128) + s.fill(r[2]) + self.screen.blit(s, (worldX - screenTopX, worldY - screenTopY)) \ No newline at end of file diff --git a/World.py b/World.py index 14d3f38..39e45da 100644 --- a/World.py +++ b/World.py @@ -1,4 +1,4 @@ -worldWidth = 10 -worldHeight = 10 +worldWidth = 40 +worldHeight = 40 world = [[]] \ No newline at end of file diff --git a/WorldGen.py b/WorldGen.py index f40cb9f..7615d88 100644 --- a/WorldGen.py +++ b/WorldGen.py @@ -1,6 +1,8 @@ +import random import WorldTemplates import World import GameObject +import Debug def generateWorld(): template = WorldTemplates.worldTemplate6 @@ -17,4 +19,109 @@ def generateWorld(): cell.objects.append(GameObject.GameObject(x, y, "floor_tile", False)) row.append(cell) output.insert(y, row) - return output \ No newline at end of file + return output + +def generateWorldGrowingRectangles(): + outputWorld = [[]] + # generate default + # TODO do this with a comprehension or something nice :) + for y in range(World.worldHeight): + row = [] + for x in range(World.worldWidth): + cell = GameObject.WorldCell() + cell.objects.append(GameObject.GameObject(x, y, "wall_side", False)) + row.append(cell) + outputWorld.insert(y, row) + + + + class Room: + def __init__(self, point): + self.cells = [[]] + self.maxX = self.minX = point[0] + self.maxY = self.minY = point[1] + self.cells[0].append(point) + + def growNorth(self): + #newRow = [(tup[0], tup[1] - 1) for _, tup in enumerate(self.cells[0])] + # I want to create a new row, that is equal to the first row, but the y coord of each cell is -1 + newRow = [] + for tup in self.cells[0]: + newRow.append((tup[0], tup[1] - 1)) + self.cells.insert(0, newRow) + self.minY -= 1 + + def growEast(self): + for row in self.cells: + row.append((row[-1][0] + 1, row[-1][1])) + self.maxX += 1 + + def growWest(self): + for row in self.cells: + row.insert(0, (row[0][0] - 1, row[0][1])) + self.minX -= 1 + + def growSouth(self): + #newRow = [(tup[0], tup[1] + 1) for _, tup in enumerate(self.cells[-1])] + newRow = [] + for tup in self.cells[-1]: + newRow.append((tup[0], tup[1] + 1)) + self.cells.append(newRow) + self.maxY += 1 + + # choose starting points + # TODO make this controllable + points = random.randint(10, 12) + #points = 1 + + rooms = [] + for _ in range(points): + #maybe limit how close these can be? + y = random.randint(1, World.worldHeight - 2) + x = random.randint(1, World.worldWidth - 2) + #cell = GameObject.WorldCell() + #cell.objects.append(GameObject.GameObject(x, y, "floor_tile", False)) + #outputWorld[y][x] = cell + rooms.append(Room((x, y))) + Debug.worldGenDebugRects.append((x, y, (0, 255, 0))) + + # Generate rooms + # TODO experiment with this + # TODO make controllable + iterations = 12 + for _ in range(iterations): + for r in rooms: + direction = random.randint(0, 3) + # OPTIONAL EXTRA: CHECK IT WONT INTERESCT WITH OTHER ONE + if direction == 0: + if r.minY > 1: + r.growNorth() + elif direction == 1: + if r.maxX < World.worldWidth - 2: + r.growEast() + elif direction == 2: + if r.maxY < World.worldHeight - 2: + r.growSouth() + elif direction == 3: + if r.minX > 1: + r.growWest() + + # paint generated rooms to output + for r in rooms: + #print ("\n====================================\n") + #print(f'minX {r.minX} maxX {r.maxX} minY {r.minY} maxY {r.maxY}') + for y in range(len(r.cells)): + #print (r.cells[y]) + for _, x in enumerate(r.cells[y]): + cell = GameObject.WorldCell() + cell.objects.append(GameObject.GameObject(x[0], x[1], "floor_tile", False)) + outputWorld[x[1]][x[0]] = cell + Debug.worldGenDebugRects.append((x[0], x[1], (0, 255, 255))) + + return outputWorld + +# So to grow north we need to prepend a new list of the same "width" +# the coords are the first lists coords -1 +# to grow east we need to append 1 to each list +# to grow south we need to APPEND a new list of same width +# to grow west we PREPEND one to each -- 2.20.1