diff options
Diffstat (limited to 'scripts/bsp_level_generator.gd')
-rw-r--r-- | scripts/bsp_level_generator.gd | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/scripts/bsp_level_generator.gd b/scripts/bsp_level_generator.gd index ac095ef..6985469 100644 --- a/scripts/bsp_level_generator.gd +++ b/scripts/bsp_level_generator.gd @@ -10,15 +10,15 @@ extends Node3D @export var wall_thickness: float = 1.3 @onready var mesh: MeshInstance3D = $MeshInstance3D +enum Direction {LEFT, RIGHT} + +signal grid_generated(grid: Array[Tile], p_grid_width: int, p_grid_height: int) 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} - class BSPNode: var axis: int var min_dims: Vector2i @@ -64,11 +64,11 @@ func generate_level(axis: int, min_space: Vector2i, max_space: Vector2i, depth: func is_leaf(node: BSPNode) -> bool: return node.left == null and node.right == null -func generate_grid(map: BSPNode, grid: Array[Tile]): +func generate_grid(map: BSPNode, grid: Array[Tile.Tile]) -> void: if is_leaf(map): 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 + grid[(y+1) * grid_width + (x+1)] = Tile.Tile.FLOOR else: generate_grid(map.left, grid) generate_grid(map.right, grid) @@ -93,15 +93,15 @@ func generate_grid(map: BSPNode, grid: Array[Tile]): # Check if there are two spaces to connect # And ensure no door already exists in the space - 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: + if grid[room_left.y * grid_width + room_left.x] == Tile.Tile.FLOOR \ + and grid[room_right.y * grid_width + room_right.x] == Tile.Tile.FLOOR \ + and grid[door_pos.y * grid_width + door_pos.x] == Tile.Tile.WALL: have_door = true # Place grid for mesh - grid[door_pos.y * grid_width + door_pos.x] = Tile.FLOOR + grid[door_pos.y * grid_width + door_pos.x] = Tile.Tile.FLOOR door_pos[map.axis] -= 1 - grid[door_pos.y * grid_width + door_pos.x] = Tile.FLOOR + grid[door_pos.y * grid_width + door_pos.x] = Tile.Tile.FLOOR tries += 1 if tries > 1000: @@ -109,7 +109,7 @@ func generate_grid(map: BSPNode, grid: Array[Tile]): get_tree().quit() return -func generate_plane(array: Array, pos: Vector3, dim: Array[Vector3], normal: Vector3): +func generate_plane(array: Array, pos: Vector3, dim: Array[Vector3], normal: Vector3) -> void: var index = len(array[Mesh.ARRAY_VERTEX]) array[Mesh.ARRAY_VERTEX].append(wall_thickness*pos) array[Mesh.ARRAY_VERTEX].append(wall_thickness*(pos+dim[0])) @@ -128,14 +128,14 @@ func generate_plane(array: Array, pos: Vector3, dim: Array[Vector3], normal: Vec array[Mesh.ARRAY_INDEX].append(index + 2) array[Mesh.ARRAY_INDEX].append(index + 3) -func generate_geo(grid: Array[Tile], array: Array): +func generate_geo(grid: Array[Tile.Tile], array: Array) -> void: for y in range(grid_height): for x in range(grid_width): var tile = grid[y * grid_width + x] - if tile == Tile.FLOOR: + if tile == Tile.Tile.FLOOR: generate_plane(array, Vector3(x, 0, y), [Vector3(1, 0, 0), Vector3(0, 0, 1)], Vector3.UP) - elif tile == Tile.WALL: + elif tile == Tile.Tile.WALL: generate_plane(array, Vector3(x, 2, y), [Vector3(0, 0, 1), Vector3(0, -2, 0)], Vector3.RIGHT) generate_plane(array, Vector3(x, 2, y+1), [Vector3(1, 0, 0), Vector3(0, -2, 0)], Vector3.BACK) generate_plane(array, Vector3(x+1, 2, y+1), [Vector3(0, 0, -1), Vector3(0, -2, 0)], Vector3.LEFT) @@ -147,9 +147,9 @@ func _ready() -> void: var min_space = Vector2i(0, 0) var max_space = Vector2i(width, height) var map = generate_level(starting_axis, min_space, max_space) - var grid: Array[Tile] = [] + var grid: Array[Tile.Tile] = [] grid.resize(grid_width * grid_height) - grid.fill(Tile.WALL) + grid.fill(Tile.Tile.WALL) var surface_array = [] surface_array.resize(Mesh.ARRAY_MAX) @@ -159,11 +159,10 @@ func _ready() -> void: surface_array[Mesh.ARRAY_NORMAL] = PackedVector3Array() generate_grid(map, grid) + grid_generated.emit(grid, grid_width, grid_height) generate_geo(grid, surface_array) mesh.mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, surface_array) var tri_mesh = mesh.mesh.create_trimesh_shape() $NavigationRegion3D/StaticBody3D/CollisionShape3D.shape = tri_mesh $NavigationRegion3D.bake_navigation_mesh() - - #ResourceSaver.save(mesh.mesh, "res://map.tres", ResourceSaver.FLAG_COMPRESS) |