I had this system as an ECS, now I am trying to convert it to a job system. My issue is I was using
World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData<Translation>( hasTarget.targetEntity );
But this does not work inside of JobSystem. I am new to this and would appreciate any help. Since this job runs on multiple entities, I am unsure of how to pass multiple Translation data to the job and I dont understand how the job will know which Translation data corresponds to each entity.
public class UnitMoveToTargetJobSystem : JobComponentSystem
{
//[ RequireComponentTag( typeof( HasTarget ) ) ]
[ ExcludeComponent( typeof( InCombat ) ) ]
[ BurstCompile ]
private struct MoveToTargetJob : IJobForEachWithEntity<HasTarget , Translation>
{
public EntityCommandBuffer.Concurrent entityCommandBuffer;
public float dt;
public bool entityHasTarget;
public void Execute( Entity unitEntity , int index , ref HasTarget hasTarget , ref Translation translation )
{
if ( hasTarget.targetEntity != null )
{
Translation targetTranslation = World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData<Translation>( hasTarget.targetEntity );
float3 targetDirection = math.normalize( targetTranslation.Value - translation.Value );
float moveSpeed = 8f;
translation.Value += targetDirection * moveSpeed * dt;
if ( math.distance( translation.Value , targetTranslation.Value ) <= 0.3f )
{
entityCommandBuffer.AddComponent( index , unitEntity , new InCombat { enemy = hasTarget.targetEntity } );
entityCommandBuffer.RemoveComponent( index , unitEntity , typeof( HasTarget ) );
//PostUpdateCommands.AddComponent( unitEntity , new InCombat { enemy = hasTarget.targetEntity } );
//PostUpdateCommands.RemoveComponent( unitEntity , typeof( HasTarget ) );
}
}
else
{
entityCommandBuffer.RemoveComponent( index , unitEntity , typeof( HasTarget ) );
//PostUpdateCommands.RemoveComponent( unitEntity , typeof( HasTarget ) );
}
}
}
private EndSimulationEntityCommandBufferSystem endSimulationEntityCommandBufferSystem;
protected override void OnCreate()
{
endSimulationEntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
base.OnCreate();
}
protected override JobHandle OnUpdate( JobHandle inputDeps )
{
MoveToTargetJob moveToTargetJob = new MoveToTargetJob
{
entityCommandBuffer = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent() ,
dt = Time.DeltaTime
};
JobHandle jobHandle = moveToTargetJob.Schedule( this , inputDeps );
endSimulationEntityCommandBufferSystem.AddJobHandleForProducer( jobHandle );
return jobHandle;
}
}