File size: 4,196 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*! Class PluginMessageManager provides some basic Message Distribution mechanics, if you get instance of this plugin class on your object, 
you can use its methods to broadcast messages.
*/
class PluginMessageManager extends PluginBase
{
	int channelsUsed = 0;
	ref array<ref MessageReceiverBase>	channelList[NUM_OF_CHANNELS];
	
	void PluginMessageManager()
	{
		for (int i = 0; i < NUM_OF_CHANNELS; i++)
		{
			channelList[i] = new array<ref MessageReceiverBase>;
		}
	}

	//! Broadcasts an empty message on a channel provided in the 'index' parameter
	void Broadcast(int index)
	{
		//Debug.Log("Broadcasting message on a channel: "+ ToString(index), "Messaging System");
		array<ref MessageReceiverBase> x = channelList[index];
		//int test = channelList[index].Count();
		for(int i = 0; i < x.Count(); i++)
		{
			MessageReceiverBase mrb = x.Get(i);
			
			if(	mrb != NULL )
			{
				//string s = "Broadcasting message to: "+ToString(mrb);
				//Log(s, LogTemplates.TEMPLATE_BROADCAST);
				mrb.OnReceive(index);
			}
		}
	}

	//! Broadcasts a message on a channel provided in the 'index' parameter, passes the Int variable
	void BroadcastInt(int index, int value)
	{
		//Debug.Log("Broadcasting message on a channel: "+ ToString(index), "Messaging System");
		array<ref MessageReceiverBase> x = channelList[index];
		//int test = channelList[index].Count();
		for(int i = 0; i < x.Count(); i++)
		{
			MessageReceiverBase mrb = x.Get(i);
			
			if(	mrb )
			{
				//string s = "Broadcasting message to: "+ToString(mrb);
				//Log(s, LogTemplates.TEMPLATE_BROADCAST);
				mrb.OnReceiveInt(index, value);
			}
			
		}
	}

	void BroadcastFloat(int index, float value)
	{
		//Debug.Log("Broadcasting message on a channel: "+ ToString(index), "Messaging System");
		array<ref MessageReceiverBase> x = channelList[index];
		//int test = channelList[index].Count();
		for(int i = 0; i < x.Count(); i++)
		{
			MessageReceiverBase mrb = x.Get(i);
			
			if(	mrb )
			{
				//string s = "Broadcasting message to: "+ToString(mrb);
				//Log(s, LogTemplates.TEMPLATE_BROADCAST);
				mrb.OnReceiveFloat(index, value);
			}
			
		}
	}

	void BroadcastString(int index, string value)
	{
		//Debug.Log("Broadcasting message on a channel: "+ ToString(index), "Messaging System");
		array<ref MessageReceiverBase> x = channelList[index];
		//int test = channelList[index].Count();
		for(int i = 0; i < x.Count(); i++)
		{
			if(	x.Get(i) )
			{
				Print("broadcasting message to: ");
				Print(x.Get(i));
				x.Get(i).OnReceiveString(index, value);
			}
		}
	}

	//! Broadcasts a message on a channel provided in the 'index' parameter, passes the Param type object to the reciever
	void BroadcastParam(int index, Param params)
	{
		//Debug.Log("Broadcasting message on a channel: "+ ToString(index), "Messaging System");
		array<ref MessageReceiverBase> x = channelList[index];
		//int test = channelList[index].Count();
		for(int i = 0; i < x.Count(); i++)
		{
		
			if(	x.Get(i) )
			{
				x.Get(i).OnReceiveParam(index, params);
			}
			
		}
	}
	
	//! Subscribes an object to listen to messages on a channel provided in the 'index' parameter
	void Subscribe(MessageReceiverBase receiver, int index)
	{
		if(index > channelsUsed) //this is used to speed up the unsubscribeAll method, instead of all channels, we sweep just those in usage
		{
			channelsUsed = index;
		}
		array<ref MessageReceiverBase> chan = channelList[index];
		if( chan.Find(receiver) >= 0 ) return;
		chan.Insert(receiver);
	}

	void Unsubscribe(MessageReceiverBase receiver, int index)
	{
		array<ref MessageReceiverBase> chan = channelList[index];
		int i = chan.Find(receiver);
		if( i >= 0 )
		{
			chan.Remove(i);
		}
	}
	
	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)
	{
		//GetGame().ProfilerStart("UnsubscribeAll");
		for (int i = 0; i <= channelsUsed; i++)
		{
			array<ref MessageReceiverBase> chan = channelList[i];
			int c = chan.Find(receiver);
			if( c >= 0 )
			{
				chan.Remove(c);
			}
		}
		//GetGame().ProfilerStop("UnsubscribeAll");
	}
}