public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
            {
                NativeArray <NavigationGridData>  navigationGrids    = chunk.GetNativeArray(this.navigationGridType);
                BufferAccessor <AStarNodeElement> nodeBufferAccessor = chunk.GetBufferAccessor(this.nodeBufferType);

                for (int ci = 0, cn = chunk.Count; ci < cn; ci++)
                {
                    if (hit[0].Entity != Entity.Null)
                    {
                        // Set navigation if valid block was selected

                        NavigationGridData navigationGrid           = navigationGrids[ci];
                        DynamicBuffer <AStarNodeElement> nodeBuffer = nodeBufferAccessor[ci];

                        // End node grid index
                        int    endNodeIndex;
                        float3 hitPosition = hit[0].Position;
                        navigationGrid.WorldToGridPosition(hitPosition, out endNodeIndex);

                        // Start node grid index
                        int startNodeIndex = this.currentNode[0].Index;

                        // Find start/end nodes from dynamic buffer
                        AStarNode startNode  = new AStarNode();
                        AStarNode endNode    = new AStarNode();
                        bool      startFound = false;
                        bool      endFound   = false;
                        for (int i = 0, n = nodeBuffer.Length; i < n; i++)
                        {
                            if (nodeBuffer[i].Value.Index == endNodeIndex)
                            {
                                endFound = true;
                                endNode  = nodeBuffer[i].Value;
                            }
                            else if (nodeBuffer[i].Value.Index == startNodeIndex)
                            {
                                startFound = true;
                                startNode  = nodeBuffer[i].Value;
                            }

                            if (startFound && endFound)
                            {
                                break;
                            }
                        }

                        // Request path if start/end both found
                        if (startFound && endFound)
                        {
                            navigationGrid.pathRequested          = true;
                            navigationGrid.pathMustBeStraightLine = true;
                            navigationGrid.pathStart = startNode;
                            navigationGrid.pathEnd   = endNode;
                        }

                        // Apply changes
                        navigationGrids[ci] = navigationGrid;
                    }
                }
            }
        public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
        {
            dstManager.AddBuffer <AStarNodeElement>(entity);

            NavigationGridData grid = dstManager.GetComponentData <NavigationGridData>(entity);

            grid.pathRequested = false;
        }
Beispiel #3
0
            public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
            {
                NativeArray <NavigationGridData>  navigationGrids    = chunk.GetNativeArray(this.navigationGridType);
                BufferAccessor <AStarNodeElement> nodeBufferAccessor = chunk.GetBufferAccessor(this.nodeBufferType);

                for (int ci = 0, cn = chunk.Count; ci < cn; ci++)
                {
                    NavigationGridData navigationGrid           = outNavigationGrid[0] = navigationGrids[ci];
                    DynamicBuffer <AStarNodeElement> nodeBuffer = nodeBufferAccessor[ci];

                    // Solve path if requested

                    if (navigationGrid.pathRequested)
                    {
                        // All nodes
                        NativeArray <AStarNode> nodes = nodeBuffer.Reinterpret <AStarNode>().ToNativeArray(Allocator.Temp);

                        // Create temp grid
                        AStarGrid aStarGrid = new AStarGrid(navigationGrid.lengthX, navigationGrid.lengthY, nodes);

                        // Start/end nodes
                        AStarNode start = navigationGrid.pathStart;
                        AStarNode end   = navigationGrid.pathEnd;

                        // Solve path
                        NativeList <AStarNode> pathNodeList = new NativeList <AStarNode>(Allocator.Temp);
                        bool pathFound  = AStarSolver.SolvePath(aStarGrid, start, end, ref pathNodeList);
                        int  pathLength = pathNodeList.Length;
                        if (pathFound && navigationGrid.pathMustBeStraightLine)
                        {
                            // Check if path is straight line if specfied as a requirement
                            bool xDiverge = false;
                            bool yDiverge = false;
                            for (int i = 1, n = pathNodeList.Length; i < n; i++)
                            {
                                if (!xDiverge)
                                {
                                    xDiverge = pathNodeList[i].XCoord != pathNodeList[i - 1].XCoord;
                                }
                                if (!yDiverge)
                                {
                                    yDiverge = pathNodeList[i].YCoord != pathNodeList[i - 1].YCoord;
                                }
                                if (xDiverge && yDiverge)
                                {
                                    pathFound = false;
                                    break;
                                }
                            }
                        }


                        // Copy path node list to output array
                        NativeSlice <AStarNode> pathNodeListSlice = new NativeSlice <AStarNode>(pathNodeList.AsArray());
                        NativeSlice <AStarNode> pathNodeSlice     = new NativeSlice <AStarNode>(this.outPathNodes, 0, pathLength);
                        pathNodeSlice.CopyFrom(pathNodeListSlice);

                        // Dispose native containers
                        pathNodeList.Dispose();
                        aStarGrid.Dispose();
                        nodes.Dispose();

                        this.outPathFound[0]                  = pathFound ? 1 : 0;
                        this.outPathFound[1]                  = pathLength;
                        navigationGrid.pathRequested          = false;
                        navigationGrid.pathMustBeStraightLine = false;

                        // Apply changes
                        navigationGrids[ci] = navigationGrid;
                    }
                }
            }