kunpolibrary/src/ecmodule/ComponentPool.ts

99 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-02-20 11:27:28 +08:00
import { Component } from "./Component";
import { ObjectBase } from "./ObjectBase";
import { ObjectFactory } from "./ObjectFactory";
export class ComponentPool {
2025-03-07 16:02:00 +08:00
/** 组件对象类型到组件类型转换 @internal */
2025-02-20 11:27:28 +08:00
private readonly _objectTypeToComponentType: number[] = new Array<number>(128);
2025-03-07 16:02:00 +08:00
/** 组件池 @internal */
2025-02-20 11:27:28 +08:00
private _pools: Map<number, ObjectFactory> = new Map();
2025-03-07 16:02:00 +08:00
/** 组件名称到组件对象类型转换 @internal */
2025-02-20 11:27:28 +08:00
private _nameToObjectType: Map<string, number> = new Map();
/**
*
* @param {number} componentObjectType
* @param {number} componentType
* @param {string} name
* @param {new () => Component} ctor
2025-03-07 16:02:00 +08:00
* @internal
2025-02-20 11:27:28 +08:00
*/
public register(componentObjectType: number, componentType: number, name: string, ctor: new () => ObjectBase): void {
if (this._pools.has(componentObjectType)) {
throw new Error(`组件(${name})已注册, 不允许重复注册`);
}
this._pools.set(componentObjectType, new ObjectFactory(componentObjectType, 128, name, ctor));
this._nameToObjectType.set(name, componentObjectType);
const objectTypeToComponentType = this._objectTypeToComponentType;
for (let i = objectTypeToComponentType.length; i <= componentObjectType; ++i) {
objectTypeToComponentType.push(i);
}
objectTypeToComponentType[componentObjectType] = componentType;
}
2025-03-07 16:02:00 +08:00
/**
*
* @param {string} componentName
* @returns {number}
*/
2025-02-20 11:27:28 +08:00
public getObjectTypeByName(componentName: string): number {
return this._nameToObjectType.get(componentName);
}
/**
*
* @param {number} componentName
* @returns {T}
2025-03-07 16:02:00 +08:00
* @template T
* @internal
2025-02-20 11:27:28 +08:00
*/
public get<T extends Component>(componentName: string): T {
let objectType = this.getObjectTypeByName(componentName);
const factory = this._pools.get(objectType);
if (!factory) {
throw new Error(`组件(${componentName})未注册,使用组件装饰器 ecclass 注册组件`);
}
const component = factory.allocate() as T;
component.name = factory.name;
component.type = this._objectTypeToComponentType[objectType];
return component;
}
/**
*
* @param {number} componentObjectType
* @returns {string}
2025-03-07 16:02:00 +08:00
* @internal
2025-02-20 11:27:28 +08:00
*/
public className(componentObjectType: number): string {
const factory = this._pools.get(componentObjectType);
if (!factory) {
throw new Error(
`组件(${componentObjectType}没有注册使用ComponentPool.register(componentObjectType, componentType, componentClass)注册组件`
);
}
return factory.name;
}
/**
*
* @param {BaseComponent} component
* @memberof ComponentPool
2025-03-07 16:02:00 +08:00
* @internal
2025-02-20 11:27:28 +08:00
*/
public recycle(component: Component): void {
const objectFactory = this._pools.get(component.objectType);
objectFactory.recycle(component);
}
2025-03-07 16:02:00 +08:00
/**
*
* @internal
*/
2025-02-20 11:27:28 +08:00
public clear(): void {
for (const factory of this._pools.values()) {
factory._clear();
}
}
}