using System.Collections; using UnityEngine; using RR.Common.Controls; using RR.Game2D.Core.Managers; namespace RR.Common.Prizes { /// /// Исполнитель приза «Хоппер». /// Выдаёт капсулы последовательно. Количество и задержка берутся из . /// Делегирует низкоуровневую работу в . /// public class HopperPrize : PrizeBase { public override PrizeType Type => PrizeType.Hopper; public override bool IsGiving => _isGiving; private bool _isGiving; private Coroutine _coroutine; public override void Execute(MonoBehaviour host, LevelPrizeConfig config) { if (_isGiving || PrizeSystem.IsPrizing) { GameManager.Logger?.Log("HopperPrize", "Уже идёт выдача приза."); return; } int count = Mathf.Max(1, config.hopperCapsuleCount); float delay = Mathf.Max(0f, config.hopperDelayBetweenCapsules); if (_coroutine != null) host.StopCoroutine(_coroutine); _coroutine = host.StartCoroutine(DispenseSequence(count, delay)); } private IEnumerator DispenseSequence(int count, float delay) { _isGiving = true; int dispensed = 0; for (int i = 0; i < count; i++) { while (PrizeSystem.IsPrizing) yield return null; bool? result = null; void OnResult(bool success) => result = success; PrizeSystem.OnPrizeResult += OnResult; PrizeSystem.GivePrize(); while (result == null) yield return null; PrizeSystem.OnPrizeResult -= OnResult; if (result == true) { dispensed++; } else { Debug.LogWarning($"[HopperPrize] Ошибка капсулы {i + 1}/{count}. Прерываю."); break; } if (i < count - 1 && delay > 0) yield return new WaitForSecondsRealtime(delay); } _isGiving = false; InvokeResult(dispensed == count); } } }