gameManager.ts 20 KB

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