Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1. public abstract class ObjectPool<T> where T : PoolingObject
  2. {
  3.     protected List<T> objects;
  4.     public List<T> Objects => objects;
  5.     protected Factory<T> factory;
  6.  
  7.     protected void Initialize(int count)
  8.     {
  9.         objects = factory.GetItems(count);
  10.     }
  11.  
  12.     public virtual T GetObject()
  13.     {
  14.         if(objects.Count == 0)
  15.         {
  16.             objects.Add(factory.CreateItem());
  17.         }
  18.  
  19.         T obj = objects[0];
  20.         objects.RemoveAt(0);
  21.         obj.GetFromPool();
  22.         return obj;
  23.     }
  24.  
  25.     public void ReturnObject(T obj)
  26.     {
  27.         objects.Add(obj);
  28.         obj.ReturnToPool();
  29.     }
  30. }
  31.  
  32. public class LineItemPool : ObjectPool<LineItem>
  33. {
  34.     public LineItemPool(VerticalLine parent, LineItem prefab, int count)
  35.     {
  36.         factory = new PlatformsFactory(parent, prefab);
  37.         Initialize(count);
  38.     }
  39.  
  40.     public LineItemParametrs GetNewParametrs()
  41.     {
  42.         return (factory as PlatformsFactory).GetRandomParametrs();
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement