Esempio n. 1
0
	public static List<Vector2> AStarPathFind (Vector2 start, Vector2 goal, Environment envr, bool ignoreMovement=false){
		List<Node> pointList = new List<Node>();
		Dictionary<Vector2, Node> closedList = new Dictionary<Vector2, Node>();

		// Initialise the start node and make it the singular point in the pointList
		Node startNode = new Node();
		startNode.InitaliseStart (start); 
		pointList.Add (startNode);

		Node goalNode = new Node();
		//print("AStarPathFindFurther");

		// While there are still valid points
		while (pointList.Count > 0){
			// Find the node with the highest fValue.
			Node currentNode = FindHighestPriority(pointList);
			closedList[currentNode.coords] = currentNode;
			pointList.Remove(currentNode);

			// If it's the goal break put of the loop
			if ( currentNode.coords.Equals(goal) )
			{
				goalNode = currentNode;
				break;
			}

			foreach (Node neighbour in FindNeighbourNodes(currentNode, envr, ignoreMovement)) {
				
				if ( !closedList.ContainsKey(neighbour.coords) ) {

					int newCost = neighbour.CalculateCost(currentNode); 
					if (pointList.Exists (x => (x.coords == neighbour.coords))) {
						int index = pointList.FindIndex (x => (x.coords == neighbour.coords));

						if (newCost < pointList [index].gValue) {
							pointList [index].SetGValue(newCost);
							pointList [index].CalculatePriority (goal); 
							pointList [index].SetParentNode (new Vector2 (currentNode.coords.x, currentNode.coords.y));
						}
					} else {
						neighbour.SetGValue(newCost);
						neighbour.CalculatePriority (goal); 
						neighbour.SetParentNode (new Vector2 (currentNode.coords.x, currentNode.coords.y));
						pointList.Add (neighbour);
					}
				}
				/*else if(pointList.Exists(x => (x.coords == neighbour.coords) )){
					List<Node> removeList = pointList.FindAll (x => (x.coords == neighbour.coords) );
					foreach (Node rm in removeList) {
						pointList.Remove (rm);
					}
					neighbour.CalculatePriority( goal ); 
					neighbour.SetParentNode(new Vector2(currentNode.coords.x, currentNode.coords.y));
					pointList.Add(neighbour);

				}*/
				/*else if(newCost < closedList[neighbour.coords].gValue){
					closedList [neighbour.coords].CalculatePriority (goal);
					closedList [neighbour.coords].SetParentNode(currentNode.coords);
					//closedList.Remove (neighbour.coords);
					neighbour.CalculatePriority( goal ); 
					neighbour.SetParentNode(currentNode.coords);
					pointList.Add(neighbour);
				}*/
			}
		}
	
		List<Vector2> goalPath = CreatePathToGoal(goalNode, start, closedList);
		goalPath.Reverse ();
		if (goalPath.Count > 0) {
			goalPath.RemoveAt (0);
		}
		if(goal != Vector2.zero && goalPath.Count > 0 && goalPath[goalPath.Count-1] == Vector2.zero){
			goalPath.Clear ();
		}
		return goalPath; 
	}