public void CreateLevel(int numObstacles) { var method = MethodBase.GetCurrentMethod(); var attr = (ServerAccess)method.GetCustomAttributes(typeof(ServerAccess), true)[0]; if (!attr.HasAccess) { return; } // Clear the current level ClearChildren(); var availablePlaces = GetAvailableSpaces(); if (numObstacles > availablePlaces) { numObstacles = availablePlaces; } for (var i = 0; i < numObstacles; i++) { // Decide the location var x = 0; var z = 0; do { x = Random.Range(0, GeneratedLevelLayout.GetLength(0)); z = Random.Range(0, GeneratedLevelLayout.GetLength(1)); } while (GeneratedLevelLayout[x, z] != "c"); // Mark the position as used GeneratedLevelLayout[x, z] = "x"; } GenerateObstacles(); }
private bool CheckDepthValid(int depth, string validPosition) { for (var i = 0; i < GeneratedLevelLayout.GetLength(0); i++) { if (GeneratedLevelLayout[i, depth] == validPosition) { return(true); } } return(false); }
public void CreateLevel(string[] challengeInfo, string validPosition) { var method = MethodBase.GetCurrentMethod(); var attr = (ServerAccess)method.GetCustomAttributes(typeof(ServerAccess), true)[0]; if (!attr.HasAccess) { return; } var depth = GeneratedLevelLayout.GetLength(1); var startDepth = Random.Range(0, 2); // Make sure to remove the empty values in the array challengeInfo = challengeInfo.Where(x => !string.IsNullOrEmpty(x)).ToArray(); for (var i = 0; i < challengeInfo.Length; i++) { // Decide the location var x = 0; var z = startDepth; // check if start depth has valid positions while (!CheckDepthValid(z, validPosition)) { // if no valid position, move forward the start depth z += 1; } do { x = Random.Range(0, GeneratedLevelLayout.GetLength(0)); } while (GeneratedLevelLayout[x, z] != validPosition); challengeInfo[i] = CheckIfOperandRequired("+", challengeInfo[i]); // Mark the position as used GeneratedLevelLayout[x, z] = challengeInfo[i]; // Avoid division by 0 TODO handle in for loop properly if (i + 1 < challengeInfo.Length) { var max = ((depth - z) / (challengeInfo.Length - (i + 1))); startDepth += Random.Range(1, max); } } }