SceneMain.ts 11 KB

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