public void RegenerateLevel() { LevelHolder.Level = null; if (LevelHolder.LevelSkeletonWithBioms == null) { Debug.Log($"[Failed] Level generation failed: {nameof(LevelHolder.LevelSkeletonWithBioms)} is null"); return; } var levelGeneratorParams = new LevelGeneratorParams() { LevelSkeleton = LevelHolder.LevelSkeletonWithBioms }; var levelGenerator = new LevelGenerator(levelGeneratorParams); LevelHolder.Level = levelGenerator.Execute(); if (LevelHolder.Level == null) { Debug.Log("[Failed] Level generation failed"); return; } Redraw(); }
private void ChooseActionPoints(System.Random rand, LevelGeneratorParams p, PlatformInfo plat) { float earlyLandMargin = Mathf.Lerp(p.minEarlyLandMargin, p.maxEarlyLandMargin, (float)rand.NextDouble()); float lateJumpMargin = Mathf.Lerp(p.minLateJumpMargin, p.maxLateJumpMargin, (float)rand.NextDouble()); plat.landPoint = Vector2.Lerp(plat.GetStartPoint(), plat.GetEndPoint(), earlyLandMargin); plat.jumpPoint = Vector2.Lerp(plat.GetStartPoint(), plat.GetEndPoint(), 1 - lateJumpMargin); }
private MovementFunc SetupJumpFunction(LevelGeneratorParams p, IList <PlatformInfo> platforms, PlatformInfo from, PlatformInfo to) { Vector2 jumpStart = from.jumpPoint; Vector2 jumpEnd = to.landPoint; MovementFunc jf = GenerateJumpFunction(p, jumpStart, jumpEnd); from.jumpFunction = jf; return(jf); }
public void RegenerateBioms(float minEs, float maxEs) { LevelHolder.LevelSkeletonWithBioms = null; LevelHolder.Level = null; if (LevelHolder.LevelSkeleton == null) { Debug.Log($"[Failed] Bioms generation failed: {nameof(LevelHolder.LevelSkeleton)} is null"); return; } var levelSkeletonBiomsGeneratorParams = new LevelSkeletonBiomsGeneratorParams() { LevelSkeleton = LevelHolder.LevelSkeleton }; if (minEs > 0) { levelSkeletonBiomsGeneratorParams.MinOpenSpacePerimeter = minEs; } if (maxEs > 0) { levelSkeletonBiomsGeneratorParams.MaxOpenSpacePerimeter = maxEs; } var levelSkeletonBiomsGenerator = new LevelSkeletonBiomsGenerator(levelSkeletonBiomsGeneratorParams); LevelHolder.LevelSkeletonWithBioms = levelSkeletonBiomsGenerator.Execute(); if (LevelHolder.LevelSkeletonWithBioms == null) { Debug.Log("[Failed] Bioms generation failed"); return; } var levelGeneratorParams = new LevelGeneratorParams() { LevelSkeleton = LevelHolder.LevelSkeletonWithBioms }; var levelGenerator = new LevelGenerator(levelGeneratorParams); LevelHolder.Level = levelGenerator.Execute(); if (LevelHolder.Level == null) { Debug.Log("[Failed] Level generation failed"); return; } Redraw(); }
private MovementFunc GenerateJumpFunction(LevelGeneratorParams p, Vector2 jumpStart, Vector2 jumpEnd) { float dx = jumpEnd.x - jumpStart.x; float dy = jumpEnd.y - jumpStart.y; float h = Mathf.Max(0, dy) + dx * p.jumpHeightRatio; float jt = Vector2.Distance(jumpStart, jumpEnd) * p.jumpTimePerDistance; float a = (dy - 2 * (h + Mathf.Sqrt(h * (-dy + h)))) / (jt * jt); float b = (2 * (h + Mathf.Sqrt(h * (-dy + h)))) / jt; float vx = dx / jt; return(new MovementFunc(t => new Vector2(vx * t, (a * t + b) * t), jt)); }
public void Regenerate(LevelSkeletonGeneratorParams levelSkeletonGeneratorParams) { LevelHolder.LevelSkeleton = null; LevelHolder.LevelSkeletonWithBioms = null; LevelHolder.Level = null; var levelSkeletonGenerator = new LevelSkeletonGenerator(levelSkeletonGeneratorParams); LevelHolder.LevelSkeleton = levelSkeletonGenerator.Execute(); if (LevelHolder.LevelSkeleton == null) { Debug.Log("[Failed] generation failed"); return; } var levelSkeletonBiomsGeneratorParams = new LevelSkeletonBiomsGeneratorParams { LevelSkeleton = LevelHolder.LevelSkeleton, MinOpenSpacePerimeter = levelSkeletonGeneratorParams.MinOpenSpacePerimeter, MaxOpenSpacePerimeter = levelSkeletonGeneratorParams.MaxOpenSpacePerimeter }; var levelSkeletonBiomsGenerator = new LevelSkeletonBiomsGenerator(levelSkeletonBiomsGeneratorParams); LevelHolder.LevelSkeletonWithBioms = levelSkeletonBiomsGenerator.Execute(); if (LevelHolder.LevelSkeletonWithBioms == null) { Debug.Log("[Failed] Bioms generation failed"); return; } var levelGeneratorParams = new LevelGeneratorParams() { LevelSkeleton = LevelHolder.LevelSkeletonWithBioms }; var levelGenerator = new LevelGenerator(levelGeneratorParams); LevelHolder.Level = levelGenerator.Execute(); if (LevelHolder.Level == null) { Debug.Log("[Failed] Level generation failed"); return; } Redraw(); }
private PlatformInfo PlaceNextPlatform(System.Random rand, LevelGeneratorParams p, PlatformInfo prevPlatform, float progress) { Vector2 prevPlatformEndpoint = prevPlatform.GetEndPoint(); float spacing = Mathf.Lerp(p.minSpacing, p.maxSpacing, (float)rand.NextDouble()); float x = prevPlatformEndpoint.x + spacing; float offsetY = Mathf.Lerp(p.minOffsetY, p.maxOffsetY, (float)rand.NextDouble()); float y = prevPlatformEndpoint.y + offsetY; float tiltAngle = Mathf.Lerp(p.minTiltAngle, p.maxTiltAngle, (float)rand.NextDouble()); float avgLength = Mathf.Lerp(p.startAvgLength, p.endAvgLength, progress); float length = avgLength + Mathf.Lerp(-p.lengthVariation, p.lengthVariation, (float)rand.NextDouble()); Vector2 start = new Vector2(x, y); Vector2 end = start + (Vector2)(Quaternion.Euler(0, 0, tiltAngle) * new Vector2(length, 0)); return(PlatformInfo.FromEndpoints(start, end)); }
private (IList <Platform> platforms, MovementFunc guidePath) ConstructLevel(LevelGeneratorParams levelGenParams) { LevelGenerator levelGen = new LevelGenerator(); // Generate platforms IList <PlatformInfo> platformInfos = levelGen.GeneratePlatforms(new System.Random(), PlatformInfo.FromLength(Vector2.zero, startPlatformLength), levelGenParams); IList <Platform> platforms = new List <Platform>(platformInfos.Count); PlacePlatforms(platforms, platformInfos); // Generate guide path float jumpDuration = platformInfos.Sum(p => p.GetJumpFunction().duration); float runDuration = levelGenParams.levelDuration - jumpDuration; float runDistance = platformInfos.Sum(p => Vector3.Distance(p.GetLandPoint(), p.GetJumpPoint())); float guideRunSpeed = runDistance / runDuration; MovementFunc guidePath = levelGen.GenerateGuidePath(platformInfos, guideRunSpeed); return(platforms, guidePath); }
public IList <PlatformInfo> GeneratePlatforms(System.Random rand, PlatformInfo startPlatform, LevelGeneratorParams p) { Assert.IsTrue(p.maxEarlyLandMargin + p.maxLateJumpMargin <= 1, "Early landing and late jump margin overlapping"); IList <PlatformInfo> platforms = new List <PlatformInfo>(); ChooseActionPoints(rand, p, startPlatform); platforms.Add(startPlatform); float accumLevelTime = Vector2.Distance(startPlatform.GetStartPoint(), startPlatform.GetJumpPoint()) / p.runSpeed; while (accumLevelTime < p.levelDuration) { float progress = accumLevelTime / p.levelDuration; PlatformInfo plat = PlaceNextPlatform(rand, p, platforms[platforms.Count - 1], progress); ChooseActionPoints(rand, p, plat); platforms.Add(plat); MovementFunc jf = SetupJumpFunction(p, platforms, platforms[platforms.Count - 2], platforms[platforms.Count - 1]); accumLevelTime += Vector2.Distance(plat.GetLandPoint(), plat.GetJumpPoint()) / p.runSpeed + jf.duration; } platforms[platforms.Count - 1].jumpFunction = new MovementFunc(_ => Vector2.zero, 0); return(platforms); }