gameManager.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. import { _decorator, assetManager, AudioClip, Component, instantiate, Node, Prefab, resources, SpriteFrame, sys } from 'cc';
  2. import { server_han_zi_zhao_bu_tong_data, server_mei_nv_zhao_xi_jie_data, server_play_info, server_play_list_data, server_shuang_tu_zhao_bu_tong_data, server_sys_info, server_user_info, unlock_levels, unlock_levels_item } from './data/server_play_list_data';
  3. import { config } from './config';
  4. import { ClientEvent } from './framework/clientEvent';
  5. import { http } from './http/http';
  6. import { StorageManager } from './framework/storageManager';
  7. import { tools } from './tools';
  8. import { AudioManager } from './framework/audioManager';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('gameManager')
  11. export class gameManager extends Component {
  12. public static g_server_sys_info:server_sys_info = new server_sys_info();
  13. public static g_server_play_list_data:server_play_list_data = new server_play_list_data();
  14. // public static g_server_shuang_tu_zhao_bu_tong_data:server_shuang_tu_zhao_bu_tong_data = new server_shuang_tu_zhao_bu_tong_data();
  15. // public static g_server_han_zi_zhao_bu_tong_data:server_han_zi_zhao_bu_tong_data = new server_han_zi_zhao_bu_tong_data();
  16. // public static g_server_mei_nv_zhao_xi_jie_data:server_mei_nv_zhao_xi_jie_data = new server_mei_nv_zhao_xi_jie_data();
  17. private static uiLayer:Node = null;
  18. private static topLayer:Node = null;
  19. private static waitLayer:Node = null;
  20. public static userInfo:server_user_info = new server_user_info();
  21. public static uiViewList:Map<string,Node> = new Map();
  22. public static topViewList:Map<string,Node> = new Map();
  23. public static user_level_data:unlock_levels = new unlock_levels();
  24. public static PreloadingLevelImgNumber:number = 0;//执行预加载的数量
  25. start() {
  26. }
  27. public static init_unlock_levels(){
  28. for (let index = 0; index < gameManager.g_server_play_list_data.list.length; index++) {
  29. const element = gameManager.g_server_play_list_data.list[index];
  30. let item:unlock_levels_item = new unlock_levels_item;
  31. item.id = element.id;
  32. item.level = 1;
  33. if(item.id===config.PLAY_TYPE.YI_QI_ZHAO_CHA){
  34. item.is_lock_game_play = 1;
  35. }else{
  36. item.is_lock_game_play = 0;
  37. }
  38. gameManager.user_level_data.list[element.id] = item;
  39. }
  40. }
  41. public static isFinishAllLevel(categoryid:number):boolean{
  42. let item = gameManager.get_cur_level_by_categoryid(categoryid)
  43. if(item.level===gameManager.get_play_level_number(categoryid)){
  44. return true;
  45. }
  46. return false;
  47. }
  48. public static get_play_level_number(categoryid:number):number{
  49. let list = gameManager.g_server_play_list_data.list;
  50. let number = 0;
  51. for (let index = 0; index < list.length; index++) {
  52. if(categoryid===list[index].id){
  53. number = list[index].barrier_number;
  54. break;
  55. }
  56. }
  57. return number;
  58. }
  59. public static get_unlock_levels():string{
  60. if(gameManager.user_level_data.list.length>0){
  61. return JSON.stringify(gameManager.user_level_data.list);
  62. }
  63. return "";
  64. }
  65. public static set_unlock_levels(unlock_levels:string){
  66. gameManager.user_level_data.list = JSON.parse(unlock_levels);
  67. }
  68. public static set_unlock_levels_data(item:unlock_levels_item){
  69. gameManager.user_level_data.list[item.id] = item;
  70. }
  71. public static setWaitLayer(node:Node){
  72. gameManager.waitLayer = node;
  73. }
  74. public static sync_data(){
  75. let form_Data = null;
  76. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME){
  77. form_Data = {"coin": gameManager.userInfo.coin+"","unlock_levels": gameManager.get_unlock_levels()}
  78. }else{
  79. form_Data = new FormData();
  80. form_Data.append("coin", gameManager.userInfo.coin+"");
  81. form_Data.append("unlock_levels", gameManager.get_unlock_levels());
  82. }
  83. http.post(config.api_url.sync_data,form_Data,(_err,c_data)=>{
  84. if(!_err){
  85. let c__data = JSON.parse(c_data);
  86. console.log("c__data",c__data)
  87. if(c__data.code==config.MSG_CODE.SUCCESS){
  88. ClientEvent.dispatchEvent(config.EVENT_MSG.ON_CHANGE_COIN_NUMBER);
  89. }
  90. }
  91. })
  92. }
  93. public static get_user_coin():number{
  94. return gameManager.userInfo.coin;
  95. }
  96. public static add_coin(coin:number){
  97. gameManager.userInfo.coin+=coin;
  98. }
  99. public static sub_coin(coin:number){
  100. gameManager.userInfo.coin-=coin;
  101. if( gameManager.userInfo.coin<0){
  102. gameManager.userInfo.coin = 0;
  103. }
  104. }
  105. public static showLoading(){
  106. gameManager.addTopView(config.PREFAB_PATH.view_loading,null);
  107. }
  108. public static hideLoading(){
  109. if(gameManager.topViewList.get(config.PREFAB_PATH.view_loading)){
  110. gameManager.topViewList.get(config.PREFAB_PATH.view_loading).removeFromParent();
  111. gameManager.topViewList.set(config.PREFAB_PATH.view_loading,null);
  112. }
  113. }
  114. public static get_cur_level_by_categoryid(categoryid:number):unlock_levels_item{
  115. if(gameManager.user_level_data.list.length<=0){
  116. return null;
  117. }
  118. switch (categoryid) {
  119. case config.PLAY_TYPE.YI_QI_ZHAO_CHA:
  120. return gameManager.user_level_data.list[config.PLAY_TYPE.YI_QI_ZHAO_CHA];
  121. case config.PLAY_TYPE.HAN_ZI_ZHAO_BU_TONG:
  122. return gameManager.user_level_data.list[config.PLAY_TYPE.HAN_ZI_ZHAO_BU_TONG];
  123. case config.PLAY_TYPE.MEI_NV_ZHAO_XI_JIE:
  124. return gameManager.user_level_data.list[config.PLAY_TYPE.MEI_NV_ZHAO_XI_JIE];
  125. }
  126. }
  127. public static next_level(categoryid:number):boolean{
  128. let item = gameManager.get_cur_level_by_categoryid(categoryid);
  129. let play_item = gameManager.get_play_list_item_by_categoryid(categoryid);
  130. if(item.level<play_item.barrier_number){
  131. item.level+=1;
  132. gameManager.set_unlock_levels_data(item);
  133. gameManager.sync_data()
  134. return true;
  135. }
  136. return false;
  137. }
  138. public static setUiLayer(node:Node){
  139. gameManager.uiLayer = node;
  140. }
  141. public static getTopLayer():Node{
  142. return gameManager.topLayer;
  143. }
  144. public static setTopLayer(node:Node){
  145. gameManager.topLayer = node;
  146. }
  147. public static addUiView(parent_path:string,call_back){
  148. if(gameManager.uiLayer==null){
  149. console.log("gameManager.uiLayer -- null!");
  150. return;
  151. }
  152. gameManager.showWaitView();
  153. resources.load(parent_path, Prefab, (err, prefab) => {
  154. if(err){
  155. console.log("addUiView -- err::",err);
  156. return;
  157. }
  158. let newNode:Node = instantiate(prefab);
  159. newNode.parent = gameManager.uiLayer;
  160. gameManager.uiViewList.set(parent_path,newNode);
  161. gameManager.hideWaitView();
  162. if(call_back!=null){
  163. call_back(newNode);
  164. }
  165. });
  166. }
  167. public static addTopView(parent_path:string,call_back){
  168. if(gameManager.topLayer==null){
  169. console.log("gameManager.topLayer -- null!");
  170. return;
  171. }
  172. gameManager.showWaitView();
  173. resources.load(parent_path, Prefab, (err, prefab) => {
  174. if(err){
  175. console.log("addTopView -- err::",err);
  176. return;
  177. }
  178. let newNode:Node = instantiate(prefab);
  179. gameManager.topViewList.set(parent_path,newNode);
  180. newNode.parent = gameManager.topLayer;
  181. gameManager.hideWaitView();
  182. if(call_back!=null){
  183. call_back(newNode);
  184. }
  185. });
  186. }
  187. public static showWaitView(){
  188. gameManager.waitLayer.active = true;
  189. }
  190. public static hideWaitView(){
  191. gameManager.waitLayer.active = false;
  192. }
  193. public static set_server_user_data(data){
  194. gameManager.userInfo = data;
  195. if(gameManager.userInfo.unlock_levels!=""){
  196. gameManager.set_unlock_levels(gameManager.userInfo.unlock_levels);
  197. }
  198. console.log("set_server_user_data::",data);
  199. }
  200. public static set_server_play_list_data(data:server_play_info){
  201. gameManager.g_server_play_list_data.list = data.wf_list;
  202. gameManager.g_server_sys_info = data.sys_info;
  203. ClientEvent.dispatchEvent(config.EVENT_MSG.ON_INIT_GAME_LIST);
  204. console.log("set_server_play_list_data::",data);
  205. }
  206. public static get_play_list_item_by_categoryid(categoryid:number){
  207. for (let index = 0; index < gameManager.g_server_play_list_data.list.length; index++) {
  208. const element = gameManager.g_server_play_list_data.list[index];
  209. if(element.id===categoryid){
  210. return element;
  211. }
  212. }
  213. return null;
  214. }
  215. public static PreloadingLevelImg(finish_call_back){
  216. if(gameManager.g_server_play_list_data.list.length<=0){
  217. return
  218. }
  219. if(gameManager.PreloadingLevelImgNumber<=0){
  220. let count = 0;
  221. let call_back = ()=>{
  222. count++;
  223. if(count>=gameManager.PreloadingLevelImgNumber){
  224. gameManager.PreloadingLevelImgNumber = 0;
  225. finish_call_back()
  226. }
  227. }
  228. for (let index = 0; index < gameManager.g_server_play_list_data.list.length; index++) {
  229. const element = gameManager.g_server_play_list_data.list[index];
  230. let item = gameManager.get_cur_level_by_categoryid(element.id);
  231. let key = `${element.id}_${item.level+1}`;
  232. let str = StorageManager.instance.getGlobalData(key);
  233. if(str===""||str===undefined||str===null){
  234. gameManager.PreloadingLevelImgNumber++;
  235. gameManager.loadLevelImg(element.id,item.level+1,call_back)
  236. }
  237. }
  238. }
  239. }
  240. public static loadLevelImg(category_id,level,call_back){
  241. let key = `${category_id}_${level}`;
  242. http.get(config.static_url.levels_info(category_id,level),(err,data)=>{
  243. if(!err){
  244. let _data = JSON.parse(data)
  245. if(_data.code===config.MSG_CODE.SUCCESS){
  246. let data = _data.content
  247. switch(category_id){
  248. case config.PLAY_TYPE.YI_QI_ZHAO_CHA:
  249. let img_count = 0;
  250. let img_call_back = ()=>{
  251. if(img_count>=2){
  252. console.log("key",key)
  253. StorageManager.instance.setGlobalData(key,"1")
  254. call_back();
  255. }
  256. }
  257. tools.loadRemoteImg(data.img1,(sf:SpriteFrame)=>{
  258. sf.addRef();
  259. img_count++;
  260. img_call_back();
  261. })
  262. tools.loadRemoteImg(data.img2,(sf)=>{
  263. sf.addRef();
  264. img_count++;
  265. img_call_back();
  266. })
  267. break;
  268. case config.PLAY_TYPE.HAN_ZI_ZHAO_BU_TONG:
  269. let img_count_font = 0;
  270. let img_call_back_font = ()=>{
  271. if(img_count_font>=3){
  272. console.log("key",key)
  273. StorageManager.instance.setGlobalData(key,"1")
  274. call_back();
  275. }
  276. }
  277. tools.loadRemoteImg(data.default_img,(sf)=>{
  278. sf.addRef();
  279. img_count_font++;
  280. img_call_back_font();
  281. })
  282. tools.loadRemoteImg(data.find_img,(sf)=>{
  283. sf.addRef();
  284. img_count_font++;
  285. img_call_back_font();
  286. })
  287. tools.loadRemoteImg(data.d_img,(sf)=>{
  288. sf.addRef();
  289. img_count_font++;
  290. img_call_back_font();
  291. })
  292. break;
  293. case config.PLAY_TYPE.MEI_NV_ZHAO_XI_JIE:
  294. let img_count_component = 0;
  295. let img_call_back_component = ()=>{
  296. if(img_count_component>=data.component.length+1){
  297. console.log("key",key)
  298. StorageManager.instance.setGlobalData(key,"1")
  299. call_back();
  300. }
  301. }
  302. tools.loadRemoteImg(data.img,(sf)=>{
  303. sf.addRef();
  304. img_count_component++;
  305. img_call_back_component();
  306. })
  307. for (let index = 0; index < data.component.length; index++) {
  308. const element = data.component[index];
  309. tools.loadRemoteImg(element.img,(sf,id)=>{
  310. sf.addRef();
  311. img_count_component++;
  312. img_call_back_component();
  313. },element.id);
  314. }
  315. break;
  316. }
  317. }
  318. }
  319. })
  320. }
  321. //通用的关闭按钮点击音效
  322. public static playCloseBtnSound( loop:boolean = false){
  323. let path = gameManager.g_server_sys_info.sys_click_button_music;
  324. AudioManager.instance.playSound(path,loop)
  325. }
  326. //通用的按钮点击音效
  327. public static playBtnSound( loop:boolean = false){
  328. let path = gameManager.g_server_sys_info.sys_close_button_music;
  329. AudioManager.instance.playSound(path,loop)
  330. }
  331. //每个玩法的胜利音效
  332. public static playWinSoundByCategoryId(id:number, loop:boolean = false){
  333. let paht = gameManager.get_play_list_item_by_categoryid(id).wf_victory_music;
  334. AudioManager.instance.playSound(paht,loop)
  335. }
  336. //每个玩法的失败音效
  337. public static playFailSoundByCategoryId(id:number, loop:boolean = false){
  338. let paht = gameManager.get_play_list_item_by_categoryid(id).wf_fail_music;
  339. AudioManager.instance.playSound(paht,loop)
  340. }
  341. //每个玩法的正确音效
  342. public static playRightSoundByCategoryId(id:number, loop:boolean = false){
  343. let paht = gameManager.get_play_list_item_by_categoryid(id).wf_right_click_music;
  344. AudioManager.instance.playSound(paht,loop)
  345. }
  346. //每个玩法的错误音效
  347. public static playErrSoundByCategoryId(id:number, loop:boolean = false){
  348. let paht = gameManager.get_play_list_item_by_categoryid(id).wf_wrong_click_music;
  349. AudioManager.instance.playSound(paht,loop)
  350. }
  351. //每个玩法的背景音乐
  352. public static playGamePlayMusicByCategoryId(id:number, loop:boolean = false){
  353. let path = gameManager.get_play_list_item_by_categoryid(id).wf_piped_music;
  354. AudioManager.instance.playMusic(path,loop)
  355. }
  356. //主页的背景音乐
  357. public static playMainMusic(loop:boolean = false){
  358. let path = gameManager.g_server_sys_info.sys_piped_music;
  359. AudioManager.instance.playMusic(path,loop)
  360. }
  361. public static loadSysMusic(call_back){
  362. let count = 0;
  363. let call = ()=>{
  364. count++;
  365. if(count>=3){
  366. call_back()
  367. }
  368. }
  369. assetManager.loadRemote(gameManager.g_server_sys_info.sys_click_button_music, (err: any, clip: AudioClip)=> {
  370. clip.addRef()
  371. call()
  372. });
  373. assetManager.loadRemote(gameManager.g_server_sys_info.sys_close_button_music, (err: any, clip: AudioClip)=> {
  374. clip.addRef()
  375. call()
  376. });
  377. assetManager.loadRemote(gameManager.g_server_sys_info.sys_piped_music, (err: any, clip: AudioClip)=> {
  378. clip.addRef()
  379. call()
  380. });
  381. }
  382. public static loadPlayMusicByCategoryId(id,call_back){
  383. let count = 0;
  384. let call = ()=>{
  385. count++;
  386. if(count>=5){
  387. call_back()
  388. }
  389. }
  390. let paly_item = gameManager.get_play_list_item_by_categoryid(id);
  391. assetManager.loadRemote(paly_item.wf_fail_music, (err: any, clip: any)=> {
  392. clip.addRef()
  393. call()
  394. });
  395. assetManager.loadRemote(paly_item.wf_victory_music, (err: any, clip: any)=> {
  396. clip.addRef()
  397. call()
  398. });
  399. assetManager.loadRemote(paly_item.wf_right_click_music, (err: any, clip: any)=> {
  400. clip.addRef()
  401. call()
  402. });
  403. assetManager.loadRemote(paly_item.wf_wrong_click_music, (err: any, clip: any)=> {
  404. clip.addRef()
  405. call()
  406. });
  407. assetManager.loadRemote(paly_item.wf_piped_music, (err: any, clip: any)=> {
  408. clip.addRef()
  409. call()
  410. });
  411. }
  412. // public static set_server_han_zi_zhao_bu_tong_data(data){
  413. // gameManager.g_server_han_zi_zhao_bu_tong_data.list = data;
  414. // console.log("set_server_han_zi_zhao_bu_tong_data::",data);
  415. // }
  416. // public static set_server_mei_nv_zhao_xi_jie_data(data){
  417. // gameManager.g_server_mei_nv_zhao_xi_jie_data.list = data;
  418. // console.log("set_server_mei_nv_zhao_xi_jie_data::",data);
  419. // }
  420. }