278 lines
8.5 KiB
C#
278 lines
8.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using game;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class ChestsWindow : ParameterWindow<ChestsGenerator>
|
|
{
|
|
[Header("Prefabs")] [SerializeField] private ChestView _viewPrefab = null;
|
|
|
|
[Header("Links")] [SerializeField] private GameObject _title = null;
|
|
[SerializeField] private GameObject _message = null;
|
|
[SerializeField] private Button _keysButton = null;
|
|
[SerializeField] private Button _claimButton = null;
|
|
|
|
[Header("Keys")] [SerializeField] private Transform _key1 = null;
|
|
[SerializeField] private Transform _key2 = null;
|
|
[SerializeField] private Transform _key3 = null;
|
|
|
|
[Header("Keys Reward")] [SerializeField] private Image _keysRewardIcon = null;
|
|
[SerializeField] private Image _keysLoadIcon = null;
|
|
|
|
[Header("Grid")] [SerializeField] private Transform _row1 = null;
|
|
[SerializeField] private Transform _row2 = null;
|
|
[SerializeField] private Transform _row3 = null;
|
|
[SerializeField] private GameObject _buttons = null;
|
|
[SerializeField] private GameObject _counter = null;
|
|
|
|
private Coroutine _keysPulse = null;
|
|
private int _rewardCount = 0;
|
|
private int _keysAdsRewardCount = 0;
|
|
private bool _windowClosing = false;
|
|
|
|
public override bool NeedShowBackground => false;
|
|
|
|
protected override void OnHidden()
|
|
{
|
|
Save.Inventory.AddConsumableItem(EnumConsumable.KEY, (uint)_rewardCount);
|
|
_rewardCount = 0;
|
|
|
|
StopCoroutine(KeysPulse());
|
|
_keysPulse = null;
|
|
}
|
|
|
|
protected override void OnShow(ChestsGenerator argument)
|
|
{
|
|
_windowClosing = false;
|
|
_keysAdsRewardCount = 0;
|
|
|
|
_buttons.SetActive(false);
|
|
_counter.SetActive(true);
|
|
_message.SetActive(true);
|
|
|
|
ChangeKeysView(true);
|
|
|
|
_keysButton.onClick.RemoveAllListeners();
|
|
_keysButton.onClick.AddListener(TakeKeysForAds);
|
|
_keysButton.interactable = Ads.HasRewardVideo();
|
|
_keysRewardIcon.gameObject.SetActive(_keysButton.interactable);
|
|
_keysLoadIcon.gameObject.SetActive(_keysButton.interactable == false);
|
|
|
|
_claimButton.gameObject.SetActive(false);
|
|
_claimButton.onClick.RemoveAllListeners();
|
|
_claimButton.onClick.AddListener(() => UI.ShowWindow<CoinsCounterWindow>());
|
|
_claimButton.onClick.AddListener(() => Save.Inventory.AddConsumableItem(EnumConsumable.COIN, (uint)_rewardCount));
|
|
_claimButton.onClick.AddListener(() => _rewardCount = 0);
|
|
_claimButton.onClick.AddListener(() => StartCoroutine(PreCloseRoutine()));
|
|
|
|
FillGrid(argument.Rewards);
|
|
}
|
|
|
|
private void ChangeKeysView(bool status)
|
|
{
|
|
Transform[] keys = {_key1, _key2, _key3};
|
|
|
|
int keysCount = (int)Save.Inventory.GetConsumableItemAmount(EnumConsumable.KEY);
|
|
for (int i = 0; i < keysCount; i++)
|
|
{
|
|
if (status)
|
|
{
|
|
keys[i].GetChild(0).gameObject.SetActive(true);
|
|
keys[i].GetChild(1).gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
keys[i].GetChild(0).gameObject.SetActive(false);
|
|
keys[i].GetChild(1).gameObject.SetActive(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void TryOpenChest(ChestView view, int prizeValue)
|
|
{
|
|
if (Save.Inventory.GetConsumableItemAmount(EnumConsumable.KEY) == 0)
|
|
return;
|
|
|
|
if (view.IsOpen())
|
|
return;
|
|
|
|
view.SetOpenView(prizeValue.ToString());
|
|
DisableKeyView();
|
|
OpenChest(prizeValue);
|
|
}
|
|
|
|
private void DisableKeyView()
|
|
{
|
|
Transform[] keys = {_key1, _key2, _key3};
|
|
|
|
for (int i = keys.Length - 1; i >= 0; i--)
|
|
{
|
|
if (keys[i].GetChild(0).gameObject.activeInHierarchy)
|
|
{
|
|
keys[i].GetChild(0).gameObject.SetActive(false);
|
|
keys[i].GetChild(1).gameObject.SetActive(true);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OpenChest(int value)
|
|
{
|
|
Save.Inventory.RemoveConsumableItem(EnumConsumable.KEY, 1);
|
|
_rewardCount += value;
|
|
}
|
|
|
|
private IEnumerator WaitForContinueButton(float time)
|
|
{
|
|
yield return new WaitForSeconds(time);
|
|
_claimButton.gameObject.SetActive(true);
|
|
}
|
|
|
|
private IEnumerator KeysPulse()
|
|
{
|
|
while (gameObject.activeInHierarchy)
|
|
{
|
|
yield return Tween.DoTween(0.33f,
|
|
(t) => { _keysButton.transform.localScale = Vector3.Lerp(Vector3.one, Vector3.one * 1.1f, t); });
|
|
|
|
yield return new WaitForSeconds(0.1f);
|
|
|
|
yield return Tween.DoTween(0.33f,
|
|
(t) => { _keysButton.transform.localScale = Vector3.Lerp(Vector3.one * 1.1f, Vector3.one, t); });
|
|
|
|
yield return new WaitForSeconds(0.1f);
|
|
}
|
|
|
|
_keysPulse = null;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Save.Inventory.GetConsumableItemAmount(EnumConsumable.KEY) > 0)
|
|
return;
|
|
|
|
if (_windowClosing == false)
|
|
{
|
|
_message.SetActive(false);
|
|
_counter.SetActive(false);
|
|
_buttons.SetActive(true);
|
|
|
|
|
|
_keysButton.gameObject.SetActive(_keysAdsRewardCount < 2);
|
|
|
|
_claimButton.gameObject.SetActive(true);
|
|
}
|
|
|
|
|
|
if (_keysPulse == null)
|
|
_keysPulse = StartCoroutine(KeysPulse());
|
|
|
|
if (_claimButton.gameObject.activeInHierarchy == false)
|
|
StartCoroutine(WaitForContinueButton(3f));
|
|
|
|
if (_keysButton.interactable == false && _keysRewardIcon.gameObject.activeInHierarchy == false)
|
|
{
|
|
_keysButton.interactable = Ads.HasRewardVideo();
|
|
_keysRewardIcon.gameObject.SetActive(_keysButton.interactable);
|
|
_keysLoadIcon.gameObject.SetActive(_keysButton.interactable == false);
|
|
|
|
if (_keysPulse == null && _keysButton.interactable)
|
|
_keysPulse = StartCoroutine(KeysPulse());
|
|
}
|
|
}
|
|
|
|
private void FillGrid(List<int> values)
|
|
{
|
|
CreateView(values, _row1, 0);
|
|
CreateView(values, _row2, 3);
|
|
CreateView(values, _row3, 6);
|
|
}
|
|
|
|
private void CreateView(List<int> target, Transform parent, int firsttIndex)
|
|
{
|
|
for (int j = 0; j < parent.childCount; j++)
|
|
{
|
|
ChestView view = Instantiate(_viewPrefab, parent.GetChild(j));
|
|
Button viewButton = view.GetComponent<Button>();
|
|
|
|
view.SetCloseView();
|
|
|
|
int chestPrizeValue = target[firsttIndex];
|
|
viewButton.onClick.AddListener(() => TryOpenChest(view, chestPrizeValue));
|
|
|
|
firsttIndex++;
|
|
}
|
|
}
|
|
|
|
private void TakeKeysForAds()
|
|
{
|
|
#if UNITY_EDITOR
|
|
_keysAdsRewardCount++;
|
|
|
|
Save.Inventory.AddConsumableItem(EnumConsumable.KEY, 3);
|
|
|
|
_message.SetActive(true);
|
|
_counter.SetActive(true);
|
|
_buttons.SetActive(false);
|
|
|
|
ChangeKeysView(true);
|
|
#else
|
|
Ads.ShowReward
|
|
("chests",
|
|
OnPlayerNeedReward,
|
|
OnPlayerFailReward,
|
|
OnPlayerSkipReward);
|
|
#endif
|
|
}
|
|
|
|
private void OnPlayerSkipReward()
|
|
{
|
|
//todo: Earn coins
|
|
StartCoroutine(PreCloseRoutine());
|
|
}
|
|
|
|
private void OnPlayerFailReward()
|
|
{
|
|
_buttons.SetActive(true);
|
|
}
|
|
|
|
private void OnPlayerNeedReward()
|
|
{
|
|
_keysAdsRewardCount++;
|
|
|
|
Save.Inventory.AddConsumableItem(EnumConsumable.KEY, 3);
|
|
|
|
_message.SetActive(true);
|
|
_counter.SetActive(true);
|
|
_buttons.SetActive(false);
|
|
|
|
ChangeKeysView(true);
|
|
StartCoroutine(PreCloseRoutine());
|
|
}
|
|
|
|
private IEnumerator PreCloseRoutine()
|
|
{
|
|
UI.ShowWindow<CoinsCounterWindow>();
|
|
_windowClosing = true;
|
|
|
|
_keysButton.gameObject.SetActive(false);
|
|
_claimButton.gameObject.SetActive(false);
|
|
_title.gameObject.SetActive(false);
|
|
|
|
float delayBeforeMove = 0.15f;
|
|
float delayAfterMove = 1f;
|
|
|
|
int coinsCount = 15;
|
|
ScreenCoins coins = gameObject.AddComponent<ScreenCoins>();
|
|
coins.Init(coinsCount);
|
|
|
|
yield return new WaitForSeconds(delayBeforeMove);
|
|
|
|
yield return StartCoroutine(coins.MoveCoins());
|
|
|
|
yield return new WaitForSeconds(delayAfterMove);
|
|
UI.ShowWindow<MainWindow>().Show();
|
|
Close();
|
|
}
|
|
} |