SceneMain.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. import AudioManager from "../src/audioUitls/AudioManager";
  10. const { ccclass, property } = cc._decorator;
  11. @ccclass
  12. export default class Scene extends cc.Component {
  13. @property({ tooltip: '游戏配置', type: cc.JsonAsset })
  14. jsonConfig: cc.JsonAsset = null;
  15. @property({ tooltip: '视频页预制体', type: cc.Prefab })
  16. prefabVideo: cc.Prefab = null;
  17. @property({ tooltip: '游戏开始预制体', type: cc.Prefab })
  18. prefabStart: cc.Prefab = null;
  19. @property({ tooltip: '游戏结束预制体', type: cc.Prefab })
  20. prefabFinish: cc.Prefab = null;
  21. @property({ tooltip: '地图预制体', type: cc.Prefab })
  22. prefabPageMap: cc.Prefab = null;
  23. @property({ tooltip: '提示预制体', type: cc.Prefab })
  24. prefabTip: cc.Prefab = null;
  25. @property({ tooltip: '下一个游戏', type: cc.Node })
  26. btnNext: cc.Node = null;
  27. nodeShow: cc.Node = null; // 显示节点
  28. nodeVideo: cc.Node = null;
  29. nodeStart: cc.Node = null;
  30. nodeFinish: cc.Node = null;
  31. nodePageMap: cc.Node = null;
  32. // 缓存数据
  33. objData: {
  34. bundles: cc.AssetManager.Bundle | any,
  35. prefabs: cc.Prefab | any,
  36. } = { bundles: {}, prefabs: {} };
  37. protected onLoad(): void {
  38. // 设置游戏窗口变化的回调(仅 Web 平台有效)
  39. cc.view.setResizeCallback(() => this.onResize());
  40. }
  41. /** 注册监听事件 */
  42. protected onEnable(): void {
  43. this.adapt();// 第一次显示的屏幕适配
  44. NotifierCenter.listen(CConst.EVENT_LJG_START, this.showLgjStart, this, false);
  45. NotifierCenter.listen(CConst.EVENT_LJG_FINISH, this.showLgjFinish, this, false);
  46. NotifierCenter.listen(CConst.EVENT_ENTER_UNIT, this.msgResultEnterUnit, this, false);
  47. NotifierCenter.listen(CConst.EVENT_ENTER_GAME, this.msgResultEnterGame, this, false);
  48. NotifierCenter.listen(CConst.EVENT_SHOW_TIP, this.msgResultShowTip, this, false);
  49. }
  50. /** 取消监听事件 */
  51. protected onDisable(): void {
  52. NotifierCenter.ignoreScope(this);
  53. }
  54. /** 游戏开始 */
  55. async start() {
  56. common.hideLoading();
  57. this.initUI();
  58. await this.initData();
  59. common.log("场景加载完成,等待进入游戏层");
  60. if (common.debugTpye == 1) {
  61. this.nodePageMap.active = false;
  62. this.addPrefab(common.getUnitNum(), common.getPageNum());
  63. }
  64. }
  65. /** 初始化游戏数据 */
  66. async initData() {
  67. await AudioCB.getInstance().init();
  68. await AudioManager.getInstance().loadAudios();
  69. // 保存地图配置
  70. let unitAll = this.jsonConfig.json;
  71. for (const key in unitAll) {
  72. if (Object.prototype.hasOwnProperty.call(unitAll, key)) {
  73. let _attribute: AttributeUnit = new AttributeUnit();
  74. const unitOne = unitAll[key];
  75. for (const key in unitOne) {
  76. if (Object.prototype.hasOwnProperty.call(unitOne, key)) {
  77. _attribute[key] = unitOne[key];
  78. }
  79. }
  80. _attribute.bundle = await Loader.loadBundle(_attribute.bundleName);
  81. _attribute.config = [];
  82. let gameAll = await Loader.loadBundleJson(_attribute.bundle, _attribute.configName);
  83. gameAll.json.forEach((gameOne, index) => {
  84. let attributeGame: AttributeGame = new AttributeGame();
  85. for (const key in gameOne) {
  86. if (Object.prototype.hasOwnProperty.call(gameOne, key)) {
  87. attributeGame[key] = gameOne[key];
  88. }
  89. }
  90. _attribute.config[index] = attributeGame;
  91. });
  92. common.attributeMap[key] = _attribute;
  93. }
  94. }
  95. }
  96. initUI() {
  97. // 初始隐藏的节点
  98. this.nodeVideo = cc.instantiate(this.prefabVideo);
  99. this.nodeStart = cc.instantiate(this.prefabStart);
  100. this.nodeFinish = cc.instantiate(this.prefabFinish);
  101. this.nodeVideo.active = false;
  102. this.nodeStart.active = false;
  103. this.nodeFinish.active = false;
  104. this.node.addChild(this.nodeVideo, CConst.ZORDER_VIDEO);
  105. this.node.addChild(this.nodeStart, CConst.ZORDER_LJG_START);
  106. this.node.addChild(this.nodeFinish, CConst.ZORDER_LJG_FINISH);
  107. this.nodePageMap = cc.instantiate(this.prefabPageMap);
  108. this.node.addChild(this.nodePageMap, CConst.ZORDER_PAGE_MAP);
  109. this.nodeShow = null;
  110. this.btnNext.zIndex = CConst.ZORDER_BUTTON_NEXT;
  111. }
  112. /** 添加预制体 分别处理游戏跟视频*/
  113. async addPrefab(unitNum: number, pageNum: number) {
  114. this.resetBtnNext(false);
  115. common.setPageNum(pageNum);
  116. let attribute: AttributeUnit = common.attributeMap[unitNum];
  117. let configOne = attribute.config[pageNum - 1]
  118. // 游戏
  119. if (configOne.prefabName) {
  120. this.addPrefabGame(attribute.bundle, configOne);
  121. }
  122. // 视频
  123. else {
  124. this.addPrefabVideo(attribute.bundle, configOne);
  125. }
  126. }
  127. /** 加载游戏节点 */
  128. async addPrefabGame(bundle, config) {
  129. // 加载新的地图页
  130. let prefab = await Loader.loadBundlePrefab(bundle, config.prefabName);
  131. this.removePrefab();// 去掉游戏页
  132. this.nodeVideo.active = false;// 隐藏视频页
  133. this.nodePageMap.active = false; // 隐藏地图页
  134. this.nodeShow = cc.instantiate(prefab);
  135. this.node.addChild(this.nodeShow, CConst.ZORDER_GAME);
  136. this.node.getChildByName('bg').active = false;
  137. this.resetBtnNext(true);
  138. }
  139. /** 加载视频 先不去除游戏节点 */
  140. async addPrefabVideo(bundle, config) {
  141. this.removePrefab();// 去掉游戏页
  142. this.nodePageMap.active = false; // 隐藏地图页
  143. // 加载视频页
  144. let script = this.getScriptByNode(this.nodeVideo, config.scriptName);
  145. if (script && script.initBundle) {
  146. script.initBundle(bundle, config);
  147. }
  148. this.nodeVideo.active = true;
  149. this.node.getChildByName('bg').active = false;
  150. this.resetBtnNext(true);
  151. }
  152. resetBtnNext(show) {
  153. if (common.debugTpye == 0) {
  154. this.btnNext.active = false;// 显示下一步按钮
  155. if (!show) return;
  156. let gameOne = common.getGameFormLocal();
  157. this.btnNext.active = Number(gameOne) >= 0;
  158. }
  159. else{
  160. this.btnNext.active = true;
  161. }
  162. }
  163. /**
  164. * 删除预制体
  165. * 游戏相关的逻辑狗卡片 倒计时都隐藏
  166. */
  167. removePrefab() {
  168. if (this.nodeShow) {
  169. this.nodeShow.removeFromParent(true);
  170. this.nodeShow.destroy();
  171. this.nodeShow = null;
  172. }
  173. this.nodeStart.active = false;
  174. this.nodeFinish.active = false;
  175. }
  176. /** 事件回调:显示逻辑狗卡片 开始 */
  177. showLgjStart(props: PropsStart): void {
  178. let script = this.nodeStart.getComponent(GameStart);
  179. script.setLuojigouCard(props);
  180. }
  181. /** 事件回调:显示逻辑狗卡片 结束 */
  182. showLgjFinish(props: PropsFinish) {
  183. let scriptFinish = this.nodeFinish.getComponent(GameFinish);
  184. scriptFinish.setAnimation(props);
  185. }
  186. /**
  187. * 返回到地图页
  188. */
  189. enterPageMap() {
  190. this.resetBtnNext(false);
  191. this.removePrefab();// 隐藏游戏相关
  192. this.nodeVideo.active = false; // 隐藏视频页
  193. this.nodePageMap.active = true; // 显示地图页
  194. };
  195. /** 事件回调:进入某个单元 */
  196. msgResultEnterUnit(unitNum: number) {
  197. common.setUnitNum(unitNum);
  198. this.addPrefab(unitNum, 1);
  199. }
  200. /** 事件回调:进入下一个游戏 */
  201. msgResultEnterGame() {
  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. * 窗口变化回调
  232. */
  233. protected onResize() {
  234. // 适配
  235. this.adapt();
  236. }
  237. /**
  238. * 适配
  239. */
  240. protected adapt() {
  241. // 实际屏幕比例
  242. const winSize = cc.winSize,
  243. screenRatio = winSize.width / winSize.height;
  244. // 设计比例
  245. const designResolution = cc.Canvas.instance.designResolution,
  246. designRatio = designResolution.width / designResolution.height;
  247. // 对比判断
  248. common.log('win ratio: ', screenRatio, '; w: ', winSize.width, '; h: ', winSize.height);
  249. common.log('des ratio: ', designRatio, '; w: ', designResolution.width, '; h: ', designResolution.height);
  250. if (screenRatio < designRatio) {
  251. common.log("适配宽 屏幕宽高比 小于 设计比例");
  252. this.setFitWidth();
  253. } else {
  254. common.log("适配高 屏幕宽高比 大于等于 设计比例");
  255. this.setFitHeight();
  256. }
  257. }
  258. /**
  259. * 适配高度模式
  260. */
  261. protected setFitHeight() {
  262. const canvas = cc.Canvas.instance;
  263. canvas.fitHeight = true;
  264. canvas.fitWidth = false;
  265. }
  266. /**
  267. * 适配宽度模式
  268. */
  269. protected setFitWidth() {
  270. const canvas = cc.Canvas.instance;
  271. canvas.fitHeight = false;
  272. canvas.fitWidth = true;
  273. }
  274. /**
  275. * 下载游戏zip
  276. * @param zipName
  277. */
  278. downLoadZip(zipName) {
  279. window['luojigou']['downloadAssets'](zipName);
  280. }
  281. /**
  282. * 接收下载进度
  283. * @param info
  284. */
  285. reseveInfoForDownLoad(info) {
  286. };
  287. }