From 3ef1fc4df2e3178d37061ad13b5d366771193dd2 Mon Sep 17 00:00:00 2001 From: Lucas Fryzek Date: Sun, 22 Sep 2024 20:46:38 +0100 Subject: Add enemy spawning and ability to kill enemies --- scripts/dungeon_manager.gd | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 scripts/dungeon_manager.gd (limited to 'scripts/dungeon_manager.gd') diff --git a/scripts/dungeon_manager.gd b/scripts/dungeon_manager.gd new file mode 100644 index 0000000..270a5a0 --- /dev/null +++ b/scripts/dungeon_manager.gd @@ -0,0 +1,36 @@ +extends Node3D + +var grid: Array[Tile.Tile] = [] +var grid_width: int +var grid_height: int + +var enemy_inst = preload("res://prefabs/enemy.tscn") + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass # Replace with function body. + +func create_enemies() -> void: + var num_enemies = randi_range(1, 10) + + for i in range(num_enemies): + var enemy = enemy_inst.instantiate() + enemy.target = $Player + + 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: + enemy.position = Vector3(pos.x, 0, pos.y) + found_pos = true + print("Placing at ", pos) + + add_child.call_deferred(enemy) + +func _grid_generated(p_grid: Array[Tile.Tile], p_grid_width: int, p_grid_height: int) -> void: + grid = p_grid + grid_width = p_grid_width + grid_height = p_grid_height + + create_enemies() -- cgit