FireEventObserver.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import LogicUnitManager from './LogicUnitManager'
  2. import DataModelManager from 'DataModelManager'
  3. import GameProcessManager from '../net/GameProcessManager'
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. },
  8. ctor: function () {
  9. // behavior
  10. this._behaviorEventIDMap = {};
  11. this.lum = LogicUnitManager;
  12. this._behaviorEvent_index = 0;
  13. // data model
  14. this._dataEventIDMap = {};
  15. this.dmm = DataModelManager;
  16. this._dataEvent_index = 0;
  17. },
  18. onBehaviorEvent(behaviorName,eventName,callbackFun)
  19. {
  20. let behavior = this.lum.getBehavior(behaviorName);
  21. if(behavior)
  22. {
  23. let eventId = behavior.on(eventName,callbackFun,this);
  24. this._behaviorEventIDMap[this._behaviorEvent_index++] = {'behavior':behavior,'id':eventId};
  25. }
  26. },
  27. onDataModelChange(modelName,attributename,callbackFun)
  28. {
  29. let model = this.dmm.getModel(modelName);
  30. if(model && typeof model.on !== 'undefined')
  31. {
  32. let eventId = model.on(attributename,callbackFun,this);
  33. this._dataEventIDMap[this._dataEvent_index++] = {'m':model,'id':eventId};
  34. }
  35. },
  36. onLoad () {
  37. },
  38. start () {
  39. },
  40. onDestroy()
  41. {
  42. // behavior destroy
  43. for(let val in this._behaviorEventIDMap)
  44. {
  45. let eid = this._behaviorEventIDMap[val]['id'];
  46. let behavior = this._behaviorEventIDMap[val]['behavior'];
  47. if(behavior)
  48. {
  49. behavior.off(eid);
  50. }
  51. }
  52. // data model destroy
  53. for(let val in this._dataEventIDMap)
  54. {
  55. let m = this._dataEventIDMap[val]['m'],eid = this._dataEventIDMap[val]['id'];
  56. if(m)
  57. {
  58. m.off(eid);
  59. }
  60. }
  61. },
  62. });