示例#1
0
文件: Seeker.cs 项目: lidan921/xyolgm
    /** Starts a Multi Target Path from multiple start points to a single target point.
     * A Multi Target Path will search from all start points to the target point in one search and will return all paths if \a pathsForAll is true, or only the shortest one if \a pathsForAll is false.\n
     *
     * \param startPoints	The start points of the path
     * \param end			The end point of the path
     * \param pathsForAll	Indicates whether or not a path from all start points should be searched for or only to the closest one
     * \param callback		The function to call when the path has been calculated
     * \param graphMask		Mask used to specify which graphs should be searched for close nodes. See Pathfinding.NNConstraint.graphMask.
     *
     * \a callback and #pathCallback will be called when the path has completed. \a Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)
     * \astarpro
     * \see Pathfinding.MultiTargetPath
     * \see \ref MultiTargetPathExample.cs "Example of how to use multi-target-paths"
     */
    public MultiTargetPath StartMultiTargetPath(Vector3[] startPoints, Vector3 end, bool pathsForAll, OnPathDelegate callback = null, int graphMask = -1)
    {
        MultiTargetPath p = MultiTargetPath.Construct(startPoints, end, null, null);

        p.pathsForAll = pathsForAll;
        return(StartMultiTargetPath(p, callback, graphMask));
    }
示例#2
0
        IEnumerator DemoMultiTargetPath()
        {
            MultiTargetPath mp = MultiTargetPath.Construct(multipoints.ToArray(), end.position, null, null);

            lastPath = mp;
            AstarPath.StartPath(mp);
            yield return(StartCoroutine(mp.WaitForPath()));

            List <GameObject> unused = new List <GameObject>(lastRender);

            lastRender.Clear();

            for (int i = 0; i < mp.vectorPaths.Length; i++)
            {
                if (mp.vectorPaths[i] == null)
                {
                    continue;
                }

                List <Vector3> vpath = mp.vectorPaths[i];

                GameObject ob = null;
                if (unused.Count > i && unused[i].GetComponent <LineRenderer>() != null)
                {
                    ob = unused[i];
                    unused.RemoveAt(i);
                }
                else
                {
                    ob = new GameObject("LineRenderer_" + i, typeof(LineRenderer));
                }

                LineRenderer lr = ob.GetComponent <LineRenderer>();
                lr.sharedMaterial = lineMat;
#if UNITY_5_5_OR_NEWER
                lr.startWidth = lineWidth;
                lr.endWidth   = lineWidth;
#if UNITY_2017_1_OR_NEWER
                lr.positionCount = vpath.Count;
#else
                lr.numPositions = vpath.Count;
#endif
#else
                lr.SetWidth(lineWidth, lineWidth);
                lr.SetVertexCount(vpath.Count);
#endif

                for (int j = 0; j < vpath.Count; j++)
                {
                    lr.SetPosition(j, vpath[j] + pathOffset);
                }

                lastRender.Add(ob);
            }

            for (int i = 0; i < unused.Count; i++)
            {
                Destroy(unused[i]);
            }
        }
示例#3
0
    /** Starts a Multi Target Path from one start point to multiple end points. A Multi Target Path will search for all the end points in one search and will return all paths if \a pathsForAll is true, or only the shortest one if \a pathsForAll is false.\n
     * \param start			The start point of the path
     * \param endPoints		The end points of the path
     * \param pathsForAll	Indicates whether or not a path to all end points should be searched for or only to the closest one
     * \param callback		The function to call when the path has been calculated
     * \param graphMask		Mask used to specify which graphs should be searched for close nodes. See Pathfinding::NNConstraint::graphMask. \astarproParam
     *
     * \a callback and #pathCallback will be called when the path has completed. \a Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)
     * \astarpro
     * \see Pathfinding::MultiTargetPath
     * \see \ref MultiTargetPath.cs "Example of how to use multi-target-paths"
     */
    public MultiTargetPath StartMultiTargetPath(Vector3 start, Vector3[] endPoints, bool pathsForAll, OnPathDelegate callback = null, int graphMask = -1)
    {
        MultiTargetPath p = new MultiTargetPath(start, endPoints, null, null);

        p.pathsForAll = pathsForAll;
        return(StartMultiTargetPath(p, callback, graphMask));
    }
