SceneMain.ts 11 KB

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