mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 08:45:33 +00:00
Autonomous bullet life system
This commit is contained in:
parent
aab69fb609
commit
36df2274c0
7 changed files with 266 additions and 15 deletions
File diff suppressed because one or more lines are too long
14
Scenes/Weapons/BaseAutonomousBulletEmitter.tscn
Normal file
14
Scenes/Weapons/BaseAutonomousBulletEmitter.tscn
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
[gd_scene load_steps=4 format=3 uid="uid://du37oyues6klq"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bvp1fwdtx2bri" path="res://Scripts/AttackPatterns/AutonomousBulletEmitter.cs" id="1_skinl"]
|
||||
[ext_resource type="Texture2D" uid="uid://oys0gjau3xbu" path="res://Sprites/Bullets/White_Bullet_Mid.png" id="2_0wpaf"]
|
||||
[ext_resource type="Script" uid="uid://c6467d6yx50qh" path="res://Scripts/Components/BulletSpawner.cs" id="3_0wpaf"]
|
||||
|
||||
[node name="BaseAutonomousBulletEmitter" type="Node2D"]
|
||||
script = ExtResource("1_skinl")
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
texture = ExtResource("2_0wpaf")
|
||||
|
||||
[node name="BulletSpawner" type="Node2D" parent="."]
|
||||
script = ExtResource("3_0wpaf")
|
||||
|
|
@ -8,7 +8,7 @@ namespace Cirno.Scripts.Actors;
|
|||
public partial class ScriptableBulletsEmitter : Node2D, IActivable, IScriptHost
|
||||
{
|
||||
[Export]
|
||||
public BulletScript Script { get; private set; }
|
||||
public BulletScript Script { get; set; }
|
||||
|
||||
[Export]
|
||||
public bool InvertSignal { get; private set; } = false;
|
||||
|
|
|
|||
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