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

102 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-03-06 07:10:31 -08:00
import { Component, Node, Prefab, director, instantiate } from 'cc';
import IPoolable from './IPoolable';
2024-03-06 01:28:01 -08:00
2024-03-06 03:09:17 -08:00
export default class ObjectPool {
2024-03-06 01:28:01 -08:00
private _inactives: Node[] = [];
private _actives: Node[] = [];
private _prefab: Prefab;
private _expandable;
2024-03-06 07:10:31 -08:00
private _poolHandlerComp: new () => any;
2024-03-06 01:28:01 -08:00
public get countInactive() {
return this._inactives.length;
}
public get countActive() {
return this._actives.length;
}
public get countAll() {
return this.countInactive + this.countActive;
}
2024-03-06 07:10:31 -08:00
constructor(prefab: Prefab, size: number, expandable = true, poolHandlerComp?: new () => any | string) {
2024-03-06 01:28:01 -08:00
this._prefab = prefab;
2024-03-06 07:10:31 -08:00
this._expandable = expandable;
this._poolHandlerComp = poolHandlerComp;
2024-03-06 01:28:01 -08:00
for (let i = 0; i < size; ++i) {
let obj = instantiate(this._prefab); // create node instance
2024-03-06 07:10:31 -08:00
obj.removeFromParent();
this._inactives.push(obj);
2024-03-07 09:45:13 -08:00
ObjectPool._poolLookUp[obj.uuid] = this;
2024-03-06 01:28:01 -08:00
}
}
2024-03-07 09:45:13 -08:00
//#region Static
private static _poolLookUp: { [key: string]: ObjectPool } = {};
public static release(obj: Node) {
ObjectPool._poolLookUp[obj.uuid].release(obj);
}
//#endregion
2024-03-06 03:09:17 -08:00
public get(parent?: Node): Node;
2024-03-06 07:10:31 -08:00
public get<T extends Component>(parent?: Node, classConstructor?: new () => T): T;
public get<T extends Component>(parent?: Node, classConstructor?: new () => T): T | Node {
2024-03-06 01:28:01 -08:00
let obj: Node = null;
let p = parent || director.getScene();
if (this._inactives.length > 0) {
2024-03-06 07:10:31 -08:00
// Pop the last object in pool
2024-03-06 01:28:01 -08:00
obj = this._inactives.pop();
} else if (this._expandable) {
// if not enough node in the pool, we call cc.instantiate to create node
obj = instantiate(this._prefab);
2024-03-07 09:45:13 -08:00
ObjectPool._poolLookUp[obj.uuid] = this;
2024-03-06 01:28:01 -08:00
} else {
2024-03-06 03:09:17 -08:00
obj = this._actives.shift();
2024-03-06 01:28:01 -08:00
}
2024-03-06 07:10:31 -08:00
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)?.reuse();
}
2024-03-06 03:09:17 -08:00
if (classConstructor) {
2024-03-06 07:10:31 -08:00
return handler == classConstructor ? handler : obj.getComponent(classConstructor);
2024-03-06 03:09:17 -08:00
}
return obj;
2024-03-06 01:28:01 -08:00
}
2024-03-06 07:10:31 -08:00
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._inactives.push(node);
// Invoke pool handler
const handler = this._poolHandlerComp ? node.getComponent(this._poolHandlerComp) : null;
if (handler) {
(handler as unknown as IPoolable)?.unuse();
2024-03-06 03:09:17 -08:00
}
2024-03-06 07:10:31 -08:00
// Remove from parent, but don't cleanup
node.removeFromParent();
2024-03-06 01:28:01 -08:00
}
public clear() {
this._inactives.forEach((obj) => obj.destroy());
this._inactives = [];
this._actives = [];
2024-03-07 09:45:13 -08:00
Object.keys(ObjectPool._poolLookUp).forEach((key) => {
if (ObjectPool._poolLookUp[key] === this) {
delete ObjectPool._poolLookUp[key];
}
});
2024-03-06 01:28:01 -08:00
}
}