75 lines
2.9 KiB
C#
75 lines
2.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
using UnityEngine.Splines;
|
|
|
|
public class CardManager : MonoBehaviour
|
|
{
|
|
// ======== serializeField ========
|
|
[SerializeField] private int maxCardNumber = 10;
|
|
[SerializeField] private List<GameObject> cardPrefabs;
|
|
[SerializeField] private SplineContainer splineContainer;
|
|
[SerializeField] private Transform dealCardPoint;
|
|
|
|
// ======== private ========
|
|
private readonly List<GameObject> hand_card_list = new List<GameObject>();
|
|
private const string card_container_name = "CardContainer";
|
|
private Transform card_container;
|
|
|
|
private void Start()
|
|
{
|
|
card_container = transform.Find(card_container_name);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.Space))
|
|
{
|
|
if (hand_card_list.Count >= maxCardNumber) return;
|
|
m_self_draw_card();
|
|
}
|
|
}
|
|
|
|
private void m_self_draw_card()
|
|
{
|
|
GameObject _card = Instantiate<GameObject>(
|
|
cardPrefabs[UnityEngine.Random.Range(0, cardPrefabs.Count)],
|
|
position: dealCardPoint.position,
|
|
rotation: dealCardPoint.rotation,
|
|
parent: card_container);
|
|
_card.GetComponent<SpriteRenderer>().sortingOrder = hand_card_list.Count + 1;
|
|
hand_card_list.Add(_card);
|
|
this.m_self_update_card_position();
|
|
}
|
|
|
|
private void m_self_update_card_position()
|
|
{
|
|
int hand_card_list_count = hand_card_list.Count;
|
|
if (hand_card_list_count == 0) return;
|
|
// 卡牌间距
|
|
float card_spacing = hand_card_list_count switch
|
|
{
|
|
<= 4 => 0.5f / (hand_card_list_count + 1),
|
|
<= 5 => 0.75f / (hand_card_list_count + 1),
|
|
_ => 1f / (hand_card_list_count + 1)
|
|
};
|
|
|
|
float first_card_position = 0.5f - (hand_card_list_count - 1) * card_spacing / 2; // 获取第一个卡牌的位置
|
|
Spline spline = splineContainer.Spline; // 获取曲线
|
|
for (int i = 0; i < hand_card_list_count; i++)
|
|
{
|
|
float position = first_card_position + i * card_spacing;
|
|
Vector3 card_world_position = spline.EvaluatePosition(position); // 获取曲线指定位置的坐标
|
|
card_world_position += card_container.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);
|
|
// // 卡牌抽出的动画
|
|
hand_card_list[i].transform.DOMove(card_world_position, 0.25f);
|
|
hand_card_list[i].transform.DORotateQuaternion(rotation, 0.25f);
|
|
}
|
|
}
|
|
} |