Exemplo n.º 1
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        EntityQuery targetQuery = GetEntityQuery(typeof(Kobolt), ComponentType.ReadOnly <Translation>(), ComponentType.Exclude <DoNotTarget>());

        EntityQuery          unitQuery = GetEntityQuery(typeof(SeekComponent), ComponentType.Exclude <HasTarget>());
        NativeArray <Entity> closestTargetEntityArray = new NativeArray <Entity>(unitQuery.CalculateEntityCount(), Allocator.TempJob);

        FindTargetSectorSystemJob findTargetSectorSystemJob = new FindTargetSectorSystemJob
        {
            SectorMultiHashMap       = SectorSystem.SectorMultiHashMap,
            closestTargetEntityArray = closestTargetEntityArray,
        };
        JobHandle jobHandle = findTargetSectorSystemJob.Schedule(this, inputDeps);

        // Add HasTarget Component to Entities that have a Closest Target
        AddComponentJob addComponentJob = new AddComponentJob
        {
            closestTargetEntityArray = closestTargetEntityArray,
            entityCommandBuffer      = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(),
        };

        jobHandle = addComponentJob.Schedule(this, jobHandle);

        endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(jobHandle);

        return(jobHandle);
    }
Exemplo n.º 2
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        EntityQuery animalQuery = GetEntityQuery(typeof(Tag_Animal), ComponentType.ReadOnly <LocalToWorld>());

        NativeArray <EntityWithPosition> closestDangerTargetArray = new NativeArray <EntityWithPosition>(animalQuery.CalculateEntityCount(), Allocator.TempJob);
        NativeArray <EntityWithPosition> closestFoodTargetArray   = new NativeArray <EntityWithPosition>(animalQuery.CalculateEntityCount(), Allocator.TempJob);

        FindEnemyQuadrantSystemJob findTargetQuadrantSystemJob = new FindEnemyQuadrantSystemJob
        {
            quadrantMultiHashMap     = QuadrantSystem.quadrantMultiHashMap,
            closestDangerTargetArray = closestDangerTargetArray,
            closestFoodTargetArray   = closestFoodTargetArray
        };
        JobHandle jobHandle = findTargetQuadrantSystemJob.Schedule(this, inputDeps);

        // Add HasTarget Component to Entities that have a Closest Target
        AddComponentJob addComponentJob = new AddComponentJob
        {
            closestDangerTargetArray = closestDangerTargetArray,
            closestFoodTargetArray   = closestFoodTargetArray,
            entityCommandBuffer      = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(),
        };

        jobHandle = addComponentJob.Schedule(this, jobHandle);

        endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(jobHandle);

        return(jobHandle);
    }
Exemplo n.º 3
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        EntityQuery query = GetEntityQuery(typeof(Target), ComponentType.ReadOnly <Translation>());

        NativeArray <Entity>      entities     = query.ToEntityArray(Allocator.TempJob);
        NativeArray <Translation> translations = query.ToComponentDataArray <Translation>(Allocator.TempJob);

        NativeArray <EntityWithPosition> entityWithPositions = new NativeArray <EntityWithPosition>(entities.Length, Allocator.TempJob);

        for (int i = 0; i < entityWithPositions.Length; i++)
        {
            entityWithPositions[i] = new EntityWithPosition
            {
                entity   = entities[i],
                position = translations[i].Value
            };
        }

        // Disponse native arrays
        entities.Dispose();
        translations.Dispose();

        EntityQuery          unitQuery      = GetEntityQuery(typeof(Unit), ComponentType.Exclude <HasTarget>());
        NativeArray <Entity> closestTargets = new NativeArray <Entity>(unitQuery.CalculateEntityCount(), Allocator.TempJob);

        //FindTargetBurstJob findTargetBurstJob = new FindTargetBurstJob
        //{
        //    targetArray = entityWithPositions,
        //    closestTargets = closestTargets
        //};
        //JobHandle handle = findTargetBurstJob.Schedule(this, inputDeps);

        FindTargetQuadrantSystemJob findTargetQuadrantSystemJob = new FindTargetQuadrantSystemJob
        {
            quadrantMap    = QuadrantSystem.quadrantMap,
            closestTargets = closestTargets
        };

        JobHandle handle = findTargetQuadrantSystemJob.Schedule(this, inputDeps);

        AddComponentJob addComponentJob = new AddComponentJob
        {
            closestTargets      = closestTargets,
            entityCommandBuffer = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent()
        };

        handle = addComponentJob.Schedule(this, handle);

        endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(handle);

        return(handle);
    }
