mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:35:34 +00:00
Simplified and optimized bullet pooling
This commit is contained in:
parent
1a455251a9
commit
da73823ac8
2 changed files with 39 additions and 31 deletions
|
|
@ -12,14 +12,15 @@ public partial class PoolingManager : Node
|
|||
{
|
||||
public static PoolingManager Instance { get; private set; }
|
||||
|
||||
[Export] public StringName ActiveBulletsGroupName { get; private set; } = "ActiveBullets";
|
||||
|
||||
[Export] public StringName InactiveBulletsGroupName { get; private set; } = "InactiveBullets";
|
||||
|
||||
[Export] public Array<PooledBulletInfo> PoolOnStart { get; private set; }
|
||||
|
||||
[Export] public bool DebugView { get; private set; } = false;
|
||||
|
||||
private readonly System.Collections.Generic.Dictionary<BulletResource, System.Collections.Generic.List<IBullet>>
|
||||
_activeBullets = new();
|
||||
|
||||
private readonly System.Collections.Generic.Dictionary<BulletResource, System.Collections.Generic.List<IBullet>>
|
||||
private readonly System.Collections.Generic.Dictionary<BulletResource, System.Collections.Generic.Stack<IBullet>>
|
||||
_inactiveBullets = new();
|
||||
|
||||
public override void _Ready()
|
||||
|
|
@ -45,21 +46,26 @@ public partial class PoolingManager : Node
|
|||
// If present move it to active, set it as active and return it
|
||||
// Otherwise spawn it and add it to active and return it
|
||||
|
||||
var activeBullets = GetActiveBulletsList(bulletResource);
|
||||
// var activeBullets = GetActiveBulletsList(bulletResource);
|
||||
var inactiveBullets = GetInactiveBulletsList(bulletResource);
|
||||
|
||||
var bullet = inactiveBullets.Count > 0 ? inactiveBullets.First() : InstantiateBullet(bulletResource);
|
||||
var bullet = inactiveBullets.Count > 0 ? inactiveBullets.Pop() : InstantiateBullet(bulletResource);
|
||||
|
||||
if (active)
|
||||
{
|
||||
inactiveBullets.Remove(bullet);
|
||||
activeBullets.Add(bullet);
|
||||
//inactiveBullets.Remove(bullet);
|
||||
//activeBullets.Add(bullet);
|
||||
bullet.RemoveFromGroup(InactiveBulletsGroupName);
|
||||
bullet.AddToGroup(ActiveBulletsGroupName);
|
||||
bullet.Enable();
|
||||
}
|
||||
else
|
||||
{
|
||||
activeBullets.Add(bullet);
|
||||
inactiveBullets.Remove(bullet);
|
||||
//activeBullets.Add(bullet);
|
||||
//inactiveBullets.Remove(bullet);
|
||||
bullet.AddToGroup(InactiveBulletsGroupName);
|
||||
bullet.RemoveFromGroup(ActiveBulletsGroupName);
|
||||
inactiveBullets.Push(bullet);
|
||||
bullet.Disable();
|
||||
}
|
||||
|
||||
|
|
@ -73,35 +79,32 @@ public partial class PoolingManager : Node
|
|||
|
||||
public IEnumerable<IBullet> GetAllActiveBullets()
|
||||
{
|
||||
return _activeBullets.Values.SelectMany(list => list);
|
||||
return GetTree().GetNodesInGroup(ActiveBulletsGroupName).Cast<IBullet>();
|
||||
//return _activeBullets.Values.SelectMany(list => list);
|
||||
}
|
||||
|
||||
public IEnumerable<IBullet> GetAllInActiveBullets()
|
||||
{
|
||||
return _activeBullets.Values.SelectMany(list => list);
|
||||
}
|
||||
// public List<IBullet> GetActiveBulletsList(BulletResource resource)
|
||||
// {
|
||||
// return GetOrCreateList(_activeBullets, resource);
|
||||
// }
|
||||
|
||||
public List<IBullet> GetActiveBulletsList(BulletResource resource)
|
||||
{
|
||||
return GetOrCreateList(_activeBullets, resource);
|
||||
}
|
||||
|
||||
private List<IBullet> GetInactiveBulletsList(BulletResource resource)
|
||||
private Stack<IBullet> GetInactiveBulletsList(BulletResource resource)
|
||||
{
|
||||
return GetOrCreateList(_inactiveBullets, resource);
|
||||
}
|
||||
|
||||
public void DisableBullet(IBullet bullet)
|
||||
{
|
||||
var activeBulletsList = GetActiveBulletsList(bullet.BulletInfo.OriginalBulletResource);
|
||||
//var activeBulletsList = GetActiveBulletsList(bullet.BulletInfo.OriginalBulletResource);
|
||||
|
||||
var inactiveBulletsList = GetInactiveBulletsList(bullet.BulletInfo.OriginalBulletResource);
|
||||
|
||||
bullet.Disable(!DebugView);
|
||||
|
||||
activeBulletsList.Remove(bullet);
|
||||
|
||||
inactiveBulletsList.Add(bullet);
|
||||
//activeBulletsList.Remove(bullet);
|
||||
bullet.AddToGroup(InactiveBulletsGroupName);
|
||||
bullet.RemoveFromGroup(ActiveBulletsGroupName);
|
||||
inactiveBulletsList.Push(bullet);
|
||||
}
|
||||
|
||||
public void DisableAllBullets()
|
||||
|
|
@ -114,17 +117,19 @@ public partial class PoolingManager : Node
|
|||
|
||||
bullet.Disable(!DebugView);
|
||||
|
||||
inactiveBulletsList.Add(bullet);
|
||||
bullet.AddToGroup(InactiveBulletsGroupName);
|
||||
bullet.RemoveFromGroup(ActiveBulletsGroupName);
|
||||
inactiveBulletsList.Push(bullet);
|
||||
}
|
||||
|
||||
foreach (var activeBulletsList in _activeBullets)
|
||||
{
|
||||
activeBulletsList.Value.Clear();
|
||||
}
|
||||
// foreach (var activeBulletsList in _activeBullets)
|
||||
// {
|
||||
// activeBulletsList.Value.Clear();
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
private List<IBullet> GetOrCreateList(System.Collections.Generic.Dictionary<BulletResource, List<IBullet>> dict, BulletResource resource)
|
||||
private Stack<IBullet> GetOrCreateList(System.Collections.Generic.Dictionary<BulletResource, Stack<IBullet>> dict, BulletResource resource)
|
||||
{
|
||||
if (dict.TryGetValue(resource, out var list)) return list;
|
||||
list = [];
|
||||
|
|
|
|||
|
|
@ -31,4 +31,7 @@ public interface IBullet
|
|||
public bool CanHit(BulletOwner bulletOwner, BulletOwner targetGroup);
|
||||
public void RequestCollisionDestruction();
|
||||
public void Freeze();
|
||||
|
||||
public void AddToGroup(StringName group, bool persistent = false);
|
||||
public void RemoveFromGroup(StringName group);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue