Guest User

UnityForum#805

a guest
Apr 8th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1.     protected override void OnUpdate()
  2.     {
  3.  
  4.         EntityCommandBuffer.Concurrent ecb = endSimulationEcbSystem.CreateCommandBuffer().ToConcurrent();
  5.         CollisionWorld collisionWorld = buildPhysicsWorld.PhysicsWorld.CollisionWorld;
  6.  
  7.         BufferFromEntity<NodeIDPath> bufferLookup = GetBufferFromEntity<NodeIDPath>();
  8.         ComponentDataFromEntity<UnitsGroup> componentLookup = GetComponentDataFromEntity<UnitsGroup>();
  9.  
  10.         float _castRadius = castRadius;
  11.  
  12.         //Find already existing unitsGroup in proximity of the unit
  13.         JobHandle FindUnitsGroupInProximityJobHandle = Entities
  14.         .ForEach((Entity entity, int entityInQueryIndex
  15.         , ref NeedUnitsGroup needUnitsGroup
  16.         , in OwnerComponent ownerComponent
  17.         , in Translation translation
  18.         , in DynamicBuffer<NodeIDPath> nodePath) =>
  19.         {
  20.             NativeList<int> hitsIndices = new NativeList<int>(Allocator.Temp);
  21.  
  22.             CollisionFilter filter = new CollisionFilter
  23.             {
  24.                 BelongsTo = 3,
  25.                 CollidesWith = 3,
  26.                 GroupIndex = 0
  27.             };
  28.             //Check for nearby unitsgroup already existing
  29.             Aabb aabb = new Aabb
  30.             {
  31.                 Min = translation.Value + new float3(-_castRadius, 0, -_castRadius),
  32.                 Max = translation.Value + new float3(_castRadius, 0, _castRadius)
  33.             };
  34.             OverlapAabbInput overlapAabbInput = new OverlapAabbInput
  35.             {
  36.                 Aabb = aabb,
  37.                 Filter = filter,
  38.             };
  39.             if (collisionWorld.OverlapAabb(overlapAabbInput, ref hitsIndices))
  40.             {
  41.                 //Foreach detected unitsGroup check we compare the unitsGroup node vs the one of the units
  42.                 for (int i = 0; i < hitsIndices.Length; i++)
  43.                 {
  44.                     Entity unitsGroupEntity = collisionWorld.Bodies[hitsIndices[i]].Entity;
  45.                     //check if of the same owner
  46.                     if (componentLookup[unitsGroupEntity].owner != ownerComponent.owner)
  47.                     {
  48.                         continue;
  49.                     }
  50.                     //Compare the nodePath with the one units
  51.                     DynamicBuffer<NodeIDPath> unitsGroupNodesPath = bufferLookup[unitsGroupEntity];
  52.                     //Check if the nodepath are equal if yes we can stop here , we found a corresponding group
  53.                     if (unitsGroupNodesPath.Equals(nodePath))
  54.                     {
  55.                         needUnitsGroup.foundUnitsGroupEntity = unitsGroupEntity;
  56.                         break;
  57.                     }
  58.                     //Check if the second node is different, if yes , this unitsgroup is not valid for the current unit
  59.                     if (unitsGroupNodesPath[1] != nodePath[1] || unitsGroupNodesPath.Length <= 2 || nodePath.Length <= 2)
  60.                     {
  61.                         continue;
  62.                     }
  63.                     //Finally we will check if the current unit can follow the group for part of the path and separate later
  64.                     //Take the shorter one to loop trough both and see if they have part of path in common
  65.                     int loopLength = unitsGroupNodesPath.Length;
  66.                     if (nodePath.Length < unitsGroupNodesPath.Length)
  67.                     {
  68.                         loopLength = nodePath.Length;
  69.                     }
  70.                     for (int x = 2; x < loopLength; x++)
  71.                     {
  72.                         if (nodePath[x] != unitsGroupNodesPath[x])
  73.                         {
  74.                             needUnitsGroup.foundUnitsGroupEntity = unitsGroupEntity;
  75.                             ecb.AddComponent<UnitsGroupSeparation>(entityInQueryIndex, entity, new UnitsGroupSeparation(x - 1));
  76.                             break;
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.             hitsIndices.Dispose();
  82.  
  83.             needUnitsGroup.proximityCheck = true;
  84.         }).Schedule(Dependency);
Add Comment
Please, Sign In to add comment