using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Splines; public class TurnHandlerDealCard : MonoBehaviour { // ======== serializeField ======== public List cardPrefabs; public SplineContainer splineContainer; public RectTransform dealCardPlace; public Camera mainCamera; public Transform cardContainer; // ======== private ======== private readonly List handCardList = new(); private Vector3 deal_card_point_world_position; private CombatScenarioEventOS combatScenarioEventOS; private void Awake() { combatScenarioEventOS = 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() // 系统开始乙方回合 { // TODO: 增加洗牌消息监听 combatScenarioEventOS.EVENT_REGISTER( EventData.EVENT_REGISTER_CARD_ENUM.EVENT_LET_OS_DEAL_CARD, callback); return; void callback(EventStruct.STRUCT_EVENT_DEAL_CARD args) { int handCardCount = args.HAND_CARD_COUNT; int deckCardCount = args.DECK_CARD_COUNT; float duration = args.DURATION; combatScenarioEventOS.LOCK_APPLY(EventLock.LOCK_EXCLUSIVE_CARD_ENUM.LOCK_OS_SHUFFLE_CARD); // 系统发牌加锁 StartCoroutine(coroutine()); return; IEnumerator coroutine() { for (var i = 0; i < handCardCount; i++) { create_a_card(duration); yield return new WaitForSeconds(duration); } combatScenarioEventOS.LOCK_RELEASE(EventLock.LOCK_EXCLUSIVE_CARD_ENUM.LOCK_OS_SHUFFLE_CARD); // 系统发牌解锁 } } } private void create_a_card(float duration) // 创建卡牌实例 { GameObject _card = Instantiate( 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(); 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); combatScenarioEventOS.EVENT_TRIGGER(EventData.EVENT_REGISTER_CARD_ENUM.EVENT_LET_CARD_DRAW_CARD, new EventStruct.STRUCT_EVENT_DRAW_CARD { ID = i, POSITION = card_world_position, ROTATION = rotation, DURATION = duration, LAYER_SORT_ORDER = i + 1, SCALE = Vector3.one, }); } } } }