fd61e42a2a66d84c25e6ba44934230624b901ac7.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, ECSMask, ECSModel, ECSEntity, _crd;
  4. //#region 辅助方法
  5. /**
  6. * 实体身上组件有增删操作,广播通知对应的观察者
  7. * @param entity 实体对象
  8. * @param componentTypeId 组件类型id
  9. */
  10. /**
  11. * 创建组件对象
  12. * @param ctor
  13. */
  14. /**
  15. * 销毁实体
  16. *
  17. * 缓存销毁的实体,下次新建实体时会优先从缓存中拿
  18. * @param entity
  19. */
  20. function broadcastCompAddOrRemove(entity, componentTypeId) {
  21. let events = (_crd && ECSModel === void 0 ? (_reportPossibleCrUseOfECSModel({
  22. error: Error()
  23. }), ECSModel) : ECSModel).compAddOrRemove.get(componentTypeId);
  24. for (let i = events.length - 1; i >= 0; i--) {
  25. events[i](entity);
  26. } // 判断是不是删了单例组件
  27. if ((_crd && ECSModel === void 0 ? (_reportPossibleCrUseOfECSModel({
  28. error: Error()
  29. }), ECSModel) : ECSModel).tid2comp.has(componentTypeId)) {
  30. (_crd && ECSModel === void 0 ? (_reportPossibleCrUseOfECSModel({
  31. error: Error()
  32. }), ECSModel) : ECSModel).tid2comp.delete(componentTypeId);
  33. }
  34. }
  35. function createComp(ctor) {
  36. const cct = (_crd && ECSModel === void 0 ? (_reportPossibleCrUseOfECSModel({
  37. error: Error()
  38. }), ECSModel) : ECSModel).compCtors[ctor.tid];
  39. if (!cct) {
  40. throw Error(`没有找到该组件的构造函数,检查${ctor.compName}是否为不可构造的组件`);
  41. }
  42. let comps = (_crd && ECSModel === void 0 ? (_reportPossibleCrUseOfECSModel({
  43. error: Error()
  44. }), ECSModel) : ECSModel).compPools.get(ctor.tid);
  45. let component = comps.pop() || new cct();
  46. return component;
  47. }
  48. function destroyEntity(entity) {
  49. if ((_crd && ECSModel === void 0 ? (_reportPossibleCrUseOfECSModel({
  50. error: Error()
  51. }), ECSModel) : ECSModel).eid2Entity.has(entity.eid)) {
  52. let entitys = (_crd && ECSModel === void 0 ? (_reportPossibleCrUseOfECSModel({
  53. error: Error()
  54. }), ECSModel) : ECSModel).entityPool.get(entity.name);
  55. if (entitys == null) {
  56. entitys = [];
  57. (_crd && ECSModel === void 0 ? (_reportPossibleCrUseOfECSModel({
  58. error: Error()
  59. }), ECSModel) : ECSModel).entityPool.set(entity.name, entitys);
  60. }
  61. entitys.push(entity);
  62. (_crd && ECSModel === void 0 ? (_reportPossibleCrUseOfECSModel({
  63. error: Error()
  64. }), ECSModel) : ECSModel).eid2Entity.delete(entity.eid);
  65. } else {
  66. console.warn('试图销毁不存在的实体');
  67. }
  68. } //#endregion
  69. /** ECS实体对象 */
  70. function _reportPossibleCrUseOfecs(extras) {
  71. _reporterNs.report("ecs", "./ECS", _context.meta, extras);
  72. }
  73. function _reportPossibleCrUseOfECSMask(extras) {
  74. _reporterNs.report("ECSMask", "./ECSMask", _context.meta, extras);
  75. }
  76. function _reportPossibleCrUseOfCompCtor(extras) {
  77. _reporterNs.report("CompCtor", "./ECSModel", _context.meta, extras);
  78. }
  79. function _reportPossibleCrUseOfCompType(extras) {
  80. _reporterNs.report("CompType", "./ECSModel", _context.meta, extras);
  81. }
  82. function _reportPossibleCrUseOfECSModel(extras) {
  83. _reporterNs.report("ECSModel", "./ECSModel", _context.meta, extras);
  84. }
  85. _export("ECSEntity", void 0);
  86. return {
  87. setters: [function (_unresolved_) {
  88. _reporterNs = _unresolved_;
  89. }, function (_cc) {
  90. _cclegacy = _cc.cclegacy;
  91. }, function (_unresolved_2) {
  92. ECSMask = _unresolved_2.ECSMask;
  93. }, function (_unresolved_3) {
  94. ECSModel = _unresolved_3.ECSModel;
  95. }],
  96. execute: function () {
  97. _crd = true;
  98. _cclegacy._RF.push({}, "1fb62WC3PZPvLhjoZQfrREJ", "ECSEntity", undefined);
  99. _export("ECSEntity", ECSEntity = class ECSEntity {
  100. constructor() {
  101. /** 实体唯一标识,不要手动修改 */
  102. this.eid = -1;
  103. /** 实体对象名 */
  104. this.name = "";
  105. /** 实体是否有效 */
  106. this.isValid = true;
  107. /** 组件过滤数据 */
  108. this.mask = new (_crd && ECSMask === void 0 ? (_reportPossibleCrUseOfECSMask({
  109. error: Error()
  110. }), ECSMask) : ECSMask)();
  111. /** 当前实体身上附加的组件构造函数 */
  112. this.compTid2Ctor = new Map();
  113. /** 配合 entity.remove(Comp, false), 记录组件实例上的缓存数据,在添加时恢复原数据 */
  114. this.compTid2Obj = new Map();
  115. this._parent = null;
  116. this._children = null;
  117. }
  118. /** 父实体 */
  119. get parent() {
  120. return this._parent;
  121. }
  122. set parent(value) {
  123. this._parent = value;
  124. }
  125. /** 子实体集合 */
  126. get children() {
  127. if (this._children == null) {
  128. this._children = new Map();
  129. }
  130. return this._children;
  131. }
  132. /**
  133. * 添加子实体
  134. * @param entity 被添加的实体对象
  135. */
  136. addChild(entity) {
  137. entity._parent = this;
  138. this.children.set(entity.eid, entity);
  139. }
  140. /**
  141. * 移除子实体
  142. * @param entity 被移除的实体对象
  143. * @param isDestroy 被移除的实体是否释放,默认为释放
  144. * @returns
  145. */
  146. removeChild(entity, isDestroy = true) {
  147. if (this.children == null) return;
  148. entity.parent = null;
  149. this.children.delete(entity.eid);
  150. if (isDestroy) entity.destroy();
  151. }
  152. /**
  153. * 根据组件类动态创建组件,并通知关心的系统。如果实体存在了这个组件,那么会先删除之前的组件然后添加新的
  154. *
  155. * 注意:不要直接new Component,new来的Component不会从Component的缓存池拿缓存的数据
  156. * @param componentTypeId 组件类
  157. * @param isReAdd true-表示用户指定这个实体可能已经存在了该组件,那么再次add组件的时候会先移除该组件然后再添加一遍。false-表示不重复添加组件
  158. */
  159. add(ctor, isReAdd = false) {
  160. if (typeof ctor === 'function') {
  161. let compTid = ctor.tid;
  162. if (ctor.tid === -1) {
  163. throw Error(`【${this.name}】实体【${ctor.compName}】组件未注册`);
  164. }
  165. if (this.compTid2Ctor.has(compTid)) {
  166. // 判断是否有该组件,如果有则先移除
  167. if (isReAdd) {
  168. this.remove(ctor);
  169. } else {
  170. console.log(`【${this.name}】实体【${ctor.compName}】组件已存在`); // @ts-ignore
  171. return this[ctor.compName];
  172. }
  173. }
  174. this.mask.set(compTid);
  175. let comp;
  176. if (this.compTid2Obj.has(compTid)) {
  177. comp = this.compTid2Obj.get(compTid);
  178. this.compTid2Obj.delete(compTid);
  179. } else {
  180. // 创建组件对象
  181. comp = createComp(ctor);
  182. } // 将组件对象直接附加到实体对象身上,方便直接获取
  183. // @ts-ignore
  184. this[ctor.compName] = comp;
  185. this.compTid2Ctor.set(compTid, ctor);
  186. comp.ent = this; // 广播实体添加组件的消息
  187. broadcastCompAddOrRemove(this, compTid);
  188. return comp;
  189. } else {
  190. let tmpCtor = ctor.constructor;
  191. let compTid = tmpCtor.tid; // console.assert(compTid !== -1 || !compTid, '组件未注册!');
  192. // console.assert(this.compTid2Ctor.has(compTid), '已存在该组件!');
  193. if (compTid === -1 || compTid == null) throw Error(`【${this.name}】实体【${tmpCtor.name}】组件未注册`);
  194. if (this.compTid2Ctor.has(compTid)) throw Error(`【${this.name}】实体【${tmpCtor.name}】组件已经存在`);
  195. this.mask.set(compTid); //@ts-ignore
  196. this[tmpCtor.compName] = ctor;
  197. this.compTid2Ctor.set(compTid, tmpCtor); //@ts-ignore
  198. ctor.ent = this; //@ts-ignore
  199. ctor.canRecycle = false;
  200. broadcastCompAddOrRemove(this, compTid);
  201. return this;
  202. }
  203. }
  204. /**
  205. * 批量添加组件
  206. * @param ctors 组件类
  207. * @returns
  208. */
  209. addComponents(...ctors) {
  210. for (let ctor of ctors) {
  211. this.add(ctor);
  212. }
  213. return this;
  214. }
  215. /**
  216. * 获取一个组件实例
  217. * @param ctor 组件类
  218. */
  219. get(ctor) {
  220. // @ts-ignore
  221. return this[ctor.compName];
  222. }
  223. /**
  224. * 组件是否在实体存在内
  225. * @param ctor 组件类
  226. */
  227. has(ctor) {
  228. if (typeof ctor == "number") {
  229. return this.mask.has(ctor);
  230. } else {
  231. return this.compTid2Ctor.has(ctor.tid);
  232. }
  233. }
  234. /**
  235. * 从实体上删除指定组件
  236. * @param ctor 组件构造函数或者组件Tag
  237. * @param isRecycle 是否回收该组件对象。对于有些组件上有大量数据,当要描述移除组件但是不想清除组件上的数据是可以
  238. * 设置该参数为false,这样该组件对象会缓存在实体身上,下次重新添加组件时会将该组件对象添加回来,不会重新从组件缓存
  239. * 池中拿一个组件来用。
  240. */
  241. remove(ctor, isRecycle = true) {
  242. let hasComp = false; //@ts-ignore
  243. let componentTypeId = ctor.tid; //@ts-ignore
  244. let compName = ctor.compName;
  245. if (this.mask.has(componentTypeId)) {
  246. hasComp = true; //@ts-ignore
  247. let comp = this[ctor.compName]; //@ts-ignore
  248. comp.ent = null;
  249. if (isRecycle) {
  250. comp.reset(); // 回收组件到指定缓存池中
  251. if (comp.canRecycle) {
  252. const compPoolsType = (_crd && ECSModel === void 0 ? (_reportPossibleCrUseOfECSModel({
  253. error: Error()
  254. }), ECSModel) : ECSModel).compPools.get(componentTypeId);
  255. compPoolsType.push(comp);
  256. }
  257. } else {
  258. this.compTid2Obj.set(componentTypeId, comp); // 用于缓存显示对象组件
  259. }
  260. } // 删除实体上的组件逻辑
  261. if (hasComp) {
  262. //@ts-ignore
  263. this[compName] = null;
  264. this.mask.delete(componentTypeId);
  265. this.compTid2Ctor.delete(componentTypeId);
  266. broadcastCompAddOrRemove(this, componentTypeId);
  267. }
  268. }
  269. /** 销毁实体,实体会被回收到实体缓存池中 */
  270. destroy() {
  271. this.isValid = false; // 如果有父模块,则移除父模块上记录的子模块
  272. if (this._parent) {
  273. this._parent.removeChild(this, false);
  274. this._parent = null;
  275. } // 移除模块上所有子模块
  276. if (this._children) {
  277. this._children.forEach(e => {
  278. this.removeChild(e);
  279. });
  280. this._children = null;
  281. } // 移除实体上所有组件
  282. this.compTid2Ctor.forEach(this._remove, this);
  283. destroyEntity(this);
  284. this.compTid2Obj.clear();
  285. }
  286. _remove(comp) {
  287. this.remove(comp, true);
  288. }
  289. });
  290. _cclegacy._RF.pop();
  291. _crd = false;
  292. }
  293. };
  294. });
  295. //# sourceMappingURL=fd61e42a2a66d84c25e6ba44934230624b901ac7.js.map