Пример #1
0
    public void TeleportDesitinationSelect()
    {
        foreach (GameObject T in GetComponent <Pathfinding>().CurrentLocation.GetComponent <Tile>().Adjacent)
        {
            PossibleTeleportDestinations.Add(T);
        }

        List <GameObject> temp = new List <GameObject>();

        for (int k = 2; k <= TeleportRange; k++)
        {
            foreach (GameObject T in PossibleTeleportDestinations)
            {
                foreach (GameObject TT in T.GetComponent <Tile>().Adjacent)
                {
                    if (PossibleTeleportDestinations.Contains(TT) == false)
                    {
                        temp.Add(TT);
                    }
                }
            }

            foreach (GameObject TTT in temp)
            {
                PossibleTeleportDestinations.Add(TTT);
            }
        }

        foreach (Transform T in Grid)
        {
            T.GetComponent <Pathnode>().Fade();
        }
        foreach (GameObject TT in PossibleTeleportDestinations)
        {
            TT.GetComponent <Pathnode>().Unfade();
        }
    }
Пример #2
0
    public float AStar(GameObject U, GameObject E)
    {
        GameObject End = E.GetComponent <Pathfinding>().CurrentLocation;

        GameObject Start = U.GetComponent <Pathfinding>().CurrentLocation;

        List <GameObject> OpenSet   = new List <GameObject>();
        List <GameObject> ClosedSet = new List <GameObject>();

        OpenSet.Add(Start);

        Start.GetComponent <Pathnode>().g = 0;

        foreach (Transform T in Grid)
        {
            T.GetComponent <Pathnode>().h = Vector3.Distance(T.position, End.transform.position);
            T.GetComponent <Pathnode>().f = T.GetComponent <Pathnode>().h;
        }

        while (OpenSet.Count > 0)
        {
            GameObject T = null;
            float      x = 10000000000000000000;

            foreach (GameObject B in OpenSet)
            {
                if (B.GetComponent <Pathnode>().f < x)
                {
                    x = B.GetComponent <Pathnode>().f;
                }
            }

            foreach (GameObject B in OpenSet)
            {
                if (B.GetComponent <Pathnode>().f == x)
                {
                    T = B;
                }
            }

            if (T == null)
            {
                T = U;
                Debug.Log("big error in A*");
                return(0);
            }

            foreach (GameObject TT in T.GetComponent <Tile>().Adjacent)
            {
                if (ClosedSet.Contains(TT) == false)
                {
                    if (TT == End)
                    {
                        TT.GetComponent <Pathnode>().g        = T.GetComponent <Pathnode>().g + TT.GetComponent <Pathnode>().MPRequired;
                        TT.GetComponent <Pathnode>().Previous = T;
                        TT.GetComponent <Pathnode>().MPRemain = -(int)TT.GetComponent <Pathnode>().g;
                        return(TT.GetComponent <Pathnode>().g);
                    }

                    TT.GetComponent <Pathnode>().Previous = T;
                    TT.GetComponent <Pathnode>().g        = T.GetComponent <Pathnode>().g + TT.GetComponent <Pathnode>().MPRequired;
                    TT.GetComponent <Pathnode>().f        = TT.GetComponent <Pathnode>().h + TT.GetComponent <Pathnode>().g;
                    TT.GetComponent <Pathnode>().MPRemain = U.GetComponent <Pathfinding>().MovePoints - (int)TT.GetComponent <Pathnode>().g;

                    if (TT.GetComponent <Pathnode>().CurrentOccupant != null)
                    {
                        TT.GetComponent <Pathnode>().g = 10000000000000;
                    }

                    //Debug.Log("name " + TT.name +
                    //    " previous " + TT.GetComponent<Pathnode>().Previous.name +
                    //    " g " + TT.GetComponent<Pathnode>().g +
                    //    " h " + TT.GetComponent<Pathnode>().h +
                    //    " f " + TT.GetComponent<Pathnode>().f +
                    //    " MP Remain " + TT.GetComponent<Pathnode>().MPRemain);

                    if (OpenSet.Contains(TT) == false)
                    {
                        OpenSet.Add(TT);
                    }
                }
            }
            OpenSet.Remove(T);
            ClosedSet.Add(T);
        }

        Debug.Log("A* returned no value. BIG PROBLEM");
        return(0);
    }