pinball/assets/_Game/Scripts/Pool/ObjectPool.ts

130 lines
4.0 KiB
TypeScript

import { Component, Node, Prefab, director, instantiate } from 'cc';
import IPoolable from './IPoolable';
import { error } from 'cc';
export default class ObjectPool {
private _inactive: Node[] = [];
private _actives: Node[] = [];
private _prefab: Prefab;
private _expandable;
private _poolHandlerComp: new () => any;
public get actives() {
return [...this._actives];
}
public get inActives() {
return [...this._inactive];
}
public get all() {
return [...this._actives, ...this._inactive];
}
public get countInactive() {
return this._inactive.length;
}
public get countActive() {
return this._actives.length;
}
public get countAll() {
return this.countInactive + this.countActive;
}
constructor(prefab: Prefab, size: number, expandable = true, poolHandlerComp?: new () => any | string) {
if (!!!prefab) {
console.error('prefab cant be null or undefine');
return;
}
this._prefab = prefab;
this._expandable = expandable;
this._poolHandlerComp = poolHandlerComp;
for (let i = 0; i < size; ++i) {
let obj = instantiate(this._prefab); // create node instance
obj.removeFromParent();
this._inactive.push(obj);
ObjectPool._poolLookUp[obj.uuid] = this;
}
}
//#region Static
private static _poolLookUp: { [key: string]: ObjectPool } = {};
public static release(obj: Node) {
ObjectPool._poolLookUp[obj.uuid].release(obj);
}
//#endregion
public get(parent?: Node): Node;
public get<T extends Component>(parent?: Node, classConstructor?: new () => T): T;
public get<T extends Component>(parent?: Node, classConstructor?: new () => T): T | Node {
if (!!!this._prefab) {
console.error('prefab cant be null or undefine');
return;
}
let obj: Node = null;
let p = parent || director.getScene();
if (this._inactive.length > 0) {
// Pop the last object in pool
obj = this._inactive.pop();
} else if (this._expandable) {
// if not enough node in the pool, we call cc.instantiate to create node
obj = instantiate(this._prefab);
ObjectPool._poolLookUp[obj.uuid] = this;
} else {
obj = this._actives.shift();
obj.removeFromParent();
}
obj.setParent(p);
this._actives.push(obj);
// Invoke pool handler
const handler = this._poolHandlerComp ? obj.getComponent(this._poolHandlerComp) : null;
if (handler) {
(handler as unknown as IPoolable)?.onGet();
}
if (classConstructor) {
return handler == classConstructor ? handler : obj.getComponent(classConstructor);
}
return obj;
}
public release(obj: Node): void;
public release<T extends Component>(obj: T): void;
public release<T extends Component>(obj: T | Node): void {
let node = obj instanceof Node ? obj : obj.node;
const index = this._actives.indexOf(node);
//check obj is belongs to pool
if (index === -1) return;
this._actives.splice(index, 1);
this._inactive.push(node);
// Invoke pool handler
const handler = this._poolHandlerComp ? node.getComponent(this._poolHandlerComp) : null;
if (handler) {
(handler as unknown as IPoolable)?.onRelease();
}
// Remove from parent, but don't cleanup
node.removeFromParent();
}
public clear() {
this.all.forEach((obj) => obj.destroy());
this._inactive = [];
this._actives = [];
Object.keys(ObjectPool._poolLookUp).forEach((key) => {
if (ObjectPool._poolLookUp[key] === this) {
delete ObjectPool._poolLookUp[key];
}
});
}
public releaseAll() {
this.all.forEach((obj) => this.release(obj));
}
}