mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-07 02:05:54 +00:00
Autonomous bullet life system
This commit is contained in:
parent
aab69fb609
commit
36df2274c0
7 changed files with 266 additions and 15 deletions
74
Scripts/AttackPatterns/AutonomousBulletEmitter.cs
Normal file
74
Scripts/AttackPatterns/AutonomousBulletEmitter.cs
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
using Cirno.Scripts.Resources;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.AttackPatterns;
|
||||
|
||||
public partial class AutonomousBulletEmitter : Node2D, IScriptHost
|
||||
{
|
||||
[Export]
|
||||
public BulletScript Script { get; set; }
|
||||
[Export]
|
||||
public bool EmitOnStart { get; set; } = false;
|
||||
|
||||
[Signal] public delegate void DeathEventHandler();
|
||||
|
||||
public double LifeTime { get; set; } = 0d;
|
||||
|
||||
private double _lifeCounter = 0d;
|
||||
|
||||
private bool _isActive = false;
|
||||
|
||||
public bool Autonomous { get; set; } = true;
|
||||
|
||||
protected BulletScript.BulletScriptMachine ScriptMachine;
|
||||
private Vector2 _homePosition;
|
||||
public Vector2 HomePosition => _homePosition;
|
||||
public void Start()
|
||||
{
|
||||
ScriptMachine = Script.Make(this);
|
||||
|
||||
_homePosition = this.GlobalPosition;
|
||||
|
||||
if (EmitOnStart)
|
||||
{
|
||||
_isActive = true;
|
||||
ScriptMachine.Start();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (!Autonomous) return;
|
||||
if (!_isActive) return;
|
||||
//this.GlobalPosition += new Vector2(10 * (float)delta, 0);
|
||||
Process(delta);
|
||||
}
|
||||
|
||||
public void Update(double delta)
|
||||
{
|
||||
if (Autonomous) return;
|
||||
if (!_isActive) return;
|
||||
|
||||
Process(delta);
|
||||
}
|
||||
|
||||
private void Process(double delta)
|
||||
{
|
||||
_lifeCounter += delta;
|
||||
if (_lifeCounter >= LifeTime)
|
||||
{
|
||||
_isActive = false;
|
||||
EmitSignalDeath();
|
||||
QueueFree();
|
||||
return;
|
||||
}
|
||||
ScriptMachine.UpdatePhase(delta);
|
||||
}
|
||||
|
||||
public void ChangeSpriteDirection(Vector2 direction)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
1
Scripts/AttackPatterns/AutonomousBulletEmitter.cs.uid
Normal file
1
Scripts/AttackPatterns/AutonomousBulletEmitter.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bvp1fwdtx2bri
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
1
Scripts/AttackPatterns/CreateEmitterPattern.cs.uid
Normal file
1
Scripts/AttackPatterns/CreateEmitterPattern.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://ca6atry8iniub
|
||||
Loading…
Add table
Add a link
Reference in a new issue