示例#4
0
    public MultiTargetPath StartMultiTargetPath(Vector3 start, Vector3[] endPoints, bool pathsForAll, OnPathDelegate callback = null, int graphMask = -1)
    {
        MultiTargetPath multiTargetPath = MultiTargetPath.Construct(start, endPoints, null, null);

        multiTargetPath.pathsForAll = pathsForAll;
        return(this.StartMultiTargetPath(multiTargetPath, callback, graphMask));
    }
    /** Starts a path specified by PathTypesDemo.activeDemo */
    public void DemoPath()
    {
        Path p = null;

        if (activeDemo == 0)
        {
            p = ABPath.Construct(start.position, end.position, OnPathComplete);
        }
        else if (activeDemo == 1)
        {
            MultiTargetPath mp = MultiTargetPath.Construct(multipoints.ToArray(), end.position, null, OnPathComplete);
            p = mp;
        }
        else if (activeDemo == 2)
        {
            RandomPath rp = RandomPath.Construct(start.position, searchLength, OnPathComplete);
            rp.spread        = spread;
            rp.aimStrength   = aimStrength;
            rp.aim           = end.position;
            rp.replaceChance = replaceChance;

            p = rp;
        }
        else if (activeDemo == 3)
        {
            FleePath fp = FleePath.Construct(start.position, end.position, searchLength, OnPathComplete);
            fp.aimStrength   = aimStrength;
            fp.replaceChance = replaceChance;
            fp.spread        = spread;

            p = fp;
        }
        else if (activeDemo == 4)
        {
            ConstantPath constPath = ConstantPath.Construct(end.position, searchLength, OnPathComplete);

            p = constPath;
        }
        else if (activeDemo == 5)
        {
            FloodPath fp = FloodPath.Construct(end.position, null);
            lastFlood = fp;
            p         = fp;
        }
        else if (activeDemo == 6 && lastFlood != null)
        {
            FloodPathTracer fp = FloodPathTracer.Construct(end.position, lastFlood, OnPathComplete);


            p = fp;
        }

        if (p != null)
        {
            AstarPath.StartPath(p);
            lastPath = p;
        }
    }
示例#6
0
 public MultiTargetPath StartMultiTargetPath(MultiTargetPath p, OnPathDelegate callback = null, int graphMask = -1)
 {
     OnPathDelegate[] array = new OnPathDelegate[p.targetPoints.Length];
     for (int i = 0; i < array.Length; i++)
     {
         array[i] = this.onPartialPathDelegate;
     }
     p.callbacks = array;
     p.callback  = (OnPathDelegate)Delegate.Combine(p.callback, new OnPathDelegate(this.OnMultiPathComplete));
     p.nnConstraint.graphMask = graphMask;
     this.StartPathInternal(p, callback);
     return(p);
 }
示例#7
0
        // Token: 0x060029ED RID: 10733 RVA: 0x001C233C File Offset: 0x001C053C
        private IEnumerator DemoMultiTargetPath()
        {
            MultiTargetPath mp = MultiTargetPath.Construct(this.multipoints.ToArray(), this.end.position, null, null);

            this.lastPath = mp;
            AstarPath.StartPath(mp, false);
            yield return(base.StartCoroutine(mp.WaitForPath()));

            List <GameObject> list = new List <GameObject>(this.lastRender);

            this.lastRender.Clear();
            for (int i = 0; i < mp.vectorPaths.Length; i++)
            {
                if (mp.vectorPaths[i] != null)
                {
                    List <Vector3> list2 = mp.vectorPaths[i];
                    GameObject     gameObject;
                    if (list.Count > i && list[i].GetComponent <LineRenderer>() != null)
                    {
                        gameObject = list[i];
                        list.RemoveAt(i);
                    }
                    else
                    {
                        gameObject = new GameObject("LineRenderer_" + i, new Type[]
                        {
                            typeof(LineRenderer)
                        });
                    }
                    LineRenderer component = gameObject.GetComponent <LineRenderer>();
                    component.sharedMaterial = this.lineMat;
                    component.startWidth     = this.lineWidth;
                    component.endWidth       = this.lineWidth;
                    component.positionCount  = list2.Count;
                    for (int j = 0; j < list2.Count; j++)
                    {
                        component.SetPosition(j, list2[j] + this.pathOffset);
                    }
                    this.lastRender.Add(gameObject);
                }
            }
            for (int k = 0; k < list.Count; k++)
            {
                UnityEngine.Object.Destroy(list[k]);
            }
            yield break;
        }
