feat: Implement enemy spawning and movement logic in the level manager

This commit is contained in:
2026-03-20 20:40:12 +11:00
parent 00af585cf4
commit 3f2cbcc6f2
4 changed files with 74 additions and 7 deletions
+17 -2
View File
@@ -3,6 +3,8 @@
# Responsible for generating, painting, and initializing the level, as well as providing pathfinding and coordinate helpers.
extends Node
const ENEMY_SCENE_PATH: String = "res://scenes/actors/enemy.tscn"
# The source of truth for the current level's layout (MapData resource).
var _data: MapData
# The TileMapLayer node where the map is visually rendered.
@@ -18,8 +20,8 @@ var _pathfinding: PathfindingHandler = PathfindingHandler.new()
# 1. Generates the map data.
# 2. Paints the map visually.
# 3. Builds the navigation graph for AI/pathfinding.
# 4. Spawns the player at the correct location.
func initialize(settings: MapSettings, layer: TileMapLayer, player: CharacterBody2D) -> void:
# 4. Spawns the player at the correct location.
func initialize(settings: MapSettings, layer: TileMapLayer, player: Player) -> void:
_layer = layer
# 1. Generate the abstract map data (rooms, corridors, etc.).
@@ -38,6 +40,19 @@ func initialize(settings: MapSettings, layer: TileMapLayer, player: CharacterBod
# Uses the TileMapLayer to convert grid coordinates to world pixel positions.
player.position = _layer.map_to_local(_data.spawn_point)
# 5. Spawn one enemy per room, skipping the player's spawn room (index 0).
var enemy_scene := load(ENEMY_SCENE_PATH) as PackedScene
var world_node := player.get_parent()
for i in range(1, _data.rooms.size()):
var enemy_instance = enemy_scene.instantiate()
var enemy = enemy_instance as Enemy
if enemy == null:
push_error("Failed to cast enemy instance to Enemy at room index %d" % i)
continue
enemy._player = player
world_node.add_child(enemy)
enemy.position = _layer.map_to_local(_data.rooms[i].get_center())
# Public API for other systems (like AI) to query the navigation grid.
# Encapsulates the PathfindingHandler so callers don't need to know about it directly.
func find_path(from: Vector2i, to: Vector2i) -> Array[Vector2i]: