Exemple #1
0
    void setting()
    {
        //It calls the clear the board function
        CleanBoard();

        //Getting the used tile type
        BlockStyle = GridManager.type.shapeOfBlock;

        //A new list is set up
        generationList = new List <GridGeneration>();

        //The origin movement cost is set to 0 and it's added to open list
        StartPosition.trackPos = 0.0f;
        generationList.Add(StartPosition);

        //If stage function is on, this line helps it to find the next stage
        PathLocatingStyle = (pathLocatingProp)((int)PathLocatingStyle + 1);
    }
Exemple #2
0
    void recreatePath()
    {
        List <GridGeneration> locateInitialList = new List <GridGeneration>();
        List <Vector3>        LocateList        = new List <Vector3>();

        GridGeneration LocatePrevoiusBlock = EndPosition;

        while (LocatePrevoiusBlock != null)
        {
            if (LocatePrevoiusBlock.currentSit != blockEnum.startBlock && LocatePrevoiusBlock.currentSit != blockEnum.endBlock)         //Colour of the Tiles are not replaced
            {
                LocatePrevoiusBlock.currentSit = blockEnum.pathTrack;
            }

            locateInitialList.Add(LocatePrevoiusBlock);
            LocateList.Add(LocatePrevoiusBlock.transform.position);

            if (LocatePrevoiusBlock == StartPosition)
            {
                break;
            }

            LocatePrevoiusBlock = LocatePrevoiusBlock.groupBlock;
        }
        //retrace line is drawn
        if (LocateList.Count <= 1)         //if there is no solution
        {
            locatePath.gameObject.SetActive(false);
        }
        else
        {
            locatePath.positionCount = LocateList.Count;
            locatePath.SetPositions(LocateList.ToArray());
            locatePath.gameObject.SetActive(true);
        }

        //Pathfinder is set to go to next stage
        PathLocatingStyle = (pathLocatingProp)((int)PathLocatingStyle + 1);
    }
