SceneMain.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 PopupManager, { PopupCacheMode } from "../src/popUp/PopupManager";
  11. import { HttpStateMap } from "../res/http/HttpStateMap";
  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. /** 注册监听事件 */
  37. protected onEnable(): void {
  38. NotifierCenter.listen(CConst.EVENT_LJG_START, this.showLgjStart, this, false);
  39. NotifierCenter.listen(CConst.EVENT_LJG_FINISH, this.showLgjFinish, this, false);
  40. NotifierCenter.listen(CConst.EVENT_ENTER_UNIT, this.msgResultEnterUnit, this, false);
  41. NotifierCenter.listen(CConst.EVENT_ENTER_GAME, this.msgResultEnterGame, this, false);
  42. }
  43. /** 取消监听事件 */
  44. protected onDisable(): void {
  45. NotifierCenter.ignoreScope(this);
  46. }
  47. /** 游戏开始 */
  48. async start() {
  49. common.hideLoading();
  50. let result = common.isDebug ? true : await this.checkUser();
  51. if (result) {
  52. this.enterGame();
  53. }
  54. else {
  55. const options = {
  56. title: '',
  57. content: '请下载应用!'
  58. };
  59. const params = {
  60. mode: PopupCacheMode.Once,
  61. };
  62. let prefab = await PopupManager.load('prefab/popUp');
  63. PopupManager.show(cc.instantiate(prefab), options, params);
  64. }
  65. return;
  66. }
  67. /** 用户检测 */
  68. async checkUser(): Promise<boolean> {
  69. // 验证token
  70. let token = Tools.getLocalStorage('token', null);
  71. return new Promise((resolve) => {
  72. let xhr = new XMLHttpRequest();
  73. xhr.open("GET", common.urlToken, true);
  74. xhr.setRequestHeader('token', token);
  75. xhr.onreadystatechange = function () {
  76. if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
  77. console.log(xhr.responseText);
  78. let json = JSON.parse(xhr.responseText);
  79. resolve(json.status == 200);
  80. }
  81. };
  82. xhr.onerror = function () {
  83. resolve(false);
  84. }
  85. xhr.send();
  86. });
  87. }
  88. /** 进入游戏 */
  89. async enterGame() {
  90. // 获取网络地址信息
  91. let json: UitlData = Tools.getNetLocationInfo();
  92. let unitNum = json.modelId ? json.modelId : common.unitCur;
  93. common.setUnitNum(unitNum);
  94. common.setItemId(json.itemId);
  95. let isHas = await this.initData(unitNum);
  96. if (isHas) {
  97. await AudioCB.instance.init();
  98. this.initUI();
  99. common.log("场景加载完成,等待进入游戏层");
  100. if (common.isDebug) {
  101. this.addPrefab(unitNum, common.getPageNum());
  102. }
  103. else {
  104. this.msgResultEnterUnit(unitNum);
  105. }
  106. }
  107. else {
  108. const options = {
  109. title: '',
  110. content: '当前动画游戏不存在!',
  111. };
  112. const params = {
  113. mode: PopupCacheMode.Once,
  114. };
  115. let prefab = await PopupManager.load('prefab/popUp');
  116. PopupManager.show(cc.instantiate(prefab), options, params);
  117. }
  118. };
  119. /** 初始化游戏数据 */
  120. async initData(key): Promise<boolean> {
  121. // 保存地图配置
  122. let unitAll = this.jsonConfig.json;
  123. if (Object.prototype.hasOwnProperty.call(unitAll, key)) {
  124. let _attribute: AttributeUnit = new AttributeUnit();
  125. const unitOne = unitAll[key];
  126. for (const key in unitOne) {
  127. if (Object.prototype.hasOwnProperty.call(unitOne, key)) {
  128. _attribute[key] = unitOne[key];
  129. }
  130. }
  131. _attribute.bundle = await Loader.loadBundle(_attribute.bundleName);
  132. _attribute.config = [];
  133. let gameAll = await Loader.loadBundleJson(_attribute.bundle, _attribute.configName);
  134. gameAll.json.forEach((gameOne, index) => {
  135. let attributeGame: AttributeGame = new AttributeGame();
  136. for (const key in gameOne) {
  137. if (Object.prototype.hasOwnProperty.call(gameOne, key)) {
  138. attributeGame[key] = gameOne[key];
  139. }
  140. }
  141. _attribute.config[index] = attributeGame;
  142. });
  143. common.attributeMap[key] = _attribute;
  144. return true;
  145. }
  146. else {
  147. return false;
  148. }
  149. }
  150. initUI() {
  151. // 初始隐藏的节点
  152. this.nodeVideo = cc.instantiate(this.prefabVideo);
  153. this.nodeStart = cc.instantiate(this.prefabStart);
  154. this.nodeFinish = cc.instantiate(this.prefabFinish);
  155. this.nodeVideo.active = false;
  156. this.nodeStart.active = false;
  157. this.nodeFinish.active = false;
  158. this.node.addChild(this.nodeVideo, CConst.ZORDER_VIDEO);
  159. this.node.addChild(this.nodeStart, CConst.ZORDER_LJG_START);
  160. this.node.addChild(this.nodeFinish, CConst.ZORDER_LJG_FINISH);
  161. this.btnNext.zIndex = CConst.ZORDER_BUTTON_NEXT;
  162. this.nodeShow = null;
  163. }
  164. /** 添加预制体 分别处理游戏跟视频*/
  165. async addPrefab(unitNum: number, pageNum: number) {
  166. common.setPageNum(pageNum);
  167. let attribute: AttributeUnit = common.attributeMap[unitNum];
  168. let configOne = attribute.config[pageNum - 1]
  169. // 游戏
  170. if (configOne.prefabName) {
  171. this.addPrefabGame(attribute.bundle, configOne);
  172. }
  173. // 视频
  174. else {
  175. this.addPrefabVideo(attribute.bundle, configOne);
  176. }
  177. }
  178. /** 加载游戏节点 */
  179. async addPrefabGame(bundle, config) {
  180. // 加载新的地图页
  181. let prefab = await Loader.loadBundlePrefab(bundle, config.prefabName);
  182. this.removePrefab();// 去掉游戏页
  183. this.nodeVideo.active = false;// 隐藏视频页
  184. this.nodeShow = cc.instantiate(prefab);
  185. let script = this.getScriptByNode(this.nodeShow, config.scriptName);
  186. if (script && script.initBundle) {
  187. script.initBundle(bundle, config);
  188. }
  189. this.node.addChild(this.nodeShow, CConst.ZORDER_GAME);
  190. this.resetBtnNext(true);
  191. }
  192. /** 加载视频 先不去除游戏节点 */
  193. async addPrefabVideo(bundle, config) {
  194. this.removePrefab();// 隐藏游戏页
  195. // 加载视频页
  196. let script = this.getScriptByNode(this.nodeVideo, config.scriptName);
  197. if (script && script.initBundle) {
  198. script.initBundle(bundle, config);
  199. }
  200. this.nodeVideo.active = true;
  201. this.resetBtnNext(true);
  202. }
  203. resetBtnNext(show) {
  204. if (common.isDebug) {
  205. this.btnNext.active = true;
  206. }
  207. else{
  208. this.btnNext.active = false;// 显示下一步按钮
  209. if (!show) return;
  210. let gameOne = common.getGameFormLocal();
  211. this.btnNext.active = Number(gameOne) >= 0;
  212. }
  213. }
  214. /**
  215. * 删除预制体
  216. * 游戏相关的逻辑狗卡片 倒计时都隐藏
  217. */
  218. removePrefab() {
  219. if (this.nodeShow) {
  220. this.nodeShow.removeFromParent(true);
  221. this.nodeShow.destroy();
  222. this.nodeShow = null;
  223. }
  224. this.nodeStart.active = false;
  225. this.nodeFinish.active = false;
  226. }
  227. /** 事件回调:显示逻辑狗卡片 开始 */
  228. showLgjStart(props: PropsStart): void {
  229. let script = this.nodeStart.getComponent(GameStart);
  230. script.setLuojigouCard(props);
  231. }
  232. /** 事件回调:显示逻辑狗卡片 结束 */
  233. showLgjFinish(props: PropsFinish) {
  234. let scriptFinish = this.nodeFinish.getComponent(GameFinish);
  235. scriptFinish.setAnimation(props);
  236. }
  237. /** 事件回调:进入某个单元 */
  238. msgResultEnterUnit(unitNum: number) {
  239. common.setUnitNum(unitNum);
  240. this.addPrefab(unitNum, 1);
  241. }
  242. /** 事件回调:进入下一个游戏 */
  243. msgResultEnterGame() {
  244. this.resetBtnNext(false);
  245. let curUint = common.getUnitNum();
  246. let curPage = common.getPageNum();
  247. let attribute: AttributeUnit = common.attributeMap[curUint];
  248. let length = attribute.config.length;
  249. console.log('curPage: ', curPage, '; length: ', length);
  250. if (curPage < length) {
  251. this.addPrefab(curUint, curPage + 1);
  252. }
  253. else{
  254. let token = Tools.getLocalStorage('token', null);
  255. let xhr = new XMLHttpRequest();
  256. xhr.open("GET", common.urlOver, true);
  257. xhr.setRequestHeader('itemId', common.getItemId());
  258. xhr.setRequestHeader('token', token);
  259. xhr.send();
  260. }
  261. }
  262. /**
  263. * 根据 组件名 获取 脚本组件
  264. * @param scriptName
  265. * @returns
  266. */
  267. getScriptByNode(node: cc.Node, scriptName: string): any {
  268. if (!node || !scriptName) return null;
  269. let script = node.getComponent(scriptName);
  270. if (!script) {
  271. script = this.nodeShow['_components'][0];
  272. }
  273. return script;
  274. };
  275. /** 下载游戏zip */
  276. downLoadZip(zipName) {
  277. window['luojigou']['downloadAssets'](zipName);
  278. }
  279. /** 接收下载进度 */
  280. reseveInfoForDownLoad(info) { };
  281. }