#if GRAPH_DESIGNER
/// ---------------------------------------------
/// Behavior Designer
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.BehaviorDesigner.Runtime.Utility
{
using System;
using Opsive.BehaviorDesigner.Runtime.Systems;
using Unity.Entities;
using UnityEngine;
///
/// Systems assembly implementation for the runtime traversal bridge.
///
public sealed class BehaviorTreeExecutionController : IBehaviorTreeExecutionController
{
///
/// Registers the controller when Unity reloads runtime subsystems.
///
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void RegisterController()
{
BehaviorTreeExecution.Register(new BehaviorTreeExecutionController());
}
///
/// Adds the core cleanup systems to the behavior tree group.
///
/// The ECS world.
/// The behavior tree system group.
public void AddCleanupSystems(World world, ComponentSystemGroup behaviorTreeSystemGroup)
{
behaviorTreeSystemGroup.AddSystemToUpdateList(world.GetOrCreateSystem());
behaviorTreeSystemGroup.AddSystemToUpdateList(world.GetOrCreateSystem());
}
///
/// Adds the reevaluation system to the before-traversal group.
///
/// The ECS world.
/// The before-traversal group.
public void AddReevaluateSystem(World world, ComponentSystemGroup beforeTraversalSystemGroup)
{
beforeTraversalSystemGroup.AddSystemToUpdateList(world.GetOrCreateSystem(typeof(ReevaluateSystem)));
}
///
/// Adds the TaskObject system to the traversal task group.
///
/// The ECS world.
/// The traversal task group.
public void AddTaskObjectSystem(World world, ComponentSystemGroup traversalTaskSystemGroup)
{
traversalTaskSystemGroup.AddSystemToUpdateList(world.GetOrCreateSystem(typeof(TaskObjectSystem)));
}
///
/// Returns the TaskObject reevaluation system type.
///
public Type TaskObjectReevaluateSystemType { get => typeof(TaskObjectReevaluateSystem); }
///
/// Completes one traversal loop and returns true if another evaluation pass should run.
///
/// The ECS world.
/// The entity manager.
/// True if the traversal group should evaluate again.
public bool CompleteTraversal(World world, EntityManager entityManager)
{
var determineEvaluationSystemHandle = world.GetExistingSystem();
var determineEvaluationSystem = entityManager.WorldUnmanaged.GetUnsafeSystemRef(determineEvaluationSystemHandle);
determineEvaluationSystem.Complete(entityManager);
var evaluate = determineEvaluationSystem.Evaluate;
CompleteEvaluation(world, entityManager, false);
return evaluate;
}
///
/// Completes the evaluation system.
///
/// The ECS world.
/// The entity manager.
/// Is the traversal group stopping?
public void CompleteEvaluation(World world, EntityManager entityManager, bool stopRunning)
{
var evaluationSystemHandle = world.GetExistingSystem();
var evaluationSystem = entityManager.WorldUnmanaged.GetUnsafeSystemRef(evaluationSystemHandle);
evaluationSystem.Complete(entityManager, stopRunning);
}
///
/// Returns true if any behavior tree is still active.
///
/// The ECS world.
/// The entity manager.
/// True if any behavior tree is still active.
public bool IsActive(World world, EntityManager entityManager)
{
var determineEvaluationSystemHandle = world.GetExistingSystem();
var determineEvaluationSystem = entityManager.WorldUnmanaged.GetUnsafeSystemRef(determineEvaluationSystemHandle);
return determineEvaluationSystem.Active;
}
}
}
#endif