62afbc2e10a672ffcd5e1628f52cfa5fbc715965.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2", "__unresolved_3", "__unresolved_4", "__unresolved_5"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, Button, Component, EventHandler, Input, Node, SpriteFrame, _decorator, input, isValid, oops, EventDispatcher, EventMessage, resLoader, ViewUtil, _dec, _class, _crd, ccclass, ResType, GameComponent;
  4. function _reportPossibleCrUseOfoops(extras) {
  5. _reporterNs.report("oops", "../../core/Oops", _context.meta, extras);
  6. }
  7. function _reportPossibleCrUseOfEventDispatcher(extras) {
  8. _reporterNs.report("EventDispatcher", "../../core/common/event/EventDispatcher", _context.meta, extras);
  9. }
  10. function _reportPossibleCrUseOfEventMessage(extras) {
  11. _reporterNs.report("EventMessage", "../../core/common/event/EventMessage", _context.meta, extras);
  12. }
  13. function _reportPossibleCrUseOfListenerFunc(extras) {
  14. _reporterNs.report("ListenerFunc", "../../core/common/event/EventMessage", _context.meta, extras);
  15. }
  16. function _reportPossibleCrUseOfAssetType(extras) {
  17. _reporterNs.report("AssetType", "../../core/common/loader/ResLoader", _context.meta, extras);
  18. }
  19. function _reportPossibleCrUseOfCompleteCallback(extras) {
  20. _reporterNs.report("CompleteCallback", "../../core/common/loader/ResLoader", _context.meta, extras);
  21. }
  22. function _reportPossibleCrUseOfPaths(extras) {
  23. _reporterNs.report("Paths", "../../core/common/loader/ResLoader", _context.meta, extras);
  24. }
  25. function _reportPossibleCrUseOfProgressCallback(extras) {
  26. _reporterNs.report("ProgressCallback", "../../core/common/loader/ResLoader", _context.meta, extras);
  27. }
  28. function _reportPossibleCrUseOfresLoader(extras) {
  29. _reporterNs.report("resLoader", "../../core/common/loader/ResLoader", _context.meta, extras);
  30. }
  31. function _reportPossibleCrUseOfViewUtil(extras) {
  32. _reporterNs.report("ViewUtil", "../../core/utils/ViewUtil", _context.meta, extras);
  33. }
  34. return {
  35. setters: [function (_unresolved_) {
  36. _reporterNs = _unresolved_;
  37. }, function (_cc) {
  38. _cclegacy = _cc.cclegacy;
  39. __checkObsolete__ = _cc.__checkObsolete__;
  40. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  41. Button = _cc.Button;
  42. Component = _cc.Component;
  43. EventHandler = _cc.EventHandler;
  44. Input = _cc.Input;
  45. Node = _cc.Node;
  46. SpriteFrame = _cc.SpriteFrame;
  47. _decorator = _cc._decorator;
  48. input = _cc.input;
  49. isValid = _cc.isValid;
  50. }, function (_unresolved_2) {
  51. oops = _unresolved_2.oops;
  52. }, function (_unresolved_3) {
  53. EventDispatcher = _unresolved_3.EventDispatcher;
  54. }, function (_unresolved_4) {
  55. EventMessage = _unresolved_4.EventMessage;
  56. }, function (_unresolved_5) {
  57. resLoader = _unresolved_5.resLoader;
  58. }, function (_unresolved_6) {
  59. ViewUtil = _unresolved_6.ViewUtil;
  60. }],
  61. execute: function () {
  62. _crd = true;
  63. _cclegacy._RF.push({}, "62362nWbWZP653j6XL/zJDq", "GameComponent", undefined);
  64. /*
  65. * @Author: dgflash
  66. * @Date: 2022-04-14 17:08:01
  67. * @LastEditors: dgflash
  68. * @LastEditTime: 2022-12-13 11:36:00
  69. */
  70. __checkObsolete__(['Asset', 'Button', 'Component', 'EventHandler', 'EventKeyboard', 'EventTouch', 'Input', 'Node', 'Sprite', 'SpriteFrame', '__private', '_decorator', 'input', 'isValid']);
  71. ({
  72. ccclass
  73. } = _decorator);
  74. /** 加载资源类型 */
  75. ResType = /*#__PURE__*/function (ResType) {
  76. ResType[ResType["Load"] = 0] = "Load";
  77. ResType[ResType["LoadDir"] = 1] = "LoadDir";
  78. ResType[ResType["Audio"] = 2] = "Audio";
  79. return ResType;
  80. }(ResType || {});
  81. /** 资源加载记录 */
  82. /**
  83. * 游戏显示对象组件模板
  84. * 1、当前对象加载的资源,会在对象释放时,自动释放引用的资源
  85. * 2、当前对象支持启动游戏引擎提供的各种常用逻辑事件
  86. */
  87. _export("GameComponent", GameComponent = (_dec = ccclass("GameComponent"), _dec(_class = class GameComponent extends Component {
  88. constructor(...args) {
  89. super(...args);
  90. //#region 全局事件管理
  91. this._event = null;
  92. //#endregion
  93. //#region 预制节点管理
  94. /** 摊平的节点集合(所有节点不能重名) */
  95. this.nodes = null;
  96. //#endregion
  97. //#region 资源加载管理
  98. /** 资源路径 */
  99. this.resPaths = null;
  100. }
  101. /** 全局事件管理器 */
  102. get event() {
  103. if (this._event == null) this._event = new (_crd && EventDispatcher === void 0 ? (_reportPossibleCrUseOfEventDispatcher({
  104. error: Error()
  105. }), EventDispatcher) : EventDispatcher)();
  106. return this._event;
  107. }
  108. /**
  109. * 注册全局事件
  110. * @param event 事件名
  111. * @param listener 处理事件的侦听器函数
  112. * @param object 侦听函数绑定的this对象
  113. */
  114. on(event, listener, object) {
  115. this.event.on(event, listener, object);
  116. }
  117. /**
  118. * 移除全局事件
  119. * @param event 事件名
  120. */
  121. off(event) {
  122. this.event.off(event);
  123. }
  124. /**
  125. * 触发全局事件
  126. * @param event 事件名
  127. * @param args 事件参数
  128. */
  129. dispatchEvent(event, ...args) {
  130. this.event.dispatchEvent(event, ...args);
  131. }
  132. /** 通过节点名获取预制上的节点,整个预制不能有重名节点 */
  133. getNode(name) {
  134. if (this.nodes) {
  135. return this.nodes.get(name);
  136. }
  137. return undefined;
  138. }
  139. /** 平摊所有节点存到Map<string, Node>中通过get(name: string)方法获取 */
  140. nodeTreeInfoLite() {
  141. this.nodes = new Map();
  142. (_crd && ViewUtil === void 0 ? (_reportPossibleCrUseOfViewUtil({
  143. error: Error()
  144. }), ViewUtil) : ViewUtil).nodeTreeInfoLite(this.node, this.nodes);
  145. }
  146. /**
  147. * 从资源缓存中找到预制资源名并创建一个显示对象
  148. * @param path 资源路径
  149. */
  150. createPrefabNode(path, bundleName = (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  151. error: Error()
  152. }), oops) : oops).res.defaultBundleName) {
  153. return (_crd && ViewUtil === void 0 ? (_reportPossibleCrUseOfViewUtil({
  154. error: Error()
  155. }), ViewUtil) : ViewUtil).createPrefabNode(path, bundleName);
  156. }
  157. /**
  158. * 加载预制并创建预制节点
  159. * @param path 资源路径
  160. * @param bundleName 资源包名
  161. */
  162. createPrefabNodeAsync(path, bundleName = (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  163. error: Error()
  164. }), oops) : oops).res.defaultBundleName) {
  165. return (_crd && ViewUtil === void 0 ? (_reportPossibleCrUseOfViewUtil({
  166. error: Error()
  167. }), ViewUtil) : ViewUtil).createPrefabNodeAsync(path, bundleName);
  168. }
  169. // 资源使用记录
  170. /**
  171. * 获取资源
  172. * @param path 资源路径
  173. * @param type 资源类型
  174. * @param bundleName 远程资源包名
  175. */
  176. getRes(path, type, bundleName) {
  177. return (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  178. error: Error()
  179. }), oops) : oops).res.get(path, type, bundleName);
  180. }
  181. /**
  182. * 添加资源使用记录
  183. * @param type 资源类型
  184. * @param bundleName 资源包名
  185. * @param paths 资源路径
  186. */
  187. addPathToRecord(type, bundleName, paths, resId) {
  188. if (this.resPaths == null) this.resPaths = new Map();
  189. var rps = this.resPaths.get(type);
  190. if (rps == null) {
  191. rps = new Map();
  192. this.resPaths.set(type, rps);
  193. }
  194. if (paths instanceof Array) {
  195. let realBundle = bundleName;
  196. for (let index = 0; index < paths.length; index++) {
  197. let realPath = paths[index];
  198. let key = this.getResKey(realBundle, realPath, resId);
  199. if (!rps.has(key)) {
  200. rps.set(key, {
  201. path: realPath,
  202. bundle: realBundle,
  203. resId: resId
  204. });
  205. }
  206. }
  207. } else if (typeof paths === "string") {
  208. let realBundle = bundleName;
  209. let realPath = paths;
  210. let key = this.getResKey(realBundle, realPath, resId);
  211. if (!rps.has(key)) {
  212. rps.set(key, {
  213. path: realPath,
  214. bundle: realBundle,
  215. resId: resId
  216. });
  217. }
  218. } else {
  219. let realBundle = (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  220. error: Error()
  221. }), oops) : oops).res.defaultBundleName;
  222. let realPath = bundleName;
  223. let key = this.getResKey(realBundle, realPath, resId);
  224. if (!rps.has(key)) {
  225. rps.set(key, {
  226. path: realPath,
  227. bundle: realBundle,
  228. resId: resId
  229. });
  230. }
  231. }
  232. }
  233. getResKey(realBundle, realPath, resId) {
  234. let key = `${realBundle}:${realPath}`;
  235. if (resId != null) key += ":" + resId;
  236. return key;
  237. }
  238. /**
  239. * 加载一个资源
  240. * @param bundleName 远程包名
  241. * @param paths 资源路径
  242. * @param type 资源类型
  243. * @param onProgress 加载进度回调
  244. * @param onComplete 加载完成回调
  245. */
  246. load(bundleName, paths, type, onProgress, onComplete) {
  247. this.addPathToRecord(ResType.Load, bundleName, paths);
  248. (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  249. error: Error()
  250. }), oops) : oops).res.load(bundleName, paths, type, onProgress, onComplete);
  251. }
  252. /**
  253. * 异步加载一个资源
  254. * @param bundleName 远程包名
  255. * @param paths 资源路径
  256. * @param type 资源类型
  257. */
  258. loadAsync(bundleName, paths, type) {
  259. this.addPathToRecord(ResType.Load, bundleName, paths);
  260. return (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  261. error: Error()
  262. }), oops) : oops).res.loadAsync(bundleName, paths, type);
  263. }
  264. /**
  265. * 加载文件夹中的资源
  266. * @param bundleName 远程包名
  267. * @param dir 文件夹名
  268. * @param type 资源类型
  269. * @param onProgress 加载进度回调
  270. * @param onComplete 加载完成回调
  271. */
  272. loadDir(bundleName, dir, type, onProgress, onComplete) {
  273. let realDir;
  274. let realBundle;
  275. if (typeof dir === "string") {
  276. realDir = dir;
  277. realBundle = bundleName;
  278. } else {
  279. realDir = bundleName;
  280. realBundle = (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  281. error: Error()
  282. }), oops) : oops).res.defaultBundleName;
  283. }
  284. this.addPathToRecord(ResType.LoadDir, realBundle, realDir);
  285. (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  286. error: Error()
  287. }), oops) : oops).res.loadDir(bundleName, dir, type, onProgress, onComplete);
  288. }
  289. /** 释放资源 */
  290. release() {
  291. if (this.resPaths) {
  292. const rps = this.resPaths.get(ResType.Load);
  293. if (rps) {
  294. rps.forEach(value => {
  295. (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  296. error: Error()
  297. }), oops) : oops).res.release(value.path, value.bundle);
  298. });
  299. rps.clear();
  300. }
  301. }
  302. }
  303. /** 释放文件夹的资源 */
  304. releaseDir() {
  305. if (this.resPaths) {
  306. const rps = this.resPaths.get(ResType.LoadDir);
  307. if (rps) {
  308. rps.forEach(value => {
  309. (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  310. error: Error()
  311. }), oops) : oops).res.releaseDir(value.path, value.bundle);
  312. });
  313. }
  314. }
  315. }
  316. /** 释放音效资源 */
  317. releaseAudioEffect() {
  318. if (this.resPaths) {
  319. const rps = this.resPaths.get(ResType.Audio);
  320. if (rps) {
  321. rps.forEach(value => {
  322. (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  323. error: Error()
  324. }), oops) : oops).audio.putEffect(value.resId, value.path, value.bundle);
  325. });
  326. }
  327. }
  328. }
  329. /**
  330. * 设置图片资源
  331. * @param target 目标精灵对象
  332. * @param path 图片资源地址
  333. * @param bundle 资源包名
  334. */
  335. async setSprite(target, path, bundle = (_crd && resLoader === void 0 ? (_reportPossibleCrUseOfresLoader({
  336. error: Error()
  337. }), resLoader) : resLoader).defaultBundleName) {
  338. const spriteFrame = await this.loadAsync(bundle, path, SpriteFrame);
  339. if (!spriteFrame || !isValid(target)) {
  340. const rps = this.resPaths.get(ResType.Load);
  341. if (rps) {
  342. const key = this.getResKey(bundle, path);
  343. rps.delete(key);
  344. (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  345. error: Error()
  346. }), oops) : oops).res.release(path, bundle);
  347. }
  348. return;
  349. }
  350. target.spriteFrame = spriteFrame;
  351. } //#endregion
  352. //#region 音频播放管理
  353. /**
  354. * 播放背景音乐(不受自动释放资源管理)
  355. * @param url 资源地址
  356. * @param callback 资源加载完成回调
  357. * @param bundleName 资源包名
  358. */
  359. playMusic(url, callback, bundleName) {
  360. (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  361. error: Error()
  362. }), oops) : oops).audio.playMusic(url, callback, bundleName);
  363. }
  364. /**
  365. * 循环播放背景音乐(不受自动释放资源管理)
  366. * @param url 资源地址
  367. * @param bundleName 资源包名
  368. */
  369. playMusicLoop(url, bundleName) {
  370. (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  371. error: Error()
  372. }), oops) : oops).audio.stopMusic();
  373. (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  374. error: Error()
  375. }), oops) : oops).audio.playMusicLoop(url, bundleName);
  376. }
  377. /**
  378. * 播放音效
  379. * @param url 资源地址
  380. * @param callback 资源加载完成回调
  381. * @param bundleName 资源包名
  382. */
  383. async playEffect(url, bundleName) {
  384. if (bundleName == null) bundleName = (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  385. error: Error()
  386. }), oops) : oops).res.defaultBundleName;
  387. await (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  388. error: Error()
  389. }), oops) : oops).audio.playEffect(url, bundleName, () => {
  390. const rps = this.resPaths.get(ResType.Audio);
  391. if (rps) {
  392. const key = this.getResKey(bundleName, url);
  393. rps.delete(key);
  394. }
  395. });
  396. this.addPathToRecord(ResType.Audio, bundleName, url);
  397. } //#endregion
  398. //#region 游戏逻辑事件
  399. /**
  400. * 批量设置当前界面按钮事件
  401. * @param bindRootEvent 是否对预制根节点绑定触摸事件
  402. * @example
  403. * 注:按钮节点Label1、Label2必须绑定UIButton等类型的按钮组件才会生效,方法名必须与节点名一致
  404. * this.setButton();
  405. *
  406. * Label1(event: EventTouch) { console.log(event.target.name); }
  407. * Label2(event: EventTouch) { console.log(event.target.name); }
  408. */
  409. setButton(bindRootEvent = true) {
  410. // 自定义按钮批量绑定触摸事件
  411. if (bindRootEvent) {
  412. this.node.on(Node.EventType.TOUCH_END, event => {
  413. const self = this;
  414. const func = self[event.target.name];
  415. if (func) {
  416. func.call(this, event);
  417. } // 不触发界面根节点触摸事件、不触发长按钮组件的触摸事件
  418. // else if (event.target != this.node && event.target.getComponent(ButtonTouchLong) == null) {
  419. // console.warn(`名为【${event.target.name}】的按钮事件方法不存在`);
  420. // }
  421. }, this);
  422. } // Cocos Creator Button组件批量绑定触摸事件(使用UIButton支持放连点功能)
  423. const regex = /<([^>]+)>/;
  424. const buttons = this.node.getComponentsInChildren(Button);
  425. buttons.forEach(b => {
  426. const node = b.node;
  427. const self = this;
  428. const func = self[node.name];
  429. if (func) {
  430. const event = new EventHandler();
  431. event.target = this.node;
  432. event.handler = b.node.name;
  433. event.component = this.name.match(regex)[1];
  434. b.clickEvents.push(event);
  435. } // else {
  436. // console.warn(`名为【${node.name}】的按钮事件方法不存在`);
  437. // }
  438. });
  439. }
  440. /**
  441. * 批量设置全局事件
  442. * @example
  443. * this.setEvent("onGlobal");
  444. * this.dispatchEvent("onGlobal", "全局事件");
  445. *
  446. * onGlobal(event: string, args: any) { console.log(args) };
  447. */
  448. setEvent(...args) {
  449. const self = this;
  450. for (const name of args) {
  451. const func = self[name];
  452. if (func) this.on(name, func, this);else console.error(`名为【${name}】的全局事方法不存在`);
  453. }
  454. }
  455. /**
  456. * 键盘事件开关
  457. * @param on 打开键盘事件为true
  458. */
  459. setKeyboard(on) {
  460. if (on) {
  461. input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
  462. input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
  463. input.on(Input.EventType.KEY_PRESSING, this.onKeyPressing, this);
  464. } else {
  465. input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
  466. input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
  467. input.off(Input.EventType.KEY_PRESSING, this.onKeyPressing, this);
  468. }
  469. }
  470. /** 键按下 */
  471. onKeyDown(event) {}
  472. /** 键放开 */
  473. onKeyUp(event) {}
  474. /** 键长按 */
  475. onKeyPressing(event) {}
  476. /** 监听游戏从后台进入事件 */
  477. setGameShow() {
  478. this.on((_crd && EventMessage === void 0 ? (_reportPossibleCrUseOfEventMessage({
  479. error: Error()
  480. }), EventMessage) : EventMessage).GAME_SHOW, this.onGameShow, this);
  481. }
  482. /** 监听游戏切到后台事件 */
  483. setGameHide() {
  484. this.on((_crd && EventMessage === void 0 ? (_reportPossibleCrUseOfEventMessage({
  485. error: Error()
  486. }), EventMessage) : EventMessage).GAME_HIDE, this.onGameHide, this);
  487. }
  488. /** 监听游戏画笔尺寸变化事件 */
  489. setGameResize() {
  490. this.on((_crd && EventMessage === void 0 ? (_reportPossibleCrUseOfEventMessage({
  491. error: Error()
  492. }), EventMessage) : EventMessage).GAME_RESIZE, this.onGameResize, this);
  493. }
  494. /** 监听游戏全屏事件 */
  495. setGameFullScreen() {
  496. this.on((_crd && EventMessage === void 0 ? (_reportPossibleCrUseOfEventMessage({
  497. error: Error()
  498. }), EventMessage) : EventMessage).GAME_FULL_SCREEN, this.onGameFullScreen, this);
  499. }
  500. /** 监听游戏旋转屏幕事件 */
  501. setGameOrientation() {
  502. this.on((_crd && EventMessage === void 0 ? (_reportPossibleCrUseOfEventMessage({
  503. error: Error()
  504. }), EventMessage) : EventMessage).GAME_ORIENTATION, this.onGameOrientation, this);
  505. }
  506. /** 游戏从后台进入事件回调 */
  507. onGameShow() {}
  508. /** 游戏切到后台事件回调 */
  509. onGameHide() {}
  510. /** 游戏画笔尺寸变化事件回调 */
  511. onGameResize() {}
  512. /** 游戏全屏事件回调 */
  513. onGameFullScreen() {}
  514. /** 游戏旋转屏幕事件回调 */
  515. onGameOrientation() {} //#endregion
  516. onDestroy() {
  517. // 释放消息对象
  518. if (this._event) {
  519. this._event.destroy();
  520. this._event = null;
  521. } // 节点引用数据清除
  522. if (this.nodes) {
  523. this.nodes.clear();
  524. this.nodes = null;
  525. } // 自动释放资源
  526. if (this.resPaths) {
  527. this.releaseAudioEffect();
  528. this.release();
  529. this.releaseDir();
  530. this.resPaths.clear();
  531. this.resPaths = null;
  532. }
  533. }
  534. }) || _class));
  535. _cclegacy._RF.pop();
  536. _crd = false;
  537. }
  538. };
  539. });
  540. //# sourceMappingURL=62afbc2e10a672ffcd5e1628f52cfa5fbc715965.js.map