bf7c971ec2733770901265830d07850f969a7c31.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. var s = '';
  45. for (var 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() {
  80. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  81. args[_key] = arguments[_key];
  82. }
  83. this.rules.push(new AnyOf(...args));
  84. this.bindMatchMethod();
  85. return this;
  86. }
  87. /**
  88. * 组件间是与的关系,表示关注拥有所有这些组件的实体。
  89. * @param args 组件索引
  90. */
  91. allOf() {
  92. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  93. args[_key2] = arguments[_key2];
  94. }
  95. this.rules.push(new AllOf(...args));
  96. this.bindMatchMethod();
  97. return this;
  98. }
  99. /**
  100. * 表示关注只拥有这些组件的实体
  101. *
  102. * 注意:
  103. * 不是特殊情况不建议使用onlyOf。因为onlyOf会监听所有组件的添加和删除事件。
  104. * @param args 组件索引
  105. */
  106. onlyOf() {
  107. for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  108. args[_key3] = arguments[_key3];
  109. }
  110. this.rules.push(new AllOf(...args));
  111. var otherTids = [];
  112. for (var ctor of (_crd && ECSModel === void 0 ? (_reportPossibleCrUseOfECSModel({
  113. error: Error()
  114. }), ECSModel) : ECSModel).compCtors) {
  115. if (args.indexOf(ctor) < 0) {
  116. otherTids.push(ctor);
  117. }
  118. }
  119. this.rules.push(new ExcludeOf(...otherTids));
  120. this.bindMatchMethod();
  121. return this;
  122. }
  123. /**
  124. * 不包含指定的任意一个组件
  125. * @param args
  126. */
  127. excludeOf() {
  128. for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
  129. args[_key4] = arguments[_key4];
  130. }
  131. this.rules.push(new ExcludeOf(...args));
  132. this.bindMatchMethod();
  133. return this;
  134. }
  135. bindMatchMethod() {
  136. if (this.rules.length === 1) {
  137. this.isMatch = this.isMatch1;
  138. } else if (this.rules.length === 2) {
  139. this.isMatch = this.isMatch2;
  140. } else {
  141. this.isMatch = this.isMatchMore;
  142. }
  143. }
  144. isMatch1(entity) {
  145. return this.rules[0].isMatch(entity);
  146. }
  147. isMatch2(entity) {
  148. return this.rules[0].isMatch(entity) && this.rules[1].isMatch(entity);
  149. }
  150. isMatchMore(entity) {
  151. for (var rule of this.rules) {
  152. if (!rule.isMatch(entity)) {
  153. return false;
  154. }
  155. }
  156. return true;
  157. }
  158. clone() {
  159. var newMatcher = new ECSMatcher();
  160. newMatcher.mid = macherId++;
  161. this.rules.forEach(rule => newMatcher.rules.push(rule));
  162. return newMatcher;
  163. }
  164. });
  165. BaseOf = class BaseOf {
  166. constructor() {
  167. this.indices = [];
  168. this.mask = new (_crd && ECSMask === void 0 ? (_reportPossibleCrUseOfECSMask({
  169. error: Error()
  170. }), ECSMask) : ECSMask)();
  171. var componentTypeId = -1;
  172. for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
  173. args[_key5] = arguments[_key5];
  174. }
  175. var len = args.length;
  176. for (var i = 0; i < len; i++) {
  177. if (typeof args[i] === "number") {
  178. componentTypeId = args[i];
  179. } else {
  180. componentTypeId = args[i].tid;
  181. }
  182. if (componentTypeId == -1) {
  183. throw Error('存在没有注册的组件!');
  184. }
  185. this.mask.set(componentTypeId);
  186. if (this.indices.indexOf(componentTypeId) < 0) {
  187. // 去重
  188. this.indices.push(componentTypeId);
  189. }
  190. }
  191. if (len > 1) {
  192. this.indices.sort((a, b) => {
  193. return a - b;
  194. }); // 对组件类型id进行排序,这样关注相同组件的系统就能共用同一个group
  195. }
  196. }
  197. toString() {
  198. return this.indices.join('-'); // 生成group的key
  199. }
  200. };
  201. /**
  202. * 用于描述包含任意一个这些组件的实体
  203. */
  204. AnyOf = class AnyOf extends BaseOf {
  205. isMatch(entity) {
  206. // @ts-ignore
  207. return this.mask.or(entity.mask);
  208. }
  209. getKey() {
  210. return 'anyOf:' + this.toString();
  211. }
  212. };
  213. /**
  214. * 用于描述包含了“这些”组件的实体,这个实体除了包含这些组件还可以包含其他组件
  215. */
  216. AllOf = class AllOf extends BaseOf {
  217. isMatch(entity) {
  218. // @ts-ignore
  219. return this.mask.and(entity.mask);
  220. }
  221. getKey() {
  222. return 'allOf:' + this.toString();
  223. }
  224. };
  225. /**
  226. * 不包含指定的任意一个组件
  227. */
  228. ExcludeOf = class ExcludeOf extends BaseOf {
  229. getKey() {
  230. return 'excludeOf:' + this.toString();
  231. }
  232. isMatch(entity) {
  233. // @ts-ignore
  234. return !this.mask.or(entity.mask);
  235. }
  236. };
  237. _cclegacy._RF.pop();
  238. _crd = false;
  239. }
  240. };
  241. });
  242. //# sourceMappingURL=bf7c971ec2733770901265830d07850f969a7c31.js.map