bf7c971ec2733770901265830d07850f969a7c31.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, ECSMask, ECSModel, ECSMatcher, BaseOf, AnyOf, AllOf, ExcludeOf, _crd, macherId;
  4. function _reportPossibleCrUseOfecs(extras) {
  5. _reporterNs.report("ecs", "./ECS", _context.meta, extras);
  6. }
  7. function _reportPossibleCrUseOfECSEntity(extras) {
  8. _reporterNs.report("ECSEntity", "./ECSEntity", _context.meta, extras);
  9. }
  10. function _reportPossibleCrUseOfECSMask(extras) {
  11. _reporterNs.report("ECSMask", "./ECSMask", _context.meta, extras);
  12. }
  13. function _reportPossibleCrUseOfCompCtor(extras) {
  14. _reporterNs.report("CompCtor", "./ECSModel", _context.meta, extras);
  15. }
  16. function _reportPossibleCrUseOfCompType(extras) {
  17. _reporterNs.report("CompType", "./ECSModel", _context.meta, extras);
  18. }
  19. function _reportPossibleCrUseOfECSModel(extras) {
  20. _reporterNs.report("ECSModel", "./ECSModel", _context.meta, extras);
  21. }
  22. _export("ECSMatcher", void 0);
  23. return {
  24. setters: [function (_unresolved_) {
  25. _reporterNs = _unresolved_;
  26. }, function (_cc) {
  27. _cclegacy = _cc.cclegacy;
  28. }, function (_unresolved_2) {
  29. ECSMask = _unresolved_2.ECSMask;
  30. }, function (_unresolved_3) {
  31. ECSModel = _unresolved_3.ECSModel;
  32. }],
  33. execute: function () {
  34. _crd = true;
  35. _cclegacy._RF.push({}, "37e8arlqPlN7amZYyHFvBIp", "ECSMatcher", undefined);
  36. macherId = 1;
  37. /**
  38. * 筛选规则间是“与”的关系
  39. * 比如:ecs.Macher.allOf(...).excludeOf(...)表达的是allOf && excludeOf,即实体有“这些组件” 并且 “没有这些组件”
  40. */
  41. _export("ECSMatcher", ECSMatcher = class ECSMatcher {
  42. get key() {
  43. if (!this._key) {
  44. let s = '';
  45. for (let i = 0; i < this.rules.length; i++) {
  46. s += this.rules[i].getKey();
  47. if (i < this.rules.length - 1) {
  48. s += ' && ';
  49. }
  50. }
  51. this._key = s;
  52. }
  53. return this._key;
  54. }
  55. constructor() {
  56. this.rules = [];
  57. this._indices = null;
  58. this.isMatch = void 0;
  59. this.mid = -1;
  60. this._key = null;
  61. this.mid = macherId++;
  62. }
  63. /**
  64. * 匹配器关注的组件索引。在创建Group时,Context根据组件id去给Group关联组件的添加和移除事件。
  65. */
  66. get indices() {
  67. if (this._indices === null) {
  68. this._indices = [];
  69. this.rules.forEach(rule => {
  70. Array.prototype.push.apply(this._indices, rule.indices);
  71. });
  72. }
  73. return this._indices;
  74. }
  75. /**
  76. * 组件间是或的关系,表示关注拥有任意一个这些组件的实体。
  77. * @param args 组件索引
  78. */
  79. anyOf(...args) {
  80. this.rules.push(new AnyOf(...args));
  81. this.bindMatchMethod();
  82. return this;
  83. }
  84. /**
  85. * 组件间是与的关系,表示关注拥有所有这些组件的实体。
  86. * @param args 组件索引
  87. */
  88. allOf(...args) {
  89. this.rules.push(new AllOf(...args));
  90. this.bindMatchMethod();
  91. return this;
  92. }
  93. /**
  94. * 表示关注只拥有这些组件的实体
  95. *
  96. * 注意:
  97. * 不是特殊情况不建议使用onlyOf。因为onlyOf会监听所有组件的添加和删除事件。
  98. * @param args 组件索引
  99. */
  100. onlyOf(...args) {
  101. this.rules.push(new AllOf(...args));
  102. let otherTids = [];
  103. for (let ctor of (_crd && ECSModel === void 0 ? (_reportPossibleCrUseOfECSModel({
  104. error: Error()
  105. }), ECSModel) : ECSModel).compCtors) {
  106. if (args.indexOf(ctor) < 0) {
  107. otherTids.push(ctor);
  108. }
  109. }
  110. this.rules.push(new ExcludeOf(...otherTids));
  111. this.bindMatchMethod();
  112. return this;
  113. }
  114. /**
  115. * 不包含指定的任意一个组件
  116. * @param args
  117. */
  118. excludeOf(...args) {
  119. this.rules.push(new ExcludeOf(...args));
  120. this.bindMatchMethod();
  121. return this;
  122. }
  123. bindMatchMethod() {
  124. if (this.rules.length === 1) {
  125. this.isMatch = this.isMatch1;
  126. } else if (this.rules.length === 2) {
  127. this.isMatch = this.isMatch2;
  128. } else {
  129. this.isMatch = this.isMatchMore;
  130. }
  131. }
  132. isMatch1(entity) {
  133. return this.rules[0].isMatch(entity);
  134. }
  135. isMatch2(entity) {
  136. return this.rules[0].isMatch(entity) && this.rules[1].isMatch(entity);
  137. }
  138. isMatchMore(entity) {
  139. for (let rule of this.rules) {
  140. if (!rule.isMatch(entity)) {
  141. return false;
  142. }
  143. }
  144. return true;
  145. }
  146. clone() {
  147. let newMatcher = new ECSMatcher();
  148. newMatcher.mid = macherId++;
  149. this.rules.forEach(rule => newMatcher.rules.push(rule));
  150. return newMatcher;
  151. }
  152. });
  153. BaseOf = class BaseOf {
  154. constructor(...args) {
  155. this.indices = [];
  156. this.mask = new (_crd && ECSMask === void 0 ? (_reportPossibleCrUseOfECSMask({
  157. error: Error()
  158. }), ECSMask) : ECSMask)();
  159. let componentTypeId = -1;
  160. let len = args.length;
  161. for (let i = 0; i < len; i++) {
  162. if (typeof args[i] === "number") {
  163. componentTypeId = args[i];
  164. } else {
  165. componentTypeId = args[i].tid;
  166. }
  167. if (componentTypeId == -1) {
  168. throw Error('存在没有注册的组件!');
  169. }
  170. this.mask.set(componentTypeId);
  171. if (this.indices.indexOf(componentTypeId) < 0) {
  172. // 去重
  173. this.indices.push(componentTypeId);
  174. }
  175. }
  176. if (len > 1) {
  177. this.indices.sort((a, b) => {
  178. return a - b;
  179. }); // 对组件类型id进行排序,这样关注相同组件的系统就能共用同一个group
  180. }
  181. }
  182. toString() {
  183. return this.indices.join('-'); // 生成group的key
  184. }
  185. };
  186. /**
  187. * 用于描述包含任意一个这些组件的实体
  188. */
  189. AnyOf = class AnyOf extends BaseOf {
  190. isMatch(entity) {
  191. // @ts-ignore
  192. return this.mask.or(entity.mask);
  193. }
  194. getKey() {
  195. return 'anyOf:' + this.toString();
  196. }
  197. };
  198. /**
  199. * 用于描述包含了“这些”组件的实体,这个实体除了包含这些组件还可以包含其他组件
  200. */
  201. AllOf = class AllOf extends BaseOf {
  202. isMatch(entity) {
  203. // @ts-ignore
  204. return this.mask.and(entity.mask);
  205. }
  206. getKey() {
  207. return 'allOf:' + this.toString();
  208. }
  209. };
  210. /**
  211. * 不包含指定的任意一个组件
  212. */
  213. ExcludeOf = class ExcludeOf extends BaseOf {
  214. getKey() {
  215. return 'excludeOf:' + this.toString();
  216. }
  217. isMatch(entity) {
  218. // @ts-ignore
  219. return !this.mask.or(entity.mask);
  220. }
  221. };
  222. _cclegacy._RF.pop();
  223. _crd = false;
  224. }
  225. };
  226. });
  227. //# sourceMappingURL=bf7c971ec2733770901265830d07850f969a7c31.js.map