Exemplo n.º 1
0
    private bool GetNextMove(DJ_Point from, DJ_Point to, float currDist, float maxDist, DJ_Distance distanceType, float currentCost)
    {
        if (from.Equals(to))
        {
            return(true);
        }

        if (!tileMap.ContainsKey(to) && DJ_Util.GetDistance(from, to, DJ_Distance.EUCLIDEAN) <= 1)
        {
            return(false);
        }

        if (currDist > maxDist)
        {
            return(true);
        }

        if (tileMap.ContainsKey(from))
        {
            DJ_Dir prefDir = DJ_Util.GetPreferredDirection(from, to);

            int index;

            //immediately return none if the preferred direction
            //happens to be none. This either means the positions, from
            //and to, are the same or that there is no path from "from" to "to"
            if (prefDir == DJ_Dir.NONE)
            {
                return(true);
            }

            if (tileMap[from].neighbors[(int)prefDir] != null && DJ_LevelManager.isTileAvailable(tileMap[from].neighbors[(int)prefDir].pos))
            {
                if (!tilesVisited.Contains(tileMap[from].neighbors[(int)prefDir].pos))
                {
                    tilesVisited.Add(tileMap[from].neighbors[(int)prefDir].pos);
                    currentCosts.Add(currentCost);
                }
                else
                {
                    index = tilesVisited.IndexOf(tileMap[from].neighbors[(int)prefDir].pos);

                    float currCost = currentCosts[index];

                    if (currentCost < currCost)
                    {
                        tilesVisited.RemoveAt(index);
                        currentCosts.RemoveAt(index);

                        tilesVisited.Add(tileMap[from].neighbors[(int)prefDir].pos);
                        currentCosts.Add(currentCost);
                    }
                    else
                    {
                        return(false);
                    }
                }

                if (GetNextMove(tileMap[from].neighbors[(int)prefDir].pos, to, currDist + 1.0f, maxDist, distanceType, currentCost + 1.0f))
                {
                    return(true);
                }

                index = tilesVisited.IndexOf(tileMap[from].neighbors[(int)prefDir].pos);

                currentCosts.RemoveAt(index);
                tilesVisited.Remove(tileMap[from].neighbors[(int)prefDir].pos);
            }

            for (int i = 0; i < tileMap[from].neighbors.Length; ++i)
            {
                if (tileMap[from].neighbors[i] != null && i != (int)prefDir && DJ_LevelManager.isTileAvailable(tileMap[from].neighbors[i].pos))
                {
                    tilesVisited.Add(tileMap[from].neighbors[i].pos);
                    currentCosts.Add(currentCost);

                    if (GetNextMove(tileMap[from].neighbors[i].pos, to, currDist + 1.0f, maxDist, distanceType, currentCost + 1.0f))
                    {
                        return(true);
                    }

                    index = tilesVisited.IndexOf(tileMap[from].neighbors[i].pos);
                    currentCosts.RemoveAt(index);
                    tilesVisited.Remove(tileMap[from].neighbors[i].pos);
                }
            }
        }

        return(false);
    }
Exemplo n.º 2
0
    public DJ_Dir GetNextMove(DJ_Point from, DJ_Point to, float maxDist, DJ_Distance distanceType)
    {
        tilesVisited.Clear();
        currentCosts.Clear();

        tilesVisited.Add(from);

        currentCosts.Add(0.0f);

        if (tileMap.ContainsKey(from))
        {
            DJ_Dir prefDir = DJ_Util.GetPreferredDirection(from, to);

            float currentCost = 0.0f;

            //immediately return none if the preferred direction
            //happens to be none. This either means the positions, from
            //and to, are the same or that there is no path from "from" to "to"
            if (prefDir == DJ_Dir.NONE)
            {
                return(prefDir);
            }

            //Debug.Log("the preferred direction is " + prefDir.ToString());

            int index;

            if (tileMap[from].neighbors[(int)prefDir] != null && DJ_LevelManager.isTileAvailable(tileMap[from].neighbors[(int)prefDir].pos))
            {
                //push info  onto queue
                tilesVisited.Add(tileMap[from].neighbors[(int)prefDir].pos);
                currentCosts.Add(currentCost);

                if (GetNextMove(tileMap[from].neighbors[(int)prefDir].pos, to, 0.0f, maxDist, distanceType, currentCost + 1.0f))
                {
                    return(prefDir);
                }

                //remove info from queue
                index = tilesVisited.IndexOf(tileMap[from].neighbors[(int)prefDir].pos);
                currentCosts.RemoveAt(index);
                tilesVisited.Remove(tileMap[from].neighbors[(int)prefDir].pos);
            }

            for (int i = 0; i < tileMap[from].neighbors.Length; ++i)
            {
                if (tileMap[from].neighbors[i] != null && i != (int)prefDir && DJ_LevelManager.isTileAvailable(tileMap[from].neighbors[i].pos))
                {
                    tilesVisited.Add(tileMap[from].neighbors[i].pos);
                    currentCosts.Add(currentCost);

                    if (GetNextMove(tileMap[from].neighbors[i].pos, to, 0.0f, maxDist, distanceType, currentCost + 1.0f))
                    {
                        return(DJ_Util.GetPreferredDirection(from, tileMap[from].neighbors[i].pos));
                    }

                    index = tilesVisited.IndexOf(tileMap[from].neighbors[i].pos);
                    currentCosts.RemoveAt(index);
                    tilesVisited.Remove(tileMap[from].neighbors[i].pos);
                }
            }
        }

        return(DJ_Dir.NONE);
    }