cirnogodot/Scripts/AttackPatterns/CreateEmitterPattern.cs

78 lines
2.2 KiB
C#
Raw Normal View History

2025-05-09 16:14:51 +02:00
using Cirno.Scripts.Actors;
using Cirno.Scripts.Resources;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.AttackPatterns;
[GlobalClass]
2025-06-03 10:11:09 +02:00
[Tool]
2025-05-09 16:14:51 +02:00
public partial class CreateEmitterPattern : AttackPattern
{
[Export] public Vector2 SpawnOffset { get; set; }
[Export] public BulletScript Script { get; set; }
[Export] public PackedScene Prefab { get; set; }
[Export] public bool CreateAsChild { get; set; }
[Export] public double LifeTime { get; set; } = 10d;
2025-06-11 15:28:26 +02:00
public override IPatternMachine MakeMachine(Node parent)
2025-05-09 16:14:51 +02:00
{
return new EmitterPatternMachine(this, parent);
}
2025-06-11 15:28:26 +02:00
public class EmitterPatternMachine(CreateEmitterPattern pattern, Node parent) : IPatternMachine
2025-05-09 16:14:51 +02:00
{
private bool _active = false;
2025-06-11 15:28:26 +02:00
public Node Parent => parent;
2025-05-09 16:14:51 +02:00
public AutonomousBulletEmitter Emitter { get; private set; }
public void Start()
{
2025-06-11 15:28:26 +02:00
if (parent is not Node2D parent2d)
{
return;
}
2025-05-09 16:14:51 +02:00
Emitter = pattern.CreateAsChild
2025-06-11 15:28:26 +02:00
? parent2d.CreateChild<AutonomousBulletEmitter>(pattern.Prefab,
parent2d.GlobalPosition + pattern.SpawnOffset)
: parent2d.CreateSibling<AutonomousBulletEmitter>(pattern.Prefab,
parent2d.GlobalPosition + pattern.SpawnOffset);
2025-05-09 16:14:51 +02:00
Emitter.Script = pattern.Script;
Emitter.EmitOnStart = true;
Emitter.LifeTime = pattern.LifeTime;
2025-05-09 17:06:45 +02:00
2025-05-09 16:14:51 +02:00
_active = true;
Emitter.Autonomous = !pattern.WaitForCompletion;
2025-05-09 17:06:45 +02:00
2025-05-09 16:14:51 +02:00
Emitter.Death += EmitterOnDeath;
2025-05-09 17:06:45 +02:00
2025-05-09 16:14:51 +02:00
Emitter.Start();
}
private void EmitterOnDeath()
{
_active = false;
2025-05-09 17:06:45 +02:00
Emitter.Death -= EmitterOnDeath;
2025-05-09 16:14:51 +02:00
}
public void UpdatePattern(double delta)
{
if (_active)
{
if (pattern.WaitForCompletion)
{
Emitter.Update(delta);
}
}
}
public bool IsComplete()
{
2025-05-09 17:06:45 +02:00
if (pattern.WaitForCompletion)
2025-05-09 16:14:51 +02:00
return !_active;
return _active;
}
}
}