示例#8
0
    /** Starts a path specified by PathTypesDemo::activeDemo */
    public void DemoPath()
    {
        Path p = null;

        if (activeDemo == 0)
        {
            p = new Path(start.position, end.position, OnPathComplete);
        }
        else if (activeDemo == 1)
        {
            MultiTargetPath mp = new MultiTargetPath(multipoints.ToArray(), end.position, null, OnPathComplete);
            p = mp;
        }
        else if (activeDemo == 2)
        {
            RandomPath rp = new RandomPath(start.position, searchLength, OnPathComplete);
            rp.spread        = spread;
            rp.aimStrength   = aimStrength;
            rp.aim           = end.position;
            rp.replaceChance = replaceChance;

            p = rp;
        }
        else if (activeDemo == 3)
        {
            FleePath fp = new FleePath(start.position, end.position, searchLength, OnPathComplete);
            fp.fleeStrength  = aimStrength;
            fp.replaceChance = replaceChance;
            fp.spread        = spread;

            p = fp;
        }
        else if (activeDemo == 4)
        {
            ConstantPath constPath = new ConstantPath(start.position, searchLength, OnPathComplete);

            p = constPath;
        }

        if (p != null)
        {
            AstarPath.StartPath(p);
        }
    }
示例#9
0
    /** Starts a Multi Target Path. Takes a MultiTargetPath and wires everything up for it to send callbacks to the seeker for post-processing.\n
     * \param p				The path to start calculating
     * \param callback		The function to call when the path has been calculated
     * \param graphMask	Mask used to specify which graphs should be searched for close nodes. See Pathfinding::NNConstraint::graphMask. \astarproParam
     *
     * \a callback and #pathCallback will be called when the path has completed. \a Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)
     * \astarpro
     * \see Pathfinding::MultiTargetPath
     * \see \ref MultiTargetPath.cs "Example of how to use multi-target-paths"
     */
    public MultiTargetPath StartMultiTargetPath(MultiTargetPath p, OnPathDelegate callback = null, int graphMask = -1)
    {
        //Cancel a previously requested path is it has not been processed yet and also make sure that it has not been recycled and used somewhere else
        if (path != null && !path.processed && lastPathID == path.pathID)
        {
            path.errorLog += "Canceled path because a new one was requested";
            path.error     = true;
            //No callback should be sent for the canceled path
        }

        OnPathDelegate[] callbacks = new OnPathDelegate[p.targetPoints.Length];
        for (int i = 0; i < callbacks.Length; i++)
        {
            callbacks[i] = OnPartialPathComplete;
        }

        p.callbacks = callbacks;
        p.callback += OnMultiPathComplete;
        p.nnConstraint.graphMask = graphMask;

        path = p;

        tmpPathCallback = callback;

        //Save the path id so we can make sure that if we cancel a path (see above) it should not have been recycled yet.
        lastPathID = path.pathID;

        //Delay the path call by one frame if it was sent the same frame as the previous call

        /*if (lastPathCall == Time.frameCount) {
         *      StartCoroutine (DelayPathStart (path));
         *      return p;
         * }
         *
         * lastPathCall = Time.frameCount;*/

        //Pre process the path
        RunModifiers(ModifierPass.PreProcess, path);

        //Send the request to the pathfinder
        AstarPath.StartPath(path);

        return(p);
    }
示例#10
0
    public void OnMultiPathComplete(Path p)
    {
        //Debug.Log("Multipath searching Completed");

        if (p.error)
        {
            Debug.LogWarning("the multipath returned error:" + p.errorLog);
            return;
        }

        MultiTargetPath path = p as MultiTargetPath;

        if (path == null)
        {
            Debug.LogWarning("Can not find multi Target Path");
            return;
        }
        drawPaths = path.vectorPaths;

        //
    }
示例#11
0
文件: Seeker.cs 项目: lidan921/xyolgm
    /** Starts a Multi Target Path.
     * Takes a MultiTargetPath and wires everything up for it to send callbacks to the seeker for post-processing.\n
     *
     * \param p				The path to start calculating
     * \param callback		The function to call when the path has been calculated
     * \param graphMask	Mask used to specify which graphs should be searched for close nodes. See Pathfinding.NNConstraint.graphMask.
     *
     * \a callback and #pathCallback will be called when the path has completed. \a Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)
     * \astarpro
     * \see Pathfinding.MultiTargetPath
     * \see \ref MultiTargetPathExample.cs "Example of how to use multi-target-paths"
     */
    public MultiTargetPath StartMultiTargetPath(MultiTargetPath p, OnPathDelegate callback = null, int graphMask = -1)
    {
        // TODO: Allocation, cache
        var callbacks = new OnPathDelegate[p.targetPoints.Length];

        for (int i = 0; i < callbacks.Length; i++)
        {
            callbacks[i] = onPartialPathDelegate;
        }

        // TODO: This method does not set the enabledTags or tagPenalties
        // as the StartPath method does... should this be changed?
        // Hard to do since the API has existed for a long time...

        p.callbacks = callbacks;
        p.callback += OnMultiPathComplete;
        p.nnConstraint.graphMask = graphMask;

        StartPathInternal(p, callback);
        return(p);
    }
