SceneMain.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. cc.macro.ENABLE_TRANSPARENT_CANVAS = true;
  2. import common, { AttributeGame, AttributeUnit } from "../src/common/common";
  3. import CConst from "../src/common/CConst";
  4. import NotifierCenter from "../src/webtcp/NotifierCenter";
  5. import Loader from "../src/config/Loader";
  6. import GameStart, { PropsStart } from "../res/luojigouStart/src/LuojigouStart";
  7. import GameFinish, { PropsFinish } from "../res/luojigouFinish/src/LuojigouFinish";
  8. import AudioCB from "../src/audioUitls/AudioCB";
  9. const { ccclass, property } = cc._decorator;
  10. @ccclass
  11. export default class Scene extends cc.Component {
  12. @property({ tooltip: '游戏配置', type: cc.JsonAsset })
  13. jsonConfig: cc.JsonAsset = null;
  14. @property({ tooltip: '视频页预制体', type: cc.Prefab })
  15. prefabVideo: cc.Prefab = null;
  16. @property({ tooltip: '游戏开始预制体', type: cc.Prefab })
  17. prefabStart: cc.Prefab = null;
  18. @property({ tooltip: '游戏结束预制体', type: cc.Prefab })
  19. prefabFinish: cc.Prefab = null;
  20. @property({ tooltip: '地图预制体', type: cc.Prefab })
  21. prefabPageMap: cc.Prefab = null;
  22. @property({ tooltip: '提示预制体', type: cc.Prefab })
  23. prefabTip: cc.Prefab = null;
  24. @property({ tooltip: '下一个游戏', type: cc.Node })
  25. btnNext: cc.Node = null;
  26. nodeShow: cc.Node = null; // 显示节点
  27. nodeVideo: cc.Node = null;
  28. nodeStart: cc.Node = null;
  29. nodeFinish: cc.Node = null;
  30. nodePageMap: cc.Node = null;
  31. // 缓存数据
  32. objData: {
  33. bundles: cc.AssetManager.Bundle | any,
  34. prefabs: cc.Prefab | any,
  35. } = { bundles: {}, prefabs: {} };
  36. /** 注册监听事件 */
  37. protected onEnable(): void {
  38. NotifierCenter.listen(CConst.EVENT_LJG_START, this.showLgjStart, this, false);
  39. NotifierCenter.listen(CConst.EVENT_LJG_FINISH, this.showLgjFinish, this, false);
  40. NotifierCenter.listen(CConst.EVENT_ENTER_UNIT, this.msgResultEnterUnit, this, false);
  41. NotifierCenter.listen(CConst.EVENT_ENTER_GAME, this.msgResultEnterGame, this, false);
  42. NotifierCenter.listen(CConst.EVENT_SHOW_TIP, this.msgResultShowTip, this, false);
  43. NotifierCenter.listen(CConst.EVENT_READY_TO_PLAY, this.msgResultReadyVideo, this, false);
  44. }
  45. /** 取消监听事件 */
  46. protected onDisable(): void {
  47. NotifierCenter.ignoreScope(this);
  48. }
  49. /** 游戏开始 */
  50. async start() {
  51. this.initUI();
  52. await this.initData();
  53. // 用于判断视频是否可以自动播放
  54. let node = new cc.Node();
  55. node.setContentSize(cc.winSize);
  56. node.on(cc.Node.EventType.TOUCH_START, () => {
  57. common.isCanPlayVideo = true;
  58. node.off(cc.Node.EventType.TOUCH_START);
  59. node.removeFromParent();
  60. });
  61. node['_touchListener'].setSwallowTouches(false); // 不吞噬触摸事件
  62. this.node.addChild(node, CConst.ZORDER_TOP);
  63. common.log("场景加载完成,等待进入游戏层");
  64. if (common.isDebug) {
  65. this.nodePageMap.active = false;
  66. this.addPrefab(common.getUnitNum(), common.getPageNum());
  67. }
  68. }
  69. /** 初始化游戏数据 */
  70. async initData() {
  71. await AudioCB.instance.init();
  72. // 保存地图配置
  73. let unitAll = this.jsonConfig.json;
  74. for (const key in unitAll) {
  75. if (Object.prototype.hasOwnProperty.call(unitAll, key)) {
  76. let _attribute: AttributeUnit = new AttributeUnit();
  77. const unitOne = unitAll[key];
  78. for (const key in unitOne) {
  79. if (Object.prototype.hasOwnProperty.call(unitOne, key)) {
  80. _attribute[key] = unitOne[key];
  81. }
  82. }
  83. _attribute.bundle = await Loader.loadBundle(_attribute.bundleName);
  84. _attribute.config = [];
  85. let gameAll = await Loader.loadBundleJson(_attribute.bundle, _attribute.configName);
  86. gameAll.json.forEach((gameOne, index) => {
  87. let attributeGame: AttributeGame = new AttributeGame();
  88. for (const key in gameOne) {
  89. if (Object.prototype.hasOwnProperty.call(gameOne, key)) {
  90. attributeGame[key] = gameOne[key];
  91. }
  92. }
  93. _attribute.config[index] = attributeGame;
  94. });
  95. common.attributeMap[key] = _attribute;
  96. }
  97. }
  98. }
  99. initUI() {
  100. // 初始隐藏的节点
  101. this.nodeVideo = cc.instantiate(this.prefabVideo);
  102. this.nodeStart = cc.instantiate(this.prefabStart);
  103. this.nodeFinish = cc.instantiate(this.prefabFinish);
  104. this.nodeVideo.active = false;
  105. this.nodeStart.active = false;
  106. this.nodeFinish.active = false;
  107. this.node.addChild(this.nodeVideo, CConst.ZORDER_VIDEO);
  108. this.node.addChild(this.nodeStart, CConst.ZORDER_LJG_START);
  109. this.node.addChild(this.nodeFinish, CConst.ZORDER_LJG_FINISH);
  110. this.nodePageMap = cc.instantiate(this.prefabPageMap);
  111. this.node.addChild(this.nodePageMap, CConst.ZORDER_PAGE_MAP);
  112. this.nodeShow = null;
  113. this.btnNext.zIndex = CConst.ZORDER_BUTTON_NEXT;
  114. this.btnNext.active = false;
  115. }
  116. /** 添加预制体 分别处理游戏跟视频*/
  117. async addPrefab(unitNum: number, pageNum: number) {
  118. common.setPageNum(pageNum);
  119. let attribute: AttributeUnit = common.attributeMap[unitNum];
  120. let configOne = attribute.config[pageNum - 1]
  121. // 游戏
  122. if (configOne.prefabName) {
  123. this.addPrefabGame(attribute.bundle, configOne);
  124. }
  125. // 视频
  126. else {
  127. this.addPrefabVideo(attribute.bundle, configOne);
  128. }
  129. }
  130. /** 加载游戏节点 */
  131. async addPrefabGame(bundle, config) {
  132. // 加载新的地图页
  133. let prefab = await Loader.loadBundlePrefab(bundle, config.prefabName);
  134. this.removePrefab();// 去掉游戏页
  135. this.nodePageMap.active = false;// 隐藏地图页
  136. this.nodeVideo.active = false;// 隐藏视频页
  137. this.nodeShow = cc.instantiate(prefab);
  138. let script = this.getScriptByNode(this.nodeShow, config.scriptName);
  139. if (script && script.initBundle) {
  140. script.initBundle(bundle, config);
  141. }
  142. this.node.addChild(this.nodeShow, CConst.ZORDER_GAME);
  143. this.btnNext.active = true;
  144. }
  145. /** 加载视频 先不去除游戏节点 */
  146. async addPrefabVideo(bundle, config) {
  147. // 加载视频页
  148. let script = this.getScriptByNode(this.nodeVideo, config.scriptName);
  149. if (script && script.initBundle) {
  150. script.initBundle(bundle, config);
  151. }
  152. this.nodeVideo.opacity = 0;
  153. this.nodeVideo.active = true;
  154. }
  155. /** 视频准备完成,去除游戏节点,显示视频 */
  156. msgResultReadyVideo() {
  157. this.removePrefab();// 隐藏游戏页
  158. this.nodePageMap.active = false;// 隐藏地图页
  159. this.nodeVideo.opacity = 255;// 显示视频
  160. this.btnNext.active = true;// 显示下一步按钮
  161. }
  162. /**
  163. * 删除预制体
  164. * 游戏相关的逻辑狗卡片 倒计时都隐藏
  165. */
  166. removePrefab() {
  167. if (this.nodeShow) {
  168. this.nodeShow.removeFromParent(true);
  169. this.nodeShow.destroy();
  170. this.nodeShow = null;
  171. }
  172. this.nodeStart.active = false;
  173. this.nodeFinish.active = false;
  174. }
  175. /** 事件回调:显示逻辑狗卡片 开始 */
  176. showLgjStart(props: PropsStart): void {
  177. let script = this.nodeStart.getComponent(GameStart);
  178. script.setLuojigouCard(props);
  179. }
  180. /** 事件回调:显示逻辑狗卡片 结束 */
  181. showLgjFinish(props: PropsFinish) {
  182. let scriptFinish = this.nodeFinish.getComponent(GameFinish);
  183. scriptFinish.setAnimation(props);
  184. }
  185. /**
  186. * 返回到地图页
  187. */
  188. enterPageMap() {
  189. this.removePrefab();// 隐藏游戏相关
  190. this.btnNext.active = false;// 隐藏下一步按钮
  191. this.nodeVideo.active = false; // 隐藏视频页
  192. this.nodePageMap.active = true; // 显示地图页
  193. };
  194. /** 事件回调:进入某个单元 */
  195. msgResultEnterUnit(unitNum: number) {
  196. common.setUnitNum(unitNum);
  197. this.addPrefab(unitNum, 1);
  198. }
  199. /** 事件回调:进入下一个游戏 */
  200. msgResultEnterGame() {
  201. this.btnNext.active = false;
  202. let curUint = common.getUnitNum();
  203. let curPage = common.getPageNum();
  204. let attribute: AttributeUnit = common.attributeMap[curUint];
  205. let length = attribute.config.length;
  206. if (curPage > length - 1) {
  207. this.enterPageMap();
  208. return;
  209. }
  210. this.addPrefab(curUint, curPage + 1);
  211. }
  212. /** 事件回调:显示提示 */
  213. msgResultShowTip() {
  214. let nodeTip = cc.instantiate(this.prefabTip);
  215. this.node.addChild(nodeTip, CConst.ZORDER_TIP);
  216. }
  217. /**
  218. * 根据 组件名 获取 脚本组件
  219. * @param scriptName
  220. * @returns
  221. */
  222. getScriptByNode(node: cc.Node, scriptName: string): any {
  223. if (!node || !scriptName) return null;
  224. let script = node.getComponent(scriptName);
  225. if (!script) {
  226. script = this.nodeShow['_components'][0];
  227. }
  228. return script;
  229. };
  230. /**
  231. * 下载游戏zip
  232. * @param zipName
  233. */
  234. downLoadZip(zipName) {
  235. window['luojigou']['downloadAssets'](zipName);
  236. }
  237. /**
  238. * 接收下载进度
  239. * @param info
  240. */
  241. reseveInfoForDownLoad(info) {
  242. };
  243. }