private void ApplyPresetToCollectableObject(PlatformObjectData platformObjectData, CollectableObjectGroup collectableObjectGroup) { foreach (Vector3 point in platformObjectData.PresetData.Positions) { Pool <CollectableObject> collectableObjectPool = poolManager.GetCollectableObjectPool(platformObjectData.ObjectType); CollectableObject collectableObject = collectableObjectPool.Allocate(); collectableObject.gameObject.SetActive(true); collectableObject.Initialize(point, platformObjectData.ObjectType, collectableObjectGroup.transform); currentPlatforms[platformIndex].platformElements.Add(collectableObjectGroup); currentPlatforms[platformIndex].platformElements.Add(collectableObject); currentCollectableObjects.Add(collectableObject); currentCollectableObjectGroup.Add(collectableObjectGroup); } }
public static void GenerateLevel(LevelPrototype levelData) { /*Summary of the platform generation: * This function is static, do not make objects of this class, only call it. * * How this code works: * 1. A list is declared to store all the platforms that are spawned * 2. A 2 dimensional int array is declared to store each tile position (this is important for rope spawning) * 3. Enter a while loop that will execute until the length of the platforms list is > our defined platform max count, * or the code exceeds a max iteration count * 4. A random point is generated within the size of the map * 5. The code now checks if the random point is valid against the perlin noise field (if the value of the nosie field * at that point is greater than the PerlinThreshold (defined in LevelData), then the code will continue) * 6. The random width of the platform is generated * 7. The random position of the platform is tested against all other platforms that exist, if it is too close, the point * is thrown away, and the while loop will start over * 8. If the platform has passed all the previous tests, it will now be added to the list of platforms * 9. The appropriate points in the 2D int array are set to 1 to store the platform tiles * 10. The ropes are generated (see summary below), and this class returns an array of platforms */ LevelPrototype ld = levelData; List <PlatformBase> platforms = new List <PlatformBase> (); int[,] levelArray = new int[ld.MapRadius, ld.MapRadius]; int iterationCount = 0; //prevent an infinite loop while (platforms.Count < ld.PlatformCount) { if (iterationCount >= ld.PlatformCount * 100) { // Debug.Log ("Stopping platform generation at " + platforms.Count); break; } Vector2 randomPoint = new Vector2(Random.Range(0, ld.MapRadius / 2), Random.Range(0, ld.MapRadius)); if (Mathf.PerlinNoise(randomPoint.x / ld.PerlinScale, randomPoint.y / ld.PerlinScale) < ld.PerlinThreshold) { continue; } int platformWidth = Random.Range(5, 40); bool tooClose = false; foreach (PlatformBase pb in platforms) { if (Vector2.Distance(pb.Pivot + Vector2.right * platformWidth / 2f, randomPoint) < ld.MinDistance || Mathf.Abs(randomPoint.y - pb.Pivot.y) < 3) { tooClose = true; break; } } if (!tooClose) { PlatformBase pb = new PlatformBase(randomPoint, platformWidth); #region Temporary Gate spawning stuff List <PlatformObjectData> po = new List <PlatformObjectData> (); //make start gate if (platforms.Count == 0) { PlatformObjectData startGate = ScriptableObject.CreateInstance <PlatformObjectData> (); startGate.position = new Vector2(pb.Pivot.x + Random.Range(1, platformWidth) + 0.5f, pb.Pivot.y + 1.5f); startGate.Object = levelData.PlatformObjects[0].Object; Level.StartGate = startGate; } //make end gate if (platforms.Count == 1) { PlatformObjectData endGate = ScriptableObject.CreateInstance <PlatformObjectData>(); endGate.position = new Vector2(pb.Pivot.x + Random.Range(1, platformWidth) + 0.5f, pb.Pivot.y + 1.5f); endGate.Object = levelData.PlatformObjects[1].Object; Level.EndGate = endGate; } pb.PlatformObjects = po.ToArray(); #endregion platforms.Add(pb); for (int i = 0; i < pb.Width; i++) { levelArray [(int)pb.Pivot.x + i, (int)pb.Pivot.y] = 1; } } iterationCount++; } Level.platforms = GenerateRopes(platforms.ToArray(), levelArray); }