2025-11-18 17:43:30 +08:00

126 lines
4.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Splines;
public class TurnHandlerDealCard : MonoBehaviour
{
// ======== serializeField ========
[SerializeField] private List<GameObject> cardPrefabs;
[SerializeField] private SplineContainer splineContainer;
[SerializeField] private RectTransform dealCardPlace;
[SerializeField] private Camera mainCamera;
[SerializeField] private Transform cardContainer;
// ======== private ========
private readonly List<Card> handCardList = new();
private Vector3 deal_card_point_world_position;
private CombatScenarioEventOS EventOS;
private void Awake()
{
EventOS = CombatScenarioEventOS.Instance;
REGISTER_EVENT_deal_card();
}
private void Start()
{
if (mainCamera == null) mainCamera = Camera.main;
RectTransformUtility.ScreenPointToWorldPointInRectangle(
rect: dealCardPlace,
screenPoint: dealCardPlace.position,
cam: mainCamera,
out deal_card_point_world_position);
}
private void REGISTER_EVENT_deal_card() // 系统发牌事件
{
EventOS.EVENT_REGISTER<EVENT_STRUCT.STRUCT_EVENT_OS_DEAL_CARD>(EVENT_ENUM.EVENT_LET_OS_DEAL_CARD, callback);
return;
void callback(EVENT_STRUCT.STRUCT_EVENT_OS_DEAL_CARD args)
{
if (args.METHOD != EVENT_METHODS.POST)
{
Debug.LogError($"消息回调失败: [{args.GetType()}], 不允许的消息类型: {args.METHOD}");
return;
}
print("系统发牌");
int handCardCount = args.HAND_CARD_COUNT;
int deckCardCount = args.DECK_CARD_COUNT;
float duration = args.DURATION;
EventOS.LOCK_APPLY(EVENT_EXCLUSIVE_LOCK.LOCK_OS_DEAL_CARD); // 系统发牌加锁
StartCoroutine(coroutine());
return;
IEnumerator coroutine()
{
for (var i = 0; i < handCardCount; i++)
{
create_a_card(duration);
yield return new WaitForSeconds(duration);
}
EventOS.LOCK_RELEASE(EVENT_EXCLUSIVE_LOCK.LOCK_OS_DEAL_CARD); // 系统发牌解锁
}
}
}
private void create_a_card(float duration) // 创建卡牌实例
{
GameObject _card = Instantiate<GameObject>(
cardPrefabs[UnityEngine.Random.Range(0, cardPrefabs.Count)],
position: deal_card_point_world_position,
rotation: Quaternion.identity,
parent: cardContainer);
_card.gameObject.name = $"Card_{handCardList.Count}";
Card cardComponent = _card.GetComponent<Card>();
cardComponent.SetID(handCardList.Count);
handCardList.Add(cardComponent);
readjust_card_card_position(duration);
}
private void readjust_card_card_position(float duration) // 调整卡牌位置
{
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; // 获取曲线
draw_card();
return;
void draw_card()
{
for (int i = 0; i < handCardListCount; i++)
{
float position = firstCardPosition + i * cardSpacing;
Vector3 card_world_position = spline.EvaluatePosition(position); // 获取曲线指定位置的坐标
card_world_position += 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);
EventOS.EVENT_DISPATCH(EVENT_ENUM.EVENT_LET_CARD_DRAW_CARD, new EVENT_STRUCT.STRUCT_EVENT_CARD_DRAW_CARD
{
ID = i,
POSITION = card_world_position,
ROTATION = rotation,
DURATION = duration,
LAYER_SORT_ORDER = i + 1,
SCALE = Vector3.one,
});
}
}
}
}