protected override JobHandle OnUpdate(JobHandle inputDependencies)
    {
        //shared ECB between all jobs
        EntityCommandBuffer.Concurrent ecb = endInitECBSystem.CreateCommandBuffer().ToConcurrent();

        //Job 1
        //First we run through all the enemies finding the closest player (generally there's only one).
        //We prepare 2 NativeArrays: a reference to the players, and to their positions. This way enemies can run distance checks and note down which Entity is the closest player.
        NativeArray <Entity>      players         = playersGroup.ToEntityArray(Allocator.TempJob, out JobHandle handle1);
        NativeArray <Translation> playerPositions = playersGroup.ToComponentDataArray <Translation>(Allocator.TempJob, out JobHandle handle2);

        //This code is running on the main thread but the 2 NativeArrays above are fetched in a job.
        //This is why we combine the dependencies and pass them all to the job when we schedule it (see below), to ensure that all the data is ready at the moment the job is launched.
        //For more info: https://gametorrahod.com/minimum-main-thread-block-with-out-jobhandle-overload/ (first section)
        JobHandle newInputDependencies = JobHandle.CombineDependencies(inputDependencies, handle1, handle2);

        var job1 = new FindClosestPlayerJob()
        {
            players   = players,
            positions = playerPositions,
            ECB       = ecb,
        };

        JobHandle job1Handle = job1.Schedule(this, newInputDependencies);

        //Job 2
        //Now iterating through the players (only one) finding the closest enemy.
        //We prepare the NativeArrays like in the job above.
        NativeArray <Entity>      enemies        = enemyGroup.ToEntityArray(Allocator.TempJob, out JobHandle handle3);
        NativeArray <Translation> enemyPositions = enemyGroup.ToComponentDataArray <Translation>(Allocator.TempJob, out JobHandle handle4);

        //Again, we combine dependencies to make sure that job1 is run as well as the fetching of the 2 NativeArrays, before running job2
        JobHandle job1Dependencies = JobHandle.CombineDependencies(job1Handle, handle3, handle4);

        var job2 = new FindClosestEnemyJob()
        {
            enemies   = enemies,
            positions = enemyPositions,
            ECB       = ecb,
        };

        JobHandle job2Handle = job2.Schedule(this, job1Dependencies);

        endInitECBSystem.AddJobHandleForProducer(job2Handle);

        //Job 3
        //All characters already with a Target but not currently dealing an attack, check if the target is still in range or dead.
        var job3 = new CheckIfTargetValidJob()
        {
            ECB = ecb,
            targetTranslations = GetComponentDataFromEntity <Translation>(true),
        };
        JobHandle job3Handle = job3.Schedule(this, job2Handle);

        endInitECBSystem.AddJobHandleForProducer(job3Handle);

        return(job3Handle);
    }
Exemplo n.º 2
0
    /// <summary>
    /// Prepare to fire query alter
    /// </summary>
    /// <param name="jobHandle"></param>
    /// <returns></returns>

    protected override JobHandle OnUpdate(JobHandle jobHandle)
    {
        var friendlyType = GetComponentTypeHandle <Friendly> (); //Should change to friendly
        var targetType   = GetComponentTypeHandle <TargetPosition> ();

        var job = new FindClosestEnemyJob {
            friendlySoldier = friendlyType,
            friendlyTarget  = targetType,

            enemyEntities = GridJobStatics.enemyEntities,
            enemies       = this.GetComponentDataFromEntity <Enemy> (),
            enMat         = this.GetComponentDataFromEntity <EnemyMaterialColor> (),
        };

        return(job.ScheduleParallel(m_FriendlyEQ, jobHandle));
    }