CapersProject/Assets/02.Scripts/Ui/Tycoon/TycoonCardArea.cs

248 lines
8.3 KiB
C#
Raw Normal View History

2024-11-12 09:33:07 +00:00
using System;
2024-10-03 07:55:56 +00:00
using System.Collections;
using BlueWater;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/*
*
* , .
*/
/*
*To do
*
* ( )
* (), , Card
*/
public class TycoonCardArea : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler ,IPointerMoveHandler
{
//Area 활성화 유무
private bool _enable = false;
public void SetEnable(bool val) { _enable = val; }
private Coroutine _currentRotationCoroutine;
private float _maxRotationAngle;
private float _returnSpeed;
2024-10-24 08:05:32 +00:00
private TycoonCard _tycoonCard; //부모
2024-10-03 07:55:56 +00:00
private float _returnSpeedTime = 0.0f; // 복원속도를 더할 메서드
private bool _isPointerInside; // 이미지 안에 마우스가 있는지 여부를 추적
private Coroutine _startRotationCoroutine;
private Coroutine _endRotationCoroutine;
private Image _image;
private RectTransform _rectTransform;
private Transform _cardComTransform;
private Camera _uiCamera;
2024-11-12 09:33:07 +00:00
private Action<TycoonCard> selectAction;
2024-10-03 07:55:56 +00:00
private void Awake()
{
2024-10-24 08:05:32 +00:00
_tycoonCard = transform.parent.GetComponent<TycoonCard>();
2024-10-03 07:55:56 +00:00
_rectTransform = this.GetComponent<RectTransform>();
_cardComTransform = transform.parent.Find("CardCom").GetComponent<Transform>();
_image = _cardComTransform.Find("Image").GetComponent<Image>();
2024-10-24 08:05:32 +00:00
_maxRotationAngle = _tycoonCard.maxRotationAngle;
_returnSpeed = _tycoonCard.returnSpeed;
2024-10-03 07:55:56 +00:00
}
void Start()
{
_uiCamera = TycoonCameraManager.Instance.MainCamera;
}
2024-11-12 09:33:07 +00:00
public void SetselectAction(Action<TycoonCard> action)
{
selectAction = action;
}
2024-10-03 07:55:56 +00:00
// 마우스가 이미지 위에 올라갔을 때 호출
public void OnPointerEnter(PointerEventData eventData)
{
if (_endRotationCoroutine != null)
{
StopCoroutine(_endRotationCoroutine);
_endRotationCoroutine = null;
}
if (_startRotationCoroutine != null)
{
StopCoroutine(_startRotationCoroutine);
_startRotationCoroutine = null;
}
if (_enable)
{
if (_currentRotationCoroutine != null)
{
StopCoroutine(_currentRotationCoroutine);
_currentRotationCoroutine = null;
}
_startRotationCoroutine = StartCoroutine(StartToRotation());
}
}
// 마우스가 이미지에서 벗어났을 때 호출
public void OnPointerExit(PointerEventData eventData)
{
if (_enable)
{
if (_endRotationCoroutine != null)
{
StopCoroutine(_endRotationCoroutine);
_endRotationCoroutine = null;
}
if (_startRotationCoroutine != null)
{
StopCoroutine(_startRotationCoroutine);
_startRotationCoroutine = null;
}
}
if (_enable)
{
_isPointerInside = false;
2024-11-12 11:25:00 +00:00
_endRotationCoroutine = StartCoroutine(ReturnToZeroRotation());
2024-10-03 07:55:56 +00:00
}
}
// 마우스 클릭 시 호출
public void OnPointerClick(PointerEventData eventData)
{
if (_enable)
{
2024-11-12 09:33:07 +00:00
//_tycoonCard.transform.parent.parent.GetComponent<TycoonSelectCard>().SelectedCard(_tycoonCard);
if (selectAction != null)
{
selectAction?.Invoke(_tycoonCard);
2024-11-12 11:25:00 +00:00
OnPointerExit(null);
2024-11-12 09:33:07 +00:00
}
2024-10-03 07:55:56 +00:00
//해당 밑줄은 따로 메소드를 만들어주자... 여기서 호출하는게 아니라 SelectCardUi에서 호출받는 방식으로...
this.SetEnable(false);
_isPointerInside = false;
}
}
public void OnPointerMove(PointerEventData eventData)
{
if (_isPointerInside)
{
RotateCard();
}
}
// ReSharper disable Unity.PerformanceAnalysis
private IEnumerator StartToRotation()
{
if (_endRotationCoroutine != null)
{
StopCoroutine(_endRotationCoroutine);
_endRotationCoroutine = null;
}
while (_returnSpeedTime < 1.0f)
{
_returnSpeedTime += Time.unscaledDeltaTime * _returnSpeed;
RotateCard();
yield return null;
}
_isPointerInside = true;
_returnSpeedTime = 1.0f;
}
// ReSharper disable Unity.PerformanceAnalysis
private IEnumerator ReturnToZeroRotation()
{
if (_startRotationCoroutine != null)
{
StopCoroutine(_startRotationCoroutine);
_startRotationCoroutine = null;
}
Quaternion startRotation = _cardComTransform.GetComponent<RectTransform>().localRotation;
2024-11-12 09:33:07 +00:00
Quaternion startRotationImg = _image.GetComponent<RectTransform>().localRotation;
2024-10-03 07:55:56 +00:00
Quaternion endRotation = Quaternion.identity;
Vector3 initialScale = _cardComTransform.localScale;
Vector3 targetScale = new Vector3(0.95f,0.95f,0.95f);
while (_returnSpeedTime > 0.0f)
{
_cardComTransform.GetComponent<RectTransform>().localRotation = Quaternion.Slerp(startRotation, endRotation, 1.0f - _returnSpeedTime);
2024-11-12 09:33:07 +00:00
_image.GetComponent<RectTransform>().localRotation = Quaternion.Slerp(startRotationImg, endRotation, 1.0f - _returnSpeedTime);
2024-10-03 07:55:56 +00:00
_returnSpeedTime -= Time.unscaledDeltaTime * _returnSpeed;
float t = (1.0f - _returnSpeedTime);
t = 1 - Mathf.Pow(2, -10 * t);
2024-10-07 06:43:01 +00:00
_cardComTransform.localScale = Vector3.Lerp(initialScale, targetScale, t);
2024-10-03 07:55:56 +00:00
yield return null;
}
_returnSpeedTime = 0.0f;
_cardComTransform.GetComponent<RectTransform>().localRotation = Quaternion.identity;
2024-11-12 09:33:07 +00:00
_image.GetComponent<RectTransform>().localRotation = Quaternion.identity;
2024-10-03 07:55:56 +00:00
_endRotationCoroutine = null;
}
private void RotateCard()
{
Vector3 initialScale = _cardComTransform.localScale;
Vector3 targetScale = new Vector3(1.05f, 1.05f, 1.0f); // 타겟 스케일 설정
// 마우스 포인터의 화면 좌표(eventData.position)를 이미지의 로컬 좌표(localPoint)로 변환
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(_rectTransform, Input.mousePosition, _uiCamera, out var localPoint))
{
// RectTransform의 중심을 기준으로 정규화된 값으로 변환
Vector2 normalizedPoint = new Vector2(
(localPoint.x / _rectTransform.rect.width) + 0.5f,
(localPoint.y / _rectTransform.rect.height) + 0.5f
);
// 좌표를 중심 기준으로 (-0.5, -0.5)에서 (0.5, 0.5)로 변환 (이미지 중앙이 0,0이 되도록)
Vector2 centeredNormalizedPoint = normalizedPoint - new Vector2(0.5f, 0.5f);
// Debug.Log($"Normalized Point: {centeredNormalizedPoint}");
// X와 Y축의 회전 각도를 마우스 위치에 따라 계산 (최대 회전 각도를 15도로 제한)
float rotationX = Mathf.Clamp(-centeredNormalizedPoint.y * _maxRotationAngle * 2 * _returnSpeedTime, -_maxRotationAngle, _maxRotationAngle);
float rotationY = Mathf.Clamp(centeredNormalizedPoint.x * _maxRotationAngle * 2 * _returnSpeedTime, -_maxRotationAngle, _maxRotationAngle);
// 회전을 적용 (X축은 위아래 기울기, Y축은 좌우 기울기)
_cardComTransform.GetComponent<RectTransform>().localRotation = Quaternion.Euler(rotationX, rotationY, 0f);
_image.GetComponent<RectTransform>().localRotation = Quaternion.Euler(-rotationX, 0f, 0f);
}
// 스케일 보간 (Lerp)
float t = Mathf.Clamp01(_returnSpeedTime);
float easedT = 1 - Mathf.Pow(2, -10 * t);
// 스케일을 Lerp 함수를 사용하여 보간
_cardComTransform.localScale = Vector3.Lerp(initialScale, targetScale, easedT);
}
}