SamsonGame/Assets/Scripts/Game/UI/Windows/DailyWindow.cs

162 lines
4.9 KiB
C#

using System;
using System.Collections;
using game;
using I2.Loc;
using UnityEngine;
namespace RND
{
public class DailyWindow : ParameterWindow<DailyReward>
{
[SerializeField] private TimerRewardButtonsSection _rewardSection;
[Header("Prefabs")] [SerializeField] private DailyRewardView _viewPrefab;
[Header("Links")] [SerializeField] private GameObject _title;
[Header("Grid")] [SerializeField] private Transform _row1;
[SerializeField] private Transform _row2;
[SerializeField] private Transform _row3;
private DailyReward _dailyReward;
private DailyRewardView _todayReward;
private bool _isCoinsReward = false;
private bool _canPowerAmount;
private int _powerAmount = 1;
public override bool NeedShowBackground => false;
protected override void OnShow(DailyReward argument)
{
_dailyReward = argument;
ConfRewardItem item = _dailyReward.GetItemForTheDay(argument.DayIndex);
_canPowerAmount = item.canMultiplie;
if (!_canPowerAmount)
_rewardSection.HideRewardButton();
_rewardSection.SetCallback(OnRewardSectionResult);
FillGrid();
}
private void FillGrid()
{
CreateView(_row1, 0);
CreateView(_row2, 3);
CreateView(_row3, 6);
}
private void CreateView(Transform parent, int dayIndex)
{
for (var j = 0; j < parent.childCount; j++)
{
DailyRewardView view = Instantiate(_viewPrefab, parent.GetChild(j));
bool isReceivedItem = dayIndex <= _dailyReward.DayIndex;
bool isTodayItem = dayIndex == _dailyReward.DayIndex;
string messageTerm = isReceivedItem ? "Earned" : isTodayItem ? "Day" : "Today";
string prefixForTerm = (!isReceivedItem && !isTodayItem) ? $"{dayIndex + 1}" : "";
string timeMessage = prefixForTerm + LocalizationManager.GetTranslation(messageTerm);
ConfRewardItem rewardItem = _dailyReward.GetItemForTheDay(dayIndex);
view.SetItemView(Assets.Load<Sprite>(rewardItem.iconPath), timeMessage);
if (isReceivedItem)
view.SetReceivedView();
if (isTodayItem)
{
_todayReward = view;
_todayReward.SetNewView();
}
if (dayIndex == _dailyReward.DaysCount - 1)
view.ChangeBackground(DailyRewardView.BackgroundEnum.LAST_DAY);
dayIndex++;
}
}
private void TakeReward()
{
if (_dailyReward.TryGetReward(out ConfRewardItem rewardItem) == false)
return;
long itemsAmount = rewardItem.item.amount * _powerAmount;
Save.Inventory.AddPurchaseItem(rewardItem.item.protoId, (uint) itemsAmount);
}
private IEnumerator PreCloseRoutine()
{
_todayReward.ChangeBackground(DailyRewardView.BackgroundEnum.RECEIVED);
yield return null;
if (_isCoinsReward == false)
{
Close();
yield break;
}
_rewardSection.Hide();
_title.gameObject.SetActive(false);
//Constants?
var delayBeforeMove = 0.15f;
var delayAfterMove = 1f;
var coinsCount = 15;
var coins = gameObject.AddComponent<ScreenCoins>();
coins.Init(coinsCount);
yield return new WaitForSeconds(delayBeforeMove);
yield return StartCoroutine(coins.MoveCoins());
yield return new WaitForSeconds(delayAfterMove);
UI.Show<MainWindow>();
UI.Show<CoinsCounterWindow>();
Close();
}
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()
{
TakeReward();
StartCoroutine(PreCloseRoutine());
}
private void OnPlayerFailReward()
{
TakeReward();
StartCoroutine(PreCloseRoutine());
}
private void OnPlayerNeedReward()
{
UI.Show<CoinsCounterWindow>();
_powerAmount = _canPowerAmount ? _rewardSection.Power : 1;
TakeReward();
}
}
}