示例#12
0
 public MultiTargetPath StartMultiTargetPath(MultiTargetPath p, OnPathDelegate callback = null, int graphMask = -1)
 {
     if (this.path != null && this.path.GetState() <= PathState.Processing && this.lastPathID == (uint)this.path.pathID)
     {
         this.path.ForceLogError("Canceled path because a new one was requested");
     }
     OnPathDelegate[] array = new OnPathDelegate[p.targetPoints.Length];
     for (int i = 0; i < array.Length; i++)
     {
         array[i] = this.onPartialPathDelegate;
     }
     p.callbacks = array;
     p.callback  = (OnPathDelegate)Delegate.Combine(p.callback, new OnPathDelegate(this.OnMultiPathComplete));
     p.nnConstraint.graphMask = graphMask;
     this.path            = p;
     this.tmpPathCallback = callback;
     this.lastPathID      = (uint)this.path.pathID;
     this.RunModifiers(Seeker.ModifierPass.PreProcess, this.path);
     AstarPath.StartPath(this.path, false);
     return(p);
 }
示例#13
0
	public Path StartPath(Path p, OnPathDelegate callback = null, int graphMask = -1)
	{
		MultiTargetPath multiTargetPath = p as MultiTargetPath;
		if (multiTargetPath != null)
		{
			OnPathDelegate[] array = new OnPathDelegate[multiTargetPath.targetPoints.Length];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = this.onPartialPathDelegate;
			}
			multiTargetPath.callbacks = array;
			p.callback = (OnPathDelegate)Delegate.Combine(p.callback, new OnPathDelegate(this.OnMultiPathComplete));
		}
		else
		{
			p.callback = (OnPathDelegate)Delegate.Combine(p.callback, this.onPathDelegate);
		}
		p.enabledTags = this.traversableTags;
		p.tagPenalties = this.tagPenalties;
		p.nnConstraint.graphMask = graphMask;
		this.StartPathInternal(p, callback);
		return p;
	}
示例#14
0
    /** Starts a Multi Target Path.
     * Takes a MultiTargetPath and wires everything up for it to send callbacks to the seeker for post-processing.\n
     *
     * \param p				The path to start calculating
     * \param callback		The function to call when the path has been calculated
     * \param graphMask	Mask used to specify which graphs should be searched for close nodes. See Pathfinding.NNConstraint.graphMask.
     *
     * \a callback and #pathCallback will be called when the path has completed. \a Callback will not be called if the path is canceled (e.g when a new path is requested before the previous one has completed)
     * \astarpro
     * \see Pathfinding.MultiTargetPath
     * \see \ref MultiTargetPathExample.cs "Example of how to use multi-target-paths"
     */
    public MultiTargetPath StartMultiTargetPath(MultiTargetPath p, OnPathDelegate callback = null, int graphMask = -1)
    {
        // Cancel a previously requested path is it has not been processed yet and also make sure that it has not been recycled and used somewhere else
        if (path != null && path.GetState() <= PathState.Processing && lastPathID == path.pathID)
        {
            path.ForceLogError("Canceled path because a new one was requested");
            // No callback should be sent for the canceled path
        }

        var callbacks = new OnPathDelegate[p.targetPoints.Length];

        for (int i = 0; i < callbacks.Length; i++)
        {
            callbacks[i] = onPartialPathDelegate;
        }

        p.callbacks = callbacks;
        p.callback += OnMultiPathComplete;

        p.nnConstraint.graphMask = graphMask;


        path = p;

        tmpPathCallback = callback;

        // Save the path id so we can make sure that if we cancel a path (see above) it should not have been recycled yet.
        lastPathID = path.pathID;

        // Pre process the path
        RunModifiers(ModifierPass.PreProcess, path);

        // Send the request to the pathfinder
        AstarPath.StartPath(path);

        return(p);
    }
