Files
2025TapTapGameJam/Assets/Script/Gameplay/Player/PlayerCardsController.cs

114 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using Quaternion = UnityEngine.Quaternion;
using Vector3 = UnityEngine.Vector3;
namespace Gameplay.Player
{
public class PlayerCardsController : MonoBehaviour
{
[SerializeField] private float radius = 2f; // 卡牌围绕玩家旋转的半径
[SerializeField] private float rotationSpeed = 50f; // 卡牌旋转
[SerializeField] private float highOffset = 1f; // 卡牌高度偏移
public bool IsRotating = true; // 是否旋转卡牌
[SerializeField] private GameObject cardPrefab; // 卡牌预制体
public List<CardViewer> Cards;
private Transform playerTransform;
private void Update()
{
if (Cards != null)
{
//RotateCards();
}
}
// 生成卡牌实体
// 生成的卡牌实体围绕着玩家旋转
public void GenerateCards(List<Card> cards)
{
playerTransform = this.transform;
Cards = new List<CardViewer>();
for (int i = 0; i < cards.Count; i++)
{
float angle = i * (360f / cards.Count);
float rad = angle * Mathf.Deg2Rad;
Vector3 cardPosition = new Vector3(
playerTransform.position.x + radius * Mathf.Cos(rad),
playerTransform.position.y + highOffset,
playerTransform.position.z + radius * Mathf.Sin(rad)
);
GameObject cardObject = Instantiate(cardPrefab, cardPosition, Quaternion.identity);
Vector3 playerPosition = new Vector3(
playerTransform.position.x,
playerTransform.position.y + highOffset,
playerTransform.position.z
);
Cards[i].transform.LookAt(playerPosition);
CardViewer cardViewer = cardObject.GetComponent<CardViewer>();
cardViewer.SetCard(cards[i]);
Cards.Add(cardViewer);
}
}
// 旋转已经生成的卡牌实体
public void RotateCards()
{
if (!IsRotating) return;
for (int i = 0; i < Cards.Count; i++)
{
float angle = i * (360f / Cards.Count) + Time.time * rotationSpeed;
float rad = angle * Mathf.Deg2Rad;
Vector3 cardPosition = new Vector3(
playerTransform.position.x + radius * Mathf.Cos(rad),
playerTransform.position.y + highOffset,
playerTransform.position.z + radius * Mathf.Sin(rad)
);
Cards[i].transform.position = cardPosition;
Vector3 playerPosition = new Vector3(
playerTransform.position.x,
playerTransform.position.y + highOffset,
playerTransform.position.z
);
Cards[i].transform.LookAt(playerPosition);
// 卡牌绕着y轴旋转180度从而让卡牌正面朝向玩家
Cards[i].transform.Rotate(0, 180, 0);
}
}
public void StopRotatingCards()
{
IsRotating = false;
}
public void StartRotatingCards()
{
IsRotating = true;
}
// 删除卡牌实体
public void DeleteCards(CardViewer card)
{
Cards.Remove(card);
Destroy(card.gameObject);
}
public void ClearCards()
{
foreach (var card in Cards)
{
Destroy(card.gameObject);
}
Cards.Clear();
}
}
}