107 lines
4.0 KiB
C#
107 lines
4.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
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 Vector3 dealCardPlaceWorldPosition;
|
|
|
|
public struct AnimationMessage
|
|
{
|
|
public float MOVE_ANIMATION_DURATION;
|
|
public float DRAG_ANIMATION_DURATION;
|
|
}
|
|
|
|
private enum CardManagerStateEnum
|
|
{
|
|
WAIT,
|
|
DEAL_CARD,
|
|
DROP_CARD,
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (turnSingleton == null) turnSingleton = TurnManagerSingleton.Instance;
|
|
if (mainCamera == null) mainCamera = Camera.main;
|
|
}
|
|
|
|
private CardManagerStateEnum GetCardManagerState() => cardManagerState;
|
|
private void SetCardManagerState(CardManagerStateEnum state) => cardManagerState = state;
|
|
|
|
public void SetupBaseMessage(AnimationMessage animationMessage)
|
|
{
|
|
moveAnimationDuration = animationMessage.MOVE_ANIMATION_DURATION;
|
|
dragAnimationDuration = animationMessage.DRAG_ANIMATION_DURATION;
|
|
}
|
|
|
|
|
|
public Card DealOneCard(List<Card> cardDeckList)
|
|
{
|
|
if (turnSingleton.GetNeedDebugLog()) turnSingleton.PrintMsg("CardManager", "动作切换", "开始发牌");
|
|
SetCardManagerState(CardManagerStateEnum.DEAL_CARD);
|
|
Card takeOutCard = cardDeckList[UnityEngine.Random.Range(0, cardDeckList.Count)];
|
|
create_a_card(takeOutCard);
|
|
return takeOutCard;
|
|
}
|
|
|
|
private void create_a_card(Card takeOutCard)
|
|
{
|
|
Instantiate<GameObject>(takeOutCard.gameObject,
|
|
position: Vector3.zero,
|
|
rotation: Quaternion.identity,
|
|
parent: cardContainer);
|
|
takeOutCard.SetupCard();
|
|
handCardList.Add(takeOutCard);
|
|
ReadjustCardPosition(takeOutCard);
|
|
}
|
|
|
|
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; // 获取第一个卡牌的位置
|
|
MoveCard(firstCardPosition, cardSpacing, takeOutCard);
|
|
}
|
|
|
|
private void MoveCard(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);
|
|
takeOutCard.MoveCard(new Card.MoveMessage
|
|
{
|
|
GO_POSITION = cardWorldPosition,
|
|
GO_ROTATION = rotation,
|
|
GO_SCALE = Vector3.one,
|
|
GO_LAYER_SORT_ORDER = i + 1,
|
|
DURATION = moveAnimationDuration
|
|
});
|
|
}
|
|
}
|
|
} |