pluginmessagemanager.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*! Class PluginMessageManager provides some basic Message Distribution mechanics, if you get instance of this plugin class on your object,
  2. you can use its methods to broadcast messages.
  3. */
  4. class PluginMessageManager extends PluginBase
  5. {
  6. int channelsUsed = 0;
  7. ref array<ref MessageReceiverBase> channelList[NUM_OF_CHANNELS];
  8. void PluginMessageManager()
  9. {
  10. for (int i = 0; i < NUM_OF_CHANNELS; i++)
  11. {
  12. channelList[i] = new array<ref MessageReceiverBase>;
  13. }
  14. }
  15. //! Broadcasts an empty message on a channel provided in the 'index' parameter
  16. void Broadcast(int index)
  17. {
  18. //Debug.Log("Broadcasting message on a channel: "+ ToString(index), "Messaging System");
  19. array<ref MessageReceiverBase> x = channelList[index];
  20. //int test = channelList[index].Count();
  21. for(int i = 0; i < x.Count(); i++)
  22. {
  23. MessageReceiverBase mrb = x.Get(i);
  24. if( mrb != NULL )
  25. {
  26. //string s = "Broadcasting message to: "+ToString(mrb);
  27. //Log(s, LogTemplates.TEMPLATE_BROADCAST);
  28. mrb.OnReceive(index);
  29. }
  30. }
  31. }
  32. //! Broadcasts a message on a channel provided in the 'index' parameter, passes the Int variable
  33. void BroadcastInt(int index, int value)
  34. {
  35. //Debug.Log("Broadcasting message on a channel: "+ ToString(index), "Messaging System");
  36. array<ref MessageReceiverBase> x = channelList[index];
  37. //int test = channelList[index].Count();
  38. for(int i = 0; i < x.Count(); i++)
  39. {
  40. MessageReceiverBase mrb = x.Get(i);
  41. if( mrb )
  42. {
  43. //string s = "Broadcasting message to: "+ToString(mrb);
  44. //Log(s, LogTemplates.TEMPLATE_BROADCAST);
  45. mrb.OnReceiveInt(index, value);
  46. }
  47. }
  48. }
  49. void BroadcastFloat(int index, float value)
  50. {
  51. //Debug.Log("Broadcasting message on a channel: "+ ToString(index), "Messaging System");
  52. array<ref MessageReceiverBase> x = channelList[index];
  53. //int test = channelList[index].Count();
  54. for(int i = 0; i < x.Count(); i++)
  55. {
  56. MessageReceiverBase mrb = x.Get(i);
  57. if( mrb )
  58. {
  59. //string s = "Broadcasting message to: "+ToString(mrb);
  60. //Log(s, LogTemplates.TEMPLATE_BROADCAST);
  61. mrb.OnReceiveFloat(index, value);
  62. }
  63. }
  64. }
  65. void BroadcastString(int index, string value)
  66. {
  67. //Debug.Log("Broadcasting message on a channel: "+ ToString(index), "Messaging System");
  68. array<ref MessageReceiverBase> x = channelList[index];
  69. //int test = channelList[index].Count();
  70. for(int i = 0; i < x.Count(); i++)
  71. {
  72. if( x.Get(i) )
  73. {
  74. Print("broadcasting message to: ");
  75. Print(x.Get(i));
  76. x.Get(i).OnReceiveString(index, value);
  77. }
  78. }
  79. }
  80. //! Broadcasts a message on a channel provided in the 'index' parameter, passes the Param type object to the reciever
  81. void BroadcastParam(int index, Param params)
  82. {
  83. //Debug.Log("Broadcasting message on a channel: "+ ToString(index), "Messaging System");
  84. array<ref MessageReceiverBase> x = channelList[index];
  85. //int test = channelList[index].Count();
  86. for(int i = 0; i < x.Count(); i++)
  87. {
  88. if( x.Get(i) )
  89. {
  90. x.Get(i).OnReceiveParam(index, params);
  91. }
  92. }
  93. }
  94. //! Subscribes an object to listen to messages on a channel provided in the 'index' parameter
  95. void Subscribe(MessageReceiverBase receiver, int index)
  96. {
  97. if(index > channelsUsed) //this is used to speed up the unsubscribeAll method, instead of all channels, we sweep just those in usage
  98. {
  99. channelsUsed = index;
  100. }
  101. array<ref MessageReceiverBase> chan = channelList[index];
  102. if( chan.Find(receiver) >= 0 ) return;
  103. chan.Insert(receiver);
  104. }
  105. void Unsubscribe(MessageReceiverBase receiver, int index)
  106. {
  107. array<ref MessageReceiverBase> chan = channelList[index];
  108. int i = chan.Find(receiver);
  109. if( i >= 0 )
  110. {
  111. chan.Remove(i);
  112. }
  113. }
  114. void UnsubscribeAll(MessageReceiverBase receiver)//REWORK.V: this is slow, should be made quicker(by registering all subscribers in a separate array upon their subscription and then going through this array instead)
  115. {
  116. //GetGame().ProfilerStart("UnsubscribeAll");
  117. for (int i = 0; i <= channelsUsed; i++)
  118. {
  119. array<ref MessageReceiverBase> chan = channelList[i];
  120. int c = chan.Find(receiver);
  121. if( c >= 0 )
  122. {
  123. chan.Remove(c);
  124. }
  125. }
  126. //GetGame().ProfilerStop("UnsubscribeAll");
  127. }
  128. }