Files
fgj-26/Scripts/Enemies/EnemySpawner.cs
T
2026-01-31 23:25:02 +02:00

39 lines
907 B
C#

using Godot;
using System.Collections.Generic;
public partial class EnemySpawner : Node
{
[Export] private Node _pathParent;
[Export] private EnemyPool _pool;
private readonly List<Path2D> _paths = new();
public override void _Ready()
{
foreach (var c in _pathParent.GetChildren())
if (c is Path2D p)
_paths.Add(p);
for (int i = 0; i < 10; i++)
SpawnWithDelay(i * 0.33f);
}
private async void SpawnWithDelay(float t)
{
await ToSignal(GetTree().CreateTimer(t), SceneTreeTimer.SignalName.Timeout);
Spawn();
}
private void Spawn()
{
if (_paths.Count == 0) return;
var path = _paths[GD.RandRange(0, _paths.Count - 1)];
var type = _pool.GetRandomType();
var enemy = _pool.Get(type);
if (enemy == null) return;
path.AddChild(enemy);
}
}