mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-02 22:39:22 +00:00
[PropSpawnTweaks] Initial builds
This commit is contained in:
parent
e2e4463663
commit
cafd24bd2f
9 changed files with 402 additions and 0 deletions
45
PropSpawnTweaks/ObjectPool.cs
Normal file
45
PropSpawnTweaks/ObjectPool.cs
Normal file
|
@ -0,0 +1,45 @@
|
|||
namespace NAK.PropSpawnTweaks;
|
||||
|
||||
public class ObjectPool<T>
|
||||
{
|
||||
public int Count { get; private set; }
|
||||
|
||||
private readonly Queue<T> _available;
|
||||
private readonly Func<T> _createObjectFunc;
|
||||
|
||||
public ObjectPool(int numPreallocated = 0, Func<T> createObjectFunc = null)
|
||||
{
|
||||
_available = new Queue<T>();
|
||||
_createObjectFunc = createObjectFunc;
|
||||
|
||||
if (numPreallocated > 0) Give(MakeObject());
|
||||
}
|
||||
|
||||
public T Take()
|
||||
{
|
||||
T t;
|
||||
if (_available.Count > 0)
|
||||
{
|
||||
t = _available.Dequeue();
|
||||
Count--;
|
||||
}
|
||||
else
|
||||
{
|
||||
t = MakeObject();
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
public void Give(T t)
|
||||
{
|
||||
_available.Enqueue(t);
|
||||
Count++;
|
||||
}
|
||||
|
||||
private T MakeObject()
|
||||
{
|
||||
if (_createObjectFunc != null) return _createObjectFunc();
|
||||
throw new InvalidOperationException("Cannot automatically create new objects without a factory method");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue