using System; using System.Collections.Generic; using System.Linq; using ModestTree; using ModestTree.Util; namespace Zenject { public class PoolableManager { readonly List _poolables; bool _isSpawned; public PoolableManager( [InjectLocal] List poolables, [Inject(Optional = true, Source = InjectSources.Local)] List> priorities) { _poolables = poolables.Select(x => CreatePoolableInfo(x, priorities)) .OrderBy(x => x.Priority).Select(x => x.Poolable).ToList(); } PoolableInfo CreatePoolableInfo(IPoolable poolable, List> priorities) { var match = priorities.Where(x => poolable.GetType().DerivesFromOrEqual(x.First)).Select(x => (int?)(x.Second)).SingleOrDefault(); int priority = match.HasValue ? match.Value : 0; return new PoolableInfo(poolable, priority); } public void TriggerOnSpawned() { Assert.That(!_isSpawned); _isSpawned = true; for (int i = 0; i < _poolables.Count; i++) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnSpawned", _poolables[i].GetType())) #endif { _poolables[i].OnSpawned(); } } } public void TriggerOnDespawned() { Assert.That(_isSpawned); _isSpawned = false; // Call OnDespawned in the reverse order just like how dispose works for (int i = _poolables.Count - 1; i >= 0; i--) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnDespawned", _poolables[i].GetType())) #endif { _poolables[i].OnDespawned(); } } } struct PoolableInfo { public IPoolable Poolable; public int Priority; public PoolableInfo(IPoolable poolable, int priority) { Poolable = poolable; Priority = priority; } } } /// /// A modified version of PoolableManager that adds a generic argument, allowing /// the passing of a parameter to all IPoolable objects in the container. /// public class PoolableManager { readonly List> _poolables; bool _isSpawned; public PoolableManager( [InjectLocal] List> poolables, [Inject(Optional = true, Source = InjectSources.Local)] List> priorities) { _poolables = poolables.Select(x => CreatePoolableInfo(x, priorities)) .OrderBy(x => x.Priority).Select(x => x.Poolable).ToList(); } PoolableInfo CreatePoolableInfo(IPoolable poolable, List> priorities) { var match = priorities.Where(x => poolable.GetType().DerivesFromOrEqual(x.First)).Select(x => (int?)(x.Second)).SingleOrDefault(); int priority = match.HasValue ? match.Value : 0; return new PoolableInfo(poolable, priority); } public void TriggerOnSpawned(T param) { Assert.That(!_isSpawned); _isSpawned = true; for (int i = 0; i < _poolables.Count; i++) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnSpawned", _poolables[i].GetType())) #endif { _poolables[i].OnSpawned(param); } } } public void TriggerOnDespawned() { Assert.That(_isSpawned); _isSpawned = false; // Call OnDespawned in the reverse order just like how dispose works for (int i = _poolables.Count - 1; i >= 0; i--) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnDespawned", _poolables[i].GetType())) #endif { _poolables[i].OnDespawned(); } } } struct PoolableInfo { public IPoolable Poolable; public int Priority; public PoolableInfo(IPoolable poolable, int priority) { Poolable = poolable; Priority = priority; } } } /// /// A modified version of PoolableManager that adds a generic argument, allowing /// the passing of a parameter to all IPoolable objects in the container. /// public class PoolableManager { readonly List> _poolables; bool _isSpawned; public PoolableManager( [InjectLocal] List> poolables, [Inject(Optional = true, Source = InjectSources.Local)] List> priorities) { _poolables = poolables.Select(x => CreatePoolableInfo(x, priorities)) .OrderBy(x => x.Priority).Select(x => x.Poolable).ToList(); } PoolableInfo CreatePoolableInfo(IPoolable poolable, List> priorities) { var match = priorities.Where(x => poolable.GetType().DerivesFromOrEqual(x.First)).Select(x => (int?)(x.Second)).SingleOrDefault(); int priority = match.HasValue ? match.Value : 0; return new PoolableInfo(poolable, priority); } public void TriggerOnSpawned(T1 p1, T2 p2) { Assert.That(!_isSpawned); _isSpawned = true; for (int i = 0; i < _poolables.Count; i++) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnSpawned", _poolables[i].GetType())) #endif { _poolables[i].OnSpawned(p1, p2); } } } public void TriggerOnDespawned() { Assert.That(_isSpawned); _isSpawned = false; // Call OnDespawned in the reverse order just like how dispose works for (int i = _poolables.Count - 1; i >= 0; i--) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnDespawned", _poolables[i].GetType())) #endif { _poolables[i].OnDespawned(); } } } struct PoolableInfo { public IPoolable Poolable; public int Priority; public PoolableInfo(IPoolable poolable, int priority) { Poolable = poolable; Priority = priority; } } } /// /// A modified version of PoolableManager that adds a generic argument, allowing /// the passing of a parameter to all IPoolable objects in the container. /// public class PoolableManager { readonly List> _poolables; bool _isSpawned; public PoolableManager( [InjectLocal] List> poolables, [Inject(Optional = true, Source = InjectSources.Local)] List> priorities) { _poolables = poolables.Select(x => CreatePoolableInfo(x, priorities)) .OrderBy(x => x.Priority).Select(x => x.Poolable).ToList(); } PoolableInfo CreatePoolableInfo(IPoolable poolable, List> priorities) { var match = priorities.Where(x => poolable.GetType().DerivesFromOrEqual(x.First)).Select(x => (int?)(x.Second)).SingleOrDefault(); int priority = match.HasValue ? match.Value : 0; return new PoolableInfo(poolable, priority); } public void TriggerOnSpawned(T1 p1, T2 p2, T3 p3) { Assert.That(!_isSpawned); _isSpawned = true; for (int i = 0; i < _poolables.Count; i++) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnSpawned", _poolables[i].GetType())) #endif { _poolables[i].OnSpawned(p1, p2, p3); } } } public void TriggerOnDespawned() { Assert.That(_isSpawned); _isSpawned = false; // Call OnDespawned in the reverse order just like how dispose works for (int i = _poolables.Count - 1; i >= 0; i--) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnDespawned", _poolables[i].GetType())) #endif { _poolables[i].OnDespawned(); } } } struct PoolableInfo { public IPoolable Poolable; public int Priority; public PoolableInfo(IPoolable poolable, int priority) { Poolable = poolable; Priority = priority; } } } /// /// A modified version of PoolableManager that adds a generic argument, allowing /// the passing of a parameter to all IPoolable objects in the container. /// public class PoolableManager { readonly List> _poolables; bool _isSpawned; public PoolableManager( [InjectLocal] List> poolables, [Inject(Optional = true, Source = InjectSources.Local)] List> priorities) { _poolables = poolables.Select(x => CreatePoolableInfo(x, priorities)) .OrderBy(x => x.Priority).Select(x => x.Poolable).ToList(); } PoolableInfo CreatePoolableInfo(IPoolable poolable, List> priorities) { var match = priorities.Where(x => poolable.GetType().DerivesFromOrEqual(x.First)).Select(x => (int?)(x.Second)).SingleOrDefault(); int priority = match.HasValue ? match.Value : 0; return new PoolableInfo(poolable, priority); } public void TriggerOnSpawned(T1 p1, T2 p2, T3 p3, T4 p4) { Assert.That(!_isSpawned); _isSpawned = true; for (int i = 0; i < _poolables.Count; i++) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnSpawned", _poolables[i].GetType())) #endif { _poolables[i].OnSpawned(p1, p2, p3, p4); } } } public void TriggerOnDespawned() { Assert.That(_isSpawned); _isSpawned = false; // Call OnDespawned in the reverse order just like how dispose works for (int i = _poolables.Count - 1; i >= 0; i--) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnDespawned", _poolables[i].GetType())) #endif { _poolables[i].OnDespawned(); } } } struct PoolableInfo { public IPoolable Poolable; public int Priority; public PoolableInfo(IPoolable poolable, int priority) { Poolable = poolable; Priority = priority; } } } /// /// A modified version of PoolableManager that adds a generic argument, allowing /// the passing of a parameter to all IPoolable objects in the container. /// public class PoolableManager { readonly List> _poolables; bool _isSpawned; public PoolableManager( [InjectLocal] List> poolables, [Inject(Optional = true, Source = InjectSources.Local)] List> priorities) { _poolables = poolables.Select(x => CreatePoolableInfo(x, priorities)) .OrderBy(x => x.Priority).Select(x => x.Poolable).ToList(); } PoolableInfo CreatePoolableInfo(IPoolable poolable, List> priorities) { var match = priorities.Where(x => poolable.GetType().DerivesFromOrEqual(x.First)).Select(x => (int?)(x.Second)).SingleOrDefault(); int priority = match.HasValue ? match.Value : 0; return new PoolableInfo(poolable, priority); } public void TriggerOnSpawned(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5) { Assert.That(!_isSpawned); _isSpawned = true; for (int i = 0; i < _poolables.Count; i++) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnSpawned", _poolables[i].GetType())) #endif { _poolables[i].OnSpawned(p1, p2, p3, p4, p5); } } } public void TriggerOnDespawned() { Assert.That(_isSpawned); _isSpawned = false; // Call OnDespawned in the reverse order just like how dispose works for (int i = _poolables.Count - 1; i >= 0; i--) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif #if UNITY_EDITOR using (ProfileBlock.Start("{0}.OnDespawned", _poolables[i].GetType())) #endif { _poolables[i].OnDespawned(); } } } struct PoolableInfo { public IPoolable Poolable; public int Priority; public PoolableInfo(IPoolable poolable, int priority) { Poolable = poolable; Priority = priority; } } } }