Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected override void OnUpdate()
- {
- EntityCommandBuffer.Concurrent ecb = endSimulationEcbSystem.CreateCommandBuffer().ToConcurrent();
- CollisionWorld collisionWorld = buildPhysicsWorld.PhysicsWorld.CollisionWorld;
- BufferFromEntity<NodeIDPath> bufferLookup = GetBufferFromEntity<NodeIDPath>();
- ComponentDataFromEntity<UnitsGroup> componentLookup = GetComponentDataFromEntity<UnitsGroup>();
- float _castRadius = castRadius;
- //Find already existing unitsGroup in proximity of the unit
- JobHandle FindUnitsGroupInProximityJobHandle = Entities
- .ForEach((Entity entity, int entityInQueryIndex
- , ref NeedUnitsGroup needUnitsGroup
- , in OwnerComponent ownerComponent
- , in Translation translation
- , in DynamicBuffer<NodeIDPath> nodePath) =>
- {
- NativeList<int> hitsIndices = new NativeList<int>(Allocator.Temp);
- CollisionFilter filter = new CollisionFilter
- {
- BelongsTo = 3,
- CollidesWith = 3,
- GroupIndex = 0
- };
- //Check for nearby unitsgroup already existing
- Aabb aabb = new Aabb
- {
- Min = translation.Value + new float3(-_castRadius, 0, -_castRadius),
- Max = translation.Value + new float3(_castRadius, 0, _castRadius)
- };
- OverlapAabbInput overlapAabbInput = new OverlapAabbInput
- {
- Aabb = aabb,
- Filter = filter,
- };
- if (collisionWorld.OverlapAabb(overlapAabbInput, ref hitsIndices))
- {
- //Foreach detected unitsGroup check we compare the unitsGroup node vs the one of the units
- for (int i = 0; i < hitsIndices.Length; i++)
- {
- Entity unitsGroupEntity = collisionWorld.Bodies[hitsIndices[i]].Entity;
- //check if of the same owner
- if (componentLookup[unitsGroupEntity].owner != ownerComponent.owner)
- {
- continue;
- }
- //Compare the nodePath with the one units
- DynamicBuffer<NodeIDPath> unitsGroupNodesPath = bufferLookup[unitsGroupEntity];
- //Check if the nodepath are equal if yes we can stop here , we found a corresponding group
- if (unitsGroupNodesPath.Equals(nodePath))
- {
- needUnitsGroup.foundUnitsGroupEntity = unitsGroupEntity;
- break;
- }
- //Check if the second node is different, if yes , this unitsgroup is not valid for the current unit
- if (unitsGroupNodesPath[1] != nodePath[1] || unitsGroupNodesPath.Length <= 2 || nodePath.Length <= 2)
- {
- continue;
- }
- //Finally we will check if the current unit can follow the group for part of the path and separate later
- //Take the shorter one to loop trough both and see if they have part of path in common
- int loopLength = unitsGroupNodesPath.Length;
- if (nodePath.Length < unitsGroupNodesPath.Length)
- {
- loopLength = nodePath.Length;
- }
- for (int x = 2; x < loopLength; x++)
- {
- if (nodePath[x] != unitsGroupNodesPath[x])
- {
- needUnitsGroup.foundUnitsGroupEntity = unitsGroupEntity;
- ecb.AddComponent<UnitsGroupSeparation>(entityInQueryIndex, entity, new UnitsGroupSeparation(x - 1));
- break;
- }
- }
- }
- }
- hitsIndices.Dispose();
- needUnitsGroup.proximityCheck = true;
- }).Schedule(Dependency);
Add Comment
Please, Sign In to add comment