Esempio n. 1
0
    public void Replace(SearchAbleArray <T> givenDictionary)
    {
        if (m_dictionary.ContainsKey(givenDictionary.GetLastItem()))
        {
            T[] givenArray = givenDictionary.GetOrderedArray();
            Dictionary <T, int> tempDict = new Dictionary <T, int>();
            T[] myarray = GetOrderedArray();

            bool add   = true;
            int  count = 0;
            for (int i = 0; i < myarray.Length; i++)
            {
                if (EqualityComparer <T> .Default.Equals(myarray[i], givenArray[0]))
                {
                    add = false;
                    for (int j = 0; j < givenArray.Length; j++)
                    {
                        tempDict.Add(givenArray[j], count++);
                    }
                }
                if (add)
                {
                    tempDict.Add(myarray[i], count++); lastItem = myarray[i];
                }
                if (EqualityComparer <T> .Default.Equals(myarray[i], givenDictionary.GetLastItem()))
                {
                    add = true;
                }
            }
            m_dictionary = tempDict;
        }
        else
        {
            Debug.Log(givenDictionary.GetLastItem() + "was not found when replacing");
            T[] givenArray = givenDictionary.GetOrderedArray();
            Dictionary <T, int> tempDict = new Dictionary <T, int>();
            for (int i = 0; i < givenArray.Length; i++)
            {
                if (i == givenArray.Length - 1)
                {
                    lastItem = givenArray[i];
                }
                tempDict.Add(givenArray[i], i);
            }
            m_dictionary = tempDict;
            Debug.Log(ToString());
        }
    }
Esempio n. 2
0
    public Tile[] GetBestPath(Tile startTile, Tile endTile, PathType givenPathType)
    {
        if (givenPathType == PathType.IgnoreAll)
        {
            SearchAbleArray <Tile> bestPath = new SearchAbleArray <Tile>();
            int[] direction = GetDirection(startTile, endTile);
            bestPath.Add(startTile);
            Tile lastItem = startTile;
            if (Mathf.Abs(direction[0]) > Mathf.Abs(direction[1]))
            {
                for (int i = 0; i < Mathf.Abs(direction[0]); i++)
                {
                    Position nextTilepos = bestPath.GetLastItem().GetPosition();
                    lastItem = GetTile(nextTilepos);
                    //formatting from float to int hack
                    if ((i < (Mathf.Abs((float)direction[1] / 2)) || i >= (Mathf.Abs((float)direction[0]) - (Mathf.Abs((float)direction[1]) / 2))))
                    {
                        //go diagonal in direction[0]
                        nextTilepos.x += direction[0] / (int)Mathf.Abs(direction[0]);
                        nextTilepos.y += direction[1] / (int)Mathf.Abs(direction[1]);
                        if (direction[1] % 2 == 1 && Mathf.Abs(direction[0]) - i < 1)
                        {
                            direction[1] -= direction[1] / (int)Mathf.Abs(direction[1]);
                        }
                    }
                    else
                    {
                        //go in direction[0]
                        nextTilepos.x += direction[0] / (int)Mathf.Abs(direction[0]);
                    }
                    bestPath.Add(GetTile(nextTilepos));
                }
                return(bestPath.GetOrderedArray());
            }
            else //if (difference < 0)
            {
                Position nextTilepos = bestPath.GetLastItem().GetPosition();
                for (int i = 0; i < Mathf.Abs(direction[1]); i++)
                {
                    if ((i < (Mathf.Abs((float)direction[0] / 2)) || i >= (Mathf.Abs((float)direction[1]) - (Mathf.Abs((float)direction[0]) / 2))))
                    {
                        //go diagonal in direction[1]
                        nextTilepos.x += direction[0] / (int)Mathf.Abs(direction[0]);
                        nextTilepos.y += direction[1] / (int)Mathf.Abs(direction[1]);
                        if (direction[0] % 2 == 1 && Mathf.Abs(direction[0]) - i < 1)
                        {
                            direction[0] -= direction[0] / (int)Mathf.Abs(direction[0]);
                        }
                    }
                    else
                    {
                        //go in direction[1]
                        nextTilepos.y += direction[1] / (int)Mathf.Abs(direction[1]);
                    }
                    bestPath.Add(GetTile(nextTilepos));
                }
            }
            return(bestPath.GetOrderedArray());
        }
        else
        {
            KeyValuePair <float, Tile[]> output = GetDijkstraPath(startTile, endTile, givenPathType);

            return(output.Value);
        }
    }