117 lines
3.1 KiB
C#
117 lines
3.1 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UIElements;
|
|
using DG.Tweening;
|
|
using UnityEngine.Serialization;
|
|
|
|
public class Card : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler, IPointerUpHandler, IPointerDownHandler
|
|
{
|
|
[FormerlySerializedAs("moveSpeedLimit")]
|
|
[Header("Movement")]
|
|
[SerializeField] private float moveDuration = .25f;
|
|
[Header("States")]
|
|
[SerializeField] private bool isHovering;
|
|
[SerializeField] public bool IsDragging;
|
|
[Header("Slots")]
|
|
[SerializeField] private GameObject slot;
|
|
|
|
[SerializeField] private CardArea parentArea;
|
|
[SerializeField] public CardArea HoveringArea;
|
|
|
|
public event Action PointerEnter;
|
|
public event Action PointerExit;
|
|
|
|
private Vector3 _grabOffset;
|
|
|
|
private void Update()
|
|
{
|
|
// ClampPosition();
|
|
|
|
if (IsDragging)
|
|
{
|
|
Vector2 targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - _grabOffset;
|
|
MoveTo(targetPosition);
|
|
}
|
|
}
|
|
|
|
// Sets the slot
|
|
public void SetSlot(GameObject slot)
|
|
{
|
|
this.slot = slot;
|
|
}
|
|
|
|
public void SetParentArea(CardArea cardArea)
|
|
{
|
|
parentArea = cardArea;
|
|
}
|
|
|
|
private void ClampPosition()
|
|
{
|
|
Vector2 screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
|
|
Vector3 clampedPosition = transform.position;
|
|
clampedPosition.x = Mathf.Clamp(clampedPosition.x, -screenBounds.x, screenBounds.x);
|
|
clampedPosition.y = Mathf.Clamp(clampedPosition.y, -screenBounds.y, screenBounds.y);
|
|
transform.position = new Vector3(clampedPosition.x, clampedPosition.y, 0);
|
|
}
|
|
|
|
private void MoveTo(Vector2 targetPosition)
|
|
{
|
|
transform.position = targetPosition;
|
|
}
|
|
|
|
private void SmoothMoveTo(Vector2 targetPosition)
|
|
{
|
|
transform.DOMove(targetPosition, moveDuration);
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
|
|
}
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
_grabOffset = mousePosition - (Vector2)transform.position;
|
|
IsDragging = true;
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
IsDragging = false;
|
|
if (HoveringArea != null)
|
|
{
|
|
parentArea.RemoveCardFromArea(this);
|
|
SetParentArea(HoveringArea);
|
|
HoveringArea.AddCardToArea(this);
|
|
HoveringArea = null;
|
|
}
|
|
MoveToSlot();
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
PointerEnter?.Invoke();
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
PointerExit?.Invoke();
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
|
|
}
|
|
|
|
public void MoveToSlot()
|
|
{
|
|
SmoothMoveTo(slot.transform.position);
|
|
}
|
|
}
|