public static List <MapHoneycomb> GetHoneycombPath(HoneycombPos start, HoneycombDir dir, int honeyDistance)
    {
        List <MapHoneycomb> path = new List <MapHoneycomb>();

        //start = WorldToHoneycomb(start);
        for (int i = 1; i <= honeyDistance; i += 1)
        {
            HoneycombPos honeyCell = GetHoneycombDirection(start, dir, i);
            //Debug.Log(honeyCell);
            path.Add(Map.StaticMap.GetHoneycomb((int)honeyCell.x, (int)honeyCell.y));
        }
        return(path);
    }
    private Vector2 getRandomLoc()
    {
        int randDist = Random.Range(1, 4) * 3;
        int ranDir   = Random.Range(0, 2);

        if (ranDir == 0)
        {
            Direction = GetNewDirection(Direction, 1);
        }
        else
        {
            Direction = GetNewDirection(Direction, -1);
        }
        //Debug.Log("HoneyDir: " + Direction + " HoneyDist: " + randDist + " HoneyPos: " + Utility.WorldToHoneycomb(Target));
        //Debug.Log("--------------------------- new Path Start----------------------------");
        //List<MapHoneycomb> path = Utility.GetHoneycombPath(Utility.WorldPointToHoneycombGrid(Target), Direction, randDist);
        //Debug.Log("path length: " + path.Count);
        //Debug.Log("--------------------------- new Path Honeycombs----------------------------");
        MapHoneycomb newTarget = Utility.GetHoneycombFreePath(Utility.WorldPointToHoneycombGrid(Target), Direction, randDist);

        //foreach (MapHoneycomb honeycomb in path)
        //{
        //    //Debug.Log(honeycomb.position);
        //    if (!honeycomb.display && honeycomb.LocationType == MapHoneycomb.LocationTypes.Chamber)
        //    {
        //        newTarget = honeycomb;
        //        pathAttempts = 0;
        //    }
        //    else
        //    {
        //        //Debug.Log(honeycomb.LocationType);
        //        break;
        //    }
        //}

        //Debug.Log("--------------------------- new Path End----------------------------");
        //return Utility.HoneycombGridToWorldPostion( Utility.GetHoneycombDirection(Utility.WorldToHoneycomb(Target), Direction, randDist));
        if (newTarget != null)
        {
            return(newTarget.position);
        }
        else
        {
            pathAttempts += 1;
            //if (pathAttempts > 3) return Vector2.zero;
            return(getRandomLoc());
        }
    }
    private HoneycombPos FindShortestPath(HoneycombPos startHex, HoneycombDir hexDir, HoneycombPos targetHex)
    {
        int          distance   = 1;
        HoneycombPos pathHex    = startHex;
        MapHoneycomb newTarget  = Utility.GetHoneycombFreePath(startHex, hexDir, distance);
        MapHoneycomb nextTarget = Utility.GetHoneycombFreePath(startHex, hexDir, distance + 1);

        while (newTarget != null && nextTarget != null && Utility.DistanceBetweenHoneycomb(Utility.WorldPointToHoneycombGrid(newTarget.position), targetHex) > Utility.DistanceBetweenHoneycomb(Utility.WorldPointToHoneycombGrid(nextTarget.position), targetHex))
        {
            newTarget  = nextTarget;
            distance  += 1;
            nextTarget = Utility.GetHoneycombFreePath(startHex, hexDir, distance + 1);
        }
        //Debug.Log("Closets HexPos: " + Utility.WorldPointToHoneycombGrid(newTarget.position));
        return(Utility.WorldPointToHoneycombGrid(newTarget.position));
    }
    private HoneycombPos compareShortestPaths(HoneycombDir dirOne, HoneycombDir dirTwo, HoneycombPos startHoneycomb, HoneycombPos targetHoneycomb)
    {
        //Vector2 targetPos = Utility.HoneycombGridToWorldPostion(targetHoneycomb);
        //Vector2 startPos = Utility.HoneycombGridToWorldPostion(startHoneycomb);
        HoneycombPos closestHex = startHoneycomb;

        HoneycombPos hexOne = FindShortestPath(startHoneycomb, dirOne, targetHoneycomb);
        HoneycombPos hexTwo = FindShortestPath(startHoneycomb, dirTwo, targetHoneycomb);

        if (Utility.DistanceBetweenHoneycomb(closestHex, targetHoneycomb) > Utility.DistanceBetweenHoneycomb(hexOne, targetHoneycomb))
        {
            closestHex = hexOne;
        }
        if (Utility.DistanceBetweenHoneycomb(closestHex, targetHoneycomb) > Utility.DistanceBetweenHoneycomb(hexTwo, targetHoneycomb))
        {
            closestHex = hexTwo;
        }
        return(closestHex);
    }
    public static MapHoneycomb GetHoneycombFreePath(HoneycombPos startHex, HoneycombDir hexDir, int hexDistance)
    {
        List <MapHoneycomb> path      = GetHoneycombPath(startHex, hexDir, hexDistance);
        MapHoneycomb        newTarget = null;

        foreach (MapHoneycomb honeycomb in path)
        {
            //Debug.Log(honeycomb.position);
            if ((!honeycomb.display || honeycomb.isFloor) && honeycomb.LocationType == MapHoneycomb.LocationTypes.Chamber)
            {
                newTarget = honeycomb;
            }
            else
            {
                //Debug.Log(honeycomb.LocationType);
                break;
            }
        }
        return(newTarget);
    }
    public static HoneycombDir WorldDirToHoneycombDir(Vector2 worldDir)
    {
        HoneycombDir honeyDir = new HoneycombDir();

        if (worldDir.x > 0)
        {
            honeyDir.x = 1;
        }
        else if (worldDir.x < 0)
        {
            honeyDir.x = 1;
        }
        if (worldDir.y > 0)
        {
            honeyDir.y = 1;
        }
        else if (worldDir.y < 0)
        {
            honeyDir.y = -1;
        }
        return(honeyDir);
    }