示例#15
0
    public virtual void OnMultiplyPathComplete(Path _p)
    {
        ABPath p = _p as ABPath;

        if (p == null)
        {
            throw new System.Exception("This function only handles ABPaths, do not use special path types");
        }

        canSearchAgain = true;

        //Claim the new path
        p.Claim(this);

        // Path couldn't be calculated of some reason.
        // More info in p.errorLog (debug string)
        if (p.error)
        {
            p.Release(this);
            return;
        }

        //Release the previous path
        if (path != null)
        {
            path.Release(this);
        }

        //Replace the old path
        path = p;

        MultiTargetPath multiPath = (MultiTargetPath)path;

        lastChoosenTarget = multiPath.chosenTarget;

        lastTargetPos = lastPoses[lastChoosenTarget];
        //Reset some variables
        currentWaypointIndex = 0;

        //The next row can be used to find out if the path could be found or not
        //If it couldn't (error == true), then a message has probably been logged to the console
        //however it can also be got using p.errorLog
        //if (p.error)

        if (closestOnPathCheck)
        {
            Vector3 p1   = Time.time - lastFoundWaypointTime < 0.3f ? lastFoundWaypointPosition : p.originalStartPoint;
            Vector3 p2   = GetFeetPosition();
            Vector3 dir  = p2 - p1;
            float   magn = dir.magnitude;
            dir /= magn;
            int steps = (int)(magn / pickNextWaypointDist);

                        #if ASTARDEBUG
            Debug.DrawLine(p1, p2, Color.red, 1);
                        #endif

            for (int i = 0; i <= steps; i++)
            {
                CalculateVelocity(p1);
                p1 += dir;
            }
        }
//      if (parent != null)
//                      parent.OnPathComplete ();
    }
示例#16
0
    /** Starts a path specified by PathTypesDemo.activeDemo */
    void DemoPath()
    {
        Path p = null;

        if (activeDemo == DemoMode.ABPath)
        {
            p = ABPath.Construct(start.position, end.position, OnPathComplete);

            if (agents != null && agents.Length > 0)
            {
                List <Vector3> pts = Pathfinding.Util.ListPool <Vector3> .Claim(agents.Length);

                Vector3 avg = Vector3.zero;
                for (int i = 0; i < agents.Length; i++)
                {
                    pts.Add(agents[i].transform.position);
                    avg += pts[i];
                }
                avg /= pts.Count;
                for (int i = 0; i < agents.Length; i++)
                {
                    pts[i] -= avg;
                }

                Pathfinding.PathUtilities.GetPointsAroundPoint(end.position, AstarPath.active.graphs[0] as IRaycastableGraph, pts, 0, 0.2f);
                for (int i = 0; i < agents.Length; i++)
                {
                    if (agents[i] == null)
                    {
                        continue;
                    }

                    agents[i].target.position = pts[i];
                    agents[i].UpdatePath();
                }
            }
        }
        else if (activeDemo == DemoMode.MultiTargetPath)
        {
            MultiTargetPath mp = MultiTargetPath.Construct(multipoints.ToArray(), end.position, null, OnPathComplete);
            p = mp;
        }
        else if (activeDemo == DemoMode.RandomPath)
        {
            RandomPath rp = RandomPath.Construct(start.position, searchLength, OnPathComplete);
            rp.spread      = spread;
            rp.aimStrength = aimStrength;
            rp.aim         = end.position;

            p = rp;
        }
        else if (activeDemo == DemoMode.FleePath)
        {
            FleePath fp = FleePath.Construct(start.position, end.position, searchLength, OnPathComplete);
            fp.aimStrength = aimStrength;
            fp.spread      = spread;

            p = fp;
        }
        else if (activeDemo == DemoMode.ConstantPath)
        {
            StartCoroutine(CalculateConstantPath());
            p = null;
        }
        else if (activeDemo == DemoMode.FloodPath)
        {
            FloodPath fp = FloodPath.Construct(end.position, null);
            lastFlood = fp;
            p         = fp;
        }
        else if (activeDemo == DemoMode.FloodPathTracer && lastFlood != null)
        {
            FloodPathTracer fp = FloodPathTracer.Construct(end.position, lastFlood, OnPathComplete);

            p = fp;
        }

        if (p != null)
        {
            AstarPath.StartPath(p);
            lastPath = p;
        }
    }
