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

267 lines
9.2 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 UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/*
*
* , .
*/
/*
*To do
*
* ( )
* (), , Card
*/
2024-11-17 04:29:57 +00:00
namespace BlueWater.Uis
2024-10-03 07:55:56 +00:00
{
2024-11-17 04:29:57 +00:00
public class TycoonCardArea : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler,
IPointerMoveHandler
2024-10-03 07:55:56 +00:00
{
2024-11-17 04:29:57 +00:00
private Coroutine _currentRotationCoroutine;
2024-10-03 07:55:56 +00:00
2024-11-17 04:29:57 +00:00
private float _maxRotationAngle;
private float _returnSpeed;
2024-10-03 07:55:56 +00:00
2024-11-17 04:29:57 +00:00
private TycoonCard _tycoonCard; //부모
private float _returnSpeedTime = 0.0f; // 복원속도를 더할 메서드
private bool _isPointerInside; // 이미지 안에 마우스가 있는지 여부를 추적
private Coroutine _startRotationCoroutine;
private Coroutine _endRotationCoroutine;
2024-11-12 09:33:07 +00:00
2024-11-17 04:29:57 +00:00
private Image _cardImage;
private RectTransform _rectTransform;
private Transform _panel;
private Camera _uiCamera;
//Area 활성화 유무
private bool _enable = false;
private Action<TycoonCard> _onSelectAction;
private void Awake()
{
_tycoonCard = transform.parent.GetComponent<TycoonCard>();
_rectTransform = GetComponent<RectTransform>();
_panel = _tycoonCard.Panel;
_cardImage = _tycoonCard.CardImage;
_maxRotationAngle = _tycoonCard.maxRotationAngle;
_returnSpeed = _tycoonCard.returnSpeed;
}
private void Start()
{
_uiCamera = TycoonCameraManager.Instance.UiCamera;
}
public void SetEnable(bool val)
{
_enable = val;
}
public void SetselectAction(Action<TycoonCard> action)
{
_onSelectAction = action;
}
// 마우스가 이미지 위에 올라갔을 때 호출
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;
_endRotationCoroutine = StartCoroutine(ReturnToZeroRotation());
}
}
// 마우스 클릭 시 호출
public void OnPointerClick(PointerEventData eventData)
{
if (_enable)
{
//_tycoonCard.transform.parent.parent.GetComponent<TycoonSelectCard>().SelectedCard(_tycoonCard);
if (_onSelectAction != null)
{
_onSelectAction?.Invoke(_tycoonCard);
//OnPointerExit(null);
}
//해당 밑줄은 따로 메소드를 만들어주자... 여기서 호출하는게 아니라 SelectCardUi에서 호출받는 방식으로...
//this.SetEnable(false);
//_isPointerInside = false;
}
}
public void OnPointerMove(PointerEventData eventData)
{
if (_isPointerInside)
{
RotateCard();
}
}
public void SuccessClick()
{
if (!_enable) return;
2024-10-03 07:55:56 +00:00
2024-11-17 04:29:57 +00:00
OnPointerExit(null);
SetEnable(false);
_isPointerInside = false;
}
2024-10-03 07:55:56 +00:00
2024-11-17 04:29:57 +00:00
// ReSharper disable Unity.PerformanceAnalysis
private IEnumerator StartToRotation()
2024-10-03 07:55:56 +00:00
{
2024-11-17 04:29:57 +00:00
if (_endRotationCoroutine != null)
{
StopCoroutine(_endRotationCoroutine);
_endRotationCoroutine = null;
}
2024-10-03 07:55:56 +00:00
2024-11-17 04:29:57 +00:00
while (_returnSpeedTime < 1.0f)
{
_returnSpeedTime += Time.unscaledDeltaTime * _returnSpeed;
2024-10-03 07:55:56 +00:00
2024-11-17 04:29:57 +00:00
RotateCard();
2024-10-03 07:55:56 +00:00
2024-11-17 04:29:57 +00:00
yield return null;
}
2024-10-03 07:55:56 +00:00
2024-11-17 04:29:57 +00:00
_isPointerInside = true;
_returnSpeedTime = 1.0f;
2024-10-03 07:55:56 +00:00
}
2024-11-17 04:29:57 +00:00
// ReSharper disable Unity.PerformanceAnalysis
private IEnumerator ReturnToZeroRotation()
{
if (_startRotationCoroutine != null)
{
StopCoroutine(_startRotationCoroutine);
_startRotationCoroutine = null;
}
Quaternion startRotation = _panel.GetComponent<RectTransform>().localRotation;
Quaternion startRotationImg = _cardImage.GetComponent<RectTransform>().localRotation;
Quaternion endRotation = Quaternion.identity;
Vector3 initialScale = _panel.localScale;
Vector3 targetScale = new Vector3(0.95f, 0.95f, 0.95f);
while (_returnSpeedTime > 0.0f)
{
_panel.GetComponent<RectTransform>().localRotation =
Quaternion.Slerp(startRotation, endRotation, 1.0f - _returnSpeedTime);
_cardImage.GetComponent<RectTransform>().localRotation =
Quaternion.Slerp(startRotationImg, endRotation, 1.0f - _returnSpeedTime);
_returnSpeedTime -= Time.unscaledDeltaTime * _returnSpeed;
float t = (1.0f - _returnSpeedTime);
t = 1 - Mathf.Pow(2, -10 * t);
_panel.localScale = Vector3.Lerp(initialScale, targetScale, t);
yield return null;
}
_returnSpeedTime = 0.0f;
_panel.GetComponent<RectTransform>().localRotation = Quaternion.identity;
_cardImage.GetComponent<RectTransform>().localRotation = Quaternion.identity;
_endRotationCoroutine = null;
}
private void RotateCard()
{
Vector3 initialScale = _panel.localScale;
Vector3 targetScale = new Vector3(1.05f, 1.05f, 1.0f); // 타겟 스케일 설정
2024-12-02 11:40:40 +00:00
/*
2024-12-02 11:28:46 +00:00
// RectTransform의 화면 좌표를 가져오기 위한 변수
RectTransform rectTransform = GetComponent<RectTransform>();
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, null, out var localPoint))
2024-11-17 04:29:57 +00:00
{
2024-12-02 11:28:46 +00:00
// RectTransform의 크기를 기준으로 좌표를 정규화
2024-11-17 04:29:57 +00:00
Vector2 normalizedPoint = new Vector2(
2024-12-02 11:28:46 +00:00
localPoint.x / rectTransform.rect.width,
localPoint.y / rectTransform.rect.height
2024-11-17 04:29:57 +00:00
);
2024-12-02 11:28:46 +00:00
// 좌표를 중심 기준으로 (-0.5, -0.5)에서 (0.5, 0.5)로 변환
Vector2 centeredNormalizedPoint = normalizedPoint * 2f;
2024-11-17 04:29:57 +00:00
2024-12-02 11:28:46 +00:00
// Debug.Log($"Normalized Point: {centeredNormalizedPoint}");
2024-11-17 04:29:57 +00:00
// 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);
2024-12-02 11:28:46 +00:00
Debug.Log($"Normalized Point: {rotationX} x {rotationY}");
2024-11-17 04:29:57 +00:00
// 회전을 적용 (X축은 위아래 기울기, Y축은 좌우 기울기)
_panel.GetComponent<RectTransform>().localRotation =
Quaternion.Euler(rotationX, rotationY, 0f);
_cardImage.GetComponent<RectTransform>().localRotation = Quaternion.Euler(-rotationX, 0f, 0f);
}
2024-12-02 11:40:40 +00:00
*/
2024-11-17 04:29:57 +00:00
// 스케일 보간 (Lerp)
float t = Mathf.Clamp01(_returnSpeedTime);
float easedT = 1 - Mathf.Pow(2, -10 * t);
// 스케일을 Lerp 함수를 사용하여 보간
_panel.localScale = Vector3.Lerp(initialScale, targetScale, easedT);
}
2024-10-03 07:55:56 +00:00
}
}