diff options
author | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2024-09-29 14:28:57 +0100 |
---|---|---|
committer | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2024-09-29 14:28:57 +0100 |
commit | 0b14efee60765701f439b5174e56c0cf0639d20c (patch) | |
tree | ba319069e4f0a8d1377f34c5254246b65cff1765 /scripts/bsp_level_generator.gd | |
parent | 688a0418fd24c9ffff7dca43dd027ccb3784a373 (diff) |
player,bsp_level_generator: Add lockers and interaction
Diffstat (limited to 'scripts/bsp_level_generator.gd')
-rw-r--r-- | scripts/bsp_level_generator.gd | 54 |
1 files changed, 45 insertions, 9 deletions
diff --git a/scripts/bsp_level_generator.gd b/scripts/bsp_level_generator.gd index f8b850d..0f38b96 100644 --- a/scripts/bsp_level_generator.gd +++ b/scripts/bsp_level_generator.gd @@ -10,6 +10,7 @@ extends Node3D @export var wall_thickness: float = 1 var wall_inst = preload("res://models/wall.blend") +var locker_inst = preload("res://prefabs/locker.tscn") var level_geo: Node3D enum Direction {LEFT, RIGHT} @@ -21,6 +22,8 @@ var min_room_size: int = min_dim * min_dim var grid_width = width + 1 var grid_height = height + 1 +var rooms: Array[Rect2i] = [] + class BSPNode: var axis: int var min_dims: Vector2i @@ -91,6 +94,10 @@ func generate_grid(map: BSPNode, grid: Array[Tile.Tile]) -> void: 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.Tile.FLOOR + + # TODO double check room dimensions are correct here + var room = Rect2i(map.min_dims + Vector2i.ONE, map.max_dims - map.min_dims) + rooms.append(room) else: generate_grid(map.left, grid) generate_grid(map.right, grid) @@ -124,6 +131,29 @@ func generate_grid(map: BSPNode, grid: Array[Tile.Tile]) -> void: get_tree().quit() return +func get_tile(grid: Array[Tile.Tile], pos: Vector2i) -> Tile.Tile: + return grid[pos.y * grid_width + pos.x] + +func populate_grid(grid: Array[Tile.Tile]) -> void: + for room in rooms: + var num_lockers = randi_range(0, 2) + print("Generating ", num_lockers, "Lockers") + for i in range(num_lockers): + var found = false + while not found: + var pos = Vector2i(randi_range(room.position.x, room.end.x - 1), randi_range(room.position.y, room.end.y - 1)) + if grid[pos.y * grid_width + pos.x] == Tile.Tile.FLOOR \ + and (get_tile(grid, pos + Vector2i(1, 0)) == Tile.Tile.WALL \ + or get_tile(grid, pos + Vector2i(-1, 0)) == Tile.Tile.WALL \ + or get_tile(grid, pos + Vector2i(0, 1)) == Tile.Tile.WALL \ + or get_tile(grid, pos + Vector2i(0, -1)) == Tile.Tile.WALL) \ + and get_tile(grid, pos + Vector2i(1, 0)) != Tile.Tile.DOOR \ + and get_tile(grid, pos + Vector2i(-1, 0)) != Tile.Tile.DOOR \ + and get_tile(grid, pos + Vector2i(0, 1)) != Tile.Tile.DOOR \ + and get_tile(grid, pos + Vector2i(0, -1)) != Tile.Tile.DOOR: + grid[pos.y * grid_width + pos.x] = Tile.Tile.LOCKER + found = true + 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) @@ -148,7 +178,15 @@ func generate_geo(grid: Array[Tile.Tile], array: Array) -> void: for x in range(grid_width): var tile = grid[y * grid_width + x] - if tile == Tile.Tile.FLOOR or tile == Tile.Tile.DOOR: + if tile == Tile.Tile.WALL: + generate_plane(array, Vector3(x, 2, y), [Vector3(0, 0, 1), Vector3(0, -2, 0)], Vector3.LEFT) + 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.RIGHT) + generate_plane(array, Vector3(x+1, 2, y), [Vector3(-1, 0, 0), Vector3(0, -2, 0)], Vector3.FORWARD) + var wall: Node3D = level_geo.get_node("./Wall").duplicate() + wall.position = Vector3(x, 0, y) + add_child(wall) + else: generate_plane(array, Vector3(x, 0, y), [Vector3(1, 0, 0), Vector3(0, 0, 1)], Vector3.UP) var floor_tile: Node3D = level_geo.get_node("./Floor").duplicate() floor_tile.position = Vector3(x, 0, y) @@ -158,14 +196,11 @@ func generate_geo(grid: Array[Tile.Tile], array: Array) -> void: ciel_tile.rotate_z(deg_to_rad(180)) ciel_tile.position = Vector3(x+1, 2, y) add_child(ciel_tile) - elif tile == Tile.Tile.WALL: - generate_plane(array, Vector3(x, 2, y), [Vector3(0, 0, 1), Vector3(0, -2, 0)], Vector3.LEFT) - 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.RIGHT) - generate_plane(array, Vector3(x+1, 2, y), [Vector3(-1, 0, 0), Vector3(0, -2, 0)], Vector3.FORWARD) - var wall: Node3D = level_geo.get_node("./Wall").duplicate() - wall.position = Vector3(x, 0, y) - add_child(wall) + + if tile == Tile.Tile.LOCKER: + var locker: Node3D = locker_inst.instantiate() + locker.position = Vector3(x, 0, y) + add_child(locker) func _ready() -> void: var starting_axis = randi_range(0, 1) @@ -184,6 +219,7 @@ func _ready() -> void: surface_array[Mesh.ARRAY_NORMAL] = PackedVector3Array() generate_grid(map, grid) + populate_grid(grid) grid_generated.emit(grid, grid_width, grid_height) level_geo = wall_inst.instantiate() generate_geo(grid, surface_array) |