示例#17
0
    /** Get the path back */
    public void OnPathComplete(Path p)
    {
        //To prevent it from creating new GameObjects when the application is quitting when using multithreading.
        if (lastRender == null)
        {
            return;
        }

        if (p.error)
        {
            ClearPrevious();
            return;
        }


        if (p.GetType() == typeof(MultiTargetPath))
        {
            List <GameObject> unused = new List <GameObject> (lastRender);
            lastRender.Clear();

            MultiTargetPath mp = p as MultiTargetPath;

            for (int i = 0; i < mp.vectorPaths.Length; i++)
            {
                if (mp.vectorPaths[i] == null)
                {
                    continue;
                }

                List <Vector3> vpath = mp.vectorPaths[i];

                GameObject ob = null;
                if (unused.Count > i && unused[i].GetComponent <LineRenderer>() != null)
                {
                    ob = unused[i];
                    unused.RemoveAt(i);
                }
                else
                {
                    ob = new GameObject("LineRenderer_" + i, typeof(LineRenderer));
                }

                LineRenderer lr = ob.GetComponent <LineRenderer>();
                lr.sharedMaterial = lineMat;
                lr.SetWidth(lineWidth, lineWidth);

                lr.SetVertexCount(vpath.Count);
                for (int j = 0; j < vpath.Count; j++)
                {
                    lr.SetPosition(j, vpath[j] + pathOffset);
                }

                lastRender.Add(ob);
            }

            for (int i = 0; i < unused.Count; i++)
            {
                Destroy(unused[i]);
            }
        }
        else if (p.GetType() == typeof(ConstantPath))
        {
            ClearPrevious();
            //The following code will build a mesh with a square for each node visited

            ConstantPath     constPath = p as ConstantPath;
            List <GraphNode> nodes     = constPath.allNodes;

            Mesh mesh = new Mesh();

            List <Vector3> verts = new List <Vector3>();

            bool drawRaysInstead = false;

            // Just some debugging code which selects random points on the nodes

            /*List<Vector3> pts = Pathfinding.PathUtilities.GetPointsOnNodes (nodes, 20, 0);
             * Vector3 avg = Vector3.zero;
             * for (int i=0;i<pts.Count;i++) {
             *      Debug.DrawRay (pts[i], Vector3.up*5, Color.red, 3);
             *      avg += pts[i];
             * }
             *
             * if (pts.Count > 0) avg /= pts.Count;
             *
             * for (int i=0;i<pts.Count;i++) {
             *      pts[i] -= avg;
             * }
             *
             * Pathfinding.PathUtilities.GetPointsAroundPoint (start.position, AstarPath.active.astarData.graphs[0] as IRaycastableGraph, pts, 0, 1);
             *
             * for (int i=0;i<pts.Count;i++) {
             *      Debug.DrawRay (pts[i], Vector3.up*5, Color.blue, 3);
             * }*/

            //This will loop through the nodes from furthest away to nearest, not really necessary... but why not :D
            //Note that the reverse does not, as common sense would suggest, loop through from the closest to the furthest away
            //since is might contain duplicates and only the node duplicate placed at the highest index is guarenteed to be ordered correctly.
            for (int i = nodes.Count - 1; i >= 0; i--)
            {
                Vector3 pos = (Vector3)nodes[i].position + pathOffset;
                if (verts.Count == 65000 && !drawRaysInstead)
                {
                    Debug.LogError("Too many nodes, rendering a mesh would throw 65K vertex error. Using Debug.DrawRay instead for the rest of the nodes");
                    drawRaysInstead = true;
                }

                if (drawRaysInstead)
                {
                    Debug.DrawRay(pos, Vector3.up, Color.blue);
                    continue;
                }

                //Add vertices in a square

                GridGraph gg    = AstarData.GetGraph(nodes[i]) as GridGraph;
                float     scale = 1F;

                if (gg != null)
                {
                    scale = gg.nodeSize;
                }

                verts.Add(pos + new Vector3(-0.5F, 0, -0.5F) * scale);
                verts.Add(pos + new Vector3(0.5F, 0, -0.5F) * scale);
                verts.Add(pos + new Vector3(-0.5F, 0, 0.5F) * scale);
                verts.Add(pos + new Vector3(0.5F, 0, 0.5F) * scale);
            }

            //Build triangles for the squares
            Vector3[] vs   = verts.ToArray();
            int[]     tris = new int[(3 * vs.Length) / 2];
            for (int i = 0, j = 0; i < vs.Length; j += 6, i += 4)
            {
                tris[j + 0] = i;
                tris[j + 1] = i + 1;
                tris[j + 2] = i + 2;

                tris[j + 3] = i + 1;
                tris[j + 4] = i + 3;
                tris[j + 5] = i + 2;
            }

            Vector2[] uv = new Vector2[vs.Length];
            //Set up some basic UV
            for (int i = 0; i < uv.Length; i += 4)
            {
                uv[i]     = new Vector2(0, 0);
                uv[i + 1] = new Vector2(1, 0);
                uv[i + 2] = new Vector2(0, 1);
                uv[i + 3] = new Vector2(1, 1);
            }

            mesh.vertices  = vs;
            mesh.triangles = tris;
            mesh.uv        = uv;
            mesh.RecalculateNormals();

            GameObject go = new GameObject("Mesh", typeof(MeshRenderer), typeof(MeshFilter));
            MeshFilter fi = go.GetComponent <MeshFilter>();
            fi.mesh = mesh;
            MeshRenderer re = go.GetComponent <MeshRenderer>();
            re.material = squareMat;

            lastRender.Add(go);
        }
        else
        {
            ClearPrevious();

            GameObject   ob = new GameObject("LineRenderer", typeof(LineRenderer));
            LineRenderer lr = ob.GetComponent <LineRenderer>();
            lr.sharedMaterial = lineMat;
            lr.SetWidth(lineWidth, lineWidth);

            lr.SetVertexCount(p.vectorPath.Count);
            for (int i = 0; i < p.vectorPath.Count; i++)
            {
                lr.SetPosition(i, p.vectorPath[i] + pathOffset);
            }

            lastRender.Add(ob);
        }
    }
