using System; using System.Collections.Generic; using System.Linq; using Debug = UnityEngine.Debug; using OHM.UnityToolkit; public static class LevelGenerator { public static List
GenerateLevel(CoreDef def, long levelIdx, Random random, List
forceLevel, List
previousSections) { var result = new List
(); ProgressionDef progression = def.GetProgression(); if (progression.initialSection != null) { result.Add(progression.initialSection); } if (forceLevel != null && forceLevel.Count >= 1) { result.AddRange(forceLevel); } else { result.AddRange(GetLevelSectionList(def, levelIdx, random, previousSections)); } Section final = progression.finalSectionABTest; if (final != null) { result.Add(final); } return result; } private static List
GetLevelSectionList(CoreDef def, long levelIdx, Random random, List
sectionsToAvoid) { ProgressionDef progression = def.GetProgression(); LevelDef level = def.GetLevel(levelIdx); var pool = new List
(progression.sections); var result = new List
(); if (sectionsToAvoid != null && sectionsToAvoid.Count >= 1) { pool.RemoveAll((Section s) => sectionsToAvoid.Contains(s)); } foreach (Section forced in level.forcedSections) { if (forced != null) { result.Add(forced); pool.Remove(forced); } } for (int i = result.Count; i < level.sectionCount; i++) { Section drawn = DrawSection(level, def, i, levelIdx, pool, random, level.maxSectionYPerLevel); if (drawn != null) { result.Add(drawn); pool.Remove(drawn); } } return result; } private static Section DrawSection(LevelDef level, CoreDef coreDef, int index, long levelIdx, List
currentSectionsPool, Random random, int maxSectionYPerLevel) { var diffPool = new List( (level.randomDiffPoolAlt != null && level.randomDiffPoolAlt.Count >= 1 && index % 2 == 1) ? level.randomDiffPoolAlt : level.randomDiffPool); if (maxSectionYPerLevel <= 0) { foreach (DifficultyPoolElem elem in new List(diffPool)) { List
match = currentSectionsPool.Where((Section s) => s.difficulty == elem.element).ToList(); if (match.Count >= 1 && match[0].differentPath) { diffPool.Remove(elem); } } } int difficulty = RandomPoolUtils.DrawElementFromPool(random, diffPool); List
candidates = currentSectionsPool.Where((Section s) => s.difficulty == difficulty).ToList(); if (candidates.Count != 0) { return RandomExtensions.ListElem(random, candidates); } Debug.LogFormat("No more section with diff {0}", difficulty); List
fallback = currentSectionsPool.Where((Section s) => s.difficulty != difficulty).ToList(); if (fallback.Count == 0) { return null; } Debug.LogWarningFormat("No section with difficulty {0} found, referenced in level {1}. Will use random other difficulty.", difficulty, levelIdx); return RandomExtensions.ListElem(random, fallback); } }