Now About Social Code
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--scripts/bsp_level_generator.gd32
1 files changed, 18 insertions, 14 deletions
diff --git a/scripts/bsp_level_generator.gd b/scripts/bsp_level_generator.gd
index 7d9e268..5d70569 100644
--- a/scripts/bsp_level_generator.gd
+++ b/scripts/bsp_level_generator.gd
@@ -11,6 +11,9 @@ extends Node3D
var min_room_size: int = min_dim * min_dim
+var grid_width = width + 1
+var grid_height = height + 1
+
enum Direction {LEFT, RIGHT}
enum Tile {FLOOR, WALL}
@@ -61,9 +64,9 @@ func is_leaf(node: BSPNode) -> bool:
func generate_grid(map: BSPNode, grid: Array[Tile]):
if is_leaf(map):
- for y in range(map.min_dims.y + 1, map.max_dims.y - 1):
- for x in range(map.min_dims.x + 1, map.max_dims.x - 1):
- grid[y * width + x] = Tile.FLOOR
+ for y in range(map.min_dims.y, map.max_dims.y - 1):
+ for x in range(map.min_dims.x, map.max_dims.x - 1):
+ grid[(y+1) * grid_width + (x+1)] = Tile.FLOOR
else:
generate_grid(map.left, grid)
generate_grid(map.right, grid)
@@ -78,34 +81,35 @@ func generate_grid(map: BSPNode, grid: Array[Tile]):
var test_door = randi_range(map.min_dims[other_axis], map.max_dims[other_axis] - 1)
var door_pos = Vector2i.ZERO
door_pos[map.axis] = split_axis
- door_pos[other_axis] = test_door
+ door_pos[other_axis] = test_door + 1
var room_left = door_pos
- room_left[map.axis] -= 2
+ room_left[map.axis] -= 1
var room_right = door_pos
room_right[map.axis] += 1
# Check if there are two spaces to connect
# And ensure no door already exists in the space
- if grid[room_left.y * width + room_left.x] == Tile.FLOOR \
- and grid[room_right.y * width + room_right.x] == Tile.FLOOR \
- and grid[door_pos.y * width + door_pos.x] == Tile.WALL:
+ if grid[room_left.y * grid_width + room_left.x] == Tile.FLOOR \
+ and grid[room_right.y * grid_width + room_right.x] == Tile.FLOOR \
+ and grid[door_pos.y * grid_width + door_pos.x] == Tile.WALL:
have_door = true
- grid[door_pos.y * width + door_pos.x] = Tile.FLOOR
+ grid[door_pos.y * grid_width + door_pos.x] = Tile.FLOOR
door_pos[map.axis] -= 1
- grid[door_pos.y * width + door_pos.x] = Tile.FLOOR
+ grid[door_pos.y * grid_width + door_pos.x] = Tile.FLOOR
tries += 1
if tries > 1000:
print("Took too many attempts to generate a door")
get_tree().quit()
+ return
func generate_geo(grid: Array[Tile]):
var csg_root = CSGCombiner3D.new()
- for y in range(height):
- for x in range(width):
- var tile = grid[y * width + x]
+ for y in range(grid_height):
+ for x in range(grid_width):
+ var tile = grid[y * grid_width + x]
if tile == Tile.FLOOR:
var box = CSGBox3D.new()
@@ -127,7 +131,7 @@ func _ready() -> void:
var max_space = Vector2i(width, height)
var map = generate_level(starting_axis, min_space, max_space)
var grid: Array[Tile] = []
- grid.resize(width * height)
+ grid.resize(grid_width * grid_height)
grid.fill(Tile.WALL)
generate_grid(map, grid)
generate_geo(grid)