using System.Collections.Generic; using UnityEngine; namespace DanroJump.Gameplay { /// /// Immutable-профиль контента платформы, рассчитанный из текущего этапа сложности. /// public sealed class PlatformContentProfile : IPlatformContentSource { private readonly IReadOnlyList collectiblePrefabs; private readonly IReadOnlyList equipmentPrefabs; private readonly IReadOnlyList enemyPrefabs; private readonly float collectibleSpawnChance; private readonly float equipmentSpawnChance; private readonly float enemySpawnChance; /// /// Создает профиль вероятностей и весов для трех категорий контента. /// public PlatformContentProfile( IReadOnlyList collectiblePrefabs, float collectibleSpawnChance, IReadOnlyList equipmentPrefabs, float equipmentSpawnChance, IReadOnlyList enemyPrefabs, float enemySpawnChance) { this.collectiblePrefabs = collectiblePrefabs; this.collectibleSpawnChance = Mathf.Clamp01(collectibleSpawnChance); this.equipmentPrefabs = equipmentPrefabs; this.equipmentSpawnChance = Mathf.Clamp01(equipmentSpawnChance); this.enemyPrefabs = enemyPrefabs; this.enemySpawnChance = Mathf.Clamp01(enemySpawnChance); } /// /// Делает бросок вероятности для выбранной категории контента. /// public bool ShouldSpawn(PlatformContentKind kind, System.Random random) { var chance = GetSpawnChance(kind); if (chance <= 0f) { return false; } if (chance >= 1f) { return true; } return (random != null ? random.NextDouble() : 0d) <= chance; } /// /// Выбирает prefab категории по весам. /// public bool TryGetPrefab(PlatformContentKind kind, System.Random random, out GameObject prefab) { System.Predicate prefabFilter = kind == PlatformContentKind.Enemy ? EnemyPrefabRules.CanSpawnOnPlatform : null; if (WeightedPrefabOption.TrySelect(GetPrefabs(kind), random, prefabFilter, out prefab)) { return true; } prefab = null; return false; } /// /// Возвращает шанс спавна для категории контента. /// private float GetSpawnChance(PlatformContentKind kind) { switch (kind) { case PlatformContentKind.Collectible: return collectibleSpawnChance; case PlatformContentKind.Equipment: return equipmentSpawnChance; case PlatformContentKind.Enemy: return enemySpawnChance; default: return 0f; } } /// /// Возвращает список prefab-вариантов для категории контента. /// private IReadOnlyList GetPrefabs(PlatformContentKind kind) { switch (kind) { case PlatformContentKind.Collectible: return collectiblePrefabs; case PlatformContentKind.Equipment: return equipmentPrefabs; case PlatformContentKind.Enemy: return enemyPrefabs; default: return collectiblePrefabs; } } } }