You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
222 lines
7.2 KiB
222 lines
7.2 KiB
using System.Collections.Generic; |
|
using DG.Tweening; |
|
using JetBrains.Annotations; |
|
using Meta; |
|
using Stages; |
|
using UnityEngine; |
|
using UnityEngine.UI; |
|
using Random = UnityEngine.Random; |
|
|
|
namespace Dialogs |
|
{ |
|
public class SlotMachineDialog : BaseDialog |
|
{ |
|
[SerializeField] GameObject m_prizeBack; |
|
|
|
[SerializeField] Sprite m_rollSprite; |
|
[SerializeField] Sprite m_onSpinSprite; |
|
[SerializeField] Sprite m_collectSprite; |
|
[SerializeField] Sprite m_watchAdSprite; |
|
[SerializeField] private Sprite _coins; |
|
[SerializeField] private Sprite _crystals; |
|
|
|
[SerializeField] Button m_spinButton; |
|
[SerializeField] Button m_closeButton; |
|
[SerializeField] private Special.Ability _healAbility; |
|
[SerializeField] private Special.Ability[] _abilities; |
|
[SerializeField] private SlotMachineItem[] _items; |
|
[SerializeField] private int _currencyValue; |
|
[SerializeField] private RectTransform[] _containers; |
|
[SerializeField] private Image[] _allIcons; |
|
private List<Sprite> _allSprites; |
|
|
|
private bool _needCheckFreeSpin; |
|
private bool _freeSpin = true; |
|
private bool _canCollectItems; |
|
|
|
public void ChangeSpinButtonSprite() |
|
{ |
|
m_spinButton.GetComponent<Image>().sprite = m_rollSprite; |
|
} |
|
|
|
private void Start() |
|
{ |
|
m_prizeBack.SetActive(false); |
|
m_type = DialogType.SLOT_MACHINE; |
|
InitButtons(); |
|
Repaint(); |
|
} |
|
|
|
private void Repaint() |
|
{ |
|
_allSprites = new List<Sprite> {_coins, _crystals, _healAbility.Sprite}; |
|
foreach (var sprite in _abilities) |
|
{ |
|
_allSprites.Add(sprite.Sprite); |
|
} |
|
|
|
foreach (var icon in _allIcons) |
|
{ |
|
icon.sprite = _allSprites[Random.Range(0, _allSprites.Count)]; |
|
icon.preserveAspect = true; |
|
} |
|
} |
|
|
|
private void InitButtons() |
|
{ |
|
if (m_spinButton) |
|
CommonFunctions.SetDisableButtonColor(m_spinButton); |
|
|
|
if (m_closeButton) |
|
CommonFunctions.SetDisableButtonColor(m_closeButton); |
|
} |
|
|
|
private void SpineFinished() |
|
{ |
|
m_prizeBack.SetActive(true); |
|
m_closeButton.interactable = true; |
|
m_spinButton.interactable = true; |
|
m_spinButton.GetComponent<Image>().sprite = m_collectSprite; |
|
_canCollectItems = true; |
|
} |
|
|
|
private void CollectPrizes() |
|
{ |
|
foreach (var item in _items) |
|
{ |
|
item.Collect(); |
|
break; |
|
} |
|
|
|
float delay = 0.1f; |
|
for (int i = 0; i < 3; ++i) |
|
{ |
|
SoundsManager.Instance.PlaySound("take_reward", delay); |
|
delay += 0.2f; |
|
} |
|
|
|
|
|
PlayerResources.Instance.Repaint(); |
|
|
|
if (!_freeSpin) |
|
HideDialog(); |
|
else |
|
{ |
|
_needCheckFreeSpin = true; |
|
if (CommonFunctions.IsRewardedAdCanBeShown()) |
|
m_spinButton.GetComponent<Image>().sprite = m_watchAdSprite; |
|
else |
|
HideDialog(); |
|
} |
|
} |
|
|
|
[UsedImplicitly] |
|
public void Spin() |
|
{ |
|
if (_canCollectItems) |
|
{ |
|
_canCollectItems = false; |
|
CollectPrizes(); |
|
return; |
|
} |
|
|
|
if (_needCheckFreeSpin) |
|
{ |
|
_needCheckFreeSpin = false; |
|
if (_freeSpin) |
|
{ |
|
_freeSpin = false; |
|
if (CommonFunctions.IsRewardedAdCanBeShown()) |
|
CommonFunctions.StartShowAd(); |
|
|
|
return; |
|
} |
|
} |
|
|
|
m_closeButton.interactable = false; |
|
m_spinButton.interactable = false; |
|
m_spinButton.GetComponent<Image>().sprite = m_onSpinSprite; |
|
SoundsManager.Instance.PlaySound("slot_machine"); |
|
|
|
for (int i = 0; i < _containers.Length; i++) |
|
{ |
|
var i1 = i; |
|
_containers[i].DOAnchorPosY(915, 0f); |
|
_containers[i].DOAnchorPosY(-400, 2 + i).OnComplete(() => |
|
{ |
|
if (i1 == _containers.Length - 1) |
|
{ |
|
SpineFinished(); |
|
} |
|
}); |
|
} |
|
|
|
|
|
var random = Random.Range(0f, 1f); |
|
var randomAbility = _abilities[Random.Range(0, _abilities.Length)]; |
|
|
|
foreach (var item in _items) |
|
{ |
|
if (random < 0.2f) |
|
{ |
|
item.SetData(_crystals, "40", () => |
|
{ |
|
GlobalsVar.gUser.IncCurrency(Shop.ShopType.Gems, 40); |
|
}); |
|
} |
|
else if (random < 0.4f) |
|
{ |
|
var currencyValue = _currencyValue * (StageProcessor.Instance.World + 1); |
|
item.SetData(_coins, currencyValue.ToString(), () => |
|
{ |
|
PlayerResources.Instance.AddCoins(currencyValue); |
|
}); |
|
} |
|
else if (random < 0.55f) |
|
{ |
|
var currencyValue = 2 * _currencyValue * (StageProcessor.Instance.World + 1); |
|
item.SetData(_coins, currencyValue.ToString(), () => |
|
{ |
|
PlayerResources.Instance.AddCoins(currencyValue); |
|
}); |
|
} |
|
else if (random < 0.65f) |
|
{ |
|
var currencyValue = 3 * _currencyValue * (StageProcessor.Instance.World + 1); |
|
item.SetData(_coins, currencyValue.ToString(), () => |
|
{ |
|
PlayerResources.Instance.AddCoins(currencyValue); |
|
}); |
|
} |
|
else if (random < 0.8f) |
|
{ |
|
item.SetData(randomAbility.Sprite, GlobalsVar.gInGameAbilities.GetAbNameByType(randomAbility.AbType), () => |
|
{ |
|
if (GlobalsVar.gBoard) |
|
{ |
|
GameBoard gameBoard = GlobalsVar.gBoard as GameBoard; |
|
if (gameBoard != null) |
|
{ |
|
gameBoard.MainCharacter.AddAbility(randomAbility.AbType); |
|
} |
|
} |
|
}); |
|
} |
|
else |
|
{ |
|
item.SetData(_healAbility.Sprite, "Heal 40%", () => |
|
{ |
|
if (GlobalsVar.gBoard) |
|
{ |
|
GameBoard gameBoard = GlobalsVar.gBoard as GameBoard; |
|
if (gameBoard != null) |
|
{ |
|
gameBoard.MainCharacter.AddAbility(_healAbility.AbType); |
|
} |
|
} |
|
}); |
|
} |
|
} |
|
} |
|
} |
|
}
|
|
|