taskServce.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. import { _decorator, Component, Node } from 'cc';
  2. import { da_guai_item_data, event_active_event_item, event_collect_event_item, event_item, event_start_event_item, task_data, zhao_xi_jie_item_data } from '../../../data/data';
  3. import { config } from '../../config';
  4. import { event_start_event } from '../../edit/event/event_start_event';
  5. import { tools } from '../../tools';
  6. import { ClientEvent } from '../../clientEvent';
  7. import { gameManager } from '../gameManager';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('taskServce')
  10. export class taskServce {
  11. private static mTaskData:task_data = null;
  12. private static exeWidgeTaskDic:Map<number,event_item> = new Map;
  13. private static taskZhaoXiJie:Map<number,zhao_xi_jie_item_data> = new Map;
  14. private static taskDaGuai:Map<number,da_guai_item_data> = new Map;
  15. private static finishZhaoXiJie:Map<number,boolean> = new Map;
  16. private static finishDaGuai:Map<number,boolean> = new Map;
  17. private static cur_collect_event:event_item = null;
  18. public static guo_ju_qing_binding_event_id:number = -1;
  19. //子场景
  20. private static mChildSceneData:task_data = null;
  21. private static childSceneZhaoXiJie:Map<number,zhao_xi_jie_item_data> = new Map;
  22. private static childSceneGuoJuQingBidingEventId:number = -1;
  23. public static initTask(data:task_data){
  24. taskServce.clearTask()
  25. taskServce.mTaskData = data;
  26. if(taskServce.mTaskData==null){
  27. // tools.showToast("当前场景没有任务")
  28. return
  29. }
  30. this.initAllBeEventListen()
  31. // this.initAllHideWidgetListen()
  32. switch (taskServce.mTaskData.type) {
  33. case config.task_type.zhao_xi_jie:
  34. taskServce.initZhaoXiJieTask()
  35. break;
  36. case config.task_type.guo_ju_qing:
  37. taskServce.guo_ju_qing_binding_event_id = taskServce.mTaskData._guo_ju_qing.binding_event_id;
  38. break;
  39. case config.task_type.guo_ju_qing_not_rule:
  40. break;
  41. case config.task_type.da_guai:
  42. taskServce.initDaBoss()
  43. break;
  44. }
  45. }
  46. public static clearTask(){
  47. taskServce.mTaskData = null;
  48. taskServce.exeWidgeTaskDic.clear();
  49. // taskServce.exeHideWidgeTaskDic.clear();
  50. taskServce.taskZhaoXiJie.clear()
  51. taskServce.taskDaGuai.clear()
  52. taskServce.finishDaGuai.clear()
  53. taskServce.finishZhaoXiJie.clear();
  54. taskServce.cur_collect_event = null;
  55. taskServce.guo_ju_qing_binding_event_id = -1;
  56. }
  57. public static initZhaoXiJieTask(){
  58. if(taskServce.mTaskData.type==config.task_type.zhao_xi_jie){
  59. if(taskServce.mTaskData._zhao_xi_jie_data===null){
  60. return tools.showToast("找细节配置错误")
  61. }
  62. }
  63. for (let index = 0; index < taskServce.mTaskData._zhao_xi_jie_data.widget_id_list.length; index++) {
  64. const widget = taskServce.mTaskData._zhao_xi_jie_data.widget_id_list[index];
  65. taskServce.taskZhaoXiJie.set(widget.widget_id,widget);
  66. }
  67. }
  68. public static initDaBoss(){
  69. if(taskServce.mTaskData.type==config.task_type.da_guai){
  70. if(taskServce.mTaskData._da_guai===null){
  71. return tools.showToast("打boss 配置错误")
  72. }
  73. }
  74. for (let index = 0; index < taskServce.mTaskData._da_guai.widget_id_list.length; index++) {
  75. const widget = taskServce.mTaskData._da_guai.widget_id_list[index];
  76. taskServce.taskDaGuai.set(widget.widget_id,widget);
  77. }
  78. }
  79. public static initChildSceneZhaoXiJieTask(){
  80. if(taskServce.mChildSceneData.type==config.task_type.zhao_xi_jie){
  81. if(taskServce.mChildSceneData._zhao_xi_jie_data===null){
  82. return tools.showToast("找细节配置错误")
  83. }
  84. }
  85. for (let index = 0; index < taskServce.mChildSceneData._zhao_xi_jie_data.widget_id_list.length; index++) {
  86. const widget = taskServce.mChildSceneData._zhao_xi_jie_data.widget_id_list[index];
  87. taskServce.childSceneZhaoXiJie.set(widget.widget_id,widget);
  88. }
  89. }
  90. public static startFirstEvent(){
  91. if(taskServce.mTaskData==null){
  92. // return tools.showToast("当前场景没有任务")
  93. }
  94. let startEvent:event_start_event_item = this.getStartEvent()
  95. if(startEvent==null){
  96. return tools.showToast("第一个任务不可以为空,编辑错误!")
  97. }
  98. let event:event_item = this.getEventById(startEvent.binding_event_id)
  99. taskServce.DistributedEvent(event)
  100. }
  101. public static StartChildSceneTask(data:task_data){
  102. taskServce.mChildSceneData = data;
  103. switch (taskServce.mChildSceneData.type) {
  104. case config.task_type.zhao_xi_jie:
  105. taskServce.initChildSceneZhaoXiJieTask()
  106. break;
  107. case config.task_type.guo_ju_qing:
  108. taskServce.childSceneGuoJuQingBidingEventId = taskServce.mChildSceneData._guo_ju_qing.binding_event_id;
  109. break;
  110. case config.task_type.guo_ju_qing_not_rule:
  111. break;
  112. case config.task_type.da_guai:
  113. break;
  114. }
  115. }
  116. public static DistributedEvent(event:event_item){
  117. // console.log("event",event)
  118. if(event==null){
  119. return
  120. }
  121. if(event.event_id===taskServce.guo_ju_qing_binding_event_id){
  122. taskServce.guo_ju_qing_binding_event_id = -1;
  123. ClientEvent.dispatchEvent(config.EventRun.ON_SHOW_RULE_BTN)
  124. }
  125. switch(event.type){
  126. case config.event_type.play_ani:
  127. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_play_ani_data.binding_widget_id,event)
  128. break;
  129. case config.event_type.top_view:
  130. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_top_view_data.binding_ui_id,event)
  131. break;
  132. case config.event_type.text_sound:
  133. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_text_sound_data.binding_widget_id,event)
  134. break;
  135. case config.event_type.change_one_item_status:
  136. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_change_one_item_status_data.binding_widget_id,event)
  137. break;
  138. case config.event_type.show_new_item:
  139. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_show_new_item_data.binding_widget_id,event)
  140. break;
  141. case config.event_type.show_question_select:
  142. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_show_question_select_data.binding_widget_id,event)
  143. break;
  144. case config.event_type.collect_event:
  145. taskServce.cur_collect_event = new event_item;
  146. taskServce.cur_collect_event.event_id = event.event_id;
  147. taskServce.cur_collect_event.type = event.type;
  148. taskServce.cur_collect_event.event_item_collect_event_data = new event_collect_event_item
  149. for (let index = 0; index < event.event_item_collect_event_data.event_id_list.length; index++) {
  150. const element = event.event_item_collect_event_data.event_id_list[index];
  151. taskServce.cur_collect_event.event_item_collect_event_data.event_id_list.push(element)
  152. }
  153. // console.log("collect_event--",taskServce.cur_collect_event)
  154. for (let index = 0; index < taskServce.cur_collect_event.event_item_collect_event_data.event_id_list.length; index++) {
  155. const element = taskServce.cur_collect_event.event_item_collect_event_data.event_id_list[index];
  156. let widget_id = taskServce.getEventById(element).event_item_be_event_data.binding_widget_id
  157. let event_data = new event_item
  158. let active_event = new event_active_event_item
  159. active_event.binding_widget_id = widget_id
  160. event_data.type = config.event_type.active_event
  161. event_data.event_item_active_event_data = active_event
  162. taskServce.DistributedEvent(event_data)
  163. console.log("event_data--",event_data)
  164. }
  165. break;
  166. // case config.event_type.start_event:
  167. // break;
  168. case config.event_type.active_event:
  169. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_active_event_data.binding_widget_id,event)
  170. break;
  171. case config.event_type.stop_active_event:
  172. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_stop_active_event_data.binding_widget_id,event)
  173. break;
  174. case config.event_type.countdown_over:
  175. break;
  176. case config.event_type.be_event:
  177. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_be_event_data.binding_widget_id,event)
  178. break;
  179. case config.event_type.hide:
  180. if(event.event_item_hide_data==null){
  181. return tools.showToast("隐藏事件配置错误~")
  182. }
  183. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_hide_data.binding_widget_id,event)
  184. break;
  185. case config.event_type.delay_exe_event:
  186. console.log("delay_exe_event")
  187. if(event.event_item_delay_exe_data==null){
  188. return tools.showToast("延迟执行事件配置错误")
  189. }
  190. let delay_time = event.event_item_delay_exe_data.delay_time;
  191. let event_id = event.event_item_delay_exe_data.binding_event_id;
  192. gameManager.Singleton.scheduleOnce(()=>{
  193. let event:event_item = this.getEventById(event_id)
  194. taskServce.DistributedEvent(event)
  195. },delay_time)
  196. break;
  197. case config.event_type.fail_event:
  198. console.log("play_sound --- 1")
  199. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
  200. break;
  201. case config.event_type.play_sound:
  202. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
  203. break;
  204. case config.event_type.show_scene_title:
  205. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
  206. break;
  207. case config.event_type.scene_ani:
  208. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
  209. break;
  210. case config.event_type.delete_drag_other_data:
  211. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
  212. break;
  213. case config.event_type.stop_play_loop_sound:
  214. ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
  215. break;
  216. }
  217. taskServce.exeNextDelayEvent(event)
  218. }
  219. /**
  220. *
  221. * @zh 根据id 获取事件
  222. *
  223. */
  224. public static getEventById(event_id:number):event_item{
  225. for (let index = 0; index < taskServce.mTaskData.event_list.length; index++) {
  226. const event = taskServce.mTaskData.event_list[index];
  227. if(event.event_id===event_id){
  228. return event;
  229. }
  230. }
  231. return null;
  232. }
  233. public static initAllBeEventListen(){ //把所有被动事件加进来
  234. for (let index = 0; index < taskServce.mTaskData.event_list.length; index++) {
  235. const element = taskServce.mTaskData.event_list[index];
  236. if(element.type===config.event_type.be_event){
  237. this.pushWidgetEvent(element.event_item_be_event_data.binding_widget_id,element)
  238. }
  239. }
  240. }
  241. // public static initAllHideWidgetListen(){ //把所有被动事件加进来
  242. // for (let index = 0; index < taskServce.mTaskData.event_list.length; index++) {
  243. // const element = taskServce.mTaskData.event_list[index];
  244. // if(element.type===config.event_type.hide){
  245. // this.pushHideWidgetEvent(element.event_item_hide_data.binding_widget_id,element)
  246. // }
  247. // }
  248. // }
  249. /**
  250. *
  251. * @zh 获取开始任务
  252. *
  253. */
  254. public static getStartEvent():event_start_event_item{
  255. if(taskServce.mTaskData==null){
  256. return null;
  257. }
  258. for (let index = 0; index < taskServce.mTaskData.event_list.length; index++) {
  259. const event = taskServce.mTaskData.event_list[index];
  260. if(event.type===config.event_type.start_event){
  261. return event.event_item_start_event_data;
  262. }
  263. }
  264. return null;
  265. }
  266. public static pushWidgetEvent(widget:number,event:event_item){
  267. this.exeWidgeTaskDic.set(widget,event)
  268. }
  269. // public static pushHideWidgetEvent(widget:number,event:event_item){
  270. // this.exeHideWidgeTaskDic.set(widget,event)
  271. // }
  272. public static onWidgetFailEvent(widget_id:number){ //监听被动控件失败了自己的任务
  273. let event = this.exeWidgeTaskDic.get(widget_id)
  274. if(event!=null){
  275. if(event.fail.is_restart){
  276. return gameManager.Singleton.reStartGame()
  277. }else{
  278. if(event.fail.is_relife){
  279. return gameManager.Singleton.reLife()
  280. }
  281. }
  282. let id = event.success.trigger_event_id;
  283. if(id!=-1){
  284. let event:event_item = this.getEventById(id)
  285. taskServce.DistributedEvent(event)
  286. }
  287. }
  288. }
  289. public static onWidgetFinishEvent(widget_id:number){ //监听被动控件完成了自己的任务
  290. if(taskServce.mTaskData==null){
  291. return
  292. }
  293. let event = this.exeWidgeTaskDic.get(widget_id)
  294. if(taskServce.cur_collect_event!=null){
  295. if(event!=null){
  296. let e_index = taskServce.cur_collect_event.event_item_collect_event_data.event_id_list.indexOf(event.event_id)
  297. if(e_index!=-1){
  298. taskServce.cur_collect_event.event_item_collect_event_data.event_id_list.splice(e_index,1)
  299. }
  300. if(taskServce.cur_collect_event.event_item_collect_event_data.event_id_list.length<=0){
  301. ClientEvent.dispatchEvent(config.EventRun.ON_WIDGET_FINISH_COLLECT_EVENT,taskServce.cur_collect_event.event_id)
  302. let collect_event = taskServce.getEventById(taskServce.cur_collect_event.event_id)
  303. if(collect_event.success.trigger_event_id!=-1){
  304. taskServce.extEventByEventId(collect_event.success.trigger_event_id)
  305. }
  306. console.log("ON_WIDGET_FINISH_COLLECT_EVENT event",collect_event)
  307. }
  308. }
  309. // console.log("event", taskServce.cur_collect_event.event_item_collect_event_data.event_id_list)
  310. }
  311. let id = taskServce.exeNextEvent(event)
  312. switch (taskServce.mTaskData.type) {
  313. case config.task_type.zhao_xi_jie:
  314. let d = taskServce.taskZhaoXiJie.get(widget_id);
  315. if(d===null||d===undefined){
  316. return
  317. }
  318. taskServce.finishZhaoXiJie.set(widget_id,true)
  319. ClientEvent.dispatchEvent(config.EventRun.SHOW_ZHAO_BU_TONG_FINISH_STATUS,d)
  320. taskServce.checkFinishZhaoXiJieTask()
  321. break;
  322. case config.task_type.guo_ju_qing:
  323. break;
  324. case config.task_type.guo_ju_qing_not_rule:
  325. break;
  326. case config.task_type.da_guai:
  327. let d_1 = taskServce.taskDaGuai.get(widget_id);
  328. if(d_1===null||d_1===undefined){
  329. return
  330. }
  331. taskServce.finishDaGuai.set(widget_id,true)
  332. ClientEvent.dispatchEvent(config.EventRun.ON_BOSS_HURT,d_1.hurt)
  333. taskServce.checkFinishDaBossTask()
  334. break;
  335. }
  336. }
  337. public static checkFinishDaBossTask(){
  338. let isFinish = true;
  339. if(!taskServce.mTaskData._da_guai){
  340. return isFinish
  341. }
  342. for (let index = 0; index < taskServce.mTaskData._da_guai.widget_id_list.length; index++) {
  343. const element = taskServce.mTaskData._da_guai.widget_id_list[index];
  344. let isShow = gameManager.Singleton.isCurScenePageCheckWidgetShow(element.widget_id)
  345. // if(!taskServce.finishDaGuai.get(element.widget_id)){
  346. // isFinish = false
  347. // break;
  348. // }
  349. if(isShow){
  350. isFinish = false;
  351. break;
  352. }
  353. }
  354. if(isFinish){
  355. ClientEvent.dispatchEvent(config.EventRun.ON_BOSS_HURT,0)
  356. }
  357. }
  358. public static checkFinishZhaoXiJieTask(){
  359. let isFinish = true;
  360. for (let index = 0; index < taskServce.mTaskData._zhao_xi_jie_data.widget_id_list.length; index++) {
  361. const element = taskServce.mTaskData._zhao_xi_jie_data.widget_id_list[index];
  362. if(!taskServce.finishZhaoXiJie.get(element.widget_id)){
  363. isFinish = false
  364. break;
  365. }
  366. }
  367. if(isFinish){
  368. let id =taskServce.mTaskData._zhao_xi_jie_data.finish_event_id;
  369. ClientEvent.dispatchEvent(config.EventRun.ON_ZHAO_BU_TONG_ALL_FINISH)
  370. if(id==-1){
  371. return tools.showToast("未设置全部找完任务的下一个事件!")
  372. }else{
  373. taskServce.DistributedEvent(taskServce.getEventById(id))
  374. }
  375. }
  376. }
  377. // public static onWidgetHideEvent(widget_id:number){
  378. // let event = this.exeHideWidgeTaskDic.get(widget_id)
  379. // if(!event){
  380. // return tools.showToast("隐藏事件没有找到!")
  381. // }
  382. // ClientEvent.dispatchEvent(config.EventRun.WIDGET_HIDE,event.event_item_hide_data.binding_widget_id,event)
  383. // taskServce.exeNextEvent(event)
  384. // }
  385. public static exeNextEvent(event:event_item){
  386. let id = -1;
  387. if(event!=null){
  388. if(event.success.is_finish_game){
  389. tools.showToast("调用了游戏结束")
  390. return gameManager.Singleton.gameOver()
  391. }else{
  392. if(event.success.is_next_scene){
  393. return gameManager.Singleton.nextScene()
  394. }
  395. }
  396. id = event.success.trigger_event_id;
  397. if(id!=-1){
  398. let _event:event_item = this.getEventById(id)
  399. taskServce.DistributedEvent(_event)
  400. }
  401. }
  402. return id;
  403. }
  404. public static extEventByEventId(event_id:number){
  405. if(event_id===-1){
  406. return
  407. }
  408. let event = taskServce.getEventById(event_id)
  409. if(event==null){
  410. return tools.showToast(`事件错误${event_id}`)
  411. }
  412. taskServce.DistributedEvent(event)
  413. }
  414. public static exeNextDelayEvent(event:event_item){
  415. if(event!=null){
  416. if(event.success.is_finish_game){
  417. return gameManager.Singleton.gameOver()
  418. }else{
  419. if(event.success.is_next_scene){
  420. return gameManager.Singleton.nextScene()
  421. }
  422. }
  423. let id = event.success.trigger_event_id;
  424. if(id!=-1){
  425. let _event:event_item = this.getEventById(id)
  426. if(_event===null){
  427. return tools.showToast(`配置延迟调用错误-${id}`)
  428. }
  429. if(_event.type == config.event_type.delay_exe_event){
  430. taskServce.DistributedEvent(_event)
  431. }
  432. }
  433. }
  434. }
  435. }