Esempio n. 1
0
    void CreateNeighboursForItem2(GridItem item)
    {
        int x = (int)item.GetGridPos().x;
        int y = (int)item.GetGridPos().y;

        if (y - 2 >= 0 && gridItems[x, y - 2] != null)
        {
            item.AddNeighbour(gridItems [x, y - 2]);
        }


        if (x + 1 < gridItems.GetLength(0) && y - 1 >= 0 && gridItems[x + 1, y - 1] != null)
        {
            item.AddNeighbour(gridItems [x + 1, y - 1]);
        }

        if (x + 1 < gridItems.GetLength(0) && y + 1 < gridItems.GetLength(1) && gridItems[x + 1, y + 1] != null)
        {
            item.AddNeighbour(gridItems [x + 1, y + 1]);
        }

        if (y + 2 < gridItems.GetLength(1) && gridItems[x, y + 2] != null)
        {
            item.AddNeighbour(gridItems [x, y + 2]);
        }

        if (y + 1 < gridItems.GetLength(1) && gridItems[x, y + 1] != null)
        {
            item.AddNeighbour(gridItems[x, y + 1]);
        }
        if (y - 1 >= 0 && gridItems[x, y - 1] != null)
        {
            item.AddNeighbour(gridItems[x, y - 1]);
        }
    }
Esempio n. 2
0
    public void GetParentPath(GridItem node, GridItem start)
    {
        int nx = (int)node.GetGridPos().x;
        int ny = (int)node.GetGridPos().y;

        int sx = (int)start.GetGridPos().x;
        int sy = (int)start.GetGridPos().y;


        if (gridItems[nx, ny] == gridItems[sx, sy])
        {
            originalPath.Add(currentPath);
            currentPath = new List <GridItem> ();
            return;
        }

        List <GridItem> nodePath = node.GetPath();

        if (gridItems[nx, ny] != gridItems[sx, sy])
        {
            currentPath.Add(nodePath[0]);
        }

        GetParentPath(nodePath[0], start);
    }
Esempio n. 3
0
    public bool AlreadyVisited(GridItem item, List <GridItem> order)
    {
        int x = (int)item.GetGridPos().x;
        int y = (int)item.GetGridPos().y;

        foreach (var hexItem in order)
        {
            int hx = (int)hexItem.GetGridPos().x;
            int hy = (int)hexItem.GetGridPos().y;

            if (gridItems[hx, hy] == gridItems[x, y])
            {
                return(true);
            }

            var path = gridItems[hx, hy].GetPath();

            foreach (var node in path)
            {
                if (node == item)
                {
                    return(true);
                }
            }
        }

        return(false);
    }
Esempio n. 4
0
    void RevertPlayerMoves()
    {
        if (playerMoves.Count > 0)
        {
            GridItem lastMove = playerMoves [playerMoves.Count - 1];
            int      nX       = (int)lastMove.GetGridPos().x;
            int      nY       = (int)lastMove.GetGridPos().y;

            gridItems [nX, nY].UnvisitBoardItem();
            playerMoves.RemoveAt(playerMoves.Count - 1);
        }
    }
Esempio n. 5
0
    void RevertEnemyMoves()
    {
        foreach (Enemy enemy in enemies)
        {
            List <GridItem> enemyMoves = enemy.GetEnemyMoves();
            if (enemyMoves.Count > 1)
            {
                GridItem lastMove = enemyMoves [enemyMoves.Count - 2];
                int      nX       = (int)lastMove.GetGridPos().x;
                int      nY       = (int)lastMove.GetGridPos().y;

                enemy.MoveToOldPosition(nX, nY, lastMove);
            }
        }
    }
Esempio n. 6
0
    public List <GridItem> FindShortPath(GridItem start, GridItem goal)
    {
        int x = (int)goal.GetGridPos().x;
        int y = (int)goal.GetGridPos().y;

        int sx = (int)start.GetGridPos().x;
        int sy = (int)start.GetGridPos().y;

        List <GridItem> goalPath = gridItems[x, y].GetPath();


        foreach (GridItem node in goalPath)
        {
            int nx = (int)node.GetGridPos().x;
            int ny = (int)node.GetGridPos().y;

            currentPath.Add(gridItems[x, y]);
            currentPath.Add(gridItems[nx, ny]);
            GetParentPath(gridItems[nx, ny], gridItems[sx, sy]);
        }
        List <GridItem> FINALPATHFINAL = new List <GridItem> ();

        FINALPATHFINAL = originalPath [0];
        bool isBlocked = false;

        for (int i = 0; i < originalPath.Count; i++)
        {
            if (originalPath [i].Count < FINALPATHFINAL.Count)
            {
                isBlocked = true;
                foreach (var item in originalPath[i])
                {
                    if (!item.IsPassable())
                    {
                        isBlocked = true;
                    }
                }
                if (isBlocked == false)
                {
                    FINALPATHFINAL = originalPath [i];
                }
            }
        }
        FINALPATHFINAL.Reverse();
        return(FINALPATHFINAL);
    }
Esempio n. 7
0
    void GenerateNeighbours()
    {
        for (int i = 0; i < gridItems.GetLength(0); i++)
        {
            for (int j = 0; j < gridItems.GetLength(1); j++)
            {
                GridItem currentItem = gridItems [i, j];

                if (currentItem.GetGridPos().y % 2 == 0)
                {
                    CreateNeighboursForItem1(currentItem);
                }
                else
                {
                    CreateNeighboursForItem2(currentItem);
                }
            }
        }
    }