135 lines
5.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Splines;
public class CardManager : MonoBehaviour
{
[SerializeField] private Camera mainCamera;
// [SerializeField] private RectTransform dealCardPlace;
[SerializeField] private Transform cardContainer;
[SerializeField] private Collider2D areaCollider2D;
[SerializeField] private SplineContainer splineContainer;
private TurnManagerSingleton turnSingleton;
private CardManagerStateEnum cardManagerState;
private readonly List<Card> handCardList = new List<Card>();
private float moveAnimationDuration;
private float dragAnimationDuration;
private readonly Queue<Sequence> animationMoveCardToDeckQueue = new Queue<Sequence>();
private bool isAnimationPlaying;
private bool isSetup;
// private Vector3 dealCardPlaceWorldPosition;
private enum CardManagerStateEnum
{
WAIT,
DEAL_CARD,
DROP_CARD,
}
public void SetupCardManager()
{
if (isSetup) return;
if (turnSingleton == null) turnSingleton = TurnManagerSingleton.Instance;
if (mainCamera == null) mainCamera = Camera.main;
isSetup = true;
}
private CardManagerStateEnum GetCardManagerState() => cardManagerState;
private void SetCardManagerState(CardManagerStateEnum state) => cardManagerState = state;
public void PlayAnimationShuffleCardToDeck(List<Sequence> ownedCardTweenList)
{
if (turnSingleton.GetNeedDebugLog()) turnSingleton.PrintMsg("CardManager", "动作切换", "开始洗牌");
while (ownedCardTweenList.Count > 0)
{
int randomIndex = UnityEngine.Random.Range(0, ownedCardTweenList.Count);
Sequence moveDeckAnimation = ownedCardTweenList[randomIndex];
animationMoveCardToDeckQueue.Enqueue(moveDeckAnimation);
ownedCardTweenList.Remove(moveDeckAnimation);
}
PlayNextMoveOwnedCardToDeck();
}
private void PlayNextMoveOwnedCardToDeck()
{
if (animationMoveCardToDeckQueue.Count == 0)
{
if (turnSingleton.GetNeedDebugLog()) turnSingleton.PrintMsg("CardManager", "动作切换", "洗牌完成");
isAnimationPlaying = false;
TurnManagerEvent.CallTurnManagerStateEnterSelfTurn(); // 洗牌完成以后, 通知TurnManager进入自己的回合
return;
}
isAnimationPlaying = true;
Sequence seq = animationMoveCardToDeckQueue.Dequeue();
seq.OnComplete(PlayNextMoveOwnedCardToDeck);
seq.Play();
}
public List<Card> TakeFiveCard(List<Card> cardDeck, int count)
{
if (turnSingleton.GetNeedDebugLog()) turnSingleton.PrintMsg("CardManager", "动作切换", "开始抽卡");
for (int i = 0; i < count; i++)
{
Card takeOutCard = cardDeck[UnityEngine.Random.Range(0, cardDeck.Count)];
cardDeck.Remove(takeOutCard);
// ReadjustCardPosition(takeOutCard);
}
return cardDeck;
}
// private void ReadjustCardPosition(Card takeOutCard) // 重新调整卡牌位置
// {
// int handCardListCount = handCardList.Count;
// if (handCardListCount == 0) return;
// float cardSpacing = handCardListCount switch // 动态调整卡牌间距
// {
// <= 4 => 0.5f / (handCardListCount + 1),
// <= 5 => 0.75f / (handCardListCount + 1),
// _ => 1f / (handCardListCount + 1)
// };
// float firstCardPosition = 0.5f - (handCardListCount - 1) * cardSpacing / 2; // 获取第一个卡牌的位置
// GetMoveCardTween(firstCardPosition, cardSpacing, takeOutCard);
// }
//
// private void GetMoveCardTween(float firstCardPosition, float cardSpacing, Card takeOutCard)
// {
// Spline spline = splineContainer.Spline; // 获取曲线
// for (int i = 0; i < handCardList.Count; i++)
// {
// float position = firstCardPosition + i * cardSpacing;
// Vector3 cardWorldPosition = spline.EvaluatePosition(position); // 获取曲线指定位置的坐标
// cardWorldPosition += cardContainer.position;
// Vector3 forward = spline.EvaluateTangent(position); // 获取曲线指定的切线方向
// Vector3 up = spline.EvaluateUpVector(position); // 获取曲线指定的UP向量
// Vector3 upwards = Vector3.Cross(up, forward).normalized; // 切线和向上的夹角计算方向
// Quaternion rotation = Quaternion.LookRotation(up, upwards);
// Sequence seq = takeOutCard.GetTweenMoveCardGoHand(movePosition: cardWorldPosition, doRotation: rotation,
// doDuration: moveAnimationDuration, doLayerSortOrder: i + 1);
// animationQueue.Enqueue(seq);
// }
//
// PlayNextMoveCardToHand();
// }
//
// private void PlayNextMoveCardToHand()
// {
// if (animationQueue.Count == 0)
// {
// isAnimationPlaying = false;
// return;
// }
//
// isAnimationPlaying = true;
// Sequence seq = animationQueue.Dequeue();
// seq.AppendCallback(PlayNextMoveCardToHand);
// seq.Play();
// }
}