diff options
author | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2024-09-26 15:35:44 +0100 |
---|---|---|
committer | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2024-09-26 15:35:44 +0100 |
commit | d29a892d287b52beb48c331c0da7bc59ac3db237 (patch) | |
tree | 2b06b4346c678cc17e2e7c86366bdc5d819ce2a7 /scripts/dungeon_manager.gd | |
parent | 8ff2487734df59855bbfd988d88b3b32273049e2 (diff) |
dungeon_manager: Add level exit, and load new level
Diffstat (limited to 'scripts/dungeon_manager.gd')
-rw-r--r-- | scripts/dungeon_manager.gd | 55 |
1 files changed, 52 insertions, 3 deletions
diff --git a/scripts/dungeon_manager.gd b/scripts/dungeon_manager.gd index ec3f5d4..12b80fa 100644 --- a/scripts/dungeon_manager.gd +++ b/scripts/dungeon_manager.gd @@ -5,10 +5,19 @@ var grid_width: int var grid_height: int var enemy_inst = preload("res://prefabs/enemy.tscn") +var exit_inst = preload("res://prefabs/exit.tscn") +var bsp_level_inst = preload("res://prefabs/bsp_level_generator.tscn") + +var level_done: bool = false # Called when the node enters the scene tree for the first time. func _ready() -> void: - pass # Replace with function body. + spawn_level() + +func spawn_level() -> void: + var bsp_level = bsp_level_inst.instantiate() + bsp_level.connect("grid_generated", _grid_generated) + $LevelContainer.add_child(bsp_level) func create_enemies() -> void: var num_enemies = randi_range(4, 10) @@ -24,11 +33,49 @@ func create_enemies() -> void: var pos = Vector2i(randi_range(0, grid_width-1), randi_range(0, grid_height-1)) if grid[pos.y * grid_width + pos.x] == Tile.Tile.FLOOR: - enemy.position = Vector3(pos.x, 0, pos.y) + enemy.position = Vector3(pos.x, 0, pos.y) + Vector3(0.5, 0, 0.5) found_pos = true print("Placing at ", pos) - add_child.call_deferred(enemy) + $LevelContainer.add_child.call_deferred(enemy) + +func create_exit() -> void: + var found_pos = false + var exit: Area3D = exit_inst.instantiate() + exit.connect("body_entered", _on_player_exit) + while not found_pos: + var pos = Vector2i(randi_range(0, grid_width-1), randi_range(0, grid_height-1)) + + if grid[pos.y * grid_width + pos.x] == Tile.Tile.FLOOR: + exit.position = Vector3(pos.x, 0, pos.y) + Vector3(0.5, 0, 0.5) + found_pos = true + + $LevelContainer.add_child.call_deferred(exit) + +func place_player() -> void: + var found_pos = false + while not found_pos: + var pos = Vector2i(randi_range(0, grid_width-1), randi_range(0, grid_height-1)) + + if grid[pos.y * grid_width + pos.x] == Tile.Tile.FLOOR: + $Player.position = Vector3(pos.x, 0, pos.y) + Vector3(0.5, 0, 0.5) + found_pos = true + +func reset_level() -> void: + for child in $LevelContainer.get_children(): + print("Child is ", child) + $LevelContainer.remove_child(child) + child.queue_free() + + spawn_level() + level_done = false + +func _process(_delta: float) -> void: + if level_done: + reset_level() + +func _on_player_exit(_body: Node3D): + level_done = true func _grid_generated(p_grid: Array[Tile.Tile], p_grid_width: int, p_grid_height: int) -> void: grid = p_grid @@ -36,3 +83,5 @@ func _grid_generated(p_grid: Array[Tile.Tile], p_grid_width: int, p_grid_height: grid_height = p_grid_height create_enemies() + create_exit() + place_player() |