void CreateGraph( ) { //Add x nodes to graph for (float i = startNode.x; i < ( targetGameObject.position.x ); i++ ){ Vector3 nodeTransformPosition = new Vector3( i, incrementY, 0 ); Vector3 inverseNodePosition = new Vector3(i, incrementY, 0); PathFinderNodes nodeObject = new PathFinderNodes( nodeTransformPosition); if( SmartGraph( nodeObject ) ){ continue; } if( counter == 1 ){ checkingNode = nodeObject; } targetNode = nodeObject; counter++; storedNodeList.Add( nodeObject, nodeTransformPosition ); } if( incrementY < ( ( targetGameObject.position.y ) ) ){ incrementY ++; CreateGraph( ); //Recursively add new vertical rows } }
bool SmartGraph( PathFinderNodes node ) { RaycastHit2D upHit = Physics2D.Raycast( node.objectPosition, Vector3.up, raycastSize ); node.extraCostWeight = 10; if ( upHit.collider != null ){ if (upHit.collider.gameObject.layer == 10 ){ return true; } } return false; }
float ManhattenHeuristic( PathFinderNodes currenNodePosition ) { Vector3 newPos = currenNodePosition.objectPosition; Vector3 endPos = targetGameObject.transform.position; float calculateHeuristic = ( Mathf.Abs( newPos.x - endPos.x ) + Mathf.Abs( newPos.y - endPos.y ) ); return calculateHeuristic; }
void FindPath( ) { if ( checkingNode.upNode != null ){ DetermineNodeValues(checkingNode, checkingNode.upNode); } if ( checkingNode.downNode != null ){ DetermineNodeValues(checkingNode, checkingNode.downNode); } if ( checkingNode.rightNode != null ){ DetermineNodeValues(checkingNode, checkingNode.rightNode); } if ( checkingNode.leftNode != null ){ DetermineNodeValues(checkingNode, checkingNode.leftNode); } if( !foundTarget ){ closedNodeList.Add( checkingNode ); checkingNode.isClosed = true; checkingNode.onOpenList = false; if( checkingNode == openQueue.Peek( ).Value ){ openQueue.DequeueValue( ); } if( openQueue.IsEmpty ){ Debug.LogError( "AI Path Cannot be found: Invalid Path" ); } else { checkingNode = openQueue.Peek( ).Value; } } else { TraceBackPath( ); } }
void FindBaseMovementCost( PathFinderNodes node ) { RaycastHit2D rightHit = Physics2D.Raycast( node.objectPosition, Vector3.right, raycastSize ); RaycastHit2D leftHit = Physics2D.Raycast(node.objectPosition, Vector3.left, raycastSize ); RaycastHit2D downHit = Physics2D.Raycast(node.objectPosition, Vector3.down, raycastSize ); node.extraCostWeight = 10; if ( rightHit.collider != null ){ if (rightHit.collider.gameObject.layer == 9 ){ node.extraCostWeight = 20; } } else if ( leftHit.collider != null ){ if( leftHit.collider.gameObject.layer == 9 ){ node.extraCostWeight = 20; } } else if ( downHit.collider ){ if( downHit.collider.gameObject.layer == 8 ){ node.extraCostWeight = -10; } if( downHit.collider.gameObject.layer == 9 ){ node.extraCostWeight = 20; } } }
void DetermineNodeValues( PathFinderNodes currentNode, PathFinderNodes testing ) { if ( testing == null ) { return; } if ( testing == targetNode ) { targetNode.parentNode = currentNode; openQueue = null; foundTarget = true; return; } if( !testing.isClosed && openQueue != null){ if( !testing.onOpenList ){ testing.parentNode = currentNode; testing.movementCost = currentNode.movementCost + testing.extraCostWeight; testing.CalculateFValues( ); testing.onOpenList = true; openQueue.Enqueue( testing.totalCost, testing ); } } }