1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- /** @format */
- import {ComType, EntityIndex} from './Const'
- /** 构造函数 */
- export interface ECSComConstructor extends Function {
- new (): any
- }
- export interface ECSTypedComConstructor<T> 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<T>(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
- }
|