Example #1
0
    void YolBul(Vector3 baslangicPos, Vector3 hedefPos)
    {
        Node baslangicDugum = grid.GridDugumleri(baslangicPos);
        Node hedefDugum     = grid.GridDugumleri(hedefPos);

        List <Node>    openKume   = new List <Node>();
        HashSet <Node> kapaliKume = new HashSet <Node>();

        openKume.Add(baslangicDugum);

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

            openKume.Remove(node);
            kapaliKume.Add(node);

            if (node == hedefDugum)
            {
                DonusYolu(baslangicDugum, hedefDugum);
                return;
            }

            foreach (Node komsudugumler in grid.KomsulukMatrisi(node))
            {
                if (!komsudugumler.walkable || kapaliKume.Contains(komsudugumler))
                {
                    continue;
                }

                int newCostTokomsudugumler = node.gCost + UzaklikOlcumu(node, komsudugumler);
                if (newCostTokomsudugumler < komsudugumler.gCost || !openKume.Contains(komsudugumler))
                {
                    komsudugumler.gCost  = newCostTokomsudugumler;
                    komsudugumler.hCost  = UzaklikOlcumu(komsudugumler, hedefDugum);
                    komsudugumler.parent = node;

                    if (!openKume.Contains(komsudugumler))
                    {
                        openKume.Add(komsudugumler);
                    }
                }
            }
        }
    }