SceneMain.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. let scriptFinish = this.nodeFinish.getComponent(GameFinish);
  171. scriptFinish.initGameFinish();
  172. }
  173. /** 加载视频 先不去除游戏节点 */
  174. async addPrefabVideo(bundle, config) {
  175. this.removePrefab();// 隐藏游戏页
  176. // 加载视频页
  177. let script = this.getScriptByNode(this.nodeVideo, config.scriptName);
  178. if (script && script.initBundle) {
  179. script.initBundle(bundle, config);
  180. }
  181. this.nodeVideo.active = true;
  182. this.node.getChildByName('bg').active = false;
  183. this.resetBtnNext(true);
  184. }
  185. resetBtnNext(show) {
  186. if (common.isDebug) {
  187. this.btnNext.active = true;
  188. }
  189. else {
  190. this.btnNext.active = false;// 显示下一步按钮
  191. if (!show) return;
  192. let gameOne = common.getGameFormLocal();
  193. this.btnNext.active = Number(gameOne) >= 0;
  194. }
  195. }
  196. /**
  197. * 删除预制体
  198. * 游戏相关的逻辑狗卡片 倒计时都隐藏
  199. */
  200. removePrefab() {
  201. if (this.nodeShow) {
  202. this.nodeShow.removeFromParent(true);
  203. this.nodeShow.destroy();
  204. this.nodeShow = null;
  205. }
  206. this.nodeStart.active = false;
  207. this.nodeFinish.active = false;
  208. }
  209. /** 事件回调:显示逻辑狗卡片 开始 */
  210. showLgjStart(props: PropsStart): void {
  211. let script = this.nodeStart.getComponent(GameStart);
  212. script.setLuojigouCard(props);
  213. }
  214. /** 事件回调:显示逻辑狗卡片 结束 */
  215. showLgjFinish(props: PropsFinish) {
  216. let scriptFinish = this.nodeFinish.getComponent(GameFinish);
  217. scriptFinish.setAnimation(props);
  218. }
  219. /** 事件回调:进入某个单元 */
  220. msgResultEnterUnit(unitNum: number) {
  221. common.setUnitNum(unitNum);
  222. this.addPrefab(unitNum, 1);
  223. }
  224. /** 事件回调:进入下一个游戏 */
  225. msgResultEnterGame() {
  226. this.resetBtnNext(false);
  227. let curUint = common.getUnitNum();
  228. let curPage = common.getPageNum();
  229. let attribute: AttributeUnit = common.attributeMap[curUint];
  230. let length = attribute.config.length;
  231. if (curPage < length) {
  232. this.addPrefab(curUint, curPage + 1);
  233. }
  234. else {
  235. this.scheduleOnce(() => {
  236. // 返回课程详情页
  237. common.httpReplaceUrl(common.urlClass);
  238. }, 0.5);
  239. }
  240. }
  241. /** 返回详情页面 */
  242. eventBack() {
  243. let url = common.urlClass + '?id=' + common.getCourseId() + '&mode=luojigou';
  244. window.location.replace(url);
  245. }
  246. /** 根据 组件名 获取 脚本组件 */
  247. getScriptByNode(node: cc.Node, scriptName: string): any {
  248. if (!node || !scriptName) return null;
  249. let script = node.getComponent(scriptName);
  250. if (!script) {
  251. script = this.nodeShow['_components'][0];
  252. }
  253. return script;
  254. };
  255. /** 窗口变化回调 */
  256. protected onResize() {
  257. // 适配
  258. this.adapt();
  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. protected setFitHeight() {
  281. const canvas = cc.Canvas.instance;
  282. canvas.fitHeight = true;
  283. canvas.fitWidth = false;
  284. }
  285. /** 适配宽度模式 */
  286. protected setFitWidth() {
  287. const canvas = cc.Canvas.instance;
  288. canvas.fitHeight = false;
  289. canvas.fitWidth = true;
  290. }
  291. /** 下载游戏zip */
  292. downLoadZip(zipName) {
  293. window['luojigou']['downloadAssets'](zipName);
  294. }
  295. /** 接收下载进度 */
  296. reseveInfoForDownLoad(info) { };
  297. }