Files
butchers-games-test/Assets/Scripts/Game/Core/LevelGenerator.cs
T
Boris Nikolaev 5f1e94b653 add project files
2026-08-14 01:58:31 +03:00

100 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Debug = UnityEngine.Debug;
using OHM.UnityToolkit;
public static class LevelGenerator
{
public static List<Section> GenerateLevel(CoreDef def, long levelIdx, Random random, List<Section> forceLevel, List<Section> previousSections)
{
var result = new List<Section>();
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<Section> GetLevelSectionList(CoreDef def, long levelIdx, Random random, List<Section> sectionsToAvoid)
{
ProgressionDef progression = def.GetProgression();
LevelDef level = def.GetLevel(levelIdx);
var pool = new List<Section>(progression.sections);
var result = new List<Section>();
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<Section> currentSectionsPool, Random random, int maxSectionYPerLevel)
{
var diffPool = new List<DifficultyPoolElem>(
(level.randomDiffPoolAlt != null && level.randomDiffPoolAlt.Count >= 1 && index % 2 == 1)
? level.randomDiffPoolAlt
: level.randomDiffPool);
if (maxSectionYPerLevel <= 0)
{
foreach (DifficultyPoolElem elem in new List<DifficultyPoolElem>(diffPool))
{
List<Section> match = currentSectionsPool.Where((Section s) => s.difficulty == elem.element).ToList();
if (match.Count >= 1 && match[0].differentPath)
{
diffPool.Remove(elem);
}
}
}
int difficulty = RandomPoolUtils.DrawElementFromPool<DifficultyPoolElem, int>(random, diffPool);
List<Section> 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<Section> 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);
}
}