Beispiel #1
0
 public PathRequest(Vector3 _start, Vector3 _end, PathCallback _callback, PathParameters _pathParameters)
 {
     pathStart      = _start;
     pathEnd        = _end;
     callback       = _callback;
     pathParameters = _pathParameters;
 }
Beispiel #2
0
    public void NewTarget(Transform pTarget, PathCallback pCallback, int pGraphMask = -1, bool pContinous = false)
    {
        if (currentCallback != null)
            currentCallback(false);
        currentCallback = pCallback;

        continousRepathing = pContinous;
        currTarget = pTarget;

        if (pGraphMask == -1)
            pGraphMask = (1 << 0);

        currGraphMask = pGraphMask;

        //Start a new path to the target, return the result to the OnPathComplete function
        if (seeker == null)
            GetComponent<Seeker>();

        if (seeker != null && transform != null && pTarget != null)
        {
            seeker.StartPath(transform.position, pTarget.position, OnPathComplete, pGraphMask);
            isCalculatingPath = true;
        }
        else
        {
            Debug.LogWarning("Could not execute path, because transform = " + (transform == null).ToString() + " and target = " + (pTarget == null).ToString());
            currentCallback(false);
            currentCallback = null;
        }
    }
    public void GetPath(Vector3 from, Vector3 to, NavSurface targetSurface, PathCallback callback)
    {
        StopAllCoroutines();

        if (untilNextSearch)
        {
            _final_path = null;
        }

        _surface = targetSurface;

        bool[]  nodes = targetSurface.Nodes;
        Vector2 size  = targetSurface.Size;

        Vector2 start = targetSurface.ClosestNodeToPoint(from);
        Vector2 end   = targetSurface.ClosestNodeToPoint(to);

        _target_node = end;

        if (!targetSurface.NodesInConectedZones(start, end))
        {
            callback(null);
            Debug.Log("Cannot path between unconected zones.");
            return;
        }

        if (g == null)
        {
            g        = new float[(int)(size.x * size.y)];
            h        = new float[(int)(size.x * size.y)];
            f        = new float[(int)(size.x * size.y)];
            cameFrom = new Vector2[(int)(size.x * size.y)];

            openList   = new List <Vector2>();
            closedList = new List <Vector2>();
        }
        else if (g.Length < size.x * size.y)
        {
            g        = new float[(int)(size.x * size.y)];
            h        = new float[(int)(size.x * size.y)];
            f        = new float[(int)(size.x * size.y)];
            cameFrom = new Vector2[(int)(size.x * size.y)];
        }

        openList.Clear();
        closedList.Clear();

        for (int i = 0; i < g.Length; i++)
        {
            g[i]        = float.MaxValue;
            h[i]        = float.MaxValue;
            f[i]        = float.MaxValue;
            cameFrom[i] = Vector2.down;
        }

        StartCoroutine(AStar(start, end, size, nodes, targetSurface, callback));
    }
 public bool MoveTo(List <MapPos> _mapPos, GridPathfinding.UnitMovementCallbackType _callbackType, object _obj, PathCallback callback)
 {
     lastMoveTo   = new LastMoveTo(_mapPos, _callbackType, _obj, callback);
     callbackType = _callbackType;
     obj          = _obj;
     pathCallback = callback;
     curX         = mapPos.x;
     curY         = mapPos.y;
     return(false);
 }
Beispiel #5
0
    public void RequestPath(Vector3 pathStart, Vector3 pathEnd, PathCallback callback, PathParameters parameters)
    {
        PathRequest newRequest = new PathRequest(
            pathStart,
            pathEnd,
            callback,
            parameters == null ? PathParameters.Default : parameters
            );

        pathRequestQueue.Enqueue(newRequest);
        TryProcessNext();
    }
        public bool MoveTo(MapPos _mapPos, GridPathfinding.UnitMovementCallbackType _callbackType = GridPathfinding.UnitMovementCallbackType.Simple, object _obj = null, PathCallback callback = null)
        {
            lastMoveTo   = new LastMoveTo(_mapPos, _callbackType, _obj, callback);
            callbackType = _callbackType;
            obj          = _obj;
            pathCallback = callback;
            curX         = mapPos.x;
            curY         = mapPos.y;

            //return MyPathfinding.FindPath(curX,curY,_mapPos,OnPathComplete);
            return(false);
        }
        internal void ForeachPath(Action <CoordinatesHandle> action)
        {
            PathCallback callback = (index, total, nativeHandle, userData) =>
            {
                if (handle != IntPtr.Zero)
                {
                    action(CoordinatesHandle.CloneFrom(nativeHandle));
                    //Destroy(nativeHandle);
                }
                return(true);
            };

            ForeachPath(handle, callback, IntPtr.Zero).WarnIfFailed("Failed to get path coordinates list from native handle");
        }
Beispiel #8
0
	IEnumerator Start ()
	{
		myPathCallback = OnPathCallback;
		myPathfinder = GetComponent<PathfinderAgent>();
		target = new GameObject ("Enemy-Target").transform;

		while (GameManager.current.Player == null)
			yield return new WaitForEndOfFrame ();

		myPathfinder.speed = NormalSpeed;
		CurrrentState = EnemyState.MovingAround;
		myPathfinder.NewFleeTarget(transform, myPathCallback, Random.Range(10, 100));

        KittyRenderer.material.SetTexture("_MainTex", KittyTextures[Random.Range(0, KittyTextures.Length)]);
	}