Exemple #3
0
    void GetAccess()
    {
        //If open list count is empty
        if (generationList.Count > 0 && !EndPosition._isScannedArea)
        {
            //It sets the current block as first on the list
            GridGeneration currentTile = generationList[0];

            //The Block with the smallest movement cost is found
            for (int i = 1; i < generationList.Count; i++)
            {
                float newHeuristic = 0.0f;
                float curHeuristic = 0.0f;

                if (algorithmType == PathDecisionAlgorithm.AStarStyleAlgorithm)
                {
                    if (BlockStyle == BlockGrid.SquareShape)
                    {
                        SquareShape newSquareShape = ((SquareShapeScript)generationList[i]).transformValue;
                        SquareShape curSquareShape = ((SquareShapeScript)currentTile).transformValue;

                        newHeuristic = newSquareShape.GetHeuristic(EndPosition.a, EndPosition.b);
                        curHeuristic = curSquareShape.GetHeuristic(EndPosition.a, EndPosition.b);
                    }
                    else if (BlockStyle == BlockGrid.HexagonShape)
                    {
                        HexagonShape newHexagonShape = ((HexagonShapeScript)generationList[i]).transformValueHexagon;
                        HexagonShape curHexagonShape = ((HexagonShapeScript)currentTile).transformValueHexagon;

                        newHeuristic = newHexagonShape.GetHeuristic(EndPosition.a, EndPosition.b);
                        curHeuristic = curHexagonShape.GetHeuristic(EndPosition.a, EndPosition.b);
                    }
                    newHeuristic *= weight;
                    curHeuristic *= weight;
                }

                if (generationList[i].trackPos + newHeuristic < currentTile.trackPos + curHeuristic)
                {
                    currentTile = generationList[i];
                }
            }

            //It removes the current Block from open list
            currentTile._isScannedArea = true;
            generationList.Remove(currentTile);

            if (BlockStyle == BlockGrid.SquareShape)
            {
                //Neighbours of current block are taken
                SquareShape   s          = ((SquareShapeScript)currentTile).transformValue;
                SquareShape[] neighbours = s.getClosestBlock();

                for (int i = 0; i < neighbours.Length; i++)
                {
                    //Coordinate valid or not
                    if
                    (
                        neighbours[i].b < 0 || neighbours[i].b >= blockGrid.GetLength(0) ||
                        neighbours[i].a < 0 || neighbours[i].a >= blockGrid.GetLength(1)
                    )
                    {
                        continue;
                    }

                    //The coordinaes are transformed to grid piece script
                    GridGeneration closestBlock = GridManager.type.getBlocks(neighbours[i].a, neighbours[i].b);

                    //If neighbour does not exist, then skip
                    if (closestBlock == null)
                    {
                        continue;
                    }
                    //If neighbour is obstacle, then skip
                    if (closestBlock.isWall)
                    {
                        continue;
                    }
                    //If crossing diagonal gaps exist, then skip
                    if (blockSidewayWall && !ignoreSidewayWall && i % 2 != 0)
                    {
                        GridGeneration prevNeighbour = GridManager.type.getBlocks(s.getClosestBlock(i - 1).a, s.getClosestBlock(i - 1).b);
                        GridGeneration nextNeighbour = GridManager.type.getBlocks(s.getClosestBlock(i + 1).a, s.getClosestBlock(i + 1).b);
                        if (prevNeighbour.isWall && nextNeighbour.isWall) //If diagonal gap, then do not cross
                        {
                            continue;
                        }
                    }
                    //If neighbour is checked, then skip
                    if (closestBlock._isScannedArea)
                    {
                        continue;
                    }

                    //Setting the new cost to the neighbour's mevement cost
                    float newCost = currentTile.trackPos + s.GetCost(i);

                    //If the new cost is smaller than the neighbour movement cost, then replace it and set it as parent tile
                    if (newCost < closestBlock.trackPos)
                    {
                        closestBlock.trackPos   = newCost;
                        closestBlock.groupBlock = currentTile;

                        if (!generationList.Contains(closestBlock))
                        {
                            generationList.Add(closestBlock);
                        }
                    }
                }
            }
            else if (BlockStyle == BlockGrid.HexagonShape)
            {
                //Getting the neighbours of the current tile for the hexagon shape
                HexagonShape   Hexagon    = ((HexagonShapeScript)currentTile).transformValueHexagon;
                HexagonShape[] neighbours = Hexagon.GetNeighbours();

                for (int i = 0; i < neighbours.Length; i++)
                {
                    //Coordinates valid or not
                    if
                    (
                        neighbours[i].ToArrayPos(GridManager.type.blockNumber).b < 0 || neighbours[i].ToArrayPos(GridManager.type.blockNumber).b >= blockGrid.GetLength(0) ||
                        neighbours[i].ToArrayPos(GridManager.type.blockNumber).a < 0 || neighbours[i].ToArrayPos(GridManager.type.blockNumber).a >= blockGrid.GetLength(1)
                    )
                    {
                        continue;
                    }

                    //Coordinates a transfromed to grid piece script
                    GridGeneration neighbour = GridManager.type.getBlocks(neighbours[i].a, neighbours[i].b);

                    //If the neighbour does not exist, then skip
                    if (neighbour == null)
                    {
                        continue;
                    }
                    //If the neighbour is an obstacle, then skip
                    if (neighbour.isWall)
                    {
                        continue;
                    }
                    //also if is checked
                    if (neighbour._isScannedArea)
                    {
                        continue;
                    }

                    //Setting the new cost to neighbour's movement cost
                    float newCost = currentTile.trackPos + Hexagon.GetCost(i);

                    // If the new cost is smaller, then replace it and set it as the parent tile
                    if (newCost < neighbour.trackPos)
                    {
                        neighbour.trackPos   = newCost;
                        neighbour.groupBlock = currentTile;

                        if (!generationList.Contains(neighbour))
                        {
                            generationList.Add(neighbour);
                        }
                    }
                }
            }
        }
        else
        {
            //else, pathfinder is finding the next stage
            PathLocatingStyle = (pathLocatingProp)((int)PathLocatingStyle + 1);
        }
    }
Exemple #4
0
 public void GeneratePathfinding()
 {
     PathLocatingStyle = pathLocatingProp.setting;
     isSearching       = true;
     isPause           = false;
 }