2025-06-08 16:33:38 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Collections.Immutable;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Cirno.Scripts.Resources;
|
2025-06-17 11:57:59 +02:00
|
|
|
|
using Cirno.Scripts.Weapons;
|
2025-06-08 16:33:38 +02:00
|
|
|
|
using Godot;
|
|
|
|
|
|
using Godot.Collections;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Cirno.Scripts.Controllers;
|
|
|
|
|
|
|
2025-06-17 11:57:59 +02:00
|
|
|
|
public partial class PoolingManager : Node
|
2025-06-08 16:33:38 +02:00
|
|
|
|
{
|
|
|
|
|
|
public static PoolingManager Instance { get; private set; }
|
|
|
|
|
|
|
2025-07-01 13:55:30 +02:00
|
|
|
|
[Export] public StringName ActiveBulletsGroupName { get; private set; } = "ActiveBullets";
|
|
|
|
|
|
|
|
|
|
|
|
[Export] public StringName InactiveBulletsGroupName { get; private set; } = "InactiveBullets";
|
|
|
|
|
|
|
2025-06-08 16:33:38 +02:00
|
|
|
|
[Export] public Array<PooledBulletInfo> PoolOnStart { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
[Export] public bool DebugView { get; private set; } = false;
|
|
|
|
|
|
|
2025-07-01 13:55:30 +02:00
|
|
|
|
private readonly System.Collections.Generic.Dictionary<BulletResource, System.Collections.Generic.Stack<IBullet>>
|
2025-06-08 16:33:38 +02:00
|
|
|
|
_inactiveBullets = new();
|
|
|
|
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
|
|
{
|
|
|
|
|
|
Instance = this;
|
2025-06-22 13:52:23 +02:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SpawnInitialBullets()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var bulletInfo in PoolOnStart)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i < bulletInfo.Amount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
SpawnBullet(bulletInfo.Bullet, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-08 16:33:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-17 11:57:59 +02:00
|
|
|
|
public IBullet SpawnBullet(BulletResource bulletResource, bool active = true)
|
2025-06-08 16:33:38 +02:00
|
|
|
|
{
|
|
|
|
|
|
// Look for bullet among the inactive ones
|
|
|
|
|
|
// If present move it to active, set it as active and return it
|
|
|
|
|
|
// Otherwise spawn it and add it to active and return it
|
|
|
|
|
|
|
2025-07-01 13:55:30 +02:00
|
|
|
|
// var activeBullets = GetActiveBulletsList(bulletResource);
|
2025-06-08 16:33:38 +02:00
|
|
|
|
var inactiveBullets = GetInactiveBulletsList(bulletResource);
|
|
|
|
|
|
|
2025-07-01 13:55:30 +02:00
|
|
|
|
var bullet = inactiveBullets.Count > 0 ? inactiveBullets.Pop() : InstantiateBullet(bulletResource);
|
2025-06-08 16:33:38 +02:00
|
|
|
|
|
|
|
|
|
|
if (active)
|
|
|
|
|
|
{
|
2025-07-01 13:55:30 +02:00
|
|
|
|
//inactiveBullets.Remove(bullet);
|
|
|
|
|
|
//activeBullets.Add(bullet);
|
|
|
|
|
|
bullet.RemoveFromGroup(InactiveBulletsGroupName);
|
|
|
|
|
|
bullet.AddToGroup(ActiveBulletsGroupName);
|
2025-06-08 16:33:38 +02:00
|
|
|
|
bullet.Enable();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-01 13:55:30 +02:00
|
|
|
|
//activeBullets.Add(bullet);
|
|
|
|
|
|
//inactiveBullets.Remove(bullet);
|
|
|
|
|
|
bullet.AddToGroup(InactiveBulletsGroupName);
|
|
|
|
|
|
bullet.RemoveFromGroup(ActiveBulletsGroupName);
|
|
|
|
|
|
inactiveBullets.Push(bullet);
|
2025-06-08 16:33:38 +02:00
|
|
|
|
bullet.Disable();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return bullet;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-17 11:57:59 +02:00
|
|
|
|
public T SpawnBullet<T>(BulletResource bulletResource, bool active = true) where T : IBullet
|
|
|
|
|
|
{
|
|
|
|
|
|
return (T)SpawnBullet(bulletResource, active);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IBullet> GetAllActiveBullets()
|
2025-06-08 16:33:38 +02:00
|
|
|
|
{
|
2025-07-01 13:55:30 +02:00
|
|
|
|
return GetTree().GetNodesInGroup(ActiveBulletsGroupName).Cast<IBullet>();
|
|
|
|
|
|
//return _activeBullets.Values.SelectMany(list => list);
|
2025-06-08 16:33:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-01 13:55:30 +02:00
|
|
|
|
// public List<IBullet> GetActiveBulletsList(BulletResource resource)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// return GetOrCreateList(_activeBullets, resource);
|
|
|
|
|
|
// }
|
2025-06-08 16:33:38 +02:00
|
|
|
|
|
2025-07-01 13:55:30 +02:00
|
|
|
|
private Stack<IBullet> GetInactiveBulletsList(BulletResource resource)
|
2025-06-08 16:33:38 +02:00
|
|
|
|
{
|
|
|
|
|
|
return GetOrCreateList(_inactiveBullets, resource);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-17 11:57:59 +02:00
|
|
|
|
public void DisableBullet(IBullet bullet)
|
2025-06-08 16:33:38 +02:00
|
|
|
|
{
|
2025-07-01 13:55:30 +02:00
|
|
|
|
//var activeBulletsList = GetActiveBulletsList(bullet.BulletInfo.OriginalBulletResource);
|
2025-06-08 16:33:38 +02:00
|
|
|
|
|
|
|
|
|
|
var inactiveBulletsList = GetInactiveBulletsList(bullet.BulletInfo.OriginalBulletResource);
|
|
|
|
|
|
|
|
|
|
|
|
bullet.Disable(!DebugView);
|
|
|
|
|
|
|
2025-07-01 13:55:30 +02:00
|
|
|
|
//activeBulletsList.Remove(bullet);
|
|
|
|
|
|
bullet.AddToGroup(InactiveBulletsGroupName);
|
|
|
|
|
|
bullet.RemoveFromGroup(ActiveBulletsGroupName);
|
|
|
|
|
|
inactiveBulletsList.Push(bullet);
|
2025-06-08 16:33:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DisableAllBullets()
|
|
|
|
|
|
{
|
|
|
|
|
|
var allActiveBullets = GetAllActiveBullets().ToImmutableList();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var bullet in allActiveBullets)
|
|
|
|
|
|
{
|
|
|
|
|
|
var inactiveBulletsList = GetInactiveBulletsList(bullet.BulletInfo.OriginalBulletResource);
|
|
|
|
|
|
|
|
|
|
|
|
bullet.Disable(!DebugView);
|
|
|
|
|
|
|
2025-07-01 13:55:30 +02:00
|
|
|
|
bullet.AddToGroup(InactiveBulletsGroupName);
|
|
|
|
|
|
bullet.RemoveFromGroup(ActiveBulletsGroupName);
|
|
|
|
|
|
inactiveBulletsList.Push(bullet);
|
2025-06-08 16:33:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-01 13:55:30 +02:00
|
|
|
|
// foreach (var activeBulletsList in _activeBullets)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// activeBulletsList.Value.Clear();
|
|
|
|
|
|
// }
|
2025-06-08 16:33:38 +02:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-01 13:55:30 +02:00
|
|
|
|
private Stack<IBullet> GetOrCreateList(System.Collections.Generic.Dictionary<BulletResource, Stack<IBullet>> dict, BulletResource resource)
|
2025-06-08 16:33:38 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (dict.TryGetValue(resource, out var list)) return list;
|
|
|
|
|
|
list = [];
|
|
|
|
|
|
dict[resource] = list;
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-17 11:57:59 +02:00
|
|
|
|
private IBullet InstantiateBullet(BulletResource bulletData)
|
2025-06-08 16:33:38 +02:00
|
|
|
|
{
|
2025-06-18 18:09:30 +02:00
|
|
|
|
var bullet = bulletData.BulletScene.Instantiate<Node>();
|
2025-06-17 11:57:59 +02:00
|
|
|
|
this.AddChild(bullet);
|
|
|
|
|
|
//this.CreateChild<Bullet>(bulletData.BulletScene);
|
2025-06-18 18:09:30 +02:00
|
|
|
|
return bullet as IBullet;
|
2025-06-08 16:33:38 +02:00
|
|
|
|
}
|
2025-06-30 17:28:19 +02:00
|
|
|
|
|
|
|
|
|
|
public void ClearBullets()
|
|
|
|
|
|
{
|
|
|
|
|
|
// TODO: Implement
|
|
|
|
|
|
}
|
2025-06-08 16:33:38 +02:00
|
|
|
|
}
|