Newer
Older
lostmynuts / shared / js / Engine / Utils / ObjectPool.js
Engine.ObjectPool = class
{
	constructor(objectClass)
	{
		this.objectClass = objectClass;
		this.pool = [];
		this.active = [];
		this.haveMessaged = false;
	}

	getNextObject(constructorParam)
	{
		var obj = null;
		if (this.pool.length > 0)
		{
			obj = this.pool[0];
			this.pool.splice(0, 1);
		} else {
			if (constructorParam !== undefined)
			{
			 	obj = new this.objectClass(constructorParam);
			} else {
				obj = new this.objectClass();
			}
		}

		this.active.push(obj);
		if (this.activeCount > 105 && !this.haveMessaged)
		{
			Debug.Log(this.objectClass.constructor.name + " has over 105 active instances!!!");
			this.haveMessaged = true;
		}
		return obj;
	}

	returnObject(obj)
	{
		if (obj !== undefined && obj !== null && !this.pool.contains(obj))
		{
			this.active.remove(obj);
			this.pool.push(obj);
		}
	}
}