-
Notifications
You must be signed in to change notification settings - Fork 0
/
LifeformPublisher.cs
345 lines (325 loc) · 16 KB
/
LifeformPublisher.cs
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
namespace AnimalSimulationVersion2
{
/// <summary>
/// Holds all delegates, events and event triggers that are should and can be used by lifeforms.
/// </summary>
class LifeformPublisher
{
//prey
public delegate void getPossiblePreyEventHandler(object sender, ControlEvents.GetPossiblePreyEventArgs args);
public event getPossiblePreyEventHandler RaiseFindPreyEvent;
public delegate void setPreyEventHandler(object sender, ControlEvents.SetPreyEventArgs args);
public event setPreyEventHandler RaiseSetPreyEvent;
public delegate void removePreyEventHandler(object sender, ControlEvents.RemovePreyEventArgs args);
public event removePreyEventHandler RaiseRemovePreyEvent;
public delegate void informPredatorOfPreyDeathEventHandler(object sender, ControlEvents.InformPredatorOfPreyDeathEventArgs args);
public event informPredatorOfPreyDeathEventHandler RaiseInformHunterOfPreyDeath;
//mat(e/ing)
public delegate void possibleMatesEventHandler(object sender, ControlEvents.PossibleMateEventArgs args);
public event possibleMatesEventHandler RaisePossibleMatesEvent;
public delegate void setMateEventHandler(object sende, ControlEvents.SetMateEventArgs args);
public event setMateEventHandler RaiseSetMateEvent;
public delegate void removeMateEventHandler(object sender, ControlEvents.RemoveMateEventArgs args);
public event removeMateEventHandler RaiseRemoveMateEvent;
public delegate void pregnacyEventHandler(object sender, ControlEvents.PregnacyEventArgs args);
public event pregnacyEventHandler RaisePregnacy;
//AI
public delegate void aiEvnetHandler(object sender, ControlEvents.AIEventArgs args);
public event aiEvnetHandler RaiseAIEvent;
//location
public delegate void getLocationEventHandler(object sender, ControlEvents.GetOtherLocationEventArgs args);
public event getLocationEventHandler RaiseGetLocation;
public delegate void getAllLocationsEventHandler(object sender, ControlEvents.GetAllLocationsEventArgs args);
public event getAllLocationsEventHandler RaiseGetAllLocations;
//deaths
public delegate void eatenEventHandler(object sender, ControlEvents.EatenEventArgs args);
public event eatenEventHandler RaiseEaten;
public delegate void diedEventHandler(object sender, ControlEvents.DeadEventArgs args);
public event diedEventHandler RaiseDied;
//damage
public delegate void damageEventHandler(object sender, ControlEvents.DoHealthDamageEventArgs args);
public event damageEventHandler RaiseDamage;
//relationship
public delegate void transmitDataEventHandler(object sender, ControlEvents.TransmitDataEventArgs args);
public event transmitDataEventHandler RaiseTransmitData;
public delegate void possibleRelationshipJoinerEvnetHandler(object sender, ControlEvents.RelationshipCandidatesEventArgs args);
public event possibleRelationshipJoinerEvnetHandler RaisePossibleRelationshipJoiner;
/// <summary>
/// Gets the location, ID and species of every single subscriber.
/// </summary>
/// <param name="senderID">The ID of the caller.</param>
/// <returns>Returns a list container the location, ID and species of every single subscriber.</returns>
public List<(Vector PreyLocation, string PreyID, string PreySpecies)> GetPossiblePreys(string senderID)
{
return OnGetPossiblePreys(new ControlEvents.GetPossiblePreyEventArgs(senderID));
}
protected List<(Vector PreyLocation, string PreyID, string PreySpecies)> OnGetPossiblePreys(ControlEvents.GetPossiblePreyEventArgs e)
{
getPossiblePreyEventHandler eventHandler = RaiseFindPreyEvent;
if(eventHandler != null)
{
eventHandler.Invoke(this, e);
return e.GetPossiblePreys();
}
return null;
}
/// <summary>
/// Informs <paramref name="receiverID"/> that it is a prey of <paramref name="senderID"/>.
/// </summary>
/// <param name="senderID">The ID of the caller.</param>
/// <param name="receiverID">The ID of the receiver.</param>
public void SetPrey(string senderID, string receiverID)
{
OnSetPrey(new ControlEvents.SetPreyEventArgs(senderID, receiverID));
}
protected void OnSetPrey(ControlEvents.SetPreyEventArgs e)
{
setPreyEventHandler eventHandler = RaiseSetPreyEvent;
if (eventHandler != null)
eventHandler.Invoke(this, e);
}
/// <summary>
/// Informs <paramref name="receiverID"/> that it is no longer a prey of <paramref name="senderID"/>.
/// </summary>
/// <param name="senderID">The ID of the caller.</param>
/// <param name="receiverID">The ID of the prey</param>
public void RemovePrey(string senderID, string receiverID)
{
OnRemovePrey(new ControlEvents.RemovePreyEventArgs(senderID, receiverID));
}
protected void OnRemovePrey(ControlEvents.RemovePreyEventArgs e)
{
removePreyEventHandler eventHandler = RaiseRemovePreyEvent;
if (eventHandler != null)
eventHandler.Invoke(this, e);
}
/// <summary>
/// Gets a list of all possible mates.
/// </summary>
/// <param name="species">The species of the caller.</param>
/// <param name="gender">The gender of the caller.</param>
/// <param name="senderID">The ID of the caller.</param>
/// <returns></returns>
public List<(string mateID, Vector Location)> PossibleMates(string species, char gender, string senderID)
{
return OnPossibleMates(new ControlEvents.PossibleMateEventArgs(species, gender, senderID));
}
protected List<(string mateID, Vector Location)> OnPossibleMates(ControlEvents.PossibleMateEventArgs e)
{
possibleMatesEventHandler eventHandler = RaisePossibleMatesEvent;
if (eventHandler != null)
{
eventHandler.Invoke(this, e);
return e.GetPossibleMates();
}
return null;
}
/// <summary>
/// Informs <paramref name="receiverID"/> that it is mate to <paramref name="senderID"/>.
/// </summary>
/// <param name="senderID">The ID of the caller.</param>
/// <param name="receiverID">The ID of the prey</param>
public void SetMate(string senderID, string receiverID)
{
OnSetMate(new ControlEvents.SetMateEventArgs(senderID, receiverID));
}
protected void OnSetMate(ControlEvents.SetMateEventArgs e)
{
setMateEventHandler eventHandler = RaiseSetMateEvent;
if (eventHandler != null)
eventHandler.Invoke(this,e);
}
/// <summary>
/// Informs <paramref name="receiverID"/> that it is no longer mate to <paramref name="senderID"/>.
/// </summary>
/// <param name="senderID">The ID of the caller.</param>
/// <param name="receiverID">The ID of the prey</param>
public void RemoveMate(string senderID, string receiverID)
{
OnRemoveMate(new ControlEvents.RemoveMateEventArgs(senderID, receiverID));
}
protected void OnRemoveMate(ControlEvents.RemoveMateEventArgs e)
{
removeMateEventHandler eventHandler = RaiseRemoveMateEvent;
if (eventHandler != null)
eventHandler.Invoke(this, e);
}
/// <summary>
/// Runs the AI and transmit <paramref name="timeSinceLastUpdate"/> with it for any code that needs to time interval between this call and the last call.
/// </summary>
/// <param name="timeSinceLastUpdate">The amount of time that has passed since the last time the AI was called.</param>
public void AI(float timeSinceLastUpdate)
{
OnAI(new ControlEvents.AIEventArgs(timeSinceLastUpdate));
}
protected void OnAI(ControlEvents.AIEventArgs e)
{
aiEvnetHandler evnetHandler = RaiseAIEvent;
if (evnetHandler != null)
evnetHandler.Invoke(this, e);
}
/// <summary>
/// Gets the location of <paramref name="receiverID"/>.
/// </summary>
/// <param name="receiverID">The ID of the lifeform whoes location is wanted.</param>
/// <returns>A Vector with the location of <paramref name="receiverID"/>.</returns>
public Vector GetLocation(string receiverID)
{
return OnGetLocation(new ControlEvents.GetOtherLocationEventArgs(receiverID));
}
protected Vector OnGetLocation(ControlEvents.GetOtherLocationEventArgs e)
{
getLocationEventHandler eventHandler = RaiseGetLocation;
if(eventHandler != null)
{
eventHandler.Invoke(this, e);
return e.GetLocation;
}
return null; //have a custom exception for eventHandler being null when the animal got a food/mate id
}
/// <summary>
/// Informs <paramref name="receiverID"/> that it has died.
/// </summary>
/// <param name="receiverID">The ID of the lifeform that have died.</param>
public void Death(string receiverID)
{
OnDeath(new ControlEvents.DeadEventArgs(receiverID));
}
protected void OnDeath(ControlEvents.DeadEventArgs e)
{
diedEventHandler eventHandler = RaiseDied;
if (eventHandler != null)
eventHandler.Invoke(this, e);
}
/// <summary>
/// Informs <paramref name="receiverID"/> that it has been eaten.
/// </summary>
/// <param name="receiverID">The ID of the lifeform that has been eaten</param>
/// <returns>The nutrient value of <paramref name="receiverID"/>.</returns>
public float Eat(string receiverID)
{
return OnEat(new ControlEvents.EatenEventArgs(receiverID));
}
protected float OnEat(ControlEvents.EatenEventArgs e)
{
eatenEventHandler eventHandler = RaiseEaten;
if(eventHandler != null)
{
eventHandler.Invoke(this, e);
return e.GetNutrient;
}
return -1;
}
/// <summary>
/// Informs <paramref name="receiverID"/> of the death of its prey <paramref name="senderID"/>.
/// </summary>
/// <param name="senderID">The ID of the prey.</param>
/// <param name="receiverID">The ID of the predator.</param>
public void InformPredatorOfPreyDeath(string senderID, string receiverID)
{ //rename this and the controlevent so their names also make sense for losing a prey
OnInformPredatorOfDeath(new ControlEvents.InformPredatorOfPreyDeathEventArgs(senderID, receiverID));
}
protected void OnInformPredatorOfDeath(ControlEvents.InformPredatorOfPreyDeathEventArgs e)
{
informPredatorOfPreyDeathEventHandler eventHandler = RaiseInformHunterOfPreyDeath;
if (eventHandler != null)
eventHandler.Invoke(this, e);
}
/// <summary>
/// Informs <paramref name="receiverID"/> that <paramref name="senderID"/> attacked it for <paramref name="damage"/>.
/// </summary>
/// <param name="senderID">The ID of the attacker.</param>
/// <param name="receiverID">The ID of the attacked.</param>
/// <param name="damage">The amount of damage done.</param>
public bool DamageLifeform(string senderID, string receiverID, byte damage)
{
return OnDamageLifeform(new ControlEvents.DoHealthDamageEventArgs(senderID, receiverID, damage));
}
protected bool OnDamageLifeform(ControlEvents.DoHealthDamageEventArgs e)
{
damageEventHandler eventHandler = RaiseDamage;
if (eventHandler != null)
{
eventHandler.Invoke(this, e);
return e.Killed;
}
return false;
}
/// <summary>
/// Gets the location of every lifeform.
/// </summary>
/// <param name="senderID">The ID of the sender, used to ensure the sender does not react to the event.</param>
public List<(Vector Location, string ID)> GetAllLocations(string senderID)
{
return OnGetAllLocations(new ControlEvents.GetAllLocationsEventArgs(senderID));
}
protected List<(Vector Location, string ID)> OnGetAllLocations(ControlEvents.GetAllLocationsEventArgs e)
{
getAllLocationsEventHandler eventHandler = RaiseGetAllLocations;
if(eventHandler != null)
{
eventHandler.Invoke(this, e);
return e.GetInformation;
}
return null;
}
/// <summary>
/// Transmit <paramref name="data"/> from <paramref name="senderID"/> to <paramref name="receivierID"/>.
/// </summary>
/// <remarks>This event can be used to tranmit any form of data from one lifeform to another as long time the receiver knows how to handle the data.</remarks>
/// <param name="senderID">The ID of the data sender.</param>
/// <param name="receivierID">The ID of the receiver of the data.</param>
/// <param name="data">An object of data.</param>
public void TransmitData(string senderID, string receivierID, object data)
{
OnTransmitData(new ControlEvents.TransmitDataEventArgs(senderID, receivierID, data));
}
protected void OnTransmitData(ControlEvents.TransmitDataEventArgs e)
{
transmitDataEventHandler eventHandler = RaiseTransmitData;
if (eventHandler != null)
eventHandler.Invoke(this, e);
}
/// <summary>
/// Allows a lifefrom to contact other lifeforms if they implement an interface of type <paramref name="type"/>. The receiver(s) can look at <paramref name="species"/> to help decide if they should response.
/// </summary>
/// <param name="senderID">The sender of this event, used to prevent them reacting to the event.</param>
/// <param name="species">The species of <paramref name="senderID"/>.</param>
/// <param name="type">A Type, should be used to check for interface implementation.</param>
/// <returns>A list of possible candidates for a pack.</returns>
public List<(Vector Location, string ID, char Gender)> PossibleRelationshipJoiner(string senderID, string species, Type type)
{
return OnPossibleRelationshipJoiner(new ControlEvents.RelationshipCandidatesEventArgs(senderID, species, type));
}
protected List<(Vector Location, string ID, char Gender)> OnPossibleRelationshipJoiner(ControlEvents.RelationshipCandidatesEventArgs e)
{
possibleRelationshipJoinerEvnetHandler evnetHandler = RaisePossibleRelationshipJoiner;
if(evnetHandler != null)
{
evnetHandler.Invoke(this, e);
return e.GetList;
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="senderID"></param>
/// <param name="receiverID"></param>
/// <param name="isPregnant"></param>
public void Pregnacy(string senderID, string receiverID, bool isPregnant)
{
OnPregnacy(new ControlEvents.PregnacyEventArgs(senderID, receiverID, isPregnant));
}
protected void OnPregnacy(ControlEvents.PregnacyEventArgs e)
{
pregnacyEventHandler eventHandler = RaisePregnacy;
if (eventHandler != null)
eventHandler.Invoke(this, e);
}
}
}