Second commit

This commit is contained in:
ThomasCharester committed 2026-09-20 15:26:03 +03:00
commit 36840f3ad4
999 files changed
+61364

No files matched your search

@@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using Reflex.Attributes;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Pool;
using Object = UnityEngine.Object;
namespace Code.Infrastructure.Particles
{
public class ParticlePool
{
public IObjectPool<ParticleSystem> Pool;
private ParticleInfo _particleInfoConfig;
private ParticleSystem _particleSystemPrefab;
public ParticlePool(ParticleInfo particleInfoConfig, ParticleSystem particleSystemPrefab)
{
_particleSystemPrefab = particleSystemPrefab;
_particleInfoConfig = particleInfoConfig;
Pool = new ObjectPool<ParticleSystem>(
createFunc: CreateItem,
// actionOnGet: OnGet,
actionOnRelease: OnRelease,
actionOnDestroy: OnDestroyItem,
collectionCheck: true, // helps catch double-release mistakes
defaultCapacity: particleInfoConfig.CountInPool,
maxSize: 20
);
}
public ParticleSystem Get() => Pool.Get();
public void Release(ParticleSystem particleSystem) => Pool.Release(particleSystem);
// Creates a new pooled GameObject the first time (and whenever the pool needs more).
private ParticleSystem CreateItem()
{
var particleSystem = Object
.Instantiate(_particleSystemPrefab) // Он уже загружен
.GetComponent<ParticleSystem>();
particleSystem.name = "PooledParticleSystem";
Object.DontDestroyOnLoad(particleSystem);
return particleSystem;
}
// Called when an item is taken from the pool.
private void OnGet(ParticleSystem source)
{
// Настроить если надо
}
// Called when an item is returned to the pool.
private void OnRelease(ParticleSystem particleSystem)
{
if (!particleSystem) return;
particleSystem.Stop();
// Настроить если надо
}
// Called when the pool decides to destroy an item (e.g., above max size).
private void OnDestroyItem(ParticleSystem source)
{
Object.Destroy(source.gameObject);
}
}
public interface IParticlePoolManagerForService
{
ParticleSystem GetParticleSystem(ParticlePartition partition, int id);
void ReleaseParticleSystem(ParticlePartition partition, int id, ParticleSystem particleSystem);
}
public interface IParticlePoolManagerForFabric
{
void CreatePoolOfParticles(ParticleInfo particleInfoConfig, ParticleSystem particleSystemPrefab);
void DeletePoolOfParticles(ParticlePartition partition, int id);
}
public class ParticlePoolManager : IParticlePoolManagerForService, IParticlePoolManagerForFabric
{
// The pool holds plain GameObjects (you can swap this for any component type).
private Dictionary<ParticlePartition, Dictionary<int, ParticlePool>> _poolDictionary = new();
public void CreatePoolOfParticles(ParticleInfo particleInfoConfig, ParticleSystem particleSystemPrefab)
{
if(!_poolDictionary.ContainsKey(particleInfoConfig.particlePartition))
_poolDictionary[particleInfoConfig.particlePartition] = new();
_poolDictionary[particleInfoConfig.particlePartition]
.Add(particleInfoConfig.particleId, new ParticlePool(particleInfoConfig, particleSystemPrefab));
}
public void DeletePoolOfParticles(ParticlePartition partition, int id)
{
_poolDictionary[partition][id].Pool.Clear();
_poolDictionary[partition].Remove(id);
}
public ParticleSystem GetParticleSystem(ParticlePartition partition, int id)
{
var source = _poolDictionary[partition][id].Get();
// Настроить если надо
return source;
}
public void ReleaseParticleSystem(ParticlePartition partition, int id, ParticleSystem particleSystem)
{
if(_poolDictionary[partition].ContainsKey(id)) _poolDictionary[partition][id].Release(particleSystem);
}
}
}