Exemplo n.º 4
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            EntityQuery targetQuery = GetEntityQuery(new EntityQueryDesc()
            {
                All = new ComponentType[]
                {
                    typeof(EzTarget),
                    ComponentType.ReadOnly <Translation>()
                },
            });
            var         pArrayEntity      = targetQuery.ToEntityArray(Allocator.TempJob);
            var         pArrayTranslation = targetQuery.ToComponentDataArray <Translation>(Allocator.TempJob);
            var         pArrayEztarget    = targetQuery.ToComponentDataArray <EzTarget>(Allocator.TempJob);
            EntityQuery pUnitNontarget    = GetEntityQuery(typeof(EzUnit), typeof(Translation), ComponentType.Exclude <EzHasTarget>());
            var         pClosedFindEntity = new NativeArray <Entity>(pUnitNontarget.CalculateEntityCount(), Allocator.TempJob);

            NativeArray <EntityWithPosition> targetArray = new NativeArray <EntityWithPosition>(pArrayEntity.Length, Allocator.TempJob);

            for (int i = 0; i < pArrayEntity.Length; ++i)
            {
                targetArray[i] = new EntityWithPosition()
                {
                    entity   = pArrayEntity[i],
                    layer    = pArrayEztarget[i].layer.layer,
                    position = pArrayTranslation[i].Value
                };
            }
            pArrayEntity.Dispose();
            pArrayTranslation.Dispose();
            pArrayEztarget.Dispose();
            FindTargetJob jobFind = new FindTargetJob()
            {
                targetArray = targetArray,
                closestTargetEntityArray = pClosedFindEntity
            };
            var pAddComponentJob = new AddComponentJob()
            {
                entityCommandBuffer      = commandBufferSystem.CreateCommandBuffer().ToConcurrent(),
                closestTargetEntityArray = pClosedFindEntity,
            };
            JobHandle pJob = jobFind.Schedule(this, inputDeps);

            pJob = pAddComponentJob.Schedule(this, pJob);
            commandBufferSystem.AddJobHandleForProducer(pJob);
            return(pJob);
        }
Exemplo n.º 5
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            EntityQuery targetQuery = GetEntityQuery(typeof(Target), ComponentType.ReadOnly <Translation>());

            NativeArray <Entity>      targetEntityArray      = targetQuery.ToEntityArray(Allocator.TempJob);
            NativeArray <Translation> targetTranslationArray = targetQuery.ToComponentDataArray <Translation>(Allocator.TempJob);

            var targetArray = new NativeArray <EntityWithPosition>(targetTranslationArray.Length, Allocator.TempJob);

            for (int i = 0; i < targetEntityArray.Length; i++)
            {
                targetArray[i] = new EntityWithPosition
                {
                    entity   = targetEntityArray[i],
                    position = targetTranslationArray[i].Value,
                };
            }

            targetEntityArray.Dispose();
            targetTranslationArray.Dispose();

            EntityQuery unitQuery = GetEntityQuery(typeof(Unit), ComponentType.Exclude <HasTarget>());
            var         closestTargetEntityArray = new NativeArray <Entity>(unitQuery.CalculateEntityCount(), Allocator.TempJob);

            var findTargetJob = new FindTargetJob
            {
                targetArray = targetArray,
                closestTargetEntityArray = closestTargetEntityArray,
            };

            JobHandle jobHandle = findTargetJob.Schedule(this, inputDeps);

            var addComponentJob = new AddComponentJob
            {
                closestTargetEntityArray = closestTargetEntityArray,
                entityCommandBuffer      = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(),
            };

            jobHandle = addComponentJob.Schedule(this, jobHandle);

            endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(jobHandle);

            return(jobHandle);
        }
