-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConditionNode.cs
More file actions
67 lines (57 loc) · 2.06 KB
/
Copy pathConditionNode.cs
File metadata and controls
67 lines (57 loc) · 2.06 KB
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
// Copyright © Gamesmiths Guild.
using Gamesmiths.Forge.Statescript.Ports;
namespace Gamesmiths.Forge.Statescript.Nodes;
/// <summary>
/// Node representing a condition in the graph. It has a single input port that triggers the evaluation of the condition
/// and two output ports: one for the true result and one for the false result.
/// </summary>
public abstract class ConditionNode : Node
{
/// <summary>
/// Port index for the input port.
/// </summary>
public const byte InputPort = 0;
/// <summary>
/// Port index for the true output port.
/// </summary>
public const byte TruePort = 0;
/// <summary>
/// Port index for the false output port.
/// </summary>
public const byte FalsePort = 1;
/// <summary>
/// Tests the condition and returns true or false. The result determines which output port will emit a message.
/// </summary>
/// <param name="graphContext">The current graph context.</param>
/// <returns><see langword="true"/> if the condition is met; otherwise, <see langword="false"/>.</returns>
protected abstract bool Test(GraphContext graphContext);
/// <inheritdoc/>
public override string Description => $"A {GetType().Name.Replace("Node", string.Empty)} condition node.";
/// <inheritdoc/>
#pragma warning disable SA1202 // Elements should be ordered by access
internal override IEnumerable<int> GetReachableOutputPorts(byte inputPortIndex)
#pragma warning restore SA1202 // Elements should be ordered by access
{
yield return TruePort;
yield return FalsePort;
}
/// <inheritdoc/>
protected override void DefinePorts(List<InputPort> inputPorts, List<OutputPort> outputPorts)
{
inputPorts.Add(CreatePort<InputPort>(InputPort, "Input"));
outputPorts.Add(CreatePort<EventPort>(TruePort, "True"));
outputPorts.Add(CreatePort<EventPort>(FalsePort, "False"));
}
/// <inheritdoc/>
protected sealed override void HandleMessage(InputPort receiverPort, GraphContext graphContext)
{
if (Test(graphContext))
{
OutputPorts[TruePort].EmitMessage(graphContext);
}
else
{
OutputPorts[FalsePort].EmitMessage(graphContext);
}
}
}