/
Thought.cs
116 lines (97 loc) · 2.58 KB
/
Thought.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
using System;
using System.Collections;
using System.Timers;
using System.Threading;
namespace Emulator
{
/// <summary>
/// Triggers actions after a set period of time
/// </summary>
///
/// <todo>
/// Add more documentation
/// </todo>
public class Thought
{
private int triggerTime;
private Thought next;
public int TriggerTime {
get { return triggerTime; }
set { triggerTime = value; }
}
public Thought Next {
get { return next; }
}
public Thought(int time)
{
triggerTime = time;
}
public virtual void Trigger()
{
}
public void Link(Thought to)
{
next = to.next;
to.next = this;
}
public void Unlink(Thought from)
{
if(from != null) {
from.next = next;
}
next = null;
}
public void AutoLink(ref Thought head)
{
if(head == null) {
head = this;
return;
}
if(TriggerTime <= head.TriggerTime) {
this.next = head;
head = this;
return;
}
Thought pos = head;
while(pos.next != null && TriggerTime > pos.next.TriggerTime) {
pos = pos.next;
}
Link(pos);
}
public static Thought UnlinkJob(Thought head, int time)
{
if(head == null || head.triggerTime > time) {
return head;
}
Thought pos = head;
while(pos.next != null && pos.next.triggerTime <= time) {
pos = pos.next;
}
Thought next = pos.next;
pos.next = null;
return next;
}
}
public class DelegateThought : Thought
{
public delegate void ThoughtCallback(Thought thought, object[] data);
public ThoughtCallback _callback;
public object[] _data;
public DelegateThought(int time, ThoughtCallback callback) : base(time)
{
_callback = callback;
_data = null;
}
public DelegateThought(int time, ThoughtCallback callback, object[] data) : base(time)
{
_callback = callback;
_data = data;
}
public override void Trigger()
{
if(_callback != null) {
_callback(this,_data);
}
}
}
}