Files
Continentis/Assets/Scripts/MainGame/UI/CombatMainPage/HandPile.cs
SoulliesOfficial c09dae7783 彻底修好了
2025-11-13 10:42:05 -05:00

103 lines
4.0 KiB
C#

using System;
using UnityEngine;
namespace Continentis.MainGame.UI
{
public partial class HandPile : PileBase
{
public RectTransform dropZone;
public bool isUpdatingLayout = true; // 是否启用自动布局更新
public float arcAngle = 15; // 手牌的总弧度
public float cardSpacingBase = 100f; // 手牌间距
public float cardSpacingFactor = 500f; // 手牌间距调整因子
public float maxVerticalOffset = 50f; // 控制最外侧卡牌的垂直偏移
private void Update()
{
if (isUpdatingLayout)
{
UpdateHandLayout();
}
}
public Vector3 GetCardPosition(int index, int count)
{
if (count == 0 || index < 0)
return Vector2.zero;
float cardSpacing = cardSpacingBase + cardSpacingFactor / count;
// 计算中间索引,保证手牌居中排列
float midIndex = (count - 1) / 2f;
// 计算当前卡牌相对于中间卡牌的偏移(负数在左,正数在右)
float offset = index - midIndex;
// 水平位置:以 cardSpacing 为单位的偏移
float posX = offset * cardSpacing;
// 垂直位置:离中心越远,垂直偏移越大(这里做了个简单的线性映射,可根据需求调整)
float posY = -Mathf.Pow(Mathf.Abs(offset) / (midIndex == 0 ? 1 : midIndex), 2) * maxVerticalOffset;
return new Vector3(posX, posY, 0);
}
public Quaternion GetCardRotation(int index, int count)
{
if (count == 0 || index < 0)
return Quaternion.identity;
// 计算中间索引,保证手牌居中排列
float midIndex = (count - 1) / 2f;
// 计算当前卡牌相对于中间卡牌的偏移(负数在左,正数在右)
float offset = index - midIndex;
// 计算卡牌旋转角度:使卡牌略微倾斜,增加弧形效果
float rotationZ = -offset * (arcAngle / (midIndex == 0 ? 1 : midIndex));
return Quaternion.Euler(0, 0, rotationZ);
}
}
public partial class HandPile
{
private void UpdateHandLayout()
{
int count = cardViews.Count;
if (count == 0) return;
// 遍历所有卡牌
for (int i = 0; i < count; i++)
{
Vector3 targetPos = GetCardPosition(i, count);
Quaternion targetRot = GetCardRotation(i, count);
RectTransform cardRect = cardViews[i].cardTransform;
// 调用 CardUI 脚本中的方法,将卡牌移动到目标位置,并设置旋转角度
if (!cardViews[i].isSelecting)
{
if ((cardViews[i].isHovering || cardViews[i].isDuringPlaying) && CombatUIManager.Instance.selectingCardView == null)
{
targetPos.y = 0;
cardRect.localPosition =
Vector3.Lerp(cardRect.localPosition, targetPos + new Vector3(0, 150f, 0), Time.deltaTime * 20f);
cardRect.localRotation =
Quaternion.Lerp(cardRect.localRotation, Quaternion.identity, Time.deltaTime * 20f);
cardRect.localScale =
Vector3.Lerp(cardRect.localScale, Vector3.one * 1.2f, Time.deltaTime * 20f);
}
else
{
cardRect.localPosition = Vector3.Lerp(cardRect.localPosition, targetPos, Time.deltaTime * 20f);
cardRect.localRotation =
Quaternion.Lerp(cardRect.localRotation, targetRot, Time.deltaTime * 20f);
cardRect.localScale = Vector3.Lerp(cardRect.localScale, Vector3.one, Time.deltaTime * 20f);
}
}
}
}
}
}