summaryrefslogtreecommitdiff
path: root/Valheim_r202102_v0.141.2/Valheim/assembly_valheim/ZDOPool.cs
blob: 57c477e4e67f0229c34eca7cd5d2613304e5f947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.Collections.Generic;
using UnityEngine;

public class ZDOPool
{
	private static int BATCH_SIZE = 64;

	private static Stack<ZDO> m_free = new Stack<ZDO>();

	private static int m_active = 0;

	public static ZDO Create(ZDOMan man, ZDOID id, Vector3 position)
	{
		ZDO zDO = Get();
		zDO.Initialize(man, id, position);
		return zDO;
	}

	public static ZDO Create(ZDOMan man)
	{
		ZDO zDO = Get();
		zDO.Initialize(man);
		return zDO;
	}

	public static void Release(Dictionary<ZDOID, ZDO> objects)
	{
		foreach (ZDO value in objects.Values)
		{
			Release(value);
		}
	}

	public static void Release(ZDO zdo)
	{
		zdo.Reset();
		m_free.Push(zdo);
		m_active--;
	}

	private static ZDO Get()
	{
		if (m_free.Count <= 0)
		{
			for (int i = 0; i < BATCH_SIZE; i++)
			{
				ZDO item = new ZDO();
				m_free.Push(item);
			}
		}
		m_active++;
		return m_free.Pop();
	}

	public static int GetPoolSize()
	{
		return m_free.Count;
	}

	public static int GetPoolActive()
	{
		return m_active;
	}

	public static int GetPoolTotal()
	{
		return m_active + m_free.Count;
	}
}