コード例 #1
0
            public Map.Path GetPath(Connection[] ca, Map m)
            {
                Map.Path       p  = new Map.Path();
                List <Vector3> pl = new List <Vector3>();

                //  Start and end points
                p.A = A;
                p.B = B;

                //  Get first connection
                Connection cs = Connection.Find(waypoints[0], waypoints[1], ca);

                if (cs != null)
                {
                    p.AE = (cs.EdgesFor(waypoints[0]));
                }

                if (waypoints.Count > 2)
                {
                    Waypoint   lw = null;
                    Connection lc = null;
                    foreach (Waypoint ws in waypoints)
                    {
                        if (lw != null && lw != ws)
                        {
                            cs = Connection.Find(lw, ws, ca);
                            if (lc != null && cs != lc && cs != null)
                            {
                                Vector3[] jointPoints = Connection.PointsBetween(lc, cs, m);
                                pl.AddRange(jointPoints);
                            }
                            lc = cs;
                        }
                        lw = ws;
                    }
                }

                //  Get last connection
                cs = Connection.Find(waypoints[waypoints.Count - 1], waypoints[waypoints.Count - 2], ca);
                if (cs != null)
                {
                    p.BE = (cs.EdgesFor(waypoints[waypoints.Count - 1]));
                }

                p.points = (pl.ToArray());

                return(p);
            }
コード例 #2
0
ファイル: Agent.cs プロジェクト: masterprompt/Pathfinding
        private void Pathing_Update()
        {
            if (Map.Current == null || this.waypointSource == null || this.waypointTarget == null || target == null)
            {
                sharedWaypoint = null;
                mapPath        = null;
                points.Clear();
                return;
            }

            if (Time.time - pathingLastUpdateTime < pathingUpdateInterval)
            {
                return;
            }
            pathingLastUpdateTime = Time.time;

            //  Can we see target?
            if (Map.Current.PointsVisible(this.transform.position, target.position))
            {
                //Debug.Log("Visible");
                points.Clear();
                points.Add(target.position);
                return;
            }

            //  Can we just connect via a single waypoint?
            if (this.waypointSource == this.waypointTarget && sharedWaypoint != this.waypointSource)
            {
                sharedWaypoint = this.waypointSource;

                points.Clear();

                //  Are we outside the waypoint? first get to waypoint edge
                if (!this.waypointSource.IsInside(this.transform.position))
                {
                    points.Add(this.waypointSource.EdgePoint(this.transform.position));
                }

                //  Is target inside waypoint?

                if (!this.waypointSource.IsInside(target.position))
                {
                    points.Add(this.waypointSource.EdgePoint(target.position));
                }

                points.Add(target.position);

                return;
            }
            sharedWaypoint = null;

            Map.Path path = Map.Current.Find(this.waypointSource, this.waypointTarget);
            if (this.mapPath == path)
            {
                return;
            }

            this.mapPath = path;
            points.Clear();
            if (this.mapPath == null)
            {
                return;
            }


            //  First add our point into waypoint if we are outside it
            if (!this.waypointSource.IsInside(this.transform.position))
            {
                points.Add(this.waypointSource.EdgePoint(this.transform.position));
            }

            //  Get middle of entrance
            points.Add(Map.Current.ToWorld(mapPath.Entrance(this.waypointSource)));

            //  Add our points
            points.AddRange(Map.Current.ToWorld(this.mapPath.PointsArray(this.waypointSource)));

            //  Get middle of entrance
            points.Add(Map.Current.ToWorld(mapPath.Entrance(this.waypointTarget)));

            //  Add our last point out of waypoint if target is outside of it
            if (!this.waypointTarget.IsInside(target.position))
            {
                points.Add(this.waypointTarget.EdgePoint(target.position));
            }
        }