Skip to content

Commit

Permalink
Add InverterNode
Browse files Browse the repository at this point in the history
  • Loading branch information
mhjort committed Dec 3, 2017
1 parent 1d1bfc8 commit 5327cab
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/InverterNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace AivoTree
{
public class InverterNode<T> : TreeNode<T>
{
private readonly TreeNode<T> node;

public InverterNode(TreeNode<T> node)
{
this.node = node;
}

public AivoTreeStatus Tick(long timeTick, T context)
{
var status = node.Tick(timeTick, context);
switch (status)
{
case AivoTreeStatus.Success:
return AivoTreeStatus.Failure;
case AivoTreeStatus.Failure:
return AivoTreeStatus.Success;
default:
return status;
}
}
}
}
1 change: 1 addition & 0 deletions src/aivo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<ItemGroup>
<Compile Include="ActionNode.cs" />
<Compile Include="AivoTreeStatus.cs" />
<Compile Include="InverterNode.cs" />
<Compile Include="SelectorNode.cs" />
<Compile Include="SequenceNode.cs" />
<Compile Include="TreeNode.cs" />
Expand Down
8 changes: 8 additions & 0 deletions test/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public void SelectorNodeContinuesWithCurrentlyRunningNodeWithNextTick()
Assert.AreEqual(AivoTreeStatus.Running, sequence.Tick(1, model));
Assert.AreEqual(AivoTreeStatus.Success, sequence.Tick(2, model));
}

[Test]
public void InverterNodeInvertsFailSuccessStateTheResul()
{
Assert.AreEqual(AivoTreeStatus.Success, new InverterNode<MyModel>(new FailingNode()).Tick(1, new MyModel()));
Assert.AreEqual(AivoTreeStatus.Failure, new InverterNode<MyModel>(new SucceedingNode()).Tick(1, new MyModel()));
Assert.AreEqual(AivoTreeStatus.Running, new InverterNode<MyModel>(new RunOnceAndSucceedNextNode()).Tick(1, new MyModel()));
}
}

public class MyModel
Expand Down

0 comments on commit 5327cab

Please sign in to comment.