save
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
|
||||
public partial class ProjectilePool : Node
|
||||
{
|
||||
[Export] private PackedScene _scene;
|
||||
[Export] private int _poolSize;
|
||||
|
||||
private readonly Queue<Projectile> _pool = new();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
for (int i = 0; i < _poolSize; i++)
|
||||
{
|
||||
_pool.Enqueue(CreateProjectile());
|
||||
}
|
||||
}
|
||||
|
||||
private Projectile CreateProjectile()
|
||||
{
|
||||
var p = _scene.Instantiate<Projectile>();
|
||||
p.OnDespawn += ReturnToPool;
|
||||
return p;
|
||||
}
|
||||
|
||||
public Projectile Get()
|
||||
{
|
||||
var p = _pool.Count > 0 ? _pool.Dequeue() : CreateProjectile();
|
||||
return p;
|
||||
}
|
||||
|
||||
private void ReturnToPool(Projectile p)
|
||||
{
|
||||
p.GetParent()?.RemoveChild(p);
|
||||
p.ProcessMode = ProcessModeEnum.Disabled;
|
||||
_pool.Enqueue(p);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user