SceneMain.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. @property({ tooltip: '返回', type: cc.Node })
  28. btnBack: cc.Node = null;
  29. nodeShow: cc.Node = null; // 显示节点
  30. nodeVideo: cc.Node = null;
  31. nodeStart: cc.Node = null;
  32. nodeFinish: cc.Node = null;
  33. // 缓存数据
  34. objData: {
  35. bundles: cc.AssetManager.Bundle | any,
  36. prefabs: cc.Prefab | any,
  37. } = { bundles: {}, prefabs: {} };
  38. protected onLoad(): void {
  39. // 设置游戏窗口变化的回调(仅 Web 平台有效)
  40. cc.view.setResizeCallback(() => this.onResize());
  41. }
  42. /** 注册监听事件 */
  43. protected onEnable(): void {
  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. this.adapt();
  49. }
  50. /** 取消监听事件 */
  51. protected onDisable(): void {
  52. NotifierCenter.ignoreScope(this);
  53. }
  54. /** 游戏开始 */
  55. async start() {
  56. common.hideLoading();
  57. Tools.getNetLocationInfo();
  58. let result = common.isDebug ? true : await common.httpCheckToken();
  59. if (result) {
  60. this.enterGame();
  61. }
  62. else {
  63. common.httpReplaceUrl(common.urlClass);
  64. }
  65. return;
  66. }
  67. /** 进入游戏 */
  68. async enterGame() {
  69. let unitNum = common.getUnitNum();
  70. let isHas = await this.initData(unitNum);
  71. if (isHas) {
  72. await AudioCB.getInstance().init();
  73. await AudioManager.getInstance().loadAudios();
  74. this.initUI();
  75. common.log("场景加载完成,等待进入游戏层");
  76. if (common.isDebug) {
  77. this.addPrefab(unitNum, common.getPageNum());
  78. }
  79. else {
  80. this.msgResultEnterUnit(unitNum);
  81. }
  82. }
  83. else {
  84. const options = {
  85. title: '',
  86. content: '当前动画游戏不存在!',
  87. };
  88. const params = {
  89. mode: PopupCacheMode.Once,
  90. };
  91. let prefab = await PopupManager.load('prefab/popUp');
  92. PopupManager.show(cc.instantiate(prefab), options, params);
  93. }
  94. };
  95. /** 初始化游戏数据 */
  96. async initData(key): Promise<boolean> {
  97. // 保存地图配置
  98. let unitAll = this.jsonConfig.json;
  99. if (Object.prototype.hasOwnProperty.call(unitAll, key)) {
  100. let _attribute: AttributeUnit = new AttributeUnit();
  101. const unitOne = unitAll[key];
  102. for (const key in unitOne) {
  103. if (Object.prototype.hasOwnProperty.call(unitOne, key)) {
  104. _attribute[key] = unitOne[key];
  105. }
  106. }
  107. _attribute.bundle = await Loader.loadBundle(_attribute.bundleName);
  108. _attribute.config = [];
  109. let gameAll = await Loader.loadBundleJson(_attribute.bundle, _attribute.configName);
  110. gameAll.json.forEach((gameOne, index) => {
  111. let attributeGame: AttributeGame = new AttributeGame();
  112. for (const key in gameOne) {
  113. if (Object.prototype.hasOwnProperty.call(gameOne, key)) {
  114. attributeGame[key] = gameOne[key];
  115. }
  116. }
  117. _attribute.config[index] = attributeGame;
  118. });
  119. common.attributeMap[key] = _attribute;
  120. return true;
  121. }
  122. else {
  123. return false;
  124. }
  125. }
  126. initUI() {
  127. // 初始隐藏的节点
  128. this.nodeVideo = cc.instantiate(this.prefabVideo);
  129. this.nodeStart = cc.instantiate(this.prefabStart);
  130. this.nodeFinish = cc.instantiate(this.prefabFinish);
  131. this.nodeVideo.active = false;
  132. this.nodeStart.active = false;
  133. this.nodeFinish.active = false;
  134. this.node.addChild(this.nodeVideo, CConst.ZORDER_VIDEO);
  135. this.node.addChild(this.nodeStart, CConst.ZORDER_LJG_START);
  136. this.node.addChild(this.nodeFinish, CConst.ZORDER_LJG_FINISH);
  137. this.btnNext.zIndex = CConst.ZORDER_BUTTON_NEXT;
  138. this.btnBack.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 = true;
  205. let gameFinish = this.nodeFinish.getComponent(GameFinish);
  206. gameFinish.nodeSuc.active = false;
  207. gameFinish.nodeCoin.active = false;
  208. gameFinish.nodeLimit.active = false;
  209. }
  210. /** 事件回调:显示逻辑狗卡片 开始 */
  211. showLgjStart(props: PropsStart): void {
  212. let script = this.nodeStart.getComponent(GameStart);
  213. script.setLuojigouCard(props);
  214. }
  215. /** 事件回调:显示逻辑狗卡片 结束 */
  216. showLgjFinish(props: PropsFinish) {
  217. let scriptFinish = this.nodeFinish.getComponent(GameFinish);
  218. scriptFinish.setAnimation(props);
  219. }
  220. /** 事件回调:进入某个单元 */
  221. msgResultEnterUnit(unitNum: number) {
  222. common.setUnitNum(unitNum);
  223. this.addPrefab(unitNum, 1);
  224. }
  225. /** 事件回调:进入下一个游戏 */
  226. msgResultEnterGame() {
  227. this.resetBtnNext(false);
  228. let curUint = common.getUnitNum();
  229. let curPage = common.getPageNum();
  230. let attribute: AttributeUnit = common.attributeMap[curUint];
  231. let length = attribute.config.length;
  232. if (curPage < length) {
  233. this.addPrefab(curUint, curPage + 1);
  234. }
  235. else {
  236. this.scheduleOnce(() => {
  237. // 返回课程详情页
  238. common.httpReplaceUrl(common.urlClass);
  239. }, 0.5);
  240. }
  241. }
  242. /** 返回详情页面 */
  243. eventBack() {
  244. let url = common.urlClass + '?id=' + common.getCourseId() + '&mode=luojigou';
  245. window.location.replace(url);
  246. }
  247. /** 根据 组件名 获取 脚本组件 */
  248. getScriptByNode(node: cc.Node, scriptName: string): any {
  249. if (!node || !scriptName) return null;
  250. let script = node.getComponent(scriptName);
  251. if (!script) {
  252. script = this.nodeShow['_components'][0];
  253. }
  254. return script;
  255. };
  256. /** 窗口变化回调 */
  257. protected onResize() {
  258. // 适配
  259. this.adapt();
  260. }
  261. /** 适配 */
  262. protected adapt() {
  263. // 实际屏幕比例
  264. const winSize = cc.winSize,
  265. screenRatio = winSize.width / winSize.height;
  266. // 设计比例
  267. const designResolution = cc.Canvas.instance.designResolution,
  268. designRatio = designResolution.width / designResolution.height;
  269. // 对比判断
  270. common.log('win ratio: ', screenRatio, '; w: ', winSize.width, '; h: ', winSize.height);
  271. common.log('des ratio: ', designRatio, '; w: ', designResolution.width, '; h: ', designResolution.height);
  272. if (screenRatio < designRatio) {
  273. common.log("适配宽 屏幕宽高比 小于 设计比例");
  274. this.setFitWidth();
  275. } else {
  276. common.log("适配高 屏幕宽高比 大于等于 设计比例");
  277. this.setFitHeight();
  278. }
  279. }
  280. /** 适配高度模式 */
  281. protected setFitHeight() {
  282. const canvas = cc.Canvas.instance;
  283. canvas.fitHeight = true;
  284. canvas.fitWidth = false;
  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. }