gameManager.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 { 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. let hz = url.substring(url.length-3,url.length);
  56. if(hz==="png"||hz==="jpg"){
  57. // if(gameManager.res_map.get(url)!=null){
  58. // call()
  59. // }else{
  60. // }
  61. tools.loadSceneImg(url,(d)=>{
  62. gameManager.res_map.set(d.url,d.sf)
  63. call()
  64. })
  65. }
  66. if(hz==="mp3"){
  67. // if(gameManager.mp3_cache.get(url)!=null){
  68. // call()
  69. // }else{
  70. // }
  71. tools.loadSceneMp3(url,(d)=>{
  72. gameManager.mp3_cache.set(d.url,d.clip)
  73. call()
  74. })
  75. }
  76. }
  77. }else{
  78. tools.showToast(`获取关卡错误${err}`)
  79. }
  80. })
  81. }
  82. }
  83. public setLevelData(data:LevelItemData){
  84. this.mLevelData = data;
  85. }
  86. public getLevelData(){
  87. return this.mLevelData;
  88. }
  89. public static setSysData(data:sysMessage){
  90. gameManager.sys_data = data;
  91. }
  92. public static getSysData(){
  93. return gameManager.sys_data;
  94. }
  95. public static loadSceneMp3(){
  96. }
  97. public static getUserData():UserData{
  98. let str = sys.localStorage.getItem(config.USER_DATA)
  99. if(str==undefined||str==""||str==null){
  100. return null;
  101. }
  102. let user:UserData = JSON.parse(str)
  103. return user;
  104. }
  105. public static getStaticUserData(){
  106. return gameManager.g_userData
  107. }
  108. public static setUserData(user:UserData){
  109. gameManager.g_userData = user;
  110. gameManager.saveUserData()
  111. }
  112. public static saveUserData(){
  113. if(gameManager.g_userData!=null){
  114. sys.localStorage.setItem(config.USER_DATA, JSON.stringify(gameManager.g_userData));
  115. }
  116. }
  117. public reLife(){
  118. gameManager.Singleton.startLevelGame()
  119. }
  120. public reStartGame(){
  121. this.mCurSceneIndex = 0;
  122. gameManager.Singleton.startLevelGame()
  123. }
  124. public levleIsUnLock(levle_id:number){
  125. let isunlock = false;
  126. for (let index = 0; index < gameManager.getStaticUserData().unlock_levels.length; index++) {
  127. const id = gameManager.getStaticUserData().unlock_levels[index];
  128. if(id==levle_id){
  129. isunlock = true;
  130. break;
  131. }
  132. }
  133. return isunlock
  134. }
  135. public unLockLevel(levle_id:number){
  136. let is_have = false;
  137. for (let index = 0; index < gameManager.getStaticUserData().unlock_levels.length; index++) {
  138. const id = gameManager.getStaticUserData().unlock_levels[index];
  139. if(id==levle_id){
  140. is_have = true;
  141. break;
  142. }
  143. }
  144. if(!is_have){
  145. gameManager.getStaticUserData().unlock_levels.push(levle_id)
  146. gameManager.saveUserData()
  147. gameManager.Singleton.sync_data()
  148. }
  149. }
  150. public sync_data(){
  151. let url = http.sync_data()
  152. let unlock_levels = gameManager.getStaticUserData().unlock_levels;
  153. http.run_post(url,{"unlock_levels":JSON.stringify(unlock_levels)},(err,data)=>{
  154. if(!err){
  155. }
  156. })
  157. }
  158. public nextScene(){
  159. this.onLaunch(()=>{
  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. this.mGameRun.unInit()
  167. this.startLevelGame()
  168. }else{
  169. // tools.showToast("没有场景了!")
  170. }
  171. })
  172. }
  173. public gameOver(){
  174. ClientEvent.dispatchEvent(config.EventRun.ON_WIN)
  175. }
  176. public backGameList(){
  177. if(this.getLevelData()!=null){
  178. this.unscheduleAllCallbacks()
  179. this.mGameRun.backGameList()
  180. }
  181. }
  182. public initEvent(){
  183. ClientEvent.offAll(config.EventRun.NOTICE_EVENT)
  184. ClientEvent.offAll(config.EventRun.WIDGET_FINISH)
  185. ClientEvent.offAll(config.EventRun.WIDGET_FAIL)
  186. ClientEvent.offAll(config.EventRun.ON_WIDGET_FINISH_COLLECT_EVENT)
  187. ClientEvent.offAll(config.EventRun.WIDGET_HIDE)
  188. ClientEvent.offAll(config.EventRun.SHOW_ZHAO_BU_TONG_FINISH_STATUS)
  189. ClientEvent.offAll(config.EventRun.TOP_VIEW_CLOSE)
  190. ClientEvent.offAll(config.EventRun.TOP_VIEW_FINISH)
  191. ClientEvent.offAll(config.EventRun.TOP_VIEW_FAIL)
  192. ClientEvent.offAll(config.EventRun.ON_COUNT_DOWN_FAIL)
  193. ClientEvent.offAll(config.EventRun.ON_COUNT_DOWN_START)
  194. ClientEvent.offAll(config.EventRun.ON_SHOW_RULE_BTN)
  195. ClientEvent.offAll(config.EventRun.ON_BOSS_HURT)
  196. ClientEvent.on(config.EventRun.WIDGET_FINISH,this.onWidgetFinishEvent.bind(this),this)
  197. ClientEvent.on(config.EventRun.WIDGET_FAIL,this.onWidgetFailEvent.bind(this),this)
  198. ClientEvent.on(config.EventRun.TOP_VIEW_CLOSE,this.onUiCloseEvent.bind(this),this)
  199. ClientEvent.on(config.EventRun.TOP_VIEW_FINISH,this.onUiFinishEvent.bind(this),this)
  200. ClientEvent.on(config.EventRun.TOP_VIEW_FAIL,this.onUiFailEvent.bind(this),this)
  201. ClientEvent.on(config.EventRun.ON_COUNT_DOWN_FAIL,this.onCountDownFailEvent.bind(this),this)
  202. ClientEvent.on(config.EventRun.ON_COUNT_DOWN_START,this.onStartCountDownEvent.bind(this),this)
  203. }
  204. public initGR(gr:game_run){
  205. this.mGameRun = gr;
  206. gameManager.Singleton = this;
  207. this.mSceneManager.init(this)
  208. this.clearRes()
  209. }
  210. public clearRes(){
  211. gameManager.res_map.clear()
  212. gameManager.mp3_cache.clear()
  213. }
  214. public init(gr:game_run,data:scene_item_data[]){
  215. this.mGameRun = gr;
  216. gameManager.Singleton = this;
  217. this.mSceneManager.init(this)
  218. this.mCurSceneIndex = main.Singleton.edit_scene_view.getCurSelectSceneIndex()
  219. // this.postLevelInfo(id,scene)
  220. // this.startLevelGame()
  221. // console.log("control.Singleton.get_bag_data().content",control.Singleton.get_bag_data())
  222. this.mGameData = data;
  223. if( this.mGameData.length<=0){
  224. return tools.showToast("错误的场景信息")
  225. }
  226. this.startLevelGame()
  227. }
  228. public runGame(data:scene_item_data[]){
  229. this.mCurSceneIndex = 0;
  230. this.mGameData = data;
  231. if( this.mGameData.length<=0){
  232. return tools.showToast("错误的场景信息")
  233. }
  234. this.startLevelGame()
  235. }
  236. public onLaunch(call){
  237. this.mGameRun.onlaunch(call)
  238. }
  239. FilterChildScene(data:scene_item_data[]){
  240. let temp_scene = []
  241. for (let index = 0; index < data.length; index++) {
  242. const element = data[index];
  243. if(!element.is_child_scene){
  244. temp_scene.push(element)
  245. }
  246. }
  247. return temp_scene;
  248. }
  249. protected onDestroy(): void {
  250. ClientEvent.off(config.EventRun.WIDGET_FINISH,this.onWidgetFinishEvent.bind(this),this)
  251. }
  252. public startLevelGame(){
  253. this.initEvent()
  254. console.log("this.mGameData",this.mGameData,this.mCurSceneIndex)
  255. this.mSceneManager.startLevelGame(this.mGameData[this.mCurSceneIndex])
  256. taskServce.initTask(this.mGameData[this.mCurSceneIndex]._task_data)
  257. this.scheduleOnce(()=>{
  258. this.startGame()
  259. },0.5)
  260. if(gameManager.Singleton.getLevelData()){
  261. this.mSceneManager.playMusic(this.mLevelData.piped_music,true)
  262. }
  263. }
  264. public loadScene(pages: scene_item_data[],type:number){
  265. this.mGameRun.loadSceneLayer(pages,type)
  266. }
  267. public loadUi(ui_widge_list:widget_item_data[]){
  268. this.mGameRun.loadUILayer(ui_widge_list)
  269. }
  270. public loadTextSound(ui_widge_list:widget_item_data[]){
  271. this.mGameRun.loadTextSound(ui_widge_list)
  272. }
  273. public loadQuestion(ui_widge_list:widget_item_data[]){
  274. this.mGameRun.initQuestionList(ui_widge_list)
  275. }
  276. public initCountDownList(ui_widge_list:widget_item_data[]){
  277. this.mGameRun.initCountDownList(ui_widge_list)
  278. }
  279. public initTaskUi(data:task_data){
  280. this.mGameRun.initTaskUi(data)
  281. }
  282. public startGame(){
  283. taskServce.startFirstEvent()
  284. }
  285. public showCheck(call){
  286. this.mGameRun.showCheck(call)
  287. }
  288. public showLoadingLevel(){
  289. this.mGameRun.showLoadingLevel()
  290. }
  291. public hideLoadingLevel(){
  292. this.mGameRun.hideLoadingLevel()
  293. }
  294. public static getWidgetList(scene_data:scene_item_data):widget_item_data[]{
  295. let temp:widget_item_data[] = []
  296. for (let index = 0; index < scene_data.page_widget_list.length; index++) {
  297. const data = scene_data.page_widget_list[index];
  298. if(data.type<=4 ){
  299. temp.push(data)
  300. }
  301. }
  302. return temp;
  303. }
  304. public static getUIWidgetList(scene_data:scene_item_data){
  305. let temp:widget_item_data[] = []
  306. for (let index = 0; index < scene_data.page_widget_list.length; index++) {
  307. const data = scene_data.page_widget_list[index];
  308. if(data.type===config.Widget_Type_List.UI_TOP){
  309. if(data.att.top_data!=null&&data.att.top_data.top_ui_type!=config.top_view_type.__null){
  310. temp.push(data)
  311. }
  312. }
  313. }
  314. return temp;
  315. }
  316. public static getQuestionwidgetList(scene_data:scene_item_data){
  317. let temp:widget_item_data[] = []
  318. for (let index = 0; index < scene_data.page_widget_list.length; index++) {
  319. const data = scene_data.page_widget_list[index];
  320. if(data.type===config.Widget_Type_List.QUESTION_SELECT){
  321. if(data.att.question_select!=null){
  322. temp.push(data)
  323. }
  324. }
  325. }
  326. return temp;
  327. }
  328. public static getTextSoundWidgetList(scene_data:scene_item_data){
  329. let temp:widget_item_data[] = []
  330. for (let index = 0; index < scene_data.page_widget_list.length; index++) {
  331. const data = scene_data.page_widget_list[index];
  332. if(data.type===config.Widget_Type_List.TEXT_SOUND){
  333. if(data.att.text_sound_data!=null){
  334. temp.push(data)
  335. }
  336. }
  337. }
  338. return temp;
  339. }
  340. public static getCountDownWidgetList(scene_data:scene_item_data){
  341. let temp:widget_item_data[] = []
  342. for (let index = 0; index < scene_data.page_widget_list.length; index++) {
  343. const data = scene_data.page_widget_list[index];
  344. if(data.type===config.Widget_Type_List.COUNT_DOWN){
  345. if(data.att.count_down!=null){
  346. temp.push(data)
  347. }
  348. }
  349. }
  350. return temp;
  351. }
  352. public static getCacheSpriteFrameByName(sfName:string){
  353. // return control.res_map.get(sfName)
  354. return gameManager.res_map.get(sfName)
  355. }
  356. public static getCacheSoundByName(sfName:string){
  357. // return control.mp3_cache.get(sfName)
  358. return gameManager.mp3_cache.get(sfName)
  359. }
  360. public static initUiBaseAtt(node:Node,att:ui_att_item){
  361. node.getComponent(UITransform).setContentSize(new Size(att.width,att.height))
  362. node.position = new Vec3(att.x,att.y);
  363. node.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(att.res)
  364. }
  365. onWidgetFinishEvent(widget_id:number){
  366. taskServce.onWidgetFinishEvent(widget_id)
  367. }
  368. onWidgetFailEvent(widget_id:number){
  369. taskServce.onWidgetFailEvent(widget_id)
  370. }
  371. onUiCloseEvent(event_id:number){
  372. taskServce.extEventByEventId(event_id)
  373. }
  374. onUiFinishEvent(event_id:number){
  375. taskServce.extEventByEventId(event_id)
  376. }
  377. onUiFailEvent(event_id:number){
  378. taskServce.extEventByEventId(event_id)
  379. }
  380. onCountDownFailEvent(event_id:number){
  381. taskServce.extEventByEventId(event_id)
  382. }
  383. onStartCountDownEvent(event_id:number){
  384. taskServce.extEventByEventId(event_id)
  385. }
  386. exeEvent(event_id:number){
  387. taskServce.extEventByEventId(event_id)
  388. }
  389. showFindRuleTips(){
  390. this.mGameRun.showFindRuleTips()
  391. }
  392. showTips(){
  393. this.mGameRun.showTips()
  394. }
  395. showRule(){
  396. this.mGameRun.showRule()
  397. }
  398. getSceneManager(){
  399. return this.mSceneManager;
  400. }
  401. IsOpenRuleStatus(){
  402. return taskServce.guo_ju_qing_binding_event_id==-1;
  403. }
  404. public checkWidgetList(list:other_widget_finish_listen_item[]):number{
  405. let event_id = -1;
  406. for (let index = 0; index < list.length; index++) {
  407. const item = list[index];
  408. let isFinish = this.mGameRun.isCurScenePageCheckWidgetFinish(item.widget_id)
  409. if(!isFinish){
  410. event_id = item.event_id;
  411. break;
  412. }
  413. }
  414. return event_id;
  415. }
  416. public isCurScenePageCheckWidgetFinish(widget_id:number){
  417. return this.mGameRun.isCurScenePageCheckWidgetFinish(widget_id);
  418. }
  419. public isCurScenePageCheckWidgetShow(widget_id:number){
  420. return this.mGameRun.isCurScenePageCheckWidgetShow(widget_id);
  421. }
  422. //点击按钮音效
  423. public sys_click_button_music(){
  424. if(gameManager.sys_data!=null){
  425. this.mSysSound.playSound(gameManager.sys_data.content.sys_click_button_music)
  426. }
  427. }
  428. // //失败吓人音效
  429. // public sys_fail_scary_music(){
  430. // if(gameManager.sys_data!=null){
  431. // this.mSysSound.playSound(gameManager.sys_data.content.sys_fail_scary_music)
  432. // }
  433. // }
  434. //失败后攻击音效
  435. public sys_fail_attack_music(){
  436. if(gameManager.sys_data!=null){
  437. this.mSysSound.playSound(gameManager.sys_data.content.sys_fail_attack_music)
  438. }
  439. }
  440. //失败提示音效
  441. public sys_fail_prompt_music(){
  442. if(gameManager.sys_data!=null){
  443. this.mSysSound.playSound(gameManager.sys_data.content.sys_fail_prompt_music)
  444. }
  445. }
  446. //正确通关音效
  447. public sys_success_complete_music(){
  448. if(gameManager.sys_data!=null){
  449. this.mSysSound.playSound(gameManager.sys_data.content.sys_success_complete_music)
  450. }
  451. }
  452. //点击正确细节音效
  453. public sys_click_correct_detail_music(){
  454. if(gameManager.sys_data!=null){
  455. this.mSysSound.playSound(gameManager.sys_data.content.sys_click_correct_detail_music)
  456. }
  457. }
  458. public getCurSceneIndex(){
  459. return this.mCurSceneIndex;
  460. }
  461. }