Ejemplo n.º 1
0
    public void SetHGoal(Vector3 newHGoal, bool group)
    {
        if (suspended)
        {
            return;
        }

        if (abilityGoal != null)         // Can't move while an ability is rotating us
        {
            return;
        }

        float selCircleRadius = parentUnit.GetSelCircleSize() * 1.85f;         // Approximation of visual area inside selection circle graphic

        if (Vector3.SqrMagnitude(new Vector3(newHGoal.x - transform.position.x, newHGoal.z - transform.position.z)) > selCircleRadius * selCircleRadius)
        {
            PathRequestHandler.RequestPath(new PathRequest(transform.position, newHGoal, OnPathFound));

            manualRotationGoal = null; // Clear any current rotation goal
        }
        else                           // Not grouped or clicked outside of selection circle
        {
            if (!reachedHGoal)         // Only able to rotate while stationary
            {
                Stop();
            }
            if (!group)
            {
                manualRotationGoal = new AbilityTarget(newHGoal);
            }
        }
        // else to do if rotation order but grouped
    }
Ejemplo n.º 2
0
    private IEnumerator FindPath(Vector2 startPos, Vector2 targetPos)
    {
        var startNode  = grid.NodeFromWorldPoint(startPos);
        var targetNode = grid.NodeFromWorldPoint(targetPos);

        var waypoints = new Vector2[0];
        var success   = false;

        if (startNode.walkable && targetNode.walkable)
        {
            var openSet   = new Heap <Node>(grid.MaxSize);
            var closedSet = new HashSet <Node>();

            openSet.Add(startNode);

            while (openSet.Count > 0)
            {
                var currentNode = openSet.RemoveFirst();
                closedSet.Add(currentNode);

                //if target node found, exit from loop
                if (currentNode == targetNode)
                {
                    success = true;
                    RetracePath(startNode, targetNode);
                    break;
                }

                foreach (Node neighbour in grid.GetNeighbours(currentNode))
                {
                    if (!neighbour.walkable || closedSet.Contains(neighbour))
                    {
                        continue;
                    }

                    int newCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);
                    if (newCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                    {
                        neighbour.gCost  = newCostToNeighbour;
                        neighbour.hCost  = GetDistance(neighbour, targetNode);
                        neighbour.parent = currentNode;

                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour);
                        }
                    }
                }
            }
        }

        yield return(null);

        if (success)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        PathRequestHandler.FinishedProcessingPath(waypoints, success);
    }
Ejemplo n.º 3
0
    void Awake()
    {
        gameRules = GameObject.FindGameObjectWithTag("GameManager").GetComponent <Manager_Game>().GameRules;
        CreateGrid();

        solver         = new PathSolver();
        requestHandler = new PathRequestHandler();
        solver.Init(this);
        requestHandler.Init(solver);
    }
Ejemplo n.º 4
0
 public void Init(PathSolver pathfinding)
 {
     solver   = pathfinding;
     instance = this;
 }
Ejemplo n.º 5
0
Archivo: Unit.cs Proyecto: cemuka/AStar
 void Start()
 {
     PathRequestHandler.RequestPath(transform.position, target.position, OnPathFound);
 }
Ejemplo n.º 6
0
 private void Awake()
 {
     grid = GetComponent <Grid> ();
     PathRequestHandler.Initialize(this);
 }