Line GetClosestLineTowards(OrthogonalDirection lineDirection, OrthogonalDirection playerDirection) { /* * Given a direction of a desired line and the direction of the player along their current line, * return the first line encountered along the playerDirection that is traveling towards lineDirection. */ Line desiredLine = null; float minSnapDistance = 1; /* Get the first corner encountered along the player direction direction */ float leftDistance = minSnapDistance; Corner leftCorner = currentLine.GetCornerInGivenDirection(playerDirection); while (leftCorner != null && desiredLine == null) { /* Get the distance from the player to this corner. If it's too far from the player, stop tracking */ leftDistance = (leftCorner.position - gamePosition).magnitude; if (leftDistance < minSnapDistance) { /* Track the first line encountered that goes along the desired direction */ desiredLine = leftCorner.AttachedLineAt(lineDirection); /* Get the next corner along the direction */ if (leftCorner.AttachedLineAt(playerDirection) != null) { leftCorner = leftCorner.AttachedLineAt(playerDirection).GetCornerInGivenDirection(playerDirection); } /* This was the last corner, so set the corner to null to stop the while loop */ else { leftCorner = null; } } /* Stop searching for the line if the corner is too far from the player */ else { leftCorner = null; } } return(desiredLine); }
private void ChangeCurrentLine(OrthogonalDirection dir1, OrthogonalDirection dir2) { /* * If the player is on the corner of their current line, change their current line * to one of the lines connected to the corner they are currently on. * * The line they will switch to is determined by the given inputs and the direction * of their current line (if they have given two inputs). * * When on a corner and holding two sepperate axis as inputs, the player will swap * to the line perpendicular to their current line. This is shown in the example bellow: * * When moving up towards a 4 way corner and holding up+right, the player, once reaching * the corner, will swap to the corner's right line. This is because if the player * wanted to not take the turn and continue upwards, they would have only held the up key. */ Corner currentCorner = GetCorner(); OrthogonalDirection primaryDirection = dir1; OrthogonalDirection secondairyDirection = dir2; /* Make sure we are on a corner first */ if (currentCorner != null) { /* Properly order the inputs if we are given two unique directions */ if (dir1 != OrthogonalDirection.NULL && dir2 != OrthogonalDirection.NULL) { /* Current line is horizontal. */ if (currentLine.IsHorizontal()) { /* If the primary direction is also horizontal... */ if (Corner.HoriDirection(primaryDirection)) { /* ...Swap it so the primary direction is now vertical */ primaryDirection = dir2; secondairyDirection = dir1; } } /* Current line is vertical. */ else if (currentLine.IsVertical()) { /* If the primary direction is also vertical... */ if (Corner.VertDirection(primaryDirection)) { /* ...Swap it so the primary direction is now horizontal */ primaryDirection = dir2; secondairyDirection = dir1; } } } /* Swap to the line at the primary direction */ if (currentCorner.AttachedLineAt(primaryDirection) != null) { ChangeCurrentLine(currentCorner.AttachedLineAt(primaryDirection)); } /* Swap to the line at the secondairy direction if the primary is not connected */ else if (currentCorner.AttachedLineAt(secondairyDirection) != null) { ChangeCurrentLine(currentCorner.AttachedLineAt(secondairyDirection)); } } }