You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.7 KiB
43 lines
1.7 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using TNodeCore.Runtime;
|
|
using TNodeCore.Runtime.Components;
|
|
using UnityEngine;
|
|
|
|
namespace TNode.TNodeCore.Runtime.Components{
|
|
public class ConditionalGraph:RuntimeGraph{
|
|
public ConditionalRuntimeNode EntryNode;
|
|
public ConditionalRuntimeNode CurrentNode{ get; set; }
|
|
public override void Build(){
|
|
base.Build();
|
|
var entry = GetRuntimeNodesOfType<EntryNode>();
|
|
if (entry.Count > 1){
|
|
Debug.LogError("There should be only one entry node in a conditional graph");
|
|
}
|
|
EntryNode = entry.FirstOrDefault() as ConditionalRuntimeNode;
|
|
}
|
|
|
|
public IEnumerator StepForward(){
|
|
CurrentNode = EntryNode;
|
|
while (CurrentNode != null){
|
|
//First let's process the node
|
|
CurrentNode.NodeData.Process();
|
|
//Then check if there are conditional transitions
|
|
var conditionalTransitions = CurrentNode.GetNextNodesId();
|
|
var transitionNode = new List<RuntimeNode>();
|
|
|
|
foreach (var conditionalTransition in conditionalTransitions){
|
|
transitionNode.Add(Get(conditionalTransition));
|
|
}
|
|
foreach (var runtimeNode in transitionNode){
|
|
if (runtimeNode is ConditionalRuntimeNode == false){
|
|
runtimeNode.Process();
|
|
}
|
|
}
|
|
CurrentNode = transitionNode.FirstOrDefault(x => x is ConditionalRuntimeNode) as ConditionalRuntimeNode;
|
|
yield return CurrentNode;
|
|
}
|
|
}
|
|
}
|
|
} |