Exemplo n.º 6
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        bool JobAdd = GameHander_test.GameHander_test_ins.JobAddComponent;

        bool JobEnabled = GameHander_test.GameHander_test_ins.JobBool;

        //Debug.Log(JobAdd);

        //共通部分,為了拿到目標物的Entity

        //因為Job中無法訪問世界所以要先訪問佇列
        //這句意思就是找有Ball_Target組件 並有Translation組件
        EntityQuery targetQuery = GetEntityQuery(typeof(Ball_Target), ComponentType.ReadOnly <Translation>());
        //轉成陣列
        NativeArray <Entity>      targetEntityArray      = targetQuery.ToEntityArray(Allocator.TempJob);
        NativeArray <Translation> targetTranslationArray = targetQuery.ToComponentDataArray <Translation>(Allocator.TempJob);
        //轉換完,這是目標圓球陣列
        NativeArray <EntityJobArray> targetArray = new NativeArray <EntityJobArray>(targetEntityArray.Length, Allocator.TempJob);

        //輸入資料
        for (int i = 0; i < targetEntityArray.Length; i++)
        {
            targetArray[i] = new EntityJobArray
            {
                entity   = targetEntityArray[i],
                position = targetTranslationArray[i].Value,
            };
        }
        //釋放陣列
        targetEntityArray.Dispose();
        targetTranslationArray.Dispose();

        JobHandle jobHandle;

        //共通結束

        //是否啟動JobSystem還是ComponentSystem
        if (JobEnabled == true)
        {
            //啟動JobSystem
            if (JobAdd == false)
            {
                //第一種作法
                FindTargetJobSystem_No1 findTargetJobSystem_No1 = new FindTargetJobSystem_No1
                {
                    targetarray         = targetArray,
                    entityCommandBuffer = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent()
                };

                jobHandle = findTargetJobSystem_No1.Schedule(this, inputDeps);
            }
            else
            {
                //第二種作法

                //這句意思就是找有Cube_Unit組件 並排除Ball_Target組件
                EntityQuery          unitQuery = GetEntityQuery(typeof(Cube_Unit), ComponentType.Exclude <Ball_Target>());
                NativeArray <Entity> closestTargetEntityArray = new NativeArray <Entity>(unitQuery.CalculateEntityCount(), Allocator.TempJob);

                FindTargetJobSystem_No2 findTargetJobSystem_No2 = new FindTargetJobSystem_No2
                {
                    targetarray       = targetArray,
                    TargetEntityArray = closestTargetEntityArray
                };

                jobHandle = findTargetJobSystem_No2.Schedule(this, inputDeps);

                AddComponentJob addComponentJob = new AddComponentJob
                {
                    TargetEntityArray   = closestTargetEntityArray,
                    entityCommandBuffer = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(),
                };

                jobHandle = addComponentJob.Schedule(this, jobHandle);
            }
        }
        else
        {
            //啟動ComponentSystem但JobSystem不能return空,所以用一空Job賦值
            FindTargetJobSystem_Null findTargetJobSystem_Null = new FindTargetJobSystem_Null {
            };
            jobHandle = findTargetJobSystem_Null.Schedule(this, inputDeps);
        }



        // 備註
        //從作業寫入命令緩衝區時,必須添加
        //通過調用此函數將該Job映射到此緩衝區系統的依賴項列表。
        //否則,當寫入作業仍在進行時,緩衝區系統可以執行命令緩衝區中當前的命令。
        //以上Unity官方
        //每當將命令緩衝區傳遞給系統時,您都需要將作業句柄返回到擁有命令緩衝區的系統(您可以簡單地將其返回到系統末尾傳遞最終的作業句柄。)
        //這是確保作業的必需條件在屏障系統運行之前,已完成使用命令緩衝區的操作。
        //以上Unity論壇
        //大致理解,就是為了保護,避免內存出意外,所以擁有EntityCommandBuffer的Job都必須加上這句,放在末尾
        endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(jobHandle);

        return(jobHandle);
    }