コード例 #1
0
            public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
            {
                //Writeable.
                NativeArray <PathFinding> pathfindingArray       = chunk.GetNativeArray(pathFindingComponentHandle);
                BufferAccessor <PathNode> pathNodeBufferAccessor = chunk.GetBufferAccessor(pathNodeBufferHandle);

                //Read Only.
                NativeArray <CurrentTarget> currentTargetArray = chunk.GetNativeArray(currentTargetComponentHandle);
                NativeArray <Translation>   translationArray   = chunk.GetNativeArray(translationComponentHandle);
                NativeArray <Entity>        entities           = chunk.GetNativeArray(entityType);

                //Create a copy of the grid for this thread and chunk.
                NativeArray2D <MapNode> gridCopy = new NativeArray2D <MapNode>(gridRef.Length0, gridRef.Length1, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

                UnsafeUtility.MemCpy(gridCopy.GetUnsafePtr(), gridRef.GetUnsafePtrReadOnly(), gridRef.Length * sizeof(MapNode));

                for (int indexInChunk = 0; indexInChunk < chunk.Count; ++indexInChunk)
                {
                    //Writable.
                    PathFinding pathfinding       = pathfindingArray[indexInChunk];
                    DynamicBuffer <PathNode> path = pathNodeBufferAccessor[indexInChunk];

                    //Read Only.
                    Entity        entity        = entities[indexInChunk];
                    CurrentTarget currentTarget = currentTargetArray[indexInChunk];
                    Translation   translation   = translationArray[indexInChunk];

                    bool hasPath = chunk.Has(hasPathComponentHandle);

                    if (!pathfinding.requestedPath)
                    {
                        //We only want to remove our has path component if we didn't request a new one, to avoid re-adding later in the job if we find a new path.
                        if (pathfinding.completedPath)
                        {
                            pathfinding.completedPath = false;
                            ecb.RemoveComponent <HasPathTag>(chunkIndex, entity);
                            pathfindingArray[indexInChunk] = pathfinding;
                        }

                        continue;
                    }

                    path.Clear();
                    pathfinding.currentIndexOnPath = 0;
                    pathfinding.completedPath      = false;
                    pathfinding.requestedPath      = false;

                    //Calculate the closest nodes to us and our target position.
                    //Don't search for path if we're already at our target node.
                    pathfinding.currentNode = FindNearestNode(translation.Value, gridCopy);
                    pathfinding.targetNode  = FindNearestNode(currentTarget.targetData.targetPos, gridCopy);
                    if (pathfinding.targetNode.Equals(pathfinding.currentNode))
                    {
                        pathfindingArray[indexInChunk] = pathfinding;
                        continue;
                    }

                    CalculateGridH(gridCopy, ref pathfinding);

                    bool pathfound = SearchForPath(pathfinding.currentNode, pathfinding.targetNode, gridCopy);

                    if (pathfound)
                    {
                        ConstructPath(gridCopy, ref pathfinding, ref path);

                        if (!hasPath)
                        {
                            ecb.AddComponent <HasPathTag>(chunkIndex, entity);
                        }
                    }
                    else if (hasPath)
                    {
                        ecb.RemoveComponent <HasPathTag>(chunkIndex, entity);
                    }

                    pathfindingArray[indexInChunk] = pathfinding;
                }

                gridCopy.Dispose();
            }