godot-visual-effects/scripts/MultiParticles.cs

66 lines
1.3 KiB
C#
Raw Normal View History

2023-12-12 22:44:18 +01:00
using System;
2023-12-12 23:45:53 +01:00
using Godot;
2023-12-12 22:44:18 +01:00
2023-12-14 06:40:19 +01:00
/**
* MultiParticles : Node3D
*
* This class turns possible to control multiple particles in sync.
* Inspired by https://www.reddit.com/r/godot/comments/181ui9c/comment/kaewcca/?context=3
*/
public partial class MultiParticles : Node3D
2023-12-12 22:44:18 +01:00
{
2023-12-14 06:40:19 +01:00
[Export]
public bool Emitting = true;
2023-12-12 22:44:18 +01:00
2023-12-14 06:40:19 +01:00
[Export]
public bool OneShot = false;
2023-12-12 22:44:18 +01:00
2023-12-14 06:40:19 +01:00
[Export]
public Godot.Collections.Array<GpuParticles3D> Particles;
2023-12-12 22:44:18 +01:00
2023-12-14 06:40:19 +01:00
double Lifetime = 0.0f;
2023-12-12 22:44:18 +01:00
2023-12-14 06:40:19 +01:00
public override void _Ready()
{
if (Emitting)
2023-12-12 22:44:18 +01:00
{
2023-12-14 06:40:19 +01:00
StartEmitters();
2023-12-12 22:44:18 +01:00
}
2023-12-14 06:40:19 +01:00
}
2023-12-12 22:44:18 +01:00
2023-12-14 06:40:19 +01:00
void GetLifetime()
{
foreach (var particle in Particles)
2023-12-12 22:44:18 +01:00
{
2023-12-14 06:40:19 +01:00
if (particle.Lifetime > Lifetime)
2023-12-12 22:44:18 +01:00
{
2023-12-14 06:40:19 +01:00
Lifetime = particle.Lifetime;
2023-12-12 22:44:18 +01:00
}
}
2023-12-14 06:40:19 +01:00
}
2023-12-12 22:44:18 +01:00
2023-12-14 06:40:19 +01:00
public async void StartEmitters()
{
GetLifetime();
2023-12-12 22:44:18 +01:00
2023-12-14 06:40:19 +01:00
foreach (var particle in Particles)
particle.Emitting = true;
2023-12-12 22:44:18 +01:00
2023-12-14 06:40:19 +01:00
await ToSignal(GetTree().CreateTimer(Lifetime), SceneTreeTimer.SignalName.Timeout);
DisposeOfEmitters();
2023-12-12 22:44:18 +01:00
2023-12-14 06:40:19 +01:00
if (!OneShot)
{
StartEmitters();
2023-12-12 22:44:18 +01:00
}
2023-12-14 06:40:19 +01:00
}
2023-12-12 22:44:18 +01:00
2023-12-14 06:40:19 +01:00
void DisposeOfEmitters()
{
foreach (var particle in Particles)
2023-12-12 22:44:18 +01:00
{
2023-12-14 06:40:19 +01:00
particle.Emitting = false;
2023-12-12 22:44:18 +01:00
}
}
}