Beispiel #9
0
 public void RequestPath(Vector3 pathStart, Vector3 pathEnd, PathCallback callback)
 {
     RequestPath(pathStart, pathEnd, callback, PathParameters.Default);
 }
 internal static extern ErrorCode ForeachPath(IntPtr /* maps_route_segment_h */ segment, PathCallback callback, IntPtr /* void */ userData);
    IEnumerator AStar(Vector2 start, Vector2 end, Vector2 size, bool[] nodes, NavSurface surface, PathCallback callback)
    {
        bool pathFound = false;

        // Initially, only the start node is known.
        openList.Add(start);

        // The cost of going from start to start is zero.
        g[Index(start, size.x)] = 0;

        // For the first node, the final and heuristic cost is the same.
        h[Index(start, size.x)] = Vector2.Distance(start, end);
        f[Index(start, size.x)] = h[Index(start, size.x)];

        // while openSet is not empty
        while (openList.Count > 0)
        {
            // current:= the node in openSet having the lowest fScore[] value
            Vector2 current = GetBestNode(openList, (int)size.x);

            // if we found the path
            if (current == end)
            {
                // return reconstructed path
                callback(ReconstructPath(current, (int)size.x, surface));
                if (showFinalPath)
                {
                    openList.Clear();
                    closedList.Clear();
                }
                pathFound = true;
                break;
            }

            // remove the current node from the open list
            openList.Remove(current);

            // and add it to the closed list
            closedList.Add(current);

            // for each neighbor of current
            for (int i = (int)Mathf.Max(current.x - 1, 0); i <= (int)Mathf.Min(current.x + 1, size.x - 1); i++)
            {
                for (int j = (int)Mathf.Max(current.y - 1, 0); j <= (int)Mathf.Min(current.y + 1, size.y - 1); j++)
                {
                    Vector2 neighbor = new Vector2(i, j);

                    // Allthough current is in the closed list and would be ignored this is cheaper for the CPU
                    if (neighbor == current)
                    {
                        continue;
                    }

                    // if neighbor in closedSet
                    if (!nodes[Index(neighbor, size.x)] || closedList.Contains(neighbor))
                    {
                        continue;
                    }

                    // if neighbor not in openList: Discover a new node. Add it to open list
                    if (!openList.Contains(neighbor))
                    {
                        openList.Add(neighbor);
                        h[Index(neighbor, size.x)] = Vector2.Distance(neighbor, end);
                    }

                    // The distance from start to a neighbor
                    // the "dist_between" function may vary as per the solution requirements.
                    float newGCost = g[Index(current, size.x)] + Vector2.Distance(current, neighbor);

                    // if gCost is less then the current gCost keep the new else continue
                    if (newGCost > g[Index(neighbor, size.x)])
                    {
                        continue;
                    }

                    // // This path is the best until now. Record it!
                    cameFrom[Index(neighbor, size.x)] = current;
                    g[Index(neighbor, size.x)]        = newGCost;

                    // update fCost for neighbor
                    f[Index(neighbor, size.x)] = g[Index(neighbor, size.x)] + h[Index(neighbor, size.x)];
                }
            }

            if (visualizeAlgorithm)               // If we are visualizing the algorithm
            // wait for (1/steps per second) seconds
            {
                yield return(new WaitForSeconds(1f / stepsPerSecond));
            }
        }

        if (!pathFound)
        {
            callback(null);
        }

        yield return(null);
    }
Beispiel #12
0
    public void OnPathComplete(Path _p)
    {
        if (!_p.error)
        {
            ABPath p = (ABPath)_p;

            lastRepath = Time.time;
            canRepath = true;

            if (!IgnoreUnwalkbales && Vector3.Distance(p.endPoint, p.originalEndPoint) > (MarginTest))
            {
                currentCallback(false);
                currentCallback = null;
                Debug.Log(name + " - Path Error: Calculated path was not reachable. Distance: " + Vector3.Distance(p.endPoint, p.originalEndPoint)
                    + "\nEndPoint " + p.endPoint + "\nOriginalEndpoint: " + p.originalEndPoint);

                currStartPoint = p.startPoint;
                currStartPointOriginal = p.originalStartPoint;
                currEndPoint = p.endPoint;
                currEndPointOriginal = p.originalEndPoint;
            }
            else
            {
                currentCallback(true);
                currentCallback = null;

                path = p;
                //Reset the waypoint counter
                currentWaypoint = 0;
                targetReached = false;
            }
        }
        else
        {
            Debug.Log(name + " - Path Error: Calculated path was not reachable");
            currentCallback(false);
            currentCallback = null;
        }
        isCalculatingPath = false;
    }
Beispiel #13
0
    public void NewFleeTarget(Transform _fleeTarget, PathCallback _callback, int _fleeLength)
    {
        if (currentCallback != null)
            currentCallback(false);
        currentCallback = _callback;

        currTarget = null;

        FleePath fleePath = FleePath.Construct(transform.position, _fleeTarget.position, _fleeLength * 1000);

        seeker.StartPath(fleePath, OnPathComplete);
        isCalculatingPath = true;
    }
Beispiel #14
0
//	public CollectableItem CurrCollectable { get { return currCollectable; } }

    #endregion

    #region Main

    void Start()
    {
        myPathCallback = OnPathCallback;
        myPathfinder = GetComponent<PathfinderAgent>();

        target = new GameObject("Player-Target").transform;

		currState = PlayerState.Walking;

        lastTimeTossed = Time.time;

        SelectionRing = GameObject.Instantiate(SelectionRing);
        SelectionRing.transform.localScale = Vector3.zero;

        Camera.main.GetComponent<CamMovement>().SetTarget(transform);

        FootPrintAS.clip = SlowWalkClip;

        currMana = MaxMana;
        currHealth = MaxHealth;
    }