hellbound/Assets/Scripts/Game/Settings/LevelSettings.cs

77 lines
2.4 KiB
C#
Raw Normal View History

2021-11-26 11:16:25 +03:00
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using game;
public class LevelSettings
{
public IReadOnlyList<uint> DefaultSequence => _defaultSequence;
public IReadOnlyList<EnumLevelType> DefaultPattern => _defaultPattern;
public IReadOnlyList<ConfLevelsPattern> Patterns => _patterns;
public IList<LevelData> GetAll => _levels;
public int TutorialLenght { get; } = 0;
public bool HasTutorial => TutorialLenght > 0;
private readonly List<uint> _defaultSequence = default;
private readonly List<EnumLevelType> _defaultPattern = default;
private readonly List<ConfLevelsPattern> _patterns = default;
private readonly List<LevelData> _levels = default;
public LevelSettings(ConfGameLevels config)
{
_defaultSequence = config.original;
_defaultPattern = config.originalPattern.sequence;
_patterns = config.customPatterns;
_levels = new List<LevelData>(config.levels.Count);
for (int i = 0; i < config.levels.Count; i++)
_levels.Add(new LevelData(config.levels[i]));
IList<uint> 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<EnumLevelType> GetRandomPattern()
{
return _patterns.RandomValue().sequence;
}
public IList<LevelData> GetBy([JetBrains.Annotations.NotNull] Predicate<LevelData> conditional)
{
if (conditional == null)
throw new ArgumentNullException(nameof(conditional));
return
(from item in _levels
where conditional.Invoke(item)
select item).ToList();
}
public IList<uint> GetIDsBy([JetBrains.Annotations.NotNull] Predicate<LevelData> 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<GameObject>(GetPrefabPath(id));
}
public static string GetPrefabPath(uint id)
{
return $"Levels/Level{id}";
}
}