Esempio n. 1
0
    public void AddObstacle(NavMeshObstacle obstacle)
    {
        Vector2 obstacleCenter = new Vector2(obstacle.gameObject.transform.position.x, obstacle.gameObject.transform.position.y);
        Vector3 obstacleSize   = obstacle.gameObject.GetComponent <Renderer>().bounds.size;

        Rect obstacleRect = new Rect(obstacleCenter, obstacleSize);

        Obstacles.Add(obstacleRect);

        BlockedHex.UnionWith(GridHelper.BoxToHexList(obstacleCenter, obstacleSize, NavMap));
    }
Esempio n. 2
0
    bool IsVisible(Vector2 observer, Vector2 destination)
    {
        List <Hex> hexesInBetween = FractionalHex.HexLinedraw(GridHelper.Vector2ToHex(observer, NavMap.MapLayout), GridHelper.Vector2ToHex(destination, NavMap.MapLayout));

        foreach (Hex hex in hexesInBetween)
        {
            if (BlockedHex.Contains(hex))
            {
                return(false);
            }
        }

        return(true);
    }
Esempio n. 3
0
    public LinkedList <Vector2> GetPath(Vector2 start, Vector2 destination)
#endif
    {
        Hex startHex       = GridHelper.Vector2ToHex(start, NavMap.MapLayout);
        Hex destinationHex = GridHelper.Vector2ToHex(destination, NavMap.MapLayout);

        Dictionary <Hex, Hex> came_from = new Dictionary <Hex, Hex>();

#if UNITY_EDITOR
        testedHex = new List <Hex>();
#endif
        if (!BlockedHex.Contains(destinationHex))
        {
            PriorityQueue <float, Hex> frontier = new PriorityQueue <float, Hex>();
            frontier.Enqueue(0, startHex);
            Dictionary <Hex, int> cost_so_far = new Dictionary <Hex, int>();

            came_from[startHex]   = startHex;
            cost_so_far[startHex] = 0;
            while (!frontier.IsEmpty)
            {
                Hex current = frontier.Dequeue();
#if UNITY_EDITOR
                testedHex.Add(current);
#endif
                if (Hex.Equals(current, destinationHex))
                {
                    break;
                }

                for (int i = 0; i < HEX_NUM_SIDES; i++)
                {
                    Hex next = Hex.Neighbor(current, i);
                    if (BlockedHex.Contains(next))
                    {
                        continue;
                    }
                    int new_cost = cost_so_far[current] + UNBLOCKED_HEX_COST;

                    if ((!cost_so_far.ContainsKey(next)) || (new_cost < cost_so_far[next]))
                    {
                        cost_so_far[next] = new_cost;
                        float priority = new_cost + CalculateHeuristic(destinationHex, next);
                        frontier.Enqueue(priority, next);

                        came_from[next] = current;
                    }
                }
            }
        }

        LinkedList <Vector2> path = null;
        if (came_from.Count > 0)
        {
            path = new LinkedList <Vector2>();
#if UNITY_EDITOR
            hexInPath = new List <Hex>();
#endif
            Hex currentHex = destinationHex;
            while (!came_from[currentHex].Equals(startHex))
            {
#if UNITY_EDITOR
                hexInPath.Add(currentHex);
#endif
                path.AddFirst(GridHelper.HexToVector2(currentHex, NavMap.MapLayout));
                currentHex = came_from[currentHex];
            }

            path.AddFirst(start);
            SmoothPath(path);
        }

        return(path);
    }