Skip to content

Commit 6059832

Browse files
committed
Add QueueIntent to execute different intents in queue
1 parent 6111186 commit 6059832

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Collections.Generic;
2+
3+
namespace BrainAI.AI.UtilityAI
4+
{
5+
public class QueueIntent<T> : IIntent<T>
6+
{
7+
private readonly Queue<IIntent<T>> intents;
8+
private IIntent<T> currentIntent;
9+
10+
public QueueIntent(params IIntent<T>[] intents)
11+
{
12+
this.intents = new Queue<IIntent<T>>(intents);
13+
}
14+
15+
public void Enter(T context)
16+
{
17+
currentIntent?.Enter(context);
18+
}
19+
20+
public bool Execute(T context)
21+
{
22+
if (currentIntent == null)
23+
{
24+
if (intents.Count == 0)
25+
{
26+
return true;
27+
}
28+
currentIntent = intents.Dequeue();
29+
currentIntent.Enter(context);
30+
}
31+
32+
var deleted = currentIntent.Execute(context);
33+
if (deleted)
34+
{
35+
currentIntent.Exit(context);
36+
currentIntent = null;
37+
}
38+
39+
return false;
40+
}
41+
42+
public void Exit(T context)
43+
{
44+
currentIntent?.Exit(context);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)