189 lines
6.7 KiB
C#
189 lines
6.7 KiB
C#
using System.Collections.Generic;
|
|
using BlueWater.Audios;
|
|
using BlueWater.Tycoons;
|
|
using UnityEngine;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.UI;
|
|
using Random = UnityEngine.Random;
|
|
|
|
namespace BlueWater.Uis
|
|
{
|
|
public class TycoonRareRewardBoxUi : PopupUi
|
|
{
|
|
[SerializeField]
|
|
private GameObject _panel;
|
|
|
|
[SerializeField]
|
|
private Transform _contents;
|
|
|
|
[SerializeField]
|
|
private Vector3 _cardLocalScale_2 = new(0.9f, 0.9f, 1f);
|
|
private Vector3 _cardLocalScale_3 = new(0.8f, 0.8f, 1f);
|
|
private Vector3 _cardLocalScale_4 = new(0.7f, 0.7f, 1f);
|
|
private Vector3 _cardLocalScale_5 = new(0.65f, 0.65f, 1f);
|
|
|
|
[SerializeField]
|
|
private string _openSfxName = "RareRewardBox";
|
|
|
|
[SerializeField]
|
|
private Button allOpenCardButton;
|
|
[SerializeField]
|
|
private Button closeButton;
|
|
|
|
private List<TycoonCard> _tycoonCards = new(5);
|
|
private int viewCardCount = 0;
|
|
|
|
private LevelData _currentLevelData;
|
|
private TycoonManager _tycoonManager;
|
|
private TycoonCardController _tycoonCardController;
|
|
private InputAction _interactionEAction;
|
|
|
|
private void Start()
|
|
{
|
|
_interactionEAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.InteractionE);
|
|
|
|
_panel.SetActive(false);
|
|
|
|
_tycoonManager = TycoonManager.Instance;
|
|
_tycoonCardController = _tycoonManager.TycoonCardController;
|
|
|
|
EventManager.OnOpenedRareRewardBox += CreateCard;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
EventManager.OnOpenedRareRewardBox -= CreateCard;
|
|
|
|
_interactionEAction = null;
|
|
}
|
|
|
|
public override void Open()
|
|
{
|
|
VisualFeedbackManager.Instance.SetBaseTimeScale(0.0f);
|
|
AudioManager.Instance.PlaySfx(_openSfxName, ignoreTimeScale: true);
|
|
PlayerInputKeyManager.Instance.SwitchCurrentActionMap(InputActionMaps.TycoonUi);
|
|
PopupUiController.RegisterPopup(this);
|
|
_panel.SetActive(true);
|
|
IsOpened = true;
|
|
|
|
allOpenCardButton.gameObject.SetActive(true);
|
|
closeButton.gameObject.SetActive(false);
|
|
EventSystem.current.SetSelectedGameObject(allOpenCardButton.gameObject);
|
|
}
|
|
|
|
public override void Close()
|
|
{
|
|
_panel.SetActive(false);
|
|
PopupUiController.UnregisterPopup(this);
|
|
PlayerInputKeyManager.Instance.SwitchCurrentActionMap(InputActionMaps.Tycoon);
|
|
IsOpened = false;
|
|
VisualFeedbackManager.Instance.ResetTimeScale();
|
|
}
|
|
|
|
public override void EnableInput()
|
|
{
|
|
_interactionEAction.performed += OnInteractionE;
|
|
}
|
|
|
|
public override void DisableInput()
|
|
{
|
|
_interactionEAction.performed -= OnInteractionE;
|
|
}
|
|
|
|
|
|
[Button("레어 상자 열기")]
|
|
private void CreateCard()
|
|
{
|
|
if (!Application.isPlaying) return;
|
|
|
|
viewCardCount = 0;
|
|
_currentLevelData = TycoonManager.Instance.GetCurrentLevelData();
|
|
_tycoonCardController.DestroyCardList(_tycoonCards);
|
|
var randomCount = Random.Range(2, 6);
|
|
|
|
_contents.GetComponent<HorizontalLayoutGroup>().spacing = randomCount * 10;
|
|
|
|
for (int i = 0; i < randomCount; i++)
|
|
{
|
|
var newCard = _tycoonCardController.CreateTycoonCard(_contents);
|
|
newCard.SetName($"Card{i:00}");
|
|
newCard.CardArea.SetselectAction(OpenCard);
|
|
switch (randomCount)
|
|
{
|
|
case 2: newCard.SetLocalScale(_cardLocalScale_2); break;
|
|
case 3: newCard.SetLocalScale(_cardLocalScale_3); break;
|
|
case 4: newCard.SetLocalScale(_cardLocalScale_4); break;
|
|
case 5: newCard.SetLocalScale(_cardLocalScale_5); break;
|
|
default: newCard.SetLocalScale(_cardLocalScale_5); break;
|
|
}
|
|
|
|
viewCardCount++;
|
|
_tycoonCards.Add(newCard);
|
|
}
|
|
|
|
Open();
|
|
|
|
//----카드 값 지정 및 초기화----
|
|
HashSet<string> hashSet = new HashSet<string>(randomCount - 1);
|
|
foreach (var element in _tycoonCards)
|
|
{
|
|
CardRareData cardRareData = null;
|
|
CardData cardData = null;
|
|
string cardIdx = null;
|
|
|
|
do
|
|
{
|
|
cardRareData = _tycoonCardController.CardRareDataSo.GetRandomCardData();
|
|
cardIdx = cardRareData.Idx;
|
|
cardRareData = _tycoonCardController.CardRareDataSo.SubstitutionLiquid(cardRareData, _currentLevelData);
|
|
cardData = _tycoonCardController.CardDataSo.GetDataByIdx(cardIdx);
|
|
} while (cardRareData == null || _tycoonCardController.CardMaxCheck(cardData) || hashSet.Contains(cardIdx));
|
|
|
|
hashSet.Add(cardIdx);
|
|
element.SetCard(cardData);
|
|
element.Create_Start();
|
|
|
|
_tycoonCardController.SelectCard(element);
|
|
}
|
|
}
|
|
|
|
private void OpenCard(TycoonCard tycoonCard)
|
|
{
|
|
tycoonCard.Rotation_Start();
|
|
tycoonCard.SetSelectAction(null);
|
|
viewCardCount--;
|
|
|
|
if (viewCardCount > 0) return;
|
|
|
|
allOpenCardButton.gameObject.SetActive(false);
|
|
closeButton.gameObject.SetActive(true);
|
|
EventSystem.current.SetSelectedGameObject(closeButton.gameObject);
|
|
}
|
|
|
|
[Button("카드 모두 열기")]
|
|
public void AllOpenCard()
|
|
{
|
|
viewCardCount = 0;
|
|
//남은 카드가 열리는 연출만 추가하자
|
|
foreach (var element in _tycoonCards)
|
|
{
|
|
element.SetSelectAction(null);
|
|
element.Rotation_Start();
|
|
}
|
|
allOpenCardButton.gameObject.SetActive(false);
|
|
closeButton.gameObject.SetActive(true);
|
|
EventSystem.current.SetSelectedGameObject(closeButton.gameObject);
|
|
}
|
|
|
|
public void OnInteractionE(InputAction.CallbackContext context)
|
|
{
|
|
var current = EventSystem.current.currentSelectedGameObject;
|
|
if (!current) return;
|
|
|
|
var currenButton = current.GetComponent<Button>();
|
|
currenButton?.onClick?.Invoke();
|
|
}
|
|
}
|
|
} |