using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using DG.Tweening; using UnityEngine; using UnityEngine.Splines; public class CardManager : MonoBehaviour { [SerializeField] private Camera mainCamera; // [SerializeField] private RectTransform dealCardPlace; [SerializeField] private Collider2D areaCollider2D; [SerializeField] private SplineContainer splineContainer; private TurnManagerSingleton turnSingleton; private CardManagerStateEnum cardManagerState; private readonly List handCardList = new List(); private float moveAnimationDuration; private float dragAnimationDuration; private Transform handCardContainer; private bool isSetup; // private Vector3 dealCardPlaceWorldPosition; private enum CardManagerStateEnum { WAIT, DEAL_CARD, DROP_CARD, } public void SetupCardManager(float moveDuration, Transform parentContainer) { if (isSetup) return; if (turnSingleton == null) turnSingleton = TurnManagerSingleton.Instance; if (mainCamera == null) mainCamera = Camera.main; moveAnimationDuration = moveDuration; handCardContainer = parentContainer; isSetup = true; } private CardManagerStateEnum GetCardManagerState() => cardManagerState; private void SetCardManagerState(CardManagerStateEnum state) => cardManagerState = state; public void TakeCard(int number) { if (turnSingleton.GetNeedDebugLog()) turnSingleton.PrintMsg("CardManager", "动作切换", $"开始抽卡:{number}"); StartCoroutine(enumerator()); return; IEnumerator enumerator() { for (int i = 0; i < number; i++) { handCardList.Add(turnSingleton.TakeOutOneCard()); if (turnSingleton.GetNeedDebugLog()) print($"[CardManager][抽卡ID]: {handCardList[i].GetID(isInstance: true)}"); ReadjustCardPosition(); yield return new WaitForSeconds(moveAnimationDuration); } } } private void ReadjustCardPosition() // 重新调整卡牌位置 { 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; // 获取第一个卡牌的位置 Spline spline = splineContainer.Spline; // 获取曲线 for (int i = 0; i < handCardList.Count; i++) { float position = firstCardPosition + i * cardSpacing; Vector3 cardWorldPosition = spline.EvaluatePosition(position); // 获取曲线指定位置的坐标 cardWorldPosition += handCardContainer.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); Card handCard = handCardList[i]; handCard.GetTweenMoveCardGoHand(movePosition: cardWorldPosition, doRotation: rotation, doDuration: moveAnimationDuration, doLayerSortOrder: i + 1, cardContainer: handCardContainer); } } }