示例#18
0
    public void DemoPath()
    {
        Path path = null;

        if (this.activeDemo == 0)
        {
            path = ABPath.Construct(this.start.position, this.end.position, new OnPathDelegate(this.OnPathComplete));
            if (this.agents != null && this.agents.Length > 0)
            {
                List <Vector3> list = ListPool <Vector3> .Claim(this.agents.Length);

                Vector3 vector = Vector3.zero;
                for (int i = 0; i < this.agents.Length; i++)
                {
                    list.Add(this.agents[i].transform.position);
                    vector += list[i];
                }
                vector /= (float)list.Count;
                for (int j = 0; j < this.agents.Length; j++)
                {
                    List <Vector3> list2;
                    int            index;
                    (list2 = list)[index = j] = list2[index] - vector;
                }
                PathUtilities.GetPointsAroundPoint(this.end.position, AstarPath.active.graphs[0] as IRaycastableGraph, list, 0f, 0.2f);
                for (int k = 0; k < this.agents.Length; k++)
                {
                    if (!(this.agents[k] == null))
                    {
                        this.agents[k].target.position = list[k];
                        this.agents[k].UpdatePath();
                    }
                }
            }
        }
        else if (this.activeDemo == 1)
        {
            MultiTargetPath multiTargetPath = MultiTargetPath.Construct(this.multipoints.ToArray(), this.end.position, null, new OnPathDelegate(this.OnPathComplete));
            path = multiTargetPath;
        }
        else if (this.activeDemo == 2)
        {
            RandomPath randomPath = RandomPath.Construct(this.start.position, this.searchLength, new OnPathDelegate(this.OnPathComplete));
            randomPath.spread      = this.spread;
            randomPath.aimStrength = this.aimStrength;
            randomPath.aim         = this.end.position;
            path = randomPath;
        }
        else if (this.activeDemo == 3)
        {
            FleePath fleePath = FleePath.Construct(this.start.position, this.end.position, this.searchLength, new OnPathDelegate(this.OnPathComplete));
            fleePath.aimStrength = this.aimStrength;
            fleePath.spread      = this.spread;
            path = fleePath;
        }
        else if (this.activeDemo == 4)
        {
            base.StartCoroutine(this.Constant());
            path = null;
        }
        else if (this.activeDemo == 5)
        {
            FloodPath floodPath = FloodPath.Construct(this.end.position, null);
            this.lastFlood = floodPath;
            path           = floodPath;
        }
        else if (this.activeDemo == 6 && this.lastFlood != null)
        {
            FloodPathTracer floodPathTracer = FloodPathTracer.Construct(this.end.position, this.lastFlood, new OnPathDelegate(this.OnPathComplete));
            path = floodPathTracer;
        }
        if (path != null)
        {
            AstarPath.StartPath(path, false);
            this.lastPath = path;
        }
    }
