WXMC/proj/unity/Assets/Scripts/Wish/CardUIManager.cs
2024-12-04 16:18:46 +08:00

143 lines
4.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
public class CardUIManager : MonoBehaviour
{
[SerializeField]
private CanvasGroup canvasGroup;
[SerializeField]
private PuzzleCardItem m_cardItemPrefab;
[SerializeField]
private Transform m_cardItemParent;
[SerializeField]
private List<PuzzleCardAnimItem> m_cardList;
[SerializeField]
private ButtonInteraction m_selectBtn;
[SerializeField]
private Transform m_newCardParent;
[SerializeField]
private PuzzleTarotCardItem m_tarotCardItemPrefab;
[SerializeField]
private PuzzleTarotCardsConfig m_tarotCardList;
private List<PuzzleTarotCard> m_cardPool;
[SerializeField]
private PuzzleExplainUI PuzzleExplainUI;
private int currCardIndex;
private List<PuzzleCardItem> puzzleCardItemList;
public void Init(PuzzleDrawTarot PuzzleDrawTarot)
{
currCardIndex = 0;
gameObject.SetActive(true);
canvasGroup.alpha = 0f;
canvasGroup.DOFade(1f, 1f);
if(puzzleCardItemList==null)
{
puzzleCardItemList= new List<PuzzleCardItem>();
}
for(int j=0;j<puzzleCardItemList.Count;j++)
{
Destroy(puzzleCardItemList[j].gameObject);
}
puzzleCardItemList.Clear();
if(puzzleTarotCardItemList==null)
{
puzzleTarotCardItemList= new List<PuzzleTarotCardItem>();
}
for(int j=0;j<puzzleTarotCardItemList.Count;j++)
{
Destroy(puzzleTarotCardItemList[j].gameObject);
}
puzzleTarotCardItemList.Clear();
for(int j=0;j<PuzzleDrawTarot.DrawCount;j++)
{
GameObject go = Instantiate(m_cardItemPrefab.gameObject,m_cardItemParent);
PuzzleCardItem currItem = go.GetComponent<PuzzleCardItem>();
puzzleCardItemList.Add(currItem);
}
m_selectBtn.Init(SelectCard);
m_cardPool = new List<PuzzleTarotCard>(m_tarotCardList.Cards);
}
public void SelectCard()
{
if(currCardIndex>=puzzleCardItemList.Count)return;
PuzzleCardAnimItem currSelectItem = FindNearestImage();
currSelectItem.Show(false);
StartCoroutine(DelayShow(currSelectItem));
GameObject go = Instantiate(currSelectItem.gameObject,m_newCardParent);
go.transform.position = currSelectItem.transform.position;
PuzzleCardAnimItem newItem = go.GetComponent<PuzzleCardAnimItem>();
newItem.MoveTo(puzzleCardItemList[currCardIndex]);
StartCoroutine(DelayCreateTarotCardItem(newItem,currCardIndex));
currCardIndex++;
}
List<PuzzleTarotCardItem> puzzleTarotCardItemList;
IEnumerator DelayCreateTarotCardItem(PuzzleCardAnimItem newItem, int currIndex)
{
GameObject go = Instantiate(m_tarotCardItemPrefab.gameObject,m_newCardParent);
go.transform.position = puzzleCardItemList[currCardIndex].transform.position;
PuzzleTarotCardItem tarotCardItem = go.GetComponent<PuzzleTarotCardItem>();
PuzzleTarotCard randomTarotCard = GetTarotCard();
tarotCardItem.Init(randomTarotCard);
puzzleTarotCardItemList.Add(tarotCardItem);
m_cardPool.Remove(randomTarotCard);
go.transform.localEulerAngles = new Vector3(0,90,0);
yield return new WaitForSeconds(1f);
newItem.transform.DORotate(new Vector3(0,90,0),1f).SetEase(Ease.Linear);
yield return new WaitForSeconds(1f);
go.transform.DORotate(new Vector3(0,0,0),1f).SetEase(Ease.Linear);
yield return new WaitForSeconds(1f);
if(currIndex>=puzzleCardItemList.Count-1)
{
canvasGroup.DOFade(0f, 1f).OnComplete(()=>
{
gameObject.SetActive(false);
PuzzleExplainUI.Init(puzzleTarotCardItemList);
});
}
}
PuzzleTarotCard GetTarotCard()
{
int currIndex = Random.Range(0, m_cardPool.Count);
return m_cardPool[currIndex];
}
IEnumerator DelayShow(PuzzleCardAnimItem currSelectItem)
{
yield return new WaitForSeconds(2f);
currSelectItem.Show(true);
}
public PuzzleCardAnimItem FindNearestImage()
{
if (m_cardList.Count == 0)
{
Debug.LogError("Image列表为空");
return null;
}
PuzzleCardAnimItem nearestImage = m_cardList[0];
float minDistance = Mathf.Abs(nearestImage.GetComponent<RectTransform>().position.x - m_selectBtn.GetComponent<RectTransform>().position.x);
foreach (PuzzleCardAnimItem image in m_cardList)
{
float distance = Mathf.Abs(image.GetComponent<RectTransform>().position.x - m_selectBtn.GetComponent<RectTransform>().position.x);
if (distance < minDistance)
{
minDistance = distance;
nearestImage = image;
}
}
return nearestImage;
}
}