新增poolcomponent 对象池组件
This commit is contained in:
22
source/src/ECS/Components/ComponentPool.ts
Normal file
22
source/src/ECS/Components/ComponentPool.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
class ComponentPool<T extends PooledComponent>{
|
||||
private _cache: T[];
|
||||
private _type: any;
|
||||
|
||||
constructor(typeClass: any){
|
||||
this._type = typeClass;
|
||||
this._cache = [];
|
||||
}
|
||||
|
||||
public obtain(): T{
|
||||
try {
|
||||
return this._cache.length > 0 ? this._cache.shift() : new this._type();
|
||||
} catch(err){
|
||||
throw new Error(this._type + err);
|
||||
}
|
||||
}
|
||||
|
||||
public free(component: T){
|
||||
component.reset();
|
||||
this._cache.push(component);
|
||||
}
|
||||
}
|
||||
4
source/src/ECS/Components/PooledComponent.ts
Normal file
4
source/src/ECS/Components/PooledComponent.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
/** 回收实例的组件类型。 */
|
||||
abstract class PooledComponent extends Component {
|
||||
public abstract reset();
|
||||
}
|
||||
Reference in New Issue
Block a user