diff --git a/Assets/Script/MouseInput.cs b/Assets/Script/MouseInput.cs index 85566d5..8947c4a 100644 --- a/Assets/Script/MouseInput.cs +++ b/Assets/Script/MouseInput.cs @@ -5,6 +5,7 @@ using static UnityEngine.Physics2D; using static UnityEngine.Time; using static UnityEngine.Vector2; +using static UnityEngine.GameObject; public sealed class MouseInput : MonoBehaviour { @@ -17,8 +18,8 @@ public sealed class MouseInput : MonoBehaviour private void Start() { - _managerCard = FindObjectOfType(); Slot = gameObject; + _managerCard = FindObjectOfType(); _setTimes = _times; } @@ -44,6 +45,16 @@ private void Update() GetMouseClick(); } + public bool StackAble(GameObject selected) + { + var s1 = Slot.GetComponent(); + var s2 = selected.GetComponent(); + + return !s2.IsDeckPile && (s2.Top + ? (s1.Suit == s2.Suit || s1.Values is 1 && s2.Suit is null) && s1.Values == s2.Values + 1 + : s1.Values == s2.Values - 1 && s1.Suit is not "C" and not "S" == s2.Suit is not "C" and not "S"); + } + private void GetMouseClick() { if (GetMouseButtonDown(0)) @@ -78,11 +89,97 @@ private void GetMouseClick() } } - private void StackBots(GameObject selected) + private bool DoubleClick() => _times < _doubleClickTime && _clickCount is 2; + + private void AutoStack(GameObject selected) + { + for (var i = 0; i < _managerCard.PosTops.Length; i++) + { + var stack = _managerCard.PosTops[i].GetComponent(); + + if (selected.GetComponent().Values is 1 && stack.Values is 0) + { + Slot = selected; + StackCard(stack.gameObject); + break; + } + else if (stack.Suit == Slot.GetComponent().Suit && stack.Values == Slot.GetComponent().Values - 1 && HasNoChildren(Slot)) + { + Slot = selected; + + StackCard(Find(stack.Suit + stack.Values switch + { + 1 => "A", + 11 => "J", + 12 => "Q", + 13 => "K", + _ => stack.Values.ToString() + })); + + break; + } + } + } + + private bool HasNoChildren(GameObject card) => card.transform.childCount is 0; + + private void StackCard(GameObject selected) { var s1 = Slot.GetComponent(); var s2 = selected.GetComponent(); + var yPos = 0.3f; + var zPos = 0.01f; + + if (s2.Top || s1.Values is 13) + { + yPos = 0; + } + + Slot.transform.position = new Vector3(selected.transform.position.x, selected.transform.position.y - yPos, selected.transform.position.z - zPos); + Slot.transform.parent = selected.transform; + + if (s1.IsDeckPile) + { + _managerCard.TripsOnDisplay.Remove(Slot.name); + } + else if (s1.Top) + { + if (s2.Top && s1.Values is 1) + { + _managerCard.PosTops[s1.Row].GetComponent().Values = default; + _managerCard.PosTops[s1.Row].GetComponent().Suit = default; + } + else + { + _managerCard.PosTops[s1.Row].GetComponent().Values = s1.Values - 1; + } + } + else + { + _managerCard.Bots[s1.Row].Remove(Slot.name); + } + + s1.IsDeckPile = default; + s1.Row = s2.Row; + if (s2.Top) + { + _managerCard.PosTops[s1.Row].GetComponent().Values = s1.Values; + _managerCard.PosTops[s1.Row].GetComponent().Suit = s1.Suit; + s1.Top = true; + } + else + { + s1.Top = default; + } + + Slot = gameObject; + } + + private void StackBots(GameObject selected) + { + var s1 = Slot.GetComponent(); + var s2 = selected.GetComponent(); var yPos = 0.3f; var zPos = 0.01f;