public BehaviorTree(BehaviorTreeBlueprint <T> blueprint, T context) { this.blueprint = blueprint; this.context = context; SetOrigin(); }
BehaviorTreeBlueprint <T> .StatusToken runningToken; //The token that was used to enter the running node public Result Tick() { var currentNode = runningNode; var currentToken = runningToken; do { var nextToken = currentNode.Next(currentToken, context); var nextNode = nextToken.ToChild ? currentNode[nextToken.index] : currentNode.Parent; if (nextToken.result == Result.running) { runningNode = currentNode; runningToken = currentToken; return(Result.running); } currentNode = nextNode; currentToken = nextToken; }while (!(currentNode is BehaviorTreeBlueprint <T> .Root)); SetOrigin(); return(currentToken.result); }
Location <T> LoadNode <T>(Location <T> parent, BehaviorTreeBlueprint <T> blueprint, int index) { TreeNodeData data = allNodes[index]; Type type = BehaviorTreeNodeAttribute.SerializedNameToType.TryGetValue(data.nodeTypeSerializableName); if (type == null) { throw new Exception($"Unexpected serialized node type name {data.nodeTypeSerializableName}."); } if (type.ContainsGenericParameters) { type = type.MakeGenericType(typeof(T)); } INodeType node; //Prepare node and parameters if (data.parameters == null || data.parameters.Length == 0) { node = (INodeType)Activator.CreateInstance(type); } else { object[] parameters = new object[data.parameters.Length]; for (int i = 0; i < data.parameters.Length; i++) { SerializableParameter parameter = data.parameters[i]; if (parameter.Type == ParameterType.behaviorAction) { parameter.LoadBehaviorAction(importData); parameters[i] = GetAction <T>(parameter); } else { parameters[i] = parameter.GetValue(); } } node = (INodeType)Activator.CreateInstance(type, parameters); } //Add to blueprint Location <T> location = blueprint.Add(parent, node, out bool success); if (!success) { throw new Exception($"Invalid node create from graph at GUID: {data.GUID}, position: {data.position}, serialized node name: {data.nodeTypeSerializableName}!"); } return(location); }
void LoadBranch <T>(Location <T> parent, BehaviorTreeBlueprint <T> blueprint, int index) { TreeNodeData data = allNodes[index]; for (int i = 0; i < data.children.Length; i++) { int childIndex = data.children[i]; var location = LoadNode(parent, blueprint, childIndex); LoadBranch(location, blueprint, childIndex); } }
public BehaviorTreeBlueprint <T> GetBlueprint <T>() { if (importData == null || targetContextTypeMethod == null) { throw new Exception("Data not ready for blueprint!"); } if (allNodes == null || allNodes.Count == 0) { throw new Exception("No serialized graph node!"); } if (!targetContextTypeMethod.TargetContextType.IsAssignableFrom(typeof(T))) { throw new Exception($"Unmatched requested type {typeof(T)} with {targetContextTypeMethod.TargetContextType}!"); } var blueprint = new BehaviorTreeBlueprint <T>(); LoadBranch(blueprint.RootLocation, blueprint, mainRootNode); blueprint.Seal(); return(blueprint); }
public ConstantNode(BehaviorTreeBlueprint <T> blueprint, int selfIndex, bool success) : base(blueprint, selfIndex) => this.success = success;
public ConditionerNode(BehaviorTreeBlueprint <T> blueprint, int selfIndex, BehaviorAction <T> condition) : base(blueprint, selfIndex) => this.condition = condition;
void SetOrigin() { runningNode = blueprint.OriginNode; runningToken = blueprint.OriginToken; }