83 lines
2.1 KiB
C#
83 lines
2.1 KiB
C#
using Godot;
|
|
using System;
|
|
using Chickensoft.AutoInject;
|
|
using Chickensoft.Introspection;
|
|
using Movementtests.managers;
|
|
|
|
[GlobalClass, Icon("res://assets/ui/IconGodotNode/node_3D/icon_duplicate.png"), Meta(typeof(IAutoNode))]
|
|
public partial class Spawner : Node3D
|
|
{
|
|
public override void _Notification(int what) => this.Notify(what);
|
|
|
|
[Dependency] public WaveManager WaveManager => this.DependOn<WaveManager>();
|
|
|
|
[Export(PropertyHint.NodeType)]
|
|
public PackedScene? EnemyToSpawn { get; set; }
|
|
|
|
[Export] public Godot.Collections.Array<EnemyDescription.EnemyType> SupportedEnemyTypes { get; set; } = [];
|
|
|
|
[Export]
|
|
public RMovement? MovementInputs { get; set; }
|
|
|
|
[Export]
|
|
public Node3D Target { get; set; }
|
|
|
|
[Export(PropertyHint.Range, "0.1, 100, 0.1, or_greater")]
|
|
public float SpawnInterval { get; set; } = 1.0f;
|
|
[Export]
|
|
public bool IsActiveOnStart { get; set; } = true;
|
|
|
|
[Node("Timer")] private Timer SpawnTimer { get; set; }
|
|
|
|
public void OnReady()
|
|
{
|
|
SpawnTimer.WaitTime = SpawnInterval;
|
|
SpawnTimer.Timeout += Spawn;
|
|
if (IsActiveOnStart) StartSpawning();
|
|
}
|
|
|
|
public void OnResolved()
|
|
{
|
|
WaveManager.RegisterSpawner(this);
|
|
}
|
|
|
|
public void Spawn()
|
|
{
|
|
if (EnemyToSpawn == null || !EnemyToSpawn.CanInstantiate()) return;
|
|
|
|
if (EnemyToSpawn.Instantiate() is not Enemy spawnedInstance) return;
|
|
|
|
spawnedInstance.Target = Target;
|
|
if (MovementInputs != null)
|
|
spawnedInstance.RMovement = MovementInputs;
|
|
spawnedInstance.Init();
|
|
|
|
GetTree().GetCurrentScene().AddChild(spawnedInstance);
|
|
spawnedInstance.GlobalPosition = GlobalPosition;
|
|
}
|
|
|
|
public Enemy? SpawnEnemy(EnemyDescription description)
|
|
{
|
|
if (description.Scene.Instantiate() is not Enemy spawnedInstance) return null;
|
|
|
|
spawnedInstance.Target = Target;
|
|
if (description.MovementOverride is not null)
|
|
spawnedInstance.RMovement = description.MovementOverride;
|
|
spawnedInstance.Init();
|
|
|
|
GetTree().GetCurrentScene().AddChild(spawnedInstance);
|
|
spawnedInstance.GlobalPosition = GlobalPosition;
|
|
return spawnedInstance;
|
|
}
|
|
|
|
public void StartSpawning()
|
|
{
|
|
SpawnTimer.Start();
|
|
}
|
|
|
|
public void StopSpawning()
|
|
{
|
|
SpawnTimer.Stop();
|
|
}
|
|
}
|