示例#19
0
 public void OnPathComplete(Path p)
 {
     if (this.lastRender == null)
     {
         return;
     }
     if (p.error)
     {
         this.ClearPrevious();
         return;
     }
     if (p.GetType() == typeof(MultiTargetPath))
     {
         List <GameObject> list = new List <GameObject>(this.lastRender);
         this.lastRender.Clear();
         MultiTargetPath multiTargetPath = p as MultiTargetPath;
         for (int i = 0; i < multiTargetPath.vectorPaths.Length; i++)
         {
             if (multiTargetPath.vectorPaths[i] != null)
             {
                 List <Vector3> list2 = multiTargetPath.vectorPaths[i];
                 GameObject     gameObject;
                 if (list.Count > i && list[i].GetComponent <LineRenderer>() != null)
                 {
                     gameObject = list[i];
                     list.RemoveAt(i);
                 }
                 else
                 {
                     gameObject = new GameObject("LineRenderer_" + i, new Type[]
                     {
                         typeof(LineRenderer)
                     });
                 }
                 LineRenderer component = gameObject.GetComponent <LineRenderer>();
                 component.sharedMaterial = this.lineMat;
                 for (int j = 0; j < list2.Count; j++)
                 {
                     component.SetPosition(j, list2[j] + this.pathOffset);
                 }
                 this.lastRender.Add(gameObject);
             }
         }
         for (int k = 0; k < list.Count; k++)
         {
             Object.Destroy(list[k]);
         }
     }
     else if (p.GetType() == typeof(ConstantPath))
     {
         this.ClearPrevious();
         ConstantPath     constantPath = p as ConstantPath;
         List <GraphNode> allNodes     = constantPath.allNodes;
         Mesh             mesh         = new Mesh();
         List <Vector3>   list3        = new List <Vector3>();
         bool             flag         = false;
         for (int l = allNodes.Count - 1; l >= 0; l--)
         {
             Vector3 vector = (Vector3)allNodes[l].position + this.pathOffset;
             if (list3.Count == 65000 && !flag)
             {
                 Debug.LogError("Too many nodes, rendering a mesh would throw 65K vertex error. Using Debug.DrawRay instead for the rest of the nodes");
                 flag = true;
             }
             if (flag)
             {
                 Debug.DrawRay(vector, Vector3.up, Color.blue);
             }
             else
             {
                 GridGraph gridGraph = AstarData.GetGraph(allNodes[l]) as GridGraph;
                 float     num       = 1f;
                 if (gridGraph != null)
                 {
                     num = gridGraph.nodeSize;
                 }
                 list3.Add(vector + new Vector3(-0.5f, 0f, -0.5f) * num);
                 list3.Add(vector + new Vector3(0.5f, 0f, -0.5f) * num);
                 list3.Add(vector + new Vector3(-0.5f, 0f, 0.5f) * num);
                 list3.Add(vector + new Vector3(0.5f, 0f, 0.5f) * num);
             }
         }
         Vector3[] array  = list3.ToArray();
         int[]     array2 = new int[3 * array.Length / 2];
         int       m      = 0;
         int       num2   = 0;
         while (m < array.Length)
         {
             array2[num2]     = m;
             array2[num2 + 1] = m + 1;
             array2[num2 + 2] = m + 2;
             array2[num2 + 3] = m + 1;
             array2[num2 + 4] = m + 3;
             array2[num2 + 5] = m + 2;
             num2            += 6;
             m += 4;
         }
         Vector2[] array3 = new Vector2[array.Length];
         for (int n = 0; n < array3.Length; n += 4)
         {
             array3[n]     = new Vector2(0f, 0f);
             array3[n + 1] = new Vector2(1f, 0f);
             array3[n + 2] = new Vector2(0f, 1f);
             array3[n + 3] = new Vector2(1f, 1f);
         }
         mesh.vertices  = array;
         mesh.triangles = array2;
         mesh.uv        = array3;
         mesh.RecalculateNormals();
         GameObject gameObject2 = new GameObject("Mesh", new Type[]
         {
             typeof(MeshRenderer),
             typeof(MeshFilter)
         });
         MeshFilter component2 = gameObject2.GetComponent <MeshFilter>();
         component2.mesh = mesh;
         MeshRenderer component3 = gameObject2.GetComponent <MeshRenderer>();
         component3.material = this.squareMat;
         this.lastRender.Add(gameObject2);
     }
     else
     {
         this.ClearPrevious();
         GameObject gameObject3 = new GameObject("LineRenderer", new Type[]
         {
             typeof(LineRenderer)
         });
         LineRenderer component4 = gameObject3.GetComponent <LineRenderer>();
         component4.sharedMaterial = this.lineMat;
         for (int num3 = 0; num3 < p.vectorPath.Count; num3++)
         {
             component4.SetPosition(num3, p.vectorPath[num3] + this.pathOffset);
         }
         this.lastRender.Add(gameObject3);
     }
 }
示例#20
0
	public MultiTargetPath StartMultiTargetPath(MultiTargetPath p, OnPathDelegate callback = null, int graphMask = -1)
	{
		this.StartPath(p, callback, graphMask);
		return p;
	}