示例#1
0
    public List <oNoeud> GetNeighbours(oNoeud node)
    {
        List <oNoeud> neighbours = new List <oNoeud>();

        for (int x = -1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                if (x == 0 && y == 0)
                {
                    continue;
                }

                int checkX = node.gridX + x;
                int checkY = node.gridY + y;

                if (checkX >= 0 && checkX < mapGenerator.largeur && checkY >= 0 && checkY < mapGenerator.hauteur)
                {
                    neighbours.Add(grid[checkX, checkY]);
                }
            }
        }

        return(neighbours);
    }
示例#2
0
    int GetDistance(oNoeud nodeA, oNoeud nodeB)
    {
        int dstX = Mathf.Abs(nodeA.gridX - nodeB.gridX);
        int dstY = Mathf.Abs(nodeA.gridY - nodeB.gridY);

        if (dstX > dstY)
        {
            return(14 * dstY + 10 * (dstX - dstY));
        }
        return(14 * dstX + 10 * (dstY - dstX));
    }
示例#3
0
 public void CreateGrid()
 {
     grid = new oNoeud[mapGenerator.largeur, mapGenerator.hauteur];
     for (int x = 0; x < mapGenerator.largeur; x++)
     {
         for (int y = 0; y < mapGenerator.hauteur; y++)
         {
             bool walkable = mapGenerator.tuilesMap[y, x] != MapGenerator.TypeTuile.Eau;
             bool arbre    = mapGenerator.arbreSurPosition(new Vector2(x, y)) != null;
             grid[x, y] = new oNoeud(walkable, arbre, x, y);
         }
     }
 }
示例#4
0
    List <oNoeud> RetracePath(oNoeud startNode, oNoeud endNode)
    {
        List <oNoeud> path        = new List <oNoeud>();
        oNoeud        currentNode = endNode;

        while (currentNode != startNode)
        {
            path.Add(currentNode);
            currentNode = currentNode.parent;
        }
        path.Reverse();
        return(path);
    }
示例#5
0
    public List <Vector2> FindPath(Vector2 startPos, Vector2 targetPos, bool arbreBloque)
    {
        oNoeud startNode  = grid.NodeFromWorldPoint(startPos);
        oNoeud targetNode = grid.NodeFromWorldPoint(targetPos);

        List <oNoeud>    openSet   = new List <oNoeud>();
        HashSet <oNoeud> closedSet = new HashSet <oNoeud>();

        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            oNoeud node = openSet[0];
            for (int i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].fCost < node.fCost || (openSet[i].fCost == node.fCost && openSet[i].hCost < node.hCost))
                {
                    if (openSet[i].hCost < node.hCost)
                    {
                        node = openSet[i];
                    }
                }
            }

            openSet.Remove(node);
            closedSet.Add(node);

            //Chemin trouvé
            if (node == targetNode)
            {
                List <oNoeud>  noeudsCheminFinal = RetracePath(startNode, targetNode);
                List <Vector2> cheminFinal       = new List <Vector2>();
                foreach (oNoeud noeud in noeudsCheminFinal)
                {
                    cheminFinal.Add(new Vector2(noeud.gridX, noeud.gridY));
                }
                return(cheminFinal);
            }

            foreach (oNoeud neighbour in grid.GetNeighbours(node))
            {
                //Verification du type de case
                if (!neighbour.walkable || (arbreBloque && neighbour.arbre) || closedSet.Contains(neighbour))
                {
                    continue;
                }

                //Verification de la diagonale dans les murs
                Vector2 differencePositionsVoisinSoi = (new Vector2(neighbour.gridX, neighbour.gridY) - new Vector2(node.gridX, node.gridY));
                if (!grid.NodeFromWorldPoint(new Vector2(node.gridX + differencePositionsVoisinSoi.x, node.gridY)).walkable ||
                    !grid.NodeFromWorldPoint(new Vector2(node.gridX, node.gridY + differencePositionsVoisinSoi.y)).walkable)
                {
                    continue;
                }
                //Vérification des diagonale dans les murs si c'est un arbre dans la diagonale
                if (arbreBloque &&
                    (grid.NodeFromWorldPoint(new Vector2(node.gridX + differencePositionsVoisinSoi.x, node.gridY)).arbre ||
                     grid.NodeFromWorldPoint(new Vector2(node.gridX, node.gridY + differencePositionsVoisinSoi.y)).arbre))
                {
                    continue;
                }

                //Verification des voisins
                int newCostToNeighbour = node.gCost + GetDistance(node, neighbour);
                if (newCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                {
                    neighbour.gCost  = newCostToNeighbour;
                    neighbour.hCost  = GetDistance(neighbour, targetNode);
                    neighbour.parent = node;

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }
        return(new List <Vector2>());
    }