123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import DateTime from "../help/DateTime";
- import { GameEvent } from "./GameEvent";
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class EventMgr extends cc.Component {
- public static Instance: EventMgr = null;
- private events_map = {};
- onLoad() {
- if (EventMgr.Instance === null) {
- EventMgr.Instance = this;
- }
- else {
- this.destroy();
- return;
- }
- }
- // func(event_name: string, udata: any)
- public add_event_listenner(event_name, caller, func) {
- if (!this.events_map[event_name]) {
- this.events_map[event_name] = [];
- }
- var event_queue = this.events_map[event_name];
- event_queue.push({
- caller: caller,
- func: func
- });
- }
- public remove_event_listenner(event_name, caller, func) {
- if (!this.events_map[event_name]) {
- return;
- }
- var event_queue = this.events_map[event_name];
- for(var i = 0; i < event_queue.length; i ++) {
- var obj = event_queue[i];
- if (obj.caller == caller && obj.func == func) {
- // if (event_name==GameEvent.HeroSuperSkill) {
- // console.error("移除监听列表:"+DateTime.formatDate());
- // console.error(obj.caller);
- // }
- event_queue.splice(i, 1);
- break;
- }
- }
- if (event_queue.length <= 0) {
- this.events_map[event_name] = null;
- }
- }
- public remove_event_all(event_name, caller) {
- if (!this.events_map[event_name]) {
- return;
- }
- var event_queue = this.events_map[event_name];
- for(var i = 0; i < event_queue.length; i ++) {
- var obj = event_queue[i];
- if (obj.caller == caller) {
- event_queue.splice(i, 1);
- break;
- }
- }
- if (event_queue.length <= 0) {
- this.events_map[event_name] = null;
- }
- }
- public dispatch_event(event_name, udata = null) {
- if (!this.events_map[event_name]) {
- return;
- }
- var event_queue = this.events_map[event_name];
-
- // if (event_name==GameEvent.HeroSuperSkill) {
- // console.warn("事件监听列表:"+ event_queue.length+" "+DateTime.formatDate());
- // }
- let eQueue= event_queue.slice();//克隆数组event_queue,否则在遍历数组过程中移除数据,会导致遍历出错
- for(var i = 0; i < eQueue.length; i ++) {
- var obj = eQueue[i];
- // if (event_name==GameEvent.HeroSuperSkill) {
- // console.warn(DateTime.formatDate());
- // console.warn(obj.caller);
- // }
- obj.func.call(obj.caller, event_name, udata);
- }
- }
- }
|