mirror of
https://github.com/kirikayakazuto/CocosCreator_ECS
synced 2024-12-27 12:08:43 +00:00
35 lines
1.1 KiB
TypeScript
Executable File
35 lines
1.1 KiB
TypeScript
Executable File
import { ComPoolIndex } from "./Const";
|
|
import { ECSTypedComConstructor } from "./ECSComponent";
|
|
|
|
/**
|
|
* 组件池
|
|
*/
|
|
export class ECSComponentPool<T> {
|
|
private _componentConstructor: ECSTypedComConstructor<T>;
|
|
public constructor(comCons: ECSTypedComConstructor<T>) {
|
|
this._componentConstructor = comCons;
|
|
}
|
|
|
|
private _components: T[] = []; // components
|
|
private _reservedIdxs: ComPoolIndex[] = []; // 缓存的component idx
|
|
|
|
|
|
public get(idx: ComPoolIndex): T {
|
|
return this._components[idx];
|
|
}
|
|
|
|
public alloc(): ComPoolIndex {
|
|
if(this._reservedIdxs.length > 0) {
|
|
let ret = this._reservedIdxs.pop();
|
|
this._componentConstructor.apply(this._components[ret]); // 重置对象
|
|
return ret;
|
|
}
|
|
let newInstance = new this._componentConstructor();
|
|
this._components.push(newInstance);
|
|
return this._components.length - 1;
|
|
}
|
|
|
|
public free(idx: ComPoolIndex) {
|
|
this._reservedIdxs.push(idx);
|
|
}
|
|
} |