using System; using UnityEngine; using System.Collections.Generic; using System.Linq; using game; namespace RND { public class LevelSettings { public IReadOnlyList DefaultSequence => _defaultSequence; public IReadOnlyList DefaultPattern => _defaultPattern; public IReadOnlyList Patterns => _patterns; public IList GetAll => _levels; public int TutorialLenght { get; } = 0; public bool HasTutorial => TutorialLenght > 0; private readonly List _defaultSequence = default; private readonly List _defaultPattern = default; private readonly List _patterns = default; private readonly List _levels = default; public LevelSettings(ConfGameLevels config) { _defaultSequence = config.original; _defaultPattern = config.originalPattern.sequence; _patterns = config.customPatterns; _levels = new List(config.levels.Count); for (int i = 0; i < config.levels.Count; i++) _levels.Add(new LevelData(config.levels[i])); IList tutorial = GetIDsBy(l => l.Type == EnumLevelType.TUTORIAL); TutorialLenght = tutorial.Count; if (_defaultSequence.ContainsAnyOf(tutorial)) throw new InvalidOperationException("Default sequence has tutorial level."); Log.Debug($"LevelSettings.Constructor: Uploaded successful {_levels.Count}"); } public List GetRandomPattern() { return _patterns.RandomValue().sequence; } public IList GetBy([JetBrains.Annotations.NotNull] Predicate conditional) { if (conditional == null) throw new ArgumentNullException(nameof(conditional)); return (from item in _levels where conditional.Invoke(item) select item).ToList(); } public IList GetIDsBy([JetBrains.Annotations.NotNull] Predicate conditional) { if (conditional == null) throw new ArgumentNullException(nameof(conditional)); return (from item in _levels where conditional.Invoke(item) select item.ID).ToList(); } public static GameObject GetPrefabByID(uint id) { return Assets.Load(GetPrefabPath(id)); } public static string GetPrefabPath(uint id) { return $"Levels/Level{id}"; } } }