hellbound/Assets/Scripts/Game/UI/Windows/ItemRewardWindow.cs

74 lines
2.4 KiB
C#
Raw Normal View History

2021-11-26 11:16:25 +03:00
using System;
using game;
using UnityEngine;
public enum RewardStatus { PlayerNeedsReward, PlayerRefused }
public delegate void RewardWindowResult(RewardStatus result);
public class ItemRewardWindow : ParameterlessWindow
{
public override bool NeedShowBackground => true;
public override bool CanBeClosedByBackground => false;
private GameObject _itemPreviewContainer;
private BaseItemPreviewer _itemPreviewer;
private RewardWindowResult _callback;
[SerializeField] private RewardButtonsSection _rewardButtonsSection;
protected override void OnInit()
{
_rewardButtonsSection.SetCallback(OnRewardSectionResult);
_itemPreviewContainer = Assets.Create<GameObject>("UI/ItemPreviews/ItemPreviewContainer");
OnClose += AfterCloseAction;
}
public void SetCallback(RewardWindowResult callback)
{
_callback = callback;
}
public void SetItem(ConfLevelRewardItem rewardItem)
{
BaseItemPreviewer previewer = GetOrCreatePreviewerForItem(rewardItem);
previewer.ChangeItem(rewardItem.item, false);
}
private BaseItemPreviewer GetOrCreatePreviewerForItem(ConfLevelRewardItem item)
{
if (_itemPreviewer != null)
return _itemPreviewer;
_itemPreviewer = FactorySectionItemPreview.CreateItemPreview(item.itemType);
_itemPreviewer.gameObject.SetParent(_itemPreviewContainer.transform);
return _itemPreviewer;
}
private void OnRewardSectionResult(EnumRewardResult result)
{
switch (result)
{
case EnumRewardResult.SKIP:
OnPlayerSkipReward();
break;
case EnumRewardResult.FAIL:
OnPlayerFailReward();
break;
case EnumRewardResult.NEED_REWARD:
OnPlayerNeedReward();
break;
default:
throw new ArgumentOutOfRangeException(nameof(result), result, null);
}
}
private void OnPlayerSkipReward() => _callback?.Invoke(RewardStatus.PlayerRefused);
private void OnPlayerFailReward() => _rewardButtonsSection.Begin();
private void OnPlayerNeedReward() => _callback?.Invoke(RewardStatus.PlayerNeedsReward);
private void AfterCloseAction(Window _)
{
Destroy(_itemPreviewContainer);
}
}