205 lines
6.8 KiB
C#
205 lines
6.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BlueWater.Audios;
|
|
using BlueWater.Tycoons;
|
|
using BlueWater.Utility;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
using Sirenix.OdinInspector;
|
|
using TMPro;
|
|
|
|
namespace BlueWater.Uis
|
|
{
|
|
public class TycoonStartShopUi : PopupUi
|
|
{
|
|
[SerializeField]
|
|
private GameObject _soldOutPrefab;
|
|
|
|
[SerializeField]
|
|
private RectTransform _endGoldUiRect;
|
|
|
|
[SerializeField]
|
|
private TMP_Text _endGoldText;
|
|
|
|
[SerializeField]
|
|
private GameObject _panel;
|
|
|
|
[SerializeField]
|
|
private Transform _contents;
|
|
|
|
[SerializeField]
|
|
private Vector3 _cardLocalScale = new(0.65f, 0.65f, 1f);
|
|
|
|
[Title("구매 성공 연출")]
|
|
[SerializeField]
|
|
private float _endGoldChangeDuration = 1f;
|
|
|
|
[SerializeField]
|
|
private string _soldOutSfxName = "SoldOut";
|
|
|
|
[Title("구매 실패 연출")]
|
|
[SerializeField]
|
|
private Color _targetColor = Color.red;
|
|
|
|
[SerializeField]
|
|
private float _duration = 0.5f;
|
|
|
|
[SerializeField]
|
|
private Vector3 _punchPosition = new(10f, 0f, 0f);
|
|
|
|
[Title("저장된 골드")]
|
|
[SerializeField]
|
|
private int _endGold;
|
|
|
|
private List<TycoonCard> _tycoonCards = new(5);
|
|
|
|
private TycoonManager _tycoonManager;
|
|
private TycoonCardController _tycoonCardController;
|
|
private Sequence _failedPurchaseSequence;
|
|
private Coroutine _changeGoldInstance;
|
|
|
|
private Color _originalColor;
|
|
|
|
private void OnValidate()
|
|
{
|
|
if (_panel.activeSelf)
|
|
{
|
|
Utils.StartUniqueCoroutine(this, ref _changeGoldInstance, AnimateGoldChange());
|
|
}
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
EventManager.OnInitializedScene += CreateCard;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_panel.SetActive(false);
|
|
|
|
_tycoonManager = TycoonManager.Instance;
|
|
_tycoonCardController = _tycoonManager.TycoonCardController;
|
|
_endGold = ES3.Load(SaveData.EndGold, 0);
|
|
_endGoldText.text = _endGold.ToString("N0");
|
|
_originalColor = _endGoldText.color;
|
|
|
|
_failedPurchaseSequence = DOTween.Sequence();
|
|
_failedPurchaseSequence
|
|
.Join(_endGoldUiRect.DOPunchPosition(_punchPosition, _duration))
|
|
.Join(_endGoldText.DOColor(_targetColor, _duration / 2f))
|
|
.Append(_endGoldText.DOColor(_originalColor, _duration / 2f))
|
|
.SetUpdate(true)
|
|
.SetAutoKill(false)
|
|
.Pause();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_failedPurchaseSequence.Kill();
|
|
|
|
EventManager.OnInitializedScene -= CreateCard;
|
|
}
|
|
|
|
public override void Open()
|
|
{
|
|
VisualFeedbackManager.Instance.SetBaseTimeScale(0.0f);
|
|
PlayerInputKeyManager.Instance.SwitchCurrentActionMap(InputActionMaps.TycoonUi);
|
|
PopupUiController.RegisterPopup(this);
|
|
_panel.SetActive(true);
|
|
IsOpened = true;
|
|
}
|
|
|
|
public override void Close()
|
|
{
|
|
_panel.SetActive(false);
|
|
PopupUiController.UnregisterPopup(this);
|
|
PlayerInputKeyManager.Instance.SwitchCurrentActionMap(InputActionMaps.Tycoon);
|
|
IsOpened = false;
|
|
VisualFeedbackManager.Instance.ResetTimeScale();
|
|
EventManager.InvokeTycoonGameStarted();
|
|
}
|
|
|
|
[Button("상점 열기")]
|
|
private void CreateCard()
|
|
{
|
|
if (!Application.isPlaying) return;
|
|
|
|
if (!ES3.Load(SaveData.CompleteFirstGame, false))
|
|
{
|
|
EventManager.InvokeTycoonGameStarted();
|
|
return;
|
|
}
|
|
|
|
Utils.StartUniqueCoroutine(this, ref _changeGoldInstance, AnimateGoldChange());
|
|
|
|
_tycoonCardController.DestroyCardList(_tycoonCards);
|
|
for (int i = 0; i < _tycoonCards.Capacity; i++)
|
|
{
|
|
var newCard = _tycoonCardController.CreateTycoonCard(_contents);
|
|
newCard.SetName($"Card{i:00}");
|
|
newCard.SetLocalScale(_cardLocalScale);
|
|
_tycoonCards.Add(newCard);
|
|
}
|
|
|
|
Open();
|
|
|
|
//----카드 값 지정 및 초기화----
|
|
HashSet<string> hashSet = new HashSet<string>(_tycoonCards.Count - 1);
|
|
foreach (var element in _tycoonCards)
|
|
{
|
|
CardShopData cardShopData = null;
|
|
CardData cardData = null;
|
|
string cardIdx = null;
|
|
|
|
do
|
|
{
|
|
cardShopData = _tycoonCardController.CardShopDataSo.GetRandomCardData();
|
|
cardIdx = cardShopData.Idx;
|
|
cardData = _tycoonCardController.CardDataSo.GetDataByIdx(cardIdx);
|
|
} while (_tycoonCardController.CardMaxCheck(cardData) || hashSet.Contains(cardIdx));
|
|
|
|
hashSet.Add(cardIdx);
|
|
element.SetCard(cardData);
|
|
element.SetSelectAction(SelectedCard);
|
|
element.SetPrice(cardShopData.Price);
|
|
element.Rotation_Start();
|
|
}
|
|
}
|
|
|
|
private void SelectedCard(TycoonCard currentTycoonCard)
|
|
{
|
|
var cardPrice = currentTycoonCard.CardPrice;
|
|
|
|
// 구매 불가능할 때,
|
|
if (_endGold < cardPrice)
|
|
{
|
|
_failedPurchaseSequence.Restart();
|
|
return;
|
|
}
|
|
|
|
// 구매 가능할 때,
|
|
_endGold -= cardPrice;
|
|
Utils.StartUniqueCoroutine(this, ref _changeGoldInstance, AnimateGoldChange());
|
|
currentTycoonCard.CardArea.SuccessClick();
|
|
Instantiate(_soldOutPrefab, currentTycoonCard.transform);
|
|
AudioManager.Instance.PlaySfx(_soldOutSfxName, ignoreTimeScale: true);
|
|
|
|
_tycoonCardController.SelectCard(currentTycoonCard);
|
|
}
|
|
|
|
private IEnumerator AnimateGoldChange()
|
|
{
|
|
var currentGold = int.Parse(_endGoldText.text.Replace(",", ""));
|
|
var targetGold = _endGold;
|
|
var elapsedTime = 0f;
|
|
while (elapsedTime <= _endGoldChangeDuration)
|
|
{
|
|
var newGold = (int)Mathf.Lerp(currentGold, targetGold, elapsedTime / _endGoldChangeDuration);
|
|
_endGoldText.text = newGold.ToString("N0");
|
|
elapsedTime += Time.unscaledDeltaTime;
|
|
yield return null;
|
|
}
|
|
_endGoldText.text = targetGold.ToString("N0");
|
|
}
|
|
}
|
|
} |