127 lines
5.1 KiB
C#
127 lines
5.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Splines;
|
|
using UnityEngine.UI;
|
|
|
|
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;
|
|
[SerializeField] private Collider2D areaCollider;
|
|
|
|
// ======== private ========
|
|
private Vector3 deal_card_point_world_position;
|
|
private TurnEventManagement turnManager;
|
|
private TurnSingleton turnSingleton;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
turnManager = GetComponent<TurnEventManagement>();
|
|
turnSingleton = TurnSingleton.Instance;
|
|
}
|
|
|
|
|
|
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(STRUCT_TURN_OS.LET_OS_DEAL_CARD args) // 系统发牌事件
|
|
{
|
|
List<REQUEST_METHODS> verificationList = new() { REQUEST_METHODS.POST };
|
|
if (!turnSingleton.VerificationRequestsMethods(args.METHOD, verificationList)) return;
|
|
if (!turnSingleton.VerificationActionStatus(turnManager.GetActionState(), TURN_ACTION_STATE.DEAL_REQUEST)) return;
|
|
if (!turnSingleton.VerificationSystemStatus(turnManager.GetState(), TURN_OS_STATE.ENTER_SELF_TURN)) return;
|
|
if (turnSingleton.GetNeedDebugLog()) print("系统发牌");
|
|
int handCardCount = args.DEFAULT_HAND_CARD_COUNT;
|
|
int deckCardCount = args.DECK_CARD_COUNT;
|
|
float move_duration = args.MOVE_ANIMATION_DURATION;
|
|
float drag_duration = args.DRAG_ANIMATION_DURATION;
|
|
turnManager.SetActionState(TURN_ACTION_STATE.DEAL_RUNNING);
|
|
turnManager.SetState(TURN_OS_STATE.STAY_SELF_TURN);
|
|
StartCoroutine(coroutine());
|
|
return;
|
|
|
|
IEnumerator coroutine()
|
|
{
|
|
for (var i = 0; i < handCardCount; i++)
|
|
{
|
|
create_a_card(move_duration, drag_duration);
|
|
yield return new WaitForSeconds(move_duration);
|
|
}
|
|
|
|
turnManager.SetActionState(TURN_ACTION_STATE.WAIT);
|
|
}
|
|
}
|
|
|
|
|
|
private void create_a_card(float move_duration, float drag_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());
|
|
cardComponent.SetTurnManager(turnManager);
|
|
cardComponent.SetHoverDuration(drag_duration);
|
|
cardComponent.SetCollider2D(areaCollider);
|
|
turnManager.AddHandCard(cardComponent);
|
|
readjust_card_card_position(move_duration);
|
|
}
|
|
|
|
private void readjust_card_card_position(float move_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 STRUCT_TURN_CARD.LET_CARD_DEAL_SELF
|
|
{
|
|
ID = i,
|
|
POSITION = card_world_position,
|
|
ROTATION = rotation,
|
|
DURATION = move_duration,
|
|
LAYER_SORT_ORDER = i + 1,
|
|
SCALE = Vector3.one,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
} |