2025-10-28 20:27:59 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using DG.Tweening;
|
2025-11-12 18:08:24 +08:00
|
|
|
using TMPro.EditorUtilities;
|
2025-10-28 20:27:59 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Splines;
|
|
|
|
|
|
|
|
|
|
public class CardManager : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
// ======== serializeField ========
|
|
|
|
|
[SerializeField] private int maxCardNumber = 10;
|
|
|
|
|
[SerializeField] private List<GameObject> cardPrefabs;
|
|
|
|
|
[SerializeField] private SplineContainer splineContainer;
|
2025-11-12 18:08:24 +08:00
|
|
|
[SerializeField] private RectTransform dealCardPlace;
|
|
|
|
|
[SerializeField] private RectTransform dropCardPlace;
|
|
|
|
|
[SerializeField] private Camera mainCamera;
|
|
|
|
|
[SerializeField] private Transform cardContainer;
|
2025-10-28 20:27:59 +08:00
|
|
|
|
|
|
|
|
// ======== private ========
|
2025-11-12 18:08:24 +08:00
|
|
|
// private readonly List<CardHandler> hand_card_list = new List<CardHandler>();
|
|
|
|
|
private readonly List<Card> card_list = new List<Card>();
|
|
|
|
|
private Vector3 deal_card_point_world_position;
|
|
|
|
|
private Vector3 drop_card_point_world_position;
|
|
|
|
|
private const float card_dotween_duration = 0.25f;
|
2025-10-28 20:27:59 +08:00
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
2025-11-12 18:08:24 +08:00
|
|
|
RectTransformUtility.ScreenPointToWorldPointInRectangle(
|
|
|
|
|
rect: dealCardPlace,
|
|
|
|
|
screenPoint: dealCardPlace.position,
|
|
|
|
|
cam: mainCamera,
|
|
|
|
|
out deal_card_point_world_position);
|
|
|
|
|
RectTransformUtility.ScreenPointToWorldPointInRectangle(
|
|
|
|
|
rect: dropCardPlace,
|
|
|
|
|
screenPoint: dropCardPlace.position,
|
|
|
|
|
cam: mainCamera,
|
|
|
|
|
out drop_card_point_world_position);
|
2025-10-28 20:27:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.Space))
|
|
|
|
|
{
|
2025-11-12 18:35:43 +08:00
|
|
|
if (CardOS.Instance.LOCK_GET(CardOSData.LOCK_EXCLUSIVE_CARD_ENUM.SORRY_PLEASE_LOCK_I_DRAG_CARD))
|
2025-11-12 18:08:24 +08:00
|
|
|
{
|
2025-11-12 18:35:43 +08:00
|
|
|
print($"状态已被锁定: {CardOSData.LOCK_EXCLUSIVE_CARD_ENUM.SORRY_PLEASE_LOCK_I_DRAG_CARD}");
|
2025-11-12 18:08:24 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (card_list.Count >= maxCardNumber) return;
|
|
|
|
|
this.m_self_create_card();
|
|
|
|
|
}
|
|
|
|
|
else if (Input.GetKeyDown(KeyCode.R))
|
|
|
|
|
{
|
|
|
|
|
// this.m_self_drop_all_card();
|
2025-10-28 20:27:59 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-12 18:08:24 +08:00
|
|
|
// private void m_self_drop_all_card()
|
|
|
|
|
// {
|
|
|
|
|
// foreach (CardHandler card_handler in hand_card_list)
|
|
|
|
|
// {
|
|
|
|
|
// card_handler.m_recv_drop_a_card(drop_card_point_world_position, card_dotween_duration - 0.1f);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// hand_card_list.Clear();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
private void m_self_create_card()
|
2025-10-28 20:27:59 +08:00
|
|
|
{
|
|
|
|
|
GameObject _card = Instantiate<GameObject>(
|
|
|
|
|
cardPrefabs[UnityEngine.Random.Range(0, cardPrefabs.Count)],
|
2025-11-12 18:08:24 +08:00
|
|
|
position: deal_card_point_world_position,
|
|
|
|
|
rotation: Quaternion.identity,
|
|
|
|
|
parent: cardContainer);
|
|
|
|
|
_card.gameObject.name = $"Card_{card_list.Count}";
|
|
|
|
|
Card _card_handler = _card.GetComponent<Card>();
|
|
|
|
|
_card_handler.ID = card_list.Count;
|
|
|
|
|
card_list.Add(_card_handler);
|
2025-10-28 20:27:59 +08:00
|
|
|
this.m_self_update_card_position();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void m_self_update_card_position()
|
|
|
|
|
{
|
2025-11-12 18:08:24 +08:00
|
|
|
int hand_card_list_count = card_list.Count;
|
2025-10-28 20:27:59 +08:00
|
|
|
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); // 获取曲线指定位置的坐标
|
2025-11-12 18:08:24 +08:00
|
|
|
card_world_position += cardContainer.position;
|
2025-10-28 20:27:59 +08:00
|
|
|
Vector3 forward = spline.EvaluateTangent(position); // 获取曲线指定的切线方向
|
|
|
|
|
Vector3 up = spline.EvaluateUpVector(position); // 获取曲线指定的UP向量
|
|
|
|
|
Vector3 upwards = Vector3.Cross(up, forward).normalized; // 切线和向上的夹角计算方向
|
|
|
|
|
Quaternion rotation = Quaternion.LookRotation(up, upwards);
|
2025-11-12 18:35:43 +08:00
|
|
|
CardOS.Instance.EVENT_TRIGGER(CardOSData.EVENT_REGISTER_CARD_ENUM.EXCUSE_ME_PLEASE_LET_ME_DRAW_CARD,
|
|
|
|
|
new CardOSData.STRUCT_EVENT_DRAW_CARD
|
2025-11-12 18:08:24 +08:00
|
|
|
{
|
|
|
|
|
ID = i,
|
|
|
|
|
POSITION = card_world_position,
|
|
|
|
|
ROTATION = rotation,
|
|
|
|
|
DURATION = card_dotween_duration,
|
|
|
|
|
LAYER_SORT_ORDER = i + 1,
|
|
|
|
|
SCALE = Vector3.one,
|
|
|
|
|
});
|
2025-10-28 20:27:59 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|