/** @format */ import {ComType, EntityIndex} from './Const' /** 构造函数 */ export interface ECSComConstructor extends Function { new (): any } export interface ECSTypedComConstructor extends ECSComConstructor { new (): T } /** 通过type存取 构造函数 */ const ComConsMap: {[key: number]: ECSComConstructor} = cc.js.createMap() function RegistComConstructor(comType: ComType, func: ECSComConstructor) { ComConsMap[comType] = func } export function GetComConstructor(comType: ComType) { return ComConsMap[comType] } /** 通过构造函数存取 type */ function SetComConstructorType(comCons: ECSComConstructor, type: ComType) { comCons['__type__'] = type } export function GetComConstructorType(comCons: {prototype: T}): ComType { return comCons['__type__'] } /** ECSComponent */ export function ECSComponent(type: ComType) { return function (func: ECSComConstructor) { SetComConstructorType(func, type) RegistComConstructor(type, func) } } export function GenFilterKey(accepts: ECSComConstructor[], rejects?: ECSComConstructor[]) { let acceptTypes: ComType[] = [] let rejectTypes: ComType[] = [] if (accepts && accepts.length > 0) { for (let i = 0; i < accepts.length; i++) { acceptTypes[i] = GetComConstructorType(accepts[i]) } } if (rejects && rejects.length > 0) { for (let i = 0; i < rejects.length; i++) { rejectTypes[i] = GetComConstructorType(rejects[i]) } } if (acceptTypes.length < 0) { console.error(`[ECSWorld]: GenFilterKey 必须要有accpters`) return '' } acceptTypes.sort() rejectTypes.sort() let key = Array.prototype.join.call(acceptTypes, ',') if (!rejectTypes || rejectTypes.length <= 0) return key key += '-' key += Array.prototype.join.call(rejectTypes, ',') return key }