Esempio n. 1
0
        public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
        {
            var blob = new NodeBlobRef(_source.GetBlobAsset());
            var bb   = new EntityBlackboard {
                Entity = entity, EntityManager = dstManager
            };

            VirtualMachine.Reset(ref blob, ref bb);

            dstManager.AddBuffer <BehaviorTreeBufferElement>(entity);
            dstManager.AddComponent <CurrentBehaviorTreeComponent>(entity);

            var behaviorTreeEntity = conversionSystem.CreateAdditionalEntity(gameObject);

#if UNITY_EDITOR
            dstManager.SetName(behaviorTreeEntity, $"[BT]{dstManager.GetName(entity)}:{_order}.{_debugName}");
#endif
            var query     = blob.GetAccessTypes();
            var dataQuery = new BlackboardDataQuery(query, components =>
                                                    // HACK: workaround for different world/dstManager in subscenes.
                                                    // dstManager.CreateEntityQuery(components.ToArray()));
                                                    World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntityQuery(components.ToArray()));
            dstManager.AddSharedComponentData(behaviorTreeEntity, dataQuery);

            dstManager.AddComponentData(behaviorTreeEntity, new BehaviorTreeComponent
            {
                Blob = blob, Thread = _thread, AutoCreation = _autoCreateTypes
            });
            dstManager.AddComponentData(behaviorTreeEntity, new BehaviorTreeTargetComponent {
                Value = entity
            });
            dstManager.AddComponentData(behaviorTreeEntity, new BehaviorTreeOrderComponent {
                Value = _order
            });
        }
Esempio n. 2
0
        public static void AddBehaviorTree(
            this Entity entity
            , EntityManager dstManager
            , NodeBlobRef blob
            , BehaviorTreeThread thread = BehaviorTreeThread.ForceRunOnMainThread
            )
        {
            switch (thread)
            {
            case BehaviorTreeThread.ForceRunOnMainThread:
            {
                var bb = new EntityBlackboard {
                    EntityManager = dstManager, Entity = entity
                };
                VirtualMachine.Reset(blob, bb);
                dstManager.AddComponentData(entity, new ForceRunOnMainThreadTag());
                break;
            }

            case BehaviorTreeThread.ForceRunOnJob:
            {
                AddJobComponents();
                dstManager.AddComponentData(entity, new ForceRunOnJobTag());
                break;
            }

            case BehaviorTreeThread.ControlledByBehaviorTree:
            {
                AddJobComponents();
                break;
            }

            default:
                throw new ArgumentOutOfRangeException(nameof(thread), thread, null);
            }

            dstManager.AddComponentData(entity, blob);

            void AddJobComponents()
            {
                var dataQuery = new BlackboardDataQuery {
                    Value = blob.GetAccessTypes()
                };
                var bb = new EntityBlackboard();

                VirtualMachine.Reset(blob, bb);
                dstManager.AddComponentData(entity, new IsRunOnMainThread {
                    Value = false
                });
                dstManager.AddSharedComponentData(entity, dataQuery);
            }
        }
Esempio n. 3
0
        public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
        {
            if (!enabled)
            {
                return;
            }

            if (_rootNode != null && _rootNode.GetComponent <StopConvertToEntity>() == null)
            {
                _rootNode.gameObject.AddComponent <StopConvertToEntity>();
            }

            var blob = _file != null?_file.ToBlob() : _rootNode.ToBlob();

            var blobRef = new NodeBlobRef {
                BlobRef = blob
            };

            entity.AddBehaviorTree(dstManager, blobRef, _thread);

            if (_autoCreateTypes != 0)
            {
                var accessTypes = dstManager.HasComponent <BlackboardDataQuery>(entity)
                    ? dstManager.GetSharedComponentData <BlackboardDataQuery>(entity).Set
                    : blobRef.GetAccessTypes()
                ;

                foreach (var componentType in accessTypes)
                {
                    if (dstManager.HasComponent(entity, componentType))
                    {
                        continue;
                    }
                    var shouldCreate =
                        HasFlag(AutoCreateType.ReadOnly) && componentType.AccessModeType == ComponentType.AccessMode.ReadOnly ||
                        HasFlag(AutoCreateType.ReadWrite) && componentType.AccessModeType == ComponentType.AccessMode.ReadWrite ||
                        HasFlag(AutoCreateType.BehaviorTreeComponent) && TypeManager.GetType(componentType.TypeIndex)?.GetCustomAttribute <BehaviorTreeComponentAttribute>() != null
                    ;
                    if (shouldCreate)
                    {
                        dstManager.AddComponent(entity, componentType);
                    }
                }
            }
        }
Esempio n. 4
0
        public void Execute <TCtx>(TCtx ctx) where TCtx : IGraphInstance
        {
            var instance = ctx as GraphInstance;

            Assert.IsNotNull(instance);
            if (instance == null)
            {
                return;
            }

            var definition = ctx.GetGraphDefinition();
            var builder    = BehaviorTree.ToBuilderNode(instance, definition).Single();
            var dstManager = ctx.EntityManager;
            var entity     = dstManager.CreateEntity();
            var blob       = new NodeBlobRef(builder.ToBlob());
            var bb         = new EntityBlackboard {
                Entity = entity, EntityManager = dstManager
            };

            VirtualMachine.Reset(ref blob, ref bb);

            var query     = blob.GetAccessTypes();
            var dataQuery = new BlackboardDataQuery(query, components => dstManager.CreateEntityQuery(components.ToArray()));

            dstManager.AddSharedComponentData(entity, dataQuery);
            dstManager.AddComponentData(entity, new BehaviorTreeComponent
            {
                Blob = blob, Thread = Thread, AutoCreation = AutoCreation
            });

            dstManager.AddComponentObject(entity, new GraphInstanceComponent {
                Value = instance
            });

#if UNITY_EDITOR
            var debugName = ctx.ReadString(DebugName);
            if (!string.IsNullOrEmpty(debugName))
            {
                dstManager.SetName(entity, debugName);
            }
#endif

            ctx.Write(BehaviorTreeEntity, entity);
        }
Esempio n. 5
0
        public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
        {
            var blob = new NodeBlobRef(_source.GetBlobAsset());
            var bb   = new EntityBlackboard {
                Entity = entity, EntityManager = dstManager
            };

            VirtualMachine.Reset(ref blob, ref bb);

#if UNITY_EDITOR
            dstManager.SetName(entity, $"[BT]{_debugName}");
#endif
            var query     = blob.GetAccessTypes();
            var dataQuery = new BlackboardDataQuery(query, components =>
                                                    // HACK: workaround for different world/dstManager in subscenes.
                                                    // dstManager.CreateEntityQuery(components.ToArray()));
                                                    World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntityQuery(components.ToArray()));
            dstManager.AddSharedComponentData(entity, dataQuery);
            dstManager.AddComponentData(entity, new BehaviorTreeComponent(blob, _thread, _autoCreateTypes));
        }
Esempio n. 6
0
        public static void AddBehaviorTree(
            this Entity entity
            , EntityManager dstManager
            , NodeBlobRef blob
            , int order = 0
            , AutoCreateType autoCreateType = AutoCreateType.None
            , BehaviorTreeThread thread     = BehaviorTreeThread.ForceRunOnMainThread
            , string debugName = default
            )
        {
            var bb = new EntityBlackboard {
                Entity = entity, EntityManager = dstManager
            };

            VirtualMachine.Reset(ref blob, ref bb);

            dstManager.AddBuffer <BehaviorTreeBufferElement>(entity);
            dstManager.AddComponent <CurrentBehaviorTreeComponent>(entity);

            var behaviorTreeEntity = dstManager.CreateEntity();

#if UNITY_EDITOR
            dstManager.SetName(behaviorTreeEntity, $"[BT]{dstManager.GetName(entity)}:{order}.{debugName}");
#endif
            var query     = blob.GetAccessTypes();
            var dataQuery = new BlackboardDataQuery(query, components =>
                                                    dstManager.CreateEntityQuery(components.ToArray()));
            dstManager.AddSharedComponentData(behaviorTreeEntity, dataQuery);

            dstManager.AddComponentData(behaviorTreeEntity, new BehaviorTreeComponent
            {
                Blob = blob, Thread = thread, AutoCreation = autoCreateType
            });
            dstManager.AddComponentData(behaviorTreeEntity, new BehaviorTreeTargetComponent {
                Value = entity
            });
            dstManager.AddComponentData(behaviorTreeEntity, new BehaviorTreeOrderComponent {
                Value = order
            });
        }
Esempio n. 7
0
        public static void AddBehaviorTree(
            this Entity entity
            , EntityManager dstManager
            , NodeBlobRef blob
            , BehaviorTreeThread thread = BehaviorTreeThread.ForceRunOnMainThread
            )
        {
            var bb = new EntityBlackboard {
                Entity = entity, EntityManager = dstManager
            };

            VirtualMachine.Reset(blob, bb);
            dstManager.AddComponentData(entity, blob);

            var query     = blob.GetAccessTypes();
            var dataQuery = new BlackboardDataQuery(query, components => dstManager.CreateEntityQuery(components.ToArray()));

            dstManager.AddSharedComponentData(entity, dataQuery);

            switch (thread)
            {
            case BehaviorTreeThread.ForceRunOnMainThread:
                dstManager.AddComponentData(entity, new RunOnMainThreadTag());
                dstManager.AddComponentData(entity, new ForceRunOnMainThreadTag());
                break;

            case BehaviorTreeThread.ForceRunOnJob:
                dstManager.AddComponentData(entity, new ForceRunOnJobTag());
                break;

            case BehaviorTreeThread.ControlledByBehaviorTree:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(thread), thread, null);
            }
        }