Example #7
0
    public HoneycombPos GetAdjecentHoneycomb(HoneycombDir dir)
    {
        HoneycombPos end = new HoneycombPos(x, y);

        end.x += dir.x;
        if (dir.x == 0)
        {
            end.y += dir.y;
        }
        else if (x % 2 == 0 && dir.y > 0 || x % 2 != 0 && dir.y < 0)
        {
            //end.y += Mathf.Sign(dir.y) * Mathf.Ceil((float)honeyDistance / 2);
            end.y += (int)(Mathf.Sign(dir.y));// * Mathf.Ceil((float)honeyDistance / 2));
        }
        else
        {
            //end.y += Mathf.Sign(dir.y) * Mathf.Ceil(((float)honeyDistance - 1) / 2);
            //end.y += (int)(Mathf.Sign(dir.y) * Mathf.Ceil(((float)honeyDistance - 1) / 2));
        }

        return(end);
    }
    /// <summary>
    /// Returns the coordinates of a target honeycomb starting from the coordinates of a honeycomb in a honeycomb vector (honeycomb direction and distance)
    /// </summary>
    /// <param name="start"></param>
    /// <param name="dir"></param>
    /// <param name="honeyDistance"></param>
    /// <returns></returns>
    public static HoneycombPos GetHoneycombDirection(HoneycombPos start, HoneycombDir dir, int honeyDistance)
    {
        //start = Utility.WorldPointToHoneycombGrid(start);
        HoneycombPos end = start;

        end.x += dir.x * honeyDistance;
        if (dir.x == 0)
        {
            end.y += dir.y * honeyDistance;
        }
        else if (start.x % 2 == 0 && dir.y > 0 || start.x % 2 != 0 && dir.y < 0)
        {
            //end.y += Mathf.Sign(dir.y) * Mathf.Ceil((float)honeyDistance / 2);
            end.y += (int)(Mathf.Sign(dir.y) * Mathf.Ceil((float)honeyDistance / 2));
        }
        else
        {
            //end.y += Mathf.Sign(dir.y) * Mathf.Ceil(((float)honeyDistance - 1) / 2);
            end.y += (int)(Mathf.Sign(dir.y) * Mathf.Ceil(((float)honeyDistance - 1) / 2));
        }

        return(end);
    }
    private HoneycombDir GetNewDirection(HoneycombDir current, int turns) //turns + for counter clockwise
    {
        HoneycombDir newDir = new HoneycombDir(0, 0);
        int          index  = 0;

        for (index = 0; index < Directions.Length; index += 1)
        {
            if (current.x == Directions[index].x && current.y == Directions[index].y)
            {
                break;
            }
        }
        if (turns > 0)
        {
            index = (index + 1) % Directions.Length;
        }
        else
        {
            index -= 1;
            if (index < 0)
            {
                index = Directions.Length - 1;
            }
        }
        newDir = Directions[index];

        turns = turns - (int)Mathf.Sign(turns) * 1;
        if (turns != 0)
        {
            return(GetNewDirection(newDir, turns));
        }
        else
        {
            return(newDir);
        }
    }
    private Vector2 findPathToHoneycomb(HoneycombPos startHoneycomb, HoneycombPos targetHoneycomb)
    {
        //Vector2 targetPos = Utility.HoneycombGridToWorldPostion(targetHoneycomb);
        //Vector2 startPos = Utility.HoneycombGridToWorldPostion(startHoneycomb);
        HoneycombPos closestHex = startHoneycomb;

        if (startHoneycomb.x < targetHoneycomb.x && startHoneycomb.y > targetHoneycomb.y)
        {
            //(1,-1) || (0,-1)
            HoneycombDir dirOne = new HoneycombDir(1, -1);
            HoneycombDir dirTwo = new HoneycombDir(0, -1);
            closestHex = compareShortestPaths(dirOne, dirTwo, startHoneycomb, targetHoneycomb);
        }
        else if (startHoneycomb.x < targetHoneycomb.x && startHoneycomb.y < targetHoneycomb.y)
        {
            //(1,1) || (0,1)
            HoneycombDir dirOne = new HoneycombDir(1, 1);
            HoneycombDir dirTwo = new HoneycombDir(0, 1);
            closestHex = compareShortestPaths(dirOne, dirTwo, startHoneycomb, targetHoneycomb);
        }
        else if (startHoneycomb.x < targetHoneycomb.x && startHoneycomb.y == targetHoneycomb.y)
        {
            //(1,1) || (1,-1)
            HoneycombDir dirOne = new HoneycombDir(1, 1);
            HoneycombDir dirTwo = new HoneycombDir(1, -1);
            closestHex = compareShortestPaths(dirOne, dirTwo, startHoneycomb, targetHoneycomb);
        }
        else if (startHoneycomb.x > targetHoneycomb.x && startHoneycomb.y > targetHoneycomb.y)
        {
            //(-1,-1) || (0,-1)
            HoneycombDir dirOne = new HoneycombDir(-1, -1);
            HoneycombDir dirTwo = new HoneycombDir(0, -1);
            closestHex = compareShortestPaths(dirOne, dirTwo, startHoneycomb, targetHoneycomb);
        }
        else if (startHoneycomb.x > targetHoneycomb.x && startHoneycomb.y < targetHoneycomb.y)
        {
            //(-1,1) || (0,1)
            HoneycombDir dirOne = new HoneycombDir(-1, 1);
            HoneycombDir dirTwo = new HoneycombDir(0, 1);
            closestHex = compareShortestPaths(dirOne, dirTwo, startHoneycomb, targetHoneycomb);
        }
        else if (startHoneycomb.x > targetHoneycomb.x && startHoneycomb.y == targetHoneycomb.y)
        {
            //(-1,1) || (-1,-1)
            HoneycombDir dirOne = new HoneycombDir(-1, 1);
            HoneycombDir dirTwo = new HoneycombDir(-1, -1);
            closestHex = compareShortestPaths(dirOne, dirTwo, startHoneycomb, targetHoneycomb);
        }
        else if (startHoneycomb.x == targetHoneycomb.x && startHoneycomb.y < targetHoneycomb.y)
        {
            // (0,1)
            HoneycombPos hexOne = FindShortestPath(startHoneycomb, new HoneycombDir(0, 1), targetHoneycomb);
            if (Utility.DistanceBetweenHoneycomb(closestHex, targetHoneycomb) > Utility.DistanceBetweenHoneycomb(hexOne, targetHoneycomb))
            {
                closestHex = hexOne;
            }
        }
        else if (startHoneycomb.x == targetHoneycomb.x && startHoneycomb.y > targetHoneycomb.y)
        {
            // (0,-1)
            HoneycombPos hexOne = FindShortestPath(startHoneycomb, new HoneycombDir(0, -1), targetHoneycomb);
            if (Utility.DistanceBetweenHoneycomb(closestHex, targetHoneycomb) > Utility.DistanceBetweenHoneycomb(hexOne, targetHoneycomb))
            {
                closestHex = hexOne;
            }
        }
        else
        {
            //(0,0)
            Debug.Log("Snake Follow Player Error");
        }
        return(Utility.HoneycombGridToWorldPostion(closestHex));
    }