gameManager.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. import { _decorator, AudioClip, AudioSource, Component, Node, Size, Sprite, SpriteFrame, sys, UITransform, Vec3 } from 'cc';
  2. import { game_run } from './game_run';
  3. import { attributes_data, LevelItemData, other_widget_finish_listen_item, scene_item_data, sysMessage, task_data, ui_att_item, UserData, widget_item_data } from '../../data/data';
  4. import { config } from '../config';
  5. import { sceneManager } from './sceneManager';
  6. import { http } from '../http';
  7. import { tools } from '../tools';
  8. import { taskServce } from './TaskSchedule/taskServce';
  9. import { ClientEvent } from '../clientEvent';
  10. import { sysSound } from './sysSound';
  11. import { SdkUtil } from '../sdkUtil';
  12. const { ccclass, property } = _decorator;
  13. @ccclass('gameManager')
  14. export class gameManager extends Component {
  15. private mGameRun:game_run = null;
  16. private mGameData:scene_item_data[] = [];
  17. private mCurSceneIndex:number =0; //当前第几个场景
  18. public static Singleton:gameManager = null;
  19. private static g_userData:UserData;
  20. public static res_map:Map<string,SpriteFrame> = new Map()
  21. public static mp3_cache:Map<string,AudioClip> = new Map()
  22. private mLevelData:LevelItemData = null;
  23. private static sys_data:sysMessage = null;
  24. private static isLoadingStatus:boolean = false;
  25. public static lookVideoAdsNumber:number = 0;
  26. public static isTestUser:boolean = false;
  27. @property(sceneManager) mSceneManager:sceneManager = null;
  28. @property(sysSound) mSysSound:sysSound = null;
  29. protected start(): void {
  30. gameManager.Singleton = this;
  31. }
  32. public static is24Free():boolean{
  33. if(gameManager.getStaticUserData().freeTime==undefined){
  34. gameManager.getStaticUserData().freeTime = 0;
  35. gameManager.saveUserData()
  36. }
  37. if(gameManager.getStaticUserData().freeTime!=0){
  38. let cur_time = new Date()
  39. let old_time = new Date(gameManager.getStaticUserData().freeTime)
  40. let timeDiff = cur_time.getTime() - old_time.getTime();
  41. // console.log("old",tools.getLocalTime(old))
  42. // console.log("cur",tools.getLocalTime(cur))
  43. var seconds = Math.floor(timeDiff / 1000);
  44. var minutes = Math.floor(seconds / 60);
  45. // var hours = Math.floor(minutes / 60);
  46. console.log("相隔几分钟",minutes)
  47. if(minutes>=1440){
  48. gameManager.getStaticUserData().freeTime = 0;
  49. gameManager.saveUserData()
  50. return false;
  51. }else{
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. public static setUser24Free(){
  58. gameManager.g_userData.freeTime = new Date().getTime()
  59. gameManager.saveUserData()
  60. }
  61. public static loadSceneRes(level_id:number,scene_num:number,cb){
  62. if(scene_num<1){
  63. return
  64. }
  65. gameManager.isLoadingStatus = true;
  66. let scene_count = 0;
  67. let start_loading_all_res =()=>{
  68. let count = 0;
  69. let num = 0;
  70. let load_call = ()=>{
  71. count++;
  72. // console.log("count",count,num,scene_count,scene_num)
  73. if(scene_count==scene_num){
  74. if(count>=num){
  75. gameManager.isLoadingStatus = false;
  76. console.log("完成全部加载!")
  77. }
  78. }
  79. }
  80. if(scene_num<=2){
  81. gameManager.isLoadingStatus = false;
  82. console.log("完成全部加载!")
  83. return
  84. }
  85. for (let i = 2; i <= scene_num; i++) {
  86. let url = http.get_level_resource(level_id,i)
  87. http.run_get(url,(err,data)=>{
  88. if(!err){
  89. let _data = JSON.parse(data)
  90. scene_count++;
  91. num += _data.content.length;
  92. if(scene_count==scene_num){
  93. if(num==0){
  94. gameManager.isLoadingStatus = false;
  95. console.log("完成全部加载!")
  96. return
  97. }else{
  98. if(count>=num){
  99. gameManager.isLoadingStatus = false;
  100. console.log("完成全部加载!")
  101. return
  102. }
  103. }
  104. }
  105. // console.log("run_get:",scene_count,_data.content.length,i)
  106. for (let index = 0; index < _data.content.length; index++) {
  107. const url:string = _data.content[index];
  108. load_res(url,load_call)
  109. }
  110. }else{
  111. tools.showToast(`获取关卡错误${err}`)
  112. }
  113. })
  114. }
  115. }
  116. // console.log("scene_num",scene_num)
  117. let load_res = (url:string,call)=>{
  118. let hz = url.substring(url.length-3,url.length);
  119. if(hz==="png"||hz==="jpg"){
  120. tools.loadSceneImg(url,(d)=>{
  121. first_load_res.delete(url)
  122. gameManager.res_map.set(d.url,d.sf)
  123. call()
  124. })
  125. }
  126. if(hz==="mp3"){
  127. tools.loadSceneMp3(url,(d)=>{
  128. first_load_res.delete(url)
  129. gameManager.mp3_cache.set(d.url,d.clip)
  130. call()
  131. })
  132. }
  133. }
  134. let first_finish_call = ()=>{
  135. first_load_res.forEach((v,k)=>{
  136. const url:string =k;
  137. load_res(url,()=>{
  138. if(first_load_res.size<=0){
  139. cb()
  140. start_loading_all_res()
  141. }
  142. })
  143. })
  144. }
  145. let first_count = 0;
  146. let first_load_res:Map<string,boolean> = new Map;
  147. for (let i = 0; i < 2; i++) {
  148. let url = http.get_level_resource(level_id,i)
  149. http.run_get(url,(err,data)=>{
  150. if(!err){
  151. let _data = JSON.parse(data)
  152. first_count++;
  153. for (let index = 0; index < _data.content.length; index++) {
  154. const url:string = _data.content[index];
  155. first_load_res.set(url,false)
  156. }
  157. if(first_count==2){
  158. scene_count=1;
  159. first_finish_call()
  160. }
  161. }
  162. })
  163. }
  164. }
  165. public setLevelData(data:LevelItemData){
  166. this.mLevelData = data;
  167. }
  168. public getLevelData(){
  169. return this.mLevelData;
  170. }
  171. public static setSysData(data:sysMessage){
  172. gameManager.sys_data = data;
  173. }
  174. public static getSysData(){
  175. return gameManager.sys_data;
  176. }
  177. public static loadSceneMp3(){
  178. }
  179. public static getUserData():UserData{
  180. let str = sys.localStorage.getItem(config.USER_DATA)
  181. if(str==undefined||str==""||str==null){
  182. return null;
  183. }
  184. let user:UserData = JSON.parse(str)
  185. return user;
  186. }
  187. public static getStaticUserData(){
  188. return gameManager.g_userData
  189. }
  190. public static setUserData(user:UserData){
  191. gameManager.g_userData = user;
  192. gameManager.saveUserData()
  193. }
  194. public static saveUserData(){
  195. if(gameManager.g_userData!=null){
  196. sys.localStorage.setItem(config.USER_DATA, JSON.stringify(gameManager.g_userData));
  197. }
  198. }
  199. public reLife(){
  200. SdkUtil.showVideoAd(config.TT_REWARD.RE_LIFE,(res)=>{
  201. if(res.isEnded){
  202. this.mGameRun.onlaunch(()=>{
  203. gameManager.Singleton.startLevelGame()
  204. })
  205. }
  206. })
  207. }
  208. public reStartGame(){
  209. this.mGameRun.onlaunch(()=>{
  210. this.mCurSceneIndex = 0;
  211. gameManager.Singleton.startLevelGame()
  212. })
  213. }
  214. public levleIsUnLock(levle_id:number){
  215. let isunlock = false;
  216. for (let index = 0; index < gameManager.getStaticUserData().unlock_levels.length; index++) {
  217. const id = gameManager.getStaticUserData().unlock_levels[index];
  218. if(id==levle_id){
  219. isunlock = true;
  220. break;
  221. }
  222. }
  223. return isunlock
  224. }
  225. public unLockLevel(levle_id:number){
  226. let is_have = false;
  227. for (let index = 0; index < gameManager.getStaticUserData().unlock_levels.length; index++) {
  228. const id = gameManager.getStaticUserData().unlock_levels[index];
  229. if(id==levle_id){
  230. is_have = true;
  231. break;
  232. }
  233. }
  234. if(!is_have){
  235. gameManager.getStaticUserData().unlock_levels.push(levle_id)
  236. gameManager.saveUserData()
  237. gameManager.Singleton.sync_data()
  238. }
  239. }
  240. public sync_data(){
  241. let url = http.sync_data()
  242. let unlock_levels = gameManager.getStaticUserData().unlock_levels;
  243. http.run_post(url,{"unlock_levels":JSON.stringify(unlock_levels)},(err,data)=>{
  244. if(!err){
  245. }
  246. })
  247. }
  248. public nextScene(){
  249. this.onLaunch(()=>{
  250. this.mCurSceneIndex++;
  251. let not_scene = false;
  252. if(this.mCurSceneIndex>=this.mGameData.length){
  253. not_scene = true;
  254. }
  255. if(!not_scene){
  256. this.mGameRun.unInit()
  257. this.startLevelGame()
  258. }else{
  259. // tools.showToast("没有场景了!")
  260. }
  261. })
  262. }
  263. public gameOver(){
  264. ClientEvent.dispatchEvent(config.EventRun.ON_WIN)
  265. }
  266. public backGameList(){
  267. if(!gameManager.isLoadingStatus){
  268. this.unscheduleAllCallbacks()
  269. this.mGameRun.backGameList()
  270. }else{
  271. console.log("没有全部加载完成!")
  272. }
  273. }
  274. public initEvent(){
  275. ClientEvent.offAll(config.EventRun.NOTICE_EVENT)
  276. ClientEvent.offAll(config.EventRun.WIDGET_FINISH)
  277. ClientEvent.offAll(config.EventRun.WIDGET_FAIL)
  278. ClientEvent.offAll(config.EventRun.ON_WIDGET_FINISH_COLLECT_EVENT)
  279. ClientEvent.offAll(config.EventRun.WIDGET_HIDE)
  280. ClientEvent.offAll(config.EventRun.SHOW_ZHAO_BU_TONG_FINISH_STATUS)
  281. ClientEvent.offAll(config.EventRun.TOP_VIEW_CLOSE)
  282. ClientEvent.offAll(config.EventRun.TOP_VIEW_FINISH)
  283. ClientEvent.offAll(config.EventRun.TOP_VIEW_FAIL)
  284. ClientEvent.offAll(config.EventRun.ON_COUNT_DOWN_FAIL)
  285. ClientEvent.offAll(config.EventRun.ON_COUNT_DOWN_START)
  286. ClientEvent.offAll(config.EventRun.ON_SHOW_RULE_BTN)
  287. ClientEvent.offAll(config.EventRun.ON_BOSS_HURT)
  288. ClientEvent.on(config.EventRun.WIDGET_FINISH,this.onWidgetFinishEvent.bind(this),this)
  289. ClientEvent.on(config.EventRun.WIDGET_FAIL,this.onWidgetFailEvent.bind(this),this)
  290. ClientEvent.on(config.EventRun.TOP_VIEW_CLOSE,this.onUiCloseEvent.bind(this),this)
  291. ClientEvent.on(config.EventRun.TOP_VIEW_FINISH,this.onUiFinishEvent.bind(this),this)
  292. ClientEvent.on(config.EventRun.TOP_VIEW_FAIL,this.onUiFailEvent.bind(this),this)
  293. ClientEvent.on(config.EventRun.ON_COUNT_DOWN_FAIL,this.onCountDownFailEvent.bind(this),this)
  294. ClientEvent.on(config.EventRun.ON_COUNT_DOWN_START,this.onStartCountDownEvent.bind(this),this)
  295. }
  296. public initGR(gr:game_run){
  297. this.mGameRun = gr;
  298. gameManager.Singleton = this;
  299. this.mSceneManager.init(this)
  300. this.clearRes()
  301. }
  302. public clearRes(){
  303. gameManager.res_map.clear()
  304. gameManager.mp3_cache.clear()
  305. }
  306. public init(gr:game_run,data:scene_item_data[]){
  307. // this.mGameRun = gr;
  308. // gameManager.Singleton = this;
  309. // this.mSceneManager.init(this)
  310. // this.mCurSceneIndex = main.Singleton.edit_scene_view.getCurSelectSceneIndex()
  311. // this.mGameData = data;
  312. // if( this.mGameData.length<=0){
  313. // return tools.showToast("错误的场景信息")
  314. // }
  315. // this.startLevelGame()
  316. }
  317. public runGame(data:scene_item_data[]){
  318. this.mCurSceneIndex = 0;
  319. this.mGameData = data;
  320. if( this.mGameData.length<=0){
  321. return tools.showToast("错误的场景信息")
  322. }
  323. this.startLevelGame()
  324. }
  325. public onLaunch(call){
  326. this.mGameRun.onlaunch(call)
  327. }
  328. FilterChildScene(data:scene_item_data[]){
  329. let temp_scene = []
  330. for (let index = 0; index < data.length; index++) {
  331. const element = data[index];
  332. if(!element.is_child_scene){
  333. temp_scene.push(element)
  334. }
  335. }
  336. return temp_scene;
  337. }
  338. protected onDestroy(): void {
  339. ClientEvent.off(config.EventRun.WIDGET_FINISH,this.onWidgetFinishEvent.bind(this),this)
  340. }
  341. public startLevelGame(){
  342. this.mGameRun.unInit()
  343. this.initEvent()
  344. // console.log("this.mGameData",this.mGameData,this.mCurSceneIndex)
  345. this.mSceneManager.startLevelGame(this.mGameData[this.mCurSceneIndex])
  346. taskServce.initTask(this.mGameData[this.mCurSceneIndex]._task_data)
  347. this.scheduleOnce(()=>{
  348. this.startGame()
  349. },0.5)
  350. if(gameManager.Singleton.getLevelData()){
  351. this.mSceneManager.playMusic(this.mLevelData.piped_music,true)
  352. }
  353. }
  354. public loadScene(pages: scene_item_data[],type:number){
  355. this.mGameRun.loadSceneLayer(pages,type)
  356. }
  357. public loadUi(ui_widge_list:widget_item_data[]){
  358. this.mGameRun.loadUILayer(ui_widge_list)
  359. }
  360. public loadTextSound(ui_widge_list:widget_item_data[]){
  361. this.mGameRun.loadTextSound(ui_widge_list)
  362. }
  363. public loadQuestion(ui_widge_list:widget_item_data[]){
  364. this.mGameRun.initQuestionList(ui_widge_list)
  365. }
  366. public initCountDownList(ui_widge_list:widget_item_data[]){
  367. this.mGameRun.initCountDownList(ui_widge_list)
  368. }
  369. public initTaskUi(data:task_data){
  370. this.mGameRun.initTaskUi(data)
  371. }
  372. public startGame(){
  373. taskServce.startFirstEvent()
  374. }
  375. public showCheck(call){
  376. this.mGameRun.showCheck(call)
  377. }
  378. public showLoadingLevel(){
  379. this.mGameRun.showLoadingLevel()
  380. }
  381. public hideLoadingLevel(){
  382. this.mGameRun.hideLoadingLevel()
  383. }
  384. public showUnLockView(lock_one_call,lock_all_one_day){
  385. this.mGameRun.showUnLockView(lock_one_call,lock_all_one_day)
  386. }
  387. public static getWidgetList(scene_data:scene_item_data):widget_item_data[]{
  388. let temp:widget_item_data[] = []
  389. for (let index = 0; index < scene_data.page_widget_list.length; index++) {
  390. const data = scene_data.page_widget_list[index];
  391. if(data.type<=4 ){
  392. temp.push(data)
  393. }
  394. }
  395. return temp;
  396. }
  397. public static getUIWidgetList(scene_data:scene_item_data){
  398. let temp:widget_item_data[] = []
  399. for (let index = 0; index < scene_data.page_widget_list.length; index++) {
  400. const data = scene_data.page_widget_list[index];
  401. if(data.type===config.Widget_Type_List.UI_TOP){
  402. if(data.att.top_data!=null&&data.att.top_data.top_ui_type!=config.top_view_type.__null){
  403. temp.push(data)
  404. }
  405. }
  406. }
  407. return temp;
  408. }
  409. public static getQuestionwidgetList(scene_data:scene_item_data){
  410. let temp:widget_item_data[] = []
  411. for (let index = 0; index < scene_data.page_widget_list.length; index++) {
  412. const data = scene_data.page_widget_list[index];
  413. if(data.type===config.Widget_Type_List.QUESTION_SELECT){
  414. if(data.att.question_select!=null){
  415. temp.push(data)
  416. }
  417. }
  418. }
  419. return temp;
  420. }
  421. public static getTextSoundWidgetList(scene_data:scene_item_data){
  422. let temp:widget_item_data[] = []
  423. for (let index = 0; index < scene_data.page_widget_list.length; index++) {
  424. const data = scene_data.page_widget_list[index];
  425. if(data.type===config.Widget_Type_List.TEXT_SOUND){
  426. if(data.att.text_sound_data!=null){
  427. temp.push(data)
  428. }
  429. }
  430. }
  431. return temp;
  432. }
  433. public static getCountDownWidgetList(scene_data:scene_item_data){
  434. let temp:widget_item_data[] = []
  435. for (let index = 0; index < scene_data.page_widget_list.length; index++) {
  436. const data = scene_data.page_widget_list[index];
  437. if(data.type===config.Widget_Type_List.COUNT_DOWN){
  438. if(data.att.count_down!=null){
  439. temp.push(data)
  440. }
  441. }
  442. }
  443. return temp;
  444. }
  445. public static getCacheSpriteFrameByName(sfName:string){
  446. // return control.res_map.get(sfName)
  447. return gameManager.res_map.get(sfName)
  448. }
  449. public static getCacheSoundByName(sfName:string){
  450. // return control.mp3_cache.get(sfName)
  451. return gameManager.mp3_cache.get(sfName)
  452. }
  453. public static initUiBaseAtt(node:Node,att:ui_att_item){
  454. node.getComponent(UITransform).setContentSize(new Size(att.width,att.height))
  455. node.position = new Vec3(att.x,att.y);
  456. node.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(att.res)
  457. }
  458. onWidgetFinishEvent(widget_id:number){
  459. taskServce.onWidgetFinishEvent(widget_id)
  460. }
  461. onWidgetFailEvent(widget_id:number){
  462. taskServce.onWidgetFailEvent(widget_id)
  463. }
  464. onUiCloseEvent(event_id:number){
  465. taskServce.extEventByEventId(event_id)
  466. }
  467. onUiFinishEvent(event_id:number){
  468. taskServce.extEventByEventId(event_id)
  469. }
  470. onUiFailEvent(event_id:number){
  471. taskServce.extEventByEventId(event_id)
  472. }
  473. onCountDownFailEvent(event_id:number){
  474. taskServce.extEventByEventId(event_id)
  475. }
  476. onStartCountDownEvent(event_id:number){
  477. taskServce.extEventByEventId(event_id)
  478. }
  479. exeEvent(event_id:number){
  480. taskServce.extEventByEventId(event_id)
  481. }
  482. showFindRuleTips(){
  483. this.mGameRun.showFindRuleTips()
  484. }
  485. showTips(){
  486. this.mGameRun.showTips()
  487. }
  488. showRule(){
  489. this.mGameRun.showRule()
  490. }
  491. getSceneManager(){
  492. return this.mSceneManager;
  493. }
  494. IsOpenRuleStatus(){
  495. return taskServce.guo_ju_qing_binding_event_id==-1;
  496. }
  497. //点击按钮音效
  498. public sys_click_button_music(){
  499. if(gameManager.sys_data!=null){
  500. this.mSysSound.playSound(gameManager.sys_data.content.sys_click_button_music)
  501. }
  502. }
  503. // //失败吓人音效
  504. // public sys_fail_scary_music(){
  505. // if(gameManager.sys_data!=null){
  506. // this.mSysSound.playSound(gameManager.sys_data.content.sys_fail_scary_music)
  507. // }
  508. // }
  509. //失败后攻击音效
  510. public sys_fail_attack_music(){
  511. if(gameManager.sys_data!=null){
  512. this.mSysSound.playSound(gameManager.sys_data.content.sys_fail_attack_music)
  513. }
  514. }
  515. //失败提示音效
  516. public sys_fail_prompt_music(){
  517. if(gameManager.sys_data!=null){
  518. this.mSysSound.playSound(gameManager.sys_data.content.sys_fail_prompt_music)
  519. }
  520. }
  521. //正确通关音效
  522. public sys_success_complete_music(){
  523. if(gameManager.sys_data!=null){
  524. this.mSysSound.playSound(gameManager.sys_data.content.sys_success_complete_music)
  525. }
  526. }
  527. //点击正确细节音效
  528. public sys_click_correct_detail_music(){
  529. if(gameManager.sys_data!=null){
  530. this.mSysSound.playSound(gameManager.sys_data.content.sys_click_correct_detail_music)
  531. }
  532. }
  533. public checkWidgetList(list:other_widget_finish_listen_item[]):number{
  534. let event_id = -1;
  535. for (let index = 0; index < list.length; index++) {
  536. const item = list[index];
  537. let isFinish = this.mGameRun.isCurScenePageCheckWidgetFinish(item.widget_id)
  538. if(!isFinish){
  539. event_id = item.event_id;
  540. break;
  541. }
  542. }
  543. return event_id;
  544. }
  545. }