118 lines
4.3 KiB
C#
118 lines
4.3 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 Vector3 deal_card_point_world_position;
|
|
private TurnEventManagement turnManager;
|
|
|
|
private void Awake()
|
|
{
|
|
turnManager = GetComponent<TurnEventManagement>();
|
|
}
|
|
|
|
|
|
private void Start()
|
|
{
|
|
if (mainCamera == null) mainCamera = Camera.main;
|
|
RectTransformUtility.ScreenPointToWorldPointInRectangle(
|
|
rect: dealCardPlace,
|
|
screenPoint: dealCardPlace.position,
|
|
cam: mainCamera,
|
|
out deal_card_point_world_position);
|
|
}
|
|
|
|
public void DealCard(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;
|
|
turnManager.SetState(EVENT_OS_STATE.DEAL_RUNNING);
|
|
StartCoroutine(coroutine());
|
|
return;
|
|
|
|
IEnumerator coroutine()
|
|
{
|
|
for (var i = 0; i < handCardCount; i++)
|
|
{
|
|
create_a_card(duration);
|
|
yield return new WaitForSeconds(duration);
|
|
}
|
|
|
|
turnManager.SetState(EVENT_OS_STATE.WAIT);
|
|
}
|
|
}
|
|
|
|
|
|
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_{turnManager.GetHandCardCount()}";
|
|
Card cardComponent = _card.GetComponent<Card>();
|
|
cardComponent.SetID(turnManager.GetHandCardCount());
|
|
turnManager.AddHandCard(cardComponent);
|
|
readjust_card_card_position(duration);
|
|
}
|
|
|
|
private void readjust_card_card_position(float duration) // 调整卡牌位置
|
|
{
|
|
int handCardListCount = turnManager.GetHandCardCount();
|
|
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);
|
|
turnManager.GetHandCard(i).DrawCard(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,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
} |