56 lines
2.1 KiB
C#
56 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using Cysharp.Threading.Tasks;
|
|
using Reflex.Attributes;
|
|
using UnityEngine;
|
|
|
|
namespace Code.Infrastructure.Particles
|
|
{
|
|
public interface IParticleService
|
|
{
|
|
UniTaskVoid EmitOneShot(ParticlePartitionIDPair pair, int particleCount, Vector3 position, Quaternion rotation);
|
|
UniTaskVoid EmitTillCancelled(ParticlePartitionIDPair pair, CancellationToken token, Vector3 position, Quaternion rotation);
|
|
}
|
|
|
|
public class ParticleService : IParticleService
|
|
{
|
|
[Inject] private IParticlePoolManagerForService _poolManagerForService;
|
|
[Inject] private IParticleLoader _particleLoader;
|
|
|
|
public async UniTaskVoid EmitOneShot(ParticlePartitionIDPair pair, int particleCount, Vector3 position, Quaternion rotation)
|
|
{
|
|
var particleSystem = _poolManagerForService.GetParticleSystem(pair.ParticlePartition, pair.ID);
|
|
|
|
particleSystem.transform.position = position;
|
|
particleSystem.transform.rotation = rotation;
|
|
|
|
particleSystem.Emit(particleCount);
|
|
|
|
await UniTask.WaitWhile(() => particleSystem.particleCount > 0);
|
|
|
|
_poolManagerForService.ReleaseParticleSystem(pair.ParticlePartition, pair.ID, particleSystem);
|
|
}
|
|
public async UniTaskVoid EmitTillCancelled(ParticlePartitionIDPair pair, CancellationToken token, Vector3 position, Quaternion rotation)
|
|
{
|
|
var particleSystem = _poolManagerForService.GetParticleSystem(pair.ParticlePartition, pair.ID);
|
|
|
|
particleSystem.transform.position = position;
|
|
particleSystem.transform.rotation = rotation;
|
|
|
|
particleSystem.Play();
|
|
try
|
|
{
|
|
await UniTask.WaitUntilCanceled(cancellationToken: token);
|
|
}
|
|
catch
|
|
{
|
|
//
|
|
}
|
|
|
|
particleSystem.Stop();
|
|
|
|
_poolManagerForService.ReleaseParticleSystem(pair.ParticlePartition, pair.ID, particleSystem);
|
|
}
|
|
}
|
|
} |