-
-
Save Folling/d156e3ee5952ceac8245b51571224bea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using UnityEngine; | |
using UnityEngine.EventSystems; | |
using UnityEngine.UI; | |
namespace Assets.Scripts.Cards { | |
[RequireComponent(typeof(RectTransform), typeof(Image))] | |
public class Card : MonoBehaviour, | |
IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, | |
IBeginDragHandler, IDragHandler, IEndDragHandler { | |
public CardData data; | |
private Vector3 offset; | |
private Vector3 originalPosition; | |
public bool played; | |
public bool shouldMoveBack; | |
public void OnBeginDrag(PointerEventData eventData) { | |
} | |
public void OnDrag(PointerEventData eventData) { | |
if (played) return; | |
var newPos = Input.mousePosition + offset; | |
newPos.z = 1.0f; | |
transform.position = newPos; | |
} | |
public void OnEndDrag(PointerEventData eventData) { | |
Deck.stoppedDragging = true; | |
StartCoroutine(DisableStopppedDragging()); | |
StartCoroutine(MoveBack()); | |
} | |
public void OnPointerEnter(PointerEventData eventData) { | |
if (transform.position != originalPosition) return; | |
transform.localScale = new Vector3(1.1f, 1.1f, 1.1f); | |
} | |
public void OnPointerExit(PointerEventData eventData) { | |
if (transform.position != originalPosition) return; | |
transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); | |
} | |
public void OnPointerDown(PointerEventData eventData) { | |
// sets the appropriate offset of mouse and position | |
// so the object doesn't jump when it's first moved | |
if (played) return; | |
offset = transform.position - Input.mousePosition; | |
Deck.currentlySelected = this; | |
} | |
private void Awake() { | |
shouldMoveBack = true; | |
} | |
private void Start() { | |
originalPosition = transform.position; | |
} | |
// Update is called once per frame | |
private void Update() { | |
} | |
private static IEnumerator DisableStopppedDragging() { | |
yield return null; | |
Deck.stoppedDragging = false; | |
} | |
private IEnumerator MoveBack() { | |
// moves a card back to it's originally position | |
// so long it's supposed to (which it's not when placed in a row) | |
yield return new WaitForEndOfFrame(); | |
if (!shouldMoveBack) yield break; | |
while (transform.position != originalPosition) | |
transform.position = Vector3.MoveTowards(transform.position, originalPosition, | |
5000.0f); | |
transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment