mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-06 22:45:53 +00:00
Autonomous bullet life system
This commit is contained in:
parent
aab69fb609
commit
36df2274c0
7 changed files with 266 additions and 15 deletions
70
Scripts/AttackPatterns/CreateEmitterPattern.cs
Normal file
70
Scripts/AttackPatterns/CreateEmitterPattern.cs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
using Cirno.Scripts.Actors;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Cirno.Scripts.AttackPatterns;
|
||||
|
||||
[GlobalClass]
|
||||
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;
|
||||
|
||||
public override IPatternMachine MakeMachine(Node2D parent)
|
||||
{
|
||||
return new EmitterPatternMachine(this, parent);
|
||||
}
|
||||
|
||||
public class EmitterPatternMachine(CreateEmitterPattern pattern, Node2D parent) : IPatternMachine
|
||||
{
|
||||
private bool _active = false;
|
||||
public Node2D Parent => parent;
|
||||
public AutonomousBulletEmitter Emitter { get; private set; }
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Emitter = pattern.CreateAsChild
|
||||
? parent.CreateChild<AutonomousBulletEmitter>(pattern.Prefab, parent.GlobalPosition + pattern.SpawnOffset)
|
||||
: parent.CreateSibling<AutonomousBulletEmitter>(pattern.Prefab, parent.GlobalPosition + pattern.SpawnOffset);
|
||||
Emitter.Script = pattern.Script;
|
||||
Emitter.EmitOnStart = true;
|
||||
Emitter.LifeTime = pattern.LifeTime;
|
||||
|
||||
_active = true;
|
||||
Emitter.Autonomous = !pattern.WaitForCompletion;
|
||||
|
||||
Emitter.Death += EmitterOnDeath;
|
||||
|
||||
Emitter.Start();
|
||||
}
|
||||
|
||||
private void EmitterOnDeath()
|
||||
{
|
||||
_active = false;
|
||||
}
|
||||
|
||||
public void UpdatePattern(double delta)
|
||||
{
|
||||
if (_active)
|
||||
{
|
||||
if (pattern.WaitForCompletion)
|
||||
{
|
||||
Emitter.Update(delta);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public bool IsComplete()
|
||||
{
|
||||
if (pattern.WaitForCompletion)
|
||||
return !_active;
|
||||
return _active;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue