SwipeField.cs 5.28 KB
Newer Older
Joram's avatar
Joram committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;

public enum SwipeDirection
{
	None = 0,

	Up = 1,
	Right = 2,
	Down = 4,
	Left = 8,

	UpLeft = Up | Left,
	UpRight = Up | Right,
	DownLeft = Down | Left,
	DownRight = Down | Right,
}

[System.Serializable]
public class OnCardEvent : UnityEvent<SwipeArea, SwipeCard> {}

public class SwipeField : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
	[Header("Cards")]
	public List<SwipeCard> Cards;
	public SwipeCard ActiveCard { get { return Cards.Count > 0? Cards[0] : null; }}

	[Header("Deck")]
	public SwipeArea DeckArea;
	public Transform DeckCardParent;
	Vector2 InitCardPosition { get { return DeckArea.transform.position; }}

	[Header("SwipeAreas")]
	public SwipeArea[] Areas;

	[Header("Events")]
	public OnCardEvent OnUserEndSwipe;
	public OnCardEvent OnCardSwiped;

	Vector2 _dragPosition;
	SwipeDirection _direction;
	Vector2 _cardOffset;

	void Awake()
	{
		SortCards();
	}

	void SortCards()
	{
		var copyCards = new List<SwipeCard>(Cards);
		Cards.Clear();

		foreach (var card in copyCards)
			AddCardToDeck(card);
	}

	public void RemoveCardFromDeck(SwipeCard card)
	{
		Cards.Remove(card);
		card.SwipeField = null;
	}

	public void AddCardToDeck(SwipeCard card)
	{
		if (Cards.Contains(card))
			Cards.Remove(card);
		Cards.Add(card);
		card.transform.SetParent(DeckArea.transform);
		card.transform.SetAsFirstSibling();
		card.SwipeField = this;
	}

	public void ShuffleCards()
	{
		for (int i = 0; i < 50; i++)
		{
			var a = Random.Range(0, Cards.Count);
			var b = Random.Range(0, Cards.Count);

			var card1 = Cards[a];
			Cards[a] = Cards[b];
			Cards[b] = card1;
		}

		foreach (var card in Cards)
			card.transform.SetAsFirstSibling();
	}

	public void SwipeCardBeckToDeck(SwipeCard card)
	{
		StartSwipeCoroutine(card, DeckArea, InitCardPosition);
	}

	public void SwipeCardAway(SwipeArea area, SwipeCard card)
	{
		RemoveCardFromDeck(card);
		StartSwipeCoroutine(card, area, InitCardPosition);
	}

	public void OnBeginDrag(PointerEventData data)
	{
		_cardOffset = InitCardPosition - data.position;
		_direction = SwipeDirection.None;

		CalculateDrag(data);
	}
	
	public void OnDrag(PointerEventData data)
	{
		CalculateDrag(data);

		ActiveCard.transform.position = _dragPosition + _cardOffset;
	}

	public void OnEndDrag(PointerEventData data)
	{
		CalculateDrag(data);

		TriggerSwipe(_direction);
	}

	SwipeArea GetArea(SwipeDirection dir)
	{
		SwipeArea foundArea = null;
		foreach (var area in Areas)
		{
			if (area.Direction == dir)
			{
				foundArea = area;
				break;
			}
		}

		if (foundArea == null)
			return DeckArea;
		return foundArea;
	}

	public void TriggerSwipe(SwipeDirection dir)
	{
		var area = GetArea(dir);
		
		var card = ActiveCard;
		OnUserEndSwipe.Invoke(area, card);
		area.OnUserEndSwipe.Invoke(area, card);
	}

	void CalculateDrag(PointerEventData data)
	{
		if (RectTransformUtility.RectangleContainsScreenPoint(DeckArea.AreaTransform, data.position + _cardOffset))
			_direction = SwipeDirection.None;

		var originPos = DeckArea.transform.position;
		foreach (var area in Areas)
		{
			if (!area.AreaTransform.gameObject.activeInHierarchy)
				continue;
			
			if (RectTransformUtility.RectangleContainsScreenPoint(area.AreaTransform, data.position + _cardOffset))
				_direction = area.Direction;
		}
		_dragPosition = data.position;
	}

	void StartSwipeCoroutine(SwipeCard card, SwipeArea area, Vector2 initCardPosition)
	{
		StartCoroutine(SwipeCoroutine(card, area, initCardPosition));
	} 

	IEnumerator SwipeCoroutine(SwipeCard card, SwipeArea area, Vector2 initCardPosition)
	{
		var cardPos = card.transform.position;
		Vector2 swipeTarget = area.SwipeTarget.transform.position;

		Vector2 cardDir = (Vector2)cardPos - initCardPosition;

		Vector2 swipeEnd = Vector2.zero;
		if (area.Direction != SwipeDirection.None)
		{
			// get the local up or down direction it needs to hit
			Vector2 swipeNormal = cardDir;
			if (area.Direction == SwipeDirection.Up || area.Direction == SwipeDirection.Down)
				swipeNormal.y = 0;
			else
				swipeNormal.x = 0;
			swipeNormal.Normalize();
			
			// intersect point
			Vector3 intersectionPoint = Vector3.zero;
			bool foundIntersect = Math3d.LineLineIntersection(out intersectionPoint, cardPos, cardDir, swipeTarget, swipeNormal);

			swipeEnd = foundIntersect? (Vector2)intersectionPoint : swipeTarget;
		}
		else
			swipeEnd = swipeTarget;


		// find the procent it has already done of its path
		var swipeEndLength = (swipeEnd - initCardPosition).magnitude;
		var doneCardLength = cardDir.magnitude;
		

		// set loop values
		Vector2 from = Vector2.zero;
		Vector2 to = Vector2.zero;
		float f = 0;
		if (doneCardLength <= swipeEndLength)
		{
			f = doneCardLength / swipeEndLength;
			from = initCardPosition;
			to = swipeEnd;
		}
		else
		{
			to = swipeEnd;
			from = cardPos;
		}

		// move for the rest of the time
		for (; f <= 1.0f; f += Time.deltaTime * 6)
		{
			var pos = Vector2.Lerp(from, to, f);
			card.transform.position = pos;
			yield return null;
		}

		// trigger
		card.transform.position = to;
		OnCardSwiped.Invoke(area, card);
		area.OnCardSwiped.Invoke(area, card);

		// var c = ActiveCard;
		// ActiveCard = SecondCard;
		// c.transform.SetAsFirstSibling();
		// SecondCard = c;
	}
}