ECSComponent.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /** @format */
  2. import {ComType, EntityIndex} from './Const'
  3. /** 构造函数 */
  4. export interface ECSComConstructor extends Function {
  5. new (): any
  6. }
  7. export interface ECSTypedComConstructor<T> extends ECSComConstructor {
  8. new (): T
  9. }
  10. /** 通过type存取 构造函数 */
  11. const ComConsMap: {[key: number]: ECSComConstructor} = cc.js.createMap()
  12. function RegistComConstructor(comType: ComType, func: ECSComConstructor) {
  13. ComConsMap[comType] = func
  14. }
  15. export function GetComConstructor(comType: ComType) {
  16. return ComConsMap[comType]
  17. }
  18. /** 通过构造函数存取 type */
  19. function SetComConstructorType(comCons: ECSComConstructor, type: ComType) {
  20. comCons['__type__'] = type
  21. }
  22. export function GetComConstructorType<T>(comCons: {prototype: T}): ComType {
  23. return comCons['__type__']
  24. }
  25. /** ECSComponent */
  26. export function ECSComponent(type: ComType) {
  27. return function (func: ECSComConstructor) {
  28. SetComConstructorType(func, type)
  29. RegistComConstructor(type, func)
  30. }
  31. }
  32. export function GenFilterKey(accepts: ECSComConstructor[], rejects?: ECSComConstructor[]) {
  33. let acceptTypes: ComType[] = []
  34. let rejectTypes: ComType[] = []
  35. if (accepts && accepts.length > 0) {
  36. for (let i = 0; i < accepts.length; i++) {
  37. acceptTypes[i] = GetComConstructorType(accepts[i])
  38. }
  39. }
  40. if (rejects && rejects.length > 0) {
  41. for (let i = 0; i < rejects.length; i++) {
  42. rejectTypes[i] = GetComConstructorType(rejects[i])
  43. }
  44. }
  45. if (acceptTypes.length < 0) {
  46. console.error(`[ECSWorld]: GenFilterKey 必须要有accpters`)
  47. return ''
  48. }
  49. acceptTypes.sort()
  50. rejectTypes.sort()
  51. let key = Array.prototype.join.call(acceptTypes, ',')
  52. if (!rejectTypes || rejectTypes.length <= 0) return key
  53. key += '-'
  54. key += Array.prototype.join.call(rejectTypes, ',')
  55. return key
  56. }