示例#1
0
    public List <Vector3> Path(Vector3 startPos, Vector3 targetPos)
    {
        //Can I see the exit
        float exitDistance = Vector3.Distance(startPos, targetPos);

        if (exitDistance > .7f)
        {
            exitDistance -= .7f;
        }
        if (!Physics.Raycast(startPos, targetPos - startPos, exitDistance))         //found path because physics.raycast returned false (did not hit collider)
        {
            List <Vector3> path = new List <Vector3>();
            path.Add(startPos);
            path.Add(targetPos);
            return(path);
        }

        GameObject[] nodes  = GameObject.FindGameObjectsWithTag(nodeTag);               //find all the nodes and put them into an array
        List <Point> points = new List <Point>();                                       //create new a list of Points

        foreach (GameObject node in nodes)
        {
            Point currNode = new Point(node.transform.position);
            points.Add(currNode);                                                                                               //fill up the list of points (positions of nodes)
        }

        Point endPoint = new Point(targetPos, 'e');

        /***Connect them together***/
        foreach (Point point in points)        //Could be optimized to not go through each connection twice
        {
            foreach (Point point2 in points)
            {
                float distance = Vector3.Distance(point.GetPos(), point2.GetPos());
                if (!Physics.Raycast(point.GetPos(), point2.GetPos() - point.GetPos(), distance))
                {
                    //Debug.DrawRay(point.GetPos(), point2.GetPos() - point.GetPos(), Color.white, 1);
                    point.AddConnectedPoint(point2);
                }
            }
            float distance2 = Vector3.Distance(targetPos, point.GetPos());
            if (!Physics.Raycast(targetPos, point.GetPos() - targetPos, distance2))
            {
                //Debug.DrawRay(targetPos, point.GetPos() - targetPos, Color.white, 1);
                point.AddConnectedPoint(endPoint);
            }
        }

        //points startPos can see
        foreach (Point point in points)
        {
            float distance = Vector3.Distance(startPos, point.GetPos());
            if (!Physics.Raycast(startPos, point.GetPos() - startPos, distance))
            {
                //Debug.DrawRay(startPos, point.GetPos() - startPos, Color.white, 1);
                Point startPoint = new Point(startPos, 's');
                point.SetPrevPoint(startPoint);
                point.SetState('o');
                point.SetScore(distance + Vector3.Distance(targetPos, point.GetPos()));
            }
        }

        //Go through until we find the exit or run out of connections
        bool searchedAll = false;
        bool foundEnd    = false;

        while (!searchedAll)
        {
            searchedAll = true;
            List <Point> foundConnections = new List <Point>();
            foreach (Point point in points)
            {
                if (point.GetState() == 'o')
                {
                    searchedAll = false;
                    List <Point> potentials = point.GetConnectedPoints();

                    foreach (Point potentialPoint in potentials)
                    {
                        if (potentialPoint.GetState() == 'u')
                        {
                            potentialPoint.AddPotentialPrevPoint(point);
                            foundConnections.Add(potentialPoint);
                            potentialPoint.SetScore(Vector3.Distance(startPos, potentialPoint.GetPos()) + Vector3.Distance(targetPos, potentialPoint.GetPos()));
                        }
                        else if (potentialPoint.GetState() == 'e')
                        {
                            //Found the exit
                            foundEnd = true;
                            endPoint.AddConnectedPoint(point);
                        }
                    }
                    point.SetState('c');
                }
            }
            foreach (Point connection in foundConnections)
            {
                connection.SetState('o');
                //Find lowest scoring prev point
                float lowestScore   = 0;
                Point bestPrevPoint = null;
                bool  first         = true;
                foreach (Point prevPoints in connection.GetPotentialPrevPoints())
                {
                    if (first)
                    {
                        lowestScore   = prevPoints.GetScore();
                        bestPrevPoint = prevPoints;
                        first         = false;
                    }
                    else
                    {
                        if (lowestScore > prevPoints.GetScore())
                        {
                            lowestScore   = prevPoints.GetScore();
                            bestPrevPoint = prevPoints;
                        }
                    }
                }
                connection.SetPrevPoint(bestPrevPoint);
            }
        }

        if (foundEnd)
        {
            //trace back finding shortest route (lowest score)
            List <Point> shortestRoute = null;
            float        lowestScore   = 0;
            bool         firstRoute    = true;

            foreach (Point point in endPoint.GetConnectedPoints())
            {
                float        score     = 0;
                bool         tracing   = true;
                Point        currPoint = point;
                List <Point> route     = new List <Point>();
                route.Add(endPoint);
                while (tracing)
                {
                    route.Add(currPoint);
                    if (currPoint.GetState() == 's')
                    {
                        if (firstRoute)
                        {
                            shortestRoute = route;
                            lowestScore   = score;
                            firstRoute    = false;
                        }
                        else
                        {
                            if (lowestScore > score)
                            {
                                shortestRoute = route;
                                lowestScore   = score;
                            }
                        }
                        tracing = false;
                        break;
                    }
                    score    += currPoint.GetScore();
                    currPoint = currPoint.GetPrevPoint();
                }
            }

            shortestRoute.Reverse();
            List <Vector3> path = new List <Vector3>();
            foreach (Point point in shortestRoute)
            {
                path.Add(point.GetPos());
            }
            return(path);
        }
        else
        {
            return(null);
        }
    }
    public List<Vector3> Path(Vector3 startPos, Vector3 targetPos, float zDiff = -1)
    {
        //Can I see the exit
        float exitDistance = Vector3.Distance(startPos, targetPos);
        //RaycastHit hitInfo;
        Physics.Raycast(startPos, targetPos, exitDistance, collisionMask);
        if (!Physics.Raycast(startPos, targetPos - startPos, exitDistance))
        {
            List<Vector3> path = new List<Vector3>();
            path.Add(startPos);
            path.Add(targetPos);
            //if (zDiff == -1 || targetPos.z - startPos.z <= zDiff)

            // else
            //  path.Add(new Vector3(startPos.x, startPos.y, targetPos.z));

            return path;
        }
        GameObject[] nodes = GameObject.FindGameObjectsWithTag(nodeTag);

        List<Point> points = new List<Point>();
        foreach (GameObject node in nodes)
        {
            if (!node.transform.IsChildOf(gameObject.transform))
            {
                Point currNode = new Point(node.transform.position);
                points.Add(currNode);
            }

        }

        Point endPoint = new Point(targetPos, 'e');

        /***Connect them together***/
        foreach (Point point in points) //Could be optimized to not go through each connection twice
        {
            foreach (Point point2 in points)
            {
                float distance = Vector3.Distance(point.GetPos(), point2.GetPos());
                if (!Physics.Raycast(point.GetPos(), point2.GetPos() - point.GetPos(), distance))
                {
                    //Debug.DrawRay(point.GetPos(), point2.GetPos() - point.GetPos(), Color.white, 1);
                    point.AddConnectedPoint(point2);
                }
            }
            float distance2 = Vector3.Distance(targetPos, point.GetPos());
            if (!Physics.Raycast(targetPos, point.GetPos() - targetPos, distance2))
            {
                //Debug.DrawRay(targetPos, point.GetPos() - targetPos, Color.white, 1);
                point.AddConnectedPoint(endPoint);
            }
        }

        //points startPos can see
        foreach (Point point in points)
        {
            float distance = Vector3.Distance(startPos, point.GetPos());
            if (!Physics.Raycast(startPos, point.GetPos() - startPos, distance))
            {
                //Debug.DrawRay(startPos, point.GetPos() - startPos, Color.white, 1);
                Point startPoint = new Point(startPos, 's');
                point.SetPrevPoint(startPoint);
                point.SetState('o');
                point.SetScore(distance + Vector3.Distance(targetPos, point.GetPos()));
            }
        }

        //Go through until we find the exit or run out of connections
        bool searchedAll = false;
        bool foundEnd = false;

        while (!searchedAll)
        {
            searchedAll = true;
            List<Point> foundConnections = new List<Point>();

            foreach (Point point in points)
            {
                if (point.GetState() == 'o')
                {
                    searchedAll = false;
                    List<Point> potentials = point.GetConnectedPoints();
                    foreach (Point potentialPoint in potentials)
                    {

                        if (potentialPoint.GetState() == 'u')
                        {
                            potentialPoint.AddPotentialPrevPoint(point);
                            foundConnections.Add(potentialPoint);
                            potentialPoint.SetScore(Vector3.Distance(startPos, potentialPoint.GetPos()) + Vector3.Distance(targetPos, potentialPoint.GetPos()));
                        }
                        else if (potentialPoint.GetState() == 'e')
                        {
                            //Found the exit
                            foundEnd = true;
                            endPoint.AddConnectedPoint(point);
                        }
                    }
                    point.SetState('c');
                }
            }
            foreach (Point connection in foundConnections)
            {
                connection.SetState('o');
                //Find lowest scoring prev point
                float lowestScore = 0;
                Point bestPrevPoint = null;
                bool first = true;
                foreach (Point prevPoints in connection.GetPotentialPrevPoints())
                {
                    if (first)
                    {
                        lowestScore = prevPoints.GetScore();
                        bestPrevPoint = prevPoints;
                        first = false;
                    }
                    else
                    {
                        if (lowestScore > prevPoints.GetScore())
                        {
                            lowestScore = prevPoints.GetScore();
                            bestPrevPoint = prevPoints;
                        }
                    }
                }
                connection.SetPrevPoint(bestPrevPoint);
            }
        }

        if (foundEnd)
        {
            //trace back finding shortest route (lowest score)
            List<Point> shortestRoute = null;
            float lowestScore = 0;
            bool firstRoute = true;

            foreach (Point point in endPoint.GetConnectedPoints())
            {
                float score = 0;
                bool tracing = true;
                Point currPoint = point;
                List<Point> route = new List<Point>();
                route.Add(endPoint);
                while (tracing)
                {
                    route.Add(currPoint);
                    if (currPoint.GetState() == 's')
                    {
                        if (firstRoute)
                        {
                            shortestRoute = route;
                            lowestScore = score;
                            firstRoute = false;
                        }
                        else
                        {
                            if (lowestScore > score)
                            {
                                shortestRoute = route;
                                lowestScore = score;
                            }
                        }
                        tracing = false;
                        break;
                    }
                    score += currPoint.GetScore();
                    currPoint = currPoint.GetPrevPoint();
                }
            }

            shortestRoute.Reverse();
            List<Vector3> path = new List<Vector3>();
            foreach (Point point in shortestRoute)
            {
                path.Add(point.GetPos());
            }
            return path;
        }
        else
        {
            List<Vector3> path = new List<Vector3>();
            path.Add(startPos);
            path.Add(targetPos);

            return path;
        }
    }
示例#3
0
文件: Node.cs 项目: Reppy-Why/TODO
	public List<Vector3> Path(Vector3 startPos, Vector3 targetPos)
	{
		//Can I see the target
		float targetDistance = Vector3.Distance(startPos, targetPos);
	  //if the target and start are the same then just return path with the two nodes
		if (!Physics.Raycast(startPos, targetPos - startPos, targetDistance))
		{
			List<Vector3> path = new List<Vector3>();
			path.Add(startPos);
			path.Add(targetPos);
			return path;
		}

		//find all the nodes with the specified nodeTag
		GameObject[] nodes = GameObject.FindGameObjectsWithTag(nodeTag);

		//create new class list
		List<Point> path_points = new List<Point>();

		foreach (GameObject n in nodes)
		{
			Point currNode = new Point(n.transform.position);
			path_points.Add(currNode);
		}

		Point endPoint = new Point(targetPos, "end");

		foreach(Point p in path_points)
		{
			foreach (Point p2 in path_points)
			{
				float distance = Vector3.Distance(p.GetPos(), p2.GetPos());
				if (!Physics.Raycast(p.GetPos(), p2.GetPos() - p.GetPos(), distance))
				{
					p.AddConnectedPoint(p2);
				}
			}
			float distance2 = Vector3.Distance(targetPos, p.GetPos());
			if (!Physics.Raycast(targetPos, p.GetPos() - targetPos, distance2))
			{
				p.AddConnectedPoint(endPoint);
			}
		}

		//path_points startPos can see
		foreach (Point p in path_points)
		{
			float distance = Vector3.Distance(startPos, p.GetPos());
			if (!Physics.Raycast(startPos, p.GetPos() - startPos, distance))
			{
				Point startPoint = new Point(startPos, "start");
				p.SetPrevPoint(startPoint);
				p.SetState("nb");
				p.SetScore(distance + Vector3.Distance(targetPos, p.GetPos()));
			}
		}

		//Go through until we find the exit or run out of connections
		bool searchedAll = false;
		bool targetFound = false;

		while(!searchedAll)
		{
			searchedAll = true;
			List<Point> foundConnections = new List<Point>();
			foreach (Point point in path_points)
			{
				if (point.GetState() == "nb")
				{
					searchedAll = false;
					List<Point> potentials = point.GetConnectedpath_points();

					foreach (Point potentialPoint in potentials)
					{
						if (potentialPoint.GetState() == "unset")
						{
							potentialPoint.AddPotentialPrevPoint(point);
							foundConnections.Add(potentialPoint);
							potentialPoint.SetScore(Vector3.Distance(startPos, potentialPoint.GetPos()) + Vector3.Distance(targetPos, potentialPoint.GetPos()));
						} else if (potentialPoint.GetState() == "end")
						{
							//Found the exit
							targetFound = true;
							endPoint.AddConnectedPoint(point);
						}
					}
					point.SetState("useless");
				}
			}
			foreach (Point c in foundConnections)
			{
				c.SetState("nb");
				float lowestScore = 0;
				Point bestPrevPoint = null;
				bool first = true;
				foreach (Point prevpath_points in c.GetPotentialPrevpath_points())
				{
					if (first)
					{
						lowestScore = prevpath_points.GetScore();
						bestPrevPoint = prevpath_points;
						first = false;
					} else
					{
						if (lowestScore > prevpath_points.GetScore())
						{
							lowestScore = prevpath_points.GetScore();
							bestPrevPoint = prevpath_points;
						}
					}
				}
				c.SetPrevPoint(bestPrevPoint);
			}
		}

		if (targetFound)
		{
			List<Point> bestPath = null;
			float lowestScore = 0;
			bool firstRoute = true;

			foreach (Point p in endPoint.GetConnectedpath_points())
			{
				float score = 0;
				bool finding = true;
				Point c_Point = p;
				List<Point> route = new List<Point>();
				route.Add(endPoint);
				while(finding)
				{
					route.Add(c_Point);
					if (c_Point.GetState() == "start")
					{
						if (firstRoute)
						{
							bestPath = route;
							lowestScore = score;
							firstRoute = false;
						} else
						{
							if (lowestScore > score)
							{
								bestPath = route;
								lowestScore = score;
							}
						}
						finding = false;
						break;
					}
					score += c_Point.GetScore();
					c_Point = c_Point.GetPrevPoint();
				}
			}

			bestPath.Reverse();
			List<Vector3> path = new List<Vector3>();
			foreach (Point p in bestPath)
			{
				path.Add(p.GetPos());
			}
			return path;
		}
		else
		{
			return null;
		}
	}
示例#4
0
    public List <Vector2> Path(GameObject finder, GameObject target, string _layerName)
    {
        //Debug.Log ("Finding Path............");
        this.layerName = _layerName;
        finderBound    = new Vector2(finder.GetComponent <BoxCollider2D> ().bounds.extents.x + 0.1f, finder.GetComponent <BoxCollider2D> ().bounds.extents.y + 0.1f);
//		startPos = finder.GetComponent<Rigidbody2D>().position;
//		targetPos = target.GetComponent<Rigidbody2D> ().position;

        startPos  = new Vector2(finder.transform.position.x, finder.transform.position.y);
        targetPos = new Vector2(target.transform.position.x, target.transform.position.y);



        hit = Physics2D.Linecast(startPos + finderBound, targetPos, layerMask);

        layerMask = 1 << LayerMask.NameToLayer(layerName);

//		if (hit)
//		{
//			if (hit.transform.name == target.name) {
//				Debug.Log ("Hit:" + hit.collider.name);
//				Debug.Log ("Adding Path............");
//				path.Add (startPos);
//				path.Add (targetPos);
//				//Debug.DrawLine (path[0], path[1],Color.blue);
//				return path;
//			} else {
//				Debug.DrawLine (startPos, hit.transform.position, Color.red);
//				Transform[] childs = hit.transform.gameObject.GetComponentsInChildren<Transform> ();
//				for (int i = 0; i < childs.Length; i++) {
//					Vector2 nodePos = new Vector2 (childs [i].position.x, childs [i].position.y);
//					RaycastHit2D cantSee = (Physics2D.Linecast (startPos + finderBound, childs [i].position));
//					Debug.Log (childs [i].tag);
//					if (childs [i].CompareTag ("Node") && !cantSee) {
//						Debug.DrawLine (startPos, childs [i].transform.position, Color.cyan);
//					}
//				}
//			}
//
//		}

        if (hit && hit.transform.name == target.name)
        {
            List <Vector2> path = new List <Vector2>();
            path.Add(startPos);
            path.Add(targetPos);
            return(path);
        }

        GameObject[] nodes  = GameObject.FindGameObjectsWithTag(nodeTag);
        List <Point> points = new List <Point> ();

        foreach (GameObject node in nodes)
        {
            if (isInCameraBound(node.transform.position))
            {
                Point currNode = new Point(node.transform.position);
                points.Add(currNode);
            }
        }

        Point endPoint = new Point(targetPos, 'e');

        Debug.Log("Number Of Node:" + points.Capacity);

        /***Connect them together***/
        foreach (Point point in points)        //Could be optimized to not go through each connection twice
        {
            foreach (Point point2 in points)
            {
                RaycastHit2D hitNode = Physics2D.Linecast(point.GetPos(), point2.GetPos(), layerMask);
                if (!hitNode)
                {
                    Debug.DrawLine(point.GetPos(), point2.GetPos(), Color.white);
                    point.AddConnectedPoint(point2);
                }
            }
            RaycastHit2D hitEndNode = Physics2D.Linecast(point.GetPos(), targetPos, layerMask);

            if (hitEndNode.collider.CompareTag(target.gameObject.tag) && hitEndNode)
            {
                //Debug.Log("I Hit With:......."+hitEndNode.collider.name+" My Pos:"+ point.GetPos().x+":"+point.GetPos().y);


                Debug.DrawLine(targetPos, point.GetPos(), Color.white);
                point.AddConnectedPoint(endPoint);
            }

            RaycastHit2D hitStartNode = Physics2D.Linecast(point.GetPos(), startPos, layerMask);

            if (hitStartNode.collider.CompareTag(finder.gameObject.tag) && hitStartNode)
            {
                float distance = Vector2.Distance(point.GetPos(), startPos);
                //Debug.Log("I Hit With:......."+hitStartNode.collider.name+" My Pos:"+ point.GetPos().x+":"+point.GetPos().y);

                if (hitStartNode.collider.gameObject == finder.gameObject)
                {
                    Debug.DrawLine(point.GetPos(), startPos, Color.white);

                    Point startPoint = new Point(startPos, 's');
                    point.SetPrevPoint(startPoint);
                    point.SetState('o');
                    point.SetScore(distance + Vector2.Distance(targetPos, point.GetPos()));
                }
            }
        }


        //Go through until we find the exit or run out of connections
        bool searchedAll = false;
        bool foundEnd    = false;

        while (!searchedAll)
        {
            searchedAll = true;
            List <Point> foundConnections = new List <Point>();

            foreach (Point point in points)
            {
                if (point.GetState() == 'o')
                {
                    //Debug.Log ("Found Open at: "+point.GetPos().x+":"+point.GetPos().y);

                    searchedAll = false;
                    List <Point> potentials = point.GetConnectedPoints();
                    //Debug.Log ("Number of ConnectedPoints: " + potentials.Count);
                    foreach (Point potentialPoint in potentials)
                    {
                        if (potentialPoint.GetState() == 'u')
                        {
                            Debug.Log("Found the U...........");
                            potentialPoint.AddPotentialPrevPoint(point);
                            foundConnections.Add(potentialPoint);
                            potentialPoint.SetScore(Vector2.Distance(startPos, potentialPoint.GetPos()) + Vector2.Distance(targetPos, potentialPoint.GetPos()));
                        }
                        else if (potentialPoint.GetState() == 'e')
                        {
                            Debug.Log("Found the exit..........");
                            //Found the exit
                            foundEnd = true;
                            endPoint.AddConnectedPoint(point);
                        }
                    }
                    point.SetState('c');
                }
            }

            //searchedAll = true;

            foreach (Point connection in foundConnections)
            {
                connection.SetState('o');
                //Find lowest scoring prev point
                float lowestScore   = 0;
                Point bestPrevPoint = null;
                bool  first         = true;
                foreach (Point prevPoints in connection.GetPotentialPrevPoints())
                {
                    if (first)
                    {
                        lowestScore   = prevPoints.GetScore();
                        bestPrevPoint = prevPoints;
                        first         = false;
                    }
                    else
                    {
                        if (lowestScore > prevPoints.GetScore())
                        {
                            lowestScore   = prevPoints.GetScore();
                            bestPrevPoint = prevPoints;
                        }
                    }
                }
                connection.SetPrevPoint(bestPrevPoint);
            }
        }
        if (foundEnd)
        {
            //trace back finding shortest route (lowest score)
            List <Point> shortestRoute = null;
            float        lowestScore   = 0;
            bool         firstRoute    = true;

            foreach (Point point in endPoint.GetConnectedPoints())
            {
                float        score     = 0;
                bool         tracing   = true;
                Point        currPoint = point;
                List <Point> route     = new List <Point>();
                route.Add(endPoint);
                while (tracing)
                {
                    route.Add(currPoint);
                    if (currPoint.GetState() == 's')
                    {
                        if (firstRoute)
                        {
                            shortestRoute = route;
                            lowestScore   = score;
                            firstRoute    = false;
                        }
                        else
                        {
                            if (lowestScore > score)
                            {
                                shortestRoute = route;
                                lowestScore   = score;
                            }
                        }
                        tracing = false;
                        break;
                    }
                    score    += currPoint.GetScore();
                    currPoint = currPoint.GetPrevPoint();
                }
            }

            shortestRoute.Reverse();
            List <Vector2> path = new List <Vector2>();
            foreach (Point point in shortestRoute)
            {
                path.Add(point.GetPos());
            }
            return(path);
        }
        else
        {
            return(null);
        }
    }