-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathRepeater.cs
71 lines (64 loc) · 1.79 KB
/
Repeater.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
namespace NPBehave
{
public class Repeater : Decorator
{
private int loopCount = -1;
private int currentLoop;
/// <param name="loopCount">number of times to execute the decoratee. Set to -1 to repeat forever, be careful with endless loops!</param>
/// <param name="decoratee">Decorated Node</param>
public Repeater(int loopCount, Node decoratee) : base("Repeater", decoratee)
{
this.loopCount = loopCount;
}
/// <param name="decoratee">Decorated Node, repeated forever</param>
public Repeater(Node decoratee) : base("Repeater", decoratee)
{
}
protected override void DoStart()
{
if (loopCount != 0)
{
currentLoop = 0;
Decoratee.Start();
}
else
{
this.Stopped(true);
}
}
override protected void DoStop()
{
this.Clock.RemoveTimer(restartDecoratee);
if (Decoratee.IsActive)
{
Decoratee.Stop();
}
else
{
Stopped(false);
}
}
protected override void DoChildStopped(Node child, bool result)
{
if (result)
{
if (IsStopRequested || (loopCount > 0 && ++currentLoop >= loopCount))
{
Stopped(true);
}
else
{
this.Clock.AddTimer(0, 0, restartDecoratee);
}
}
else
{
Stopped(false);
}
}
protected void restartDecoratee()
{
Decoratee.Start();
}
}
}