/// <summary> /// Checks the neiboring nodes /// </summary> /// <param name="inDirection">0-7, counter-clockwise starting at 0 degrees. (degrees = direction*45)</param> private static void checkNeighborNode(Node inNode, int inDirection) { int nodeX = inNode.getX(); int nodeY = inNode.getY(); int checkX = nodeX; int checkY = nodeY; //Simple equation to set the distance multiplication float distanceMultiplication = (inDirection % 2 == 0) ? 1.0f : 1.414f; //Get the neighboring node's position switch (inDirection) { case 0: checkX += 1; break; case 1: checkX += 1; checkY -= 1; break; case 2: checkY -= 1; break; case 3: checkX -= 1; checkY -= 1; break; case 4: checkX -= 1; break; case 5: checkX -= 1; checkY += 1; break; case 6: checkY += 1; break; case 7: checkX += 1; checkY += 1; break; } //Only continue if the check position is still on the grid if (checkX < gridWidth && checkX >= 0 && checkY < gridHeight && checkY >= 0) { //Now that we know it's still on the grid, get the node at that position Node checkNode = getNodeAtPosition(checkX, checkY); //Now only continue if the checkNode hasn't been circled and it's passable if (!checkNode.getCircled() && checkNode.getValue() != IMPASSABLE_VALUE) { //Now calculate the various values for the check node float currentNodeDistanceFromStart = inNode.getDistanceFromStart(); float toNodeDistance = checkNode.getValue() * distanceMultiplication; float estimatedDistance = currentNodeDistanceFromStart + toNodeDistance + checkNode.getManhattanDistance(); //Update the estimated distance if it'll be smaller //or if it hasn't been set yet if (estimatedDistance < checkNode.getEstimatedDistance() || checkNode.getEstimatedDistance() < 0) { checkNode.setParent(inNode); checkNode.setDistanceFromStart(currentNodeDistanceFromStart + toNodeDistance); checkNode.setEstimatedDistance(estimatedDistance); addToCheckList(checkNode); } } } }
//Adds a node to the check list and orders it according to its estimated distance private static void addToCheckList(Node inNode) { //If it's already in the checkList then delete it if (checkNodes.Contains(inNode)) { checkNodes.Remove(inNode); } //Now try to insert it as early as possible to the 0th index will always be the lowest bool inserted = false; for (int ii = 0; ii < checkNodes.Count; ii += 1) { if (inNode.getEstimatedDistance() < ((Node)checkNodes[ii]).getEstimatedDistance()) { checkNodes.Insert(ii, inNode); inserted = true; break; } } //If it wasn't inserted then that means it's the largest so add it to the end if (inserted == false) { checkNodes.Add(inNode); } }