151 lines
5.7 KiB
C#
151 lines
5.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
using UnityEngine.Splines;
|
|
|
|
public class TurnCardManager : MonoBehaviour
|
|
{
|
|
[SerializeField] private Collider2D areaCollider2D;
|
|
[SerializeField] private SplineContainer splineContainer;
|
|
private Camera mainCamera;
|
|
private TurnManagerSingleton turnSingleton;
|
|
private CardManagerStateEnum cardManagerState;
|
|
private readonly List<Card> handCardList = new List<Card>();
|
|
private float moveAnimationDuration;
|
|
private float dragAnimationDuration;
|
|
private float dropAnimationDuration;
|
|
private Transform handCardContainer;
|
|
private bool isSetup;
|
|
private bool lockOneCardBeingUsed = false; // 是否锁定一个卡牌正在使用
|
|
private Vector3 dropCardWorldPosition;
|
|
private CardManagerStateEnum actionRecordState;
|
|
|
|
private enum CardManagerStateEnum
|
|
{
|
|
WAIT,
|
|
DEAL_CARD,
|
|
DROP_CARD,
|
|
DRAG_CARD,
|
|
}
|
|
|
|
public void SetupCardManager(float moveDuration, Transform parentContainer, float dragDuration, RectTransform dropCardUI,
|
|
float dropCardDuration)
|
|
{
|
|
if (isSetup) return;
|
|
if (turnSingleton == null) turnSingleton = TurnManagerSingleton.Instance;
|
|
if (mainCamera == null) mainCamera = Camera.main;
|
|
moveAnimationDuration = moveDuration;
|
|
handCardContainer = parentContainer;
|
|
dragAnimationDuration = dragDuration;
|
|
dropAnimationDuration = dropCardDuration;
|
|
RectTransformUtility.ScreenPointToWorldPointInRectangle(
|
|
rect: dropCardUI,
|
|
screenPoint: dropCardUI.position,
|
|
cam: mainCamera,
|
|
out dropCardWorldPosition);
|
|
isSetup = true;
|
|
}
|
|
|
|
public bool SetLockOneCardBeingUsed(bool isLock) => lockOneCardBeingUsed = isLock;
|
|
public bool GetLockOneCardBeingUsed() => lockOneCardBeingUsed;
|
|
|
|
private CardManagerStateEnum GetCardManagerState() => cardManagerState;
|
|
|
|
public bool GetCardManagerStateIsWait()
|
|
{
|
|
if (GetCardManagerState() != CardManagerStateEnum.WAIT)
|
|
{
|
|
turnSingleton.PrintErrorMsg("CardManager", "Error", $"CardManagerState = [{GetCardManagerState()}], Need State = [Wait]");
|
|
return false;
|
|
}
|
|
|
|
if (GetLockOneCardBeingUsed())
|
|
{
|
|
turnSingleton.PrintErrorMsg("CardManager", "Error", "One Card is being used");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
private void SetCardManagerState(CardManagerStateEnum state) => cardManagerState = state;
|
|
|
|
public void DropCardAllFromHand()
|
|
{
|
|
if (turnSingleton.GetNeedDebugLog()) turnSingleton.PrintMsg("CardManager", "动作切换", "开始弃牌");
|
|
StartCoroutine(coroutine());
|
|
return;
|
|
|
|
IEnumerator coroutine()
|
|
{
|
|
SetCardManagerState(CardManagerStateEnum.DROP_CARD);
|
|
foreach (var card in handCardList)
|
|
{
|
|
Sequence tweenAnimation = card.CardGotoDrop(dropCardWorldPosition, dropAnimationDuration);
|
|
yield return tweenAnimation.WaitForCompletion();
|
|
}
|
|
|
|
handCardList.Clear();
|
|
SetCardManagerState(CardManagerStateEnum.WAIT);
|
|
TurnManagerEvent.CallTurnManagerEnterEnemyTurn();
|
|
}
|
|
}
|
|
|
|
public void TakeCard(int number)
|
|
{
|
|
if (turnSingleton.GetNeedDebugLog()) turnSingleton.PrintMsg("CardManager", "动作切换", $"开始抽卡:{number}");
|
|
SetCardManagerState(CardManagerStateEnum.DEAL_CARD);
|
|
StartCoroutine(enumerator());
|
|
return;
|
|
|
|
IEnumerator enumerator()
|
|
{
|
|
for (int i = 0; i < number; i++)
|
|
{
|
|
Card card = turnSingleton.TakeOutOneCard();
|
|
card.SetAreaCollider2D(areaCollider2D, dragAnimationDuration);
|
|
handCardList.Add(card);
|
|
if (turnSingleton.GetNeedDebugLog()) print($"[CardManager][抽卡ID]: {card.GetID(isInstance: true)}");
|
|
ReadjustCardPosition();
|
|
yield return new WaitForSeconds(moveAnimationDuration);
|
|
}
|
|
|
|
SetCardManagerState(CardManagerStateEnum.WAIT);
|
|
TurnManagerEvent.CallTurnManagerStaySelfTurn();
|
|
}
|
|
}
|
|
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |