/
EventManager.cs
92 lines (78 loc) · 2.9 KB
/
EventManager.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Text;
namespace Ymfas {
public delegate void GameEventFiringHandler(GameEvent e);
public abstract class GameEvent {
/// <summary>
/// A byte array representation of the data contained in this object. Must be parseable by SetDataFromByteArray.
/// </summary>
/// <returns>The data contained in the object as a String</returns>
public abstract Byte[] ToByteArray();
/// <summary>
/// Sets the data based on a byte array using the same format as returned from ToByteArray()
/// </summary>
/// <param name="byteArray">A parseable string containing the object data</param>
public abstract void SetDataFromByteArray(Byte[] byteArray);
//public static event GameEventFiringHandler FiringEvent;
/// <summary>
/// Fires the FiringEvent event
/// </summary>
public abstract void FireEvent();
/*
public virtual void FireEvent() {
if (FiringEvent != null) {
FiringEvent(this);
}
}
*/
public abstract Lidgren.Library.Network.NetChannel DeliveryType { get; }
}
public class EventManager
{
private SpiderBase net;
private Queue<GameEvent> EventQueue;
/// <summary>
/// Creates a new EventManager
/// </summary>
/// <param name="networkEngine">The network engine to monitor for events</param>
public EventManager(SpiderBase _spider)
{
net = _spider;
EventQueue = new Queue<GameEvent>();
}
/// <summary>
/// Processes all events currently in the queue
/// </summary>
public void Update()
{
net.Update();
SpiderMessage msg = null;
while ((msg = net.GetNextMessage()) != null) {
try {
//The type is contained in the label
Type eventType = Type.GetType(msg.Label);
//Create an event object
GameEvent msgEvent = (GameEvent)System.Activator.CreateInstance(eventType);
//Add data to the event
msgEvent.SetDataFromByteArray((byte[])msg.Data);
//Fire
msgEvent.FireEvent();
}
catch (Exception e) {
Util.RecordException(e);
}
}
}
/// <summary>
/// Sends a game event over the network. If the caller is a server, it will be broadcast, otherwise it will be sent to the server.
/// </summary>
/// <param name="e">The game event to be sent</param>
public void SendEvent(GameEvent e)
{
SpiderMessage msg = new SpiderMessage(e.ToByteArray(), SpiderMessageType.Bytes, e.GetType().ToString());
net.SendMessage(msg, e.DeliveryType);
}
}
}