SceneMain.ts 11 KB

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