From 9492cd04a91abb42b0b2eb672fd9aa6e33d96fba Mon Sep 17 00:00:00 2001 From: Yami An Date: Wed, 1 Nov 2023 23:42:38 +0700 Subject: [PATCH] 231101 --- Assets/Script/ManagerCard.cs | 50 ++------ Assets/Script/ManagerGame.cs | 1 - Assets/Script/MouseInput.cs | 223 ++++------------------------------- Assets/Script/PosCardUp.cs | 13 +- Assets/Script/Selectable.cs | 1 - 5 files changed, 35 insertions(+), 253 deletions(-) diff --git a/Assets/Script/ManagerCard.cs b/Assets/Script/ManagerCard.cs index 6d03e42..6eed437 100644 --- a/Assets/Script/ManagerCard.cs +++ b/Assets/Script/ManagerCard.cs @@ -7,36 +7,15 @@ public sealed class ManagerCard : MonoBehaviour { + public Sprite[] FaceCard; public GameObject[] PosTops; public GameObject[] PosBots; - public Sprite[] FaceCard; public GameObject CardPrefab; public GameObject DeckButton; - public static string[] SetCard = new string[] - { - "H", - "D", - "C", - "S" - }; + public static string[] SetCard = new string[] { "H", "D", "C", "S" }; - public static string[] Values = new string[] - { - "A", - "2", - "3", - "4", - "5", - "6", - "7", - "8", - "9", - "10", - "J", - "Q", - "K" - }; + public static string[] Values = new string[] { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; public List> DeckTrips = new(); public List[] Tops; @@ -55,16 +34,7 @@ public sealed class ManagerCard : MonoBehaviour private int _trips; private int _tripsRemainder; - private void Start() => Bots = new List[] - { - _bot0, - _bot1, - _bot2, - _bot3, - _bot4, - _bot5, - _bot6 - }; + private void Start() => Bots = new List[] { _bot0, _bot1, _bot2, _bot3, _bot4, _bot5, _bot6 }; public void DealCard() { @@ -96,7 +66,7 @@ public void ShuffleCard(List list) public static List GenerateDeckCard() => SetCard.SelectMany(x => Values.Select(y => x + y)).ToList(); - public void DealFromDeck() + public IEnumerator DealFromDeck() { foreach (Transform nameCard in DeckButton.transform) { @@ -115,13 +85,15 @@ public void DealFromDeck() var xPos = 6f; var zPos = 0f; - DeckTrips[_deckLocation].ForEach(x => + foreach (var item in DeckTrips[_deckLocation]) { - TripsOnDisplay.Add(x); + yield return new WaitForSeconds(0.03f); + + TripsOnDisplay.Add(item); var newTopCard = Instantiate(CardPrefab, new Vector3(DeckButton.transform.position.x + xPos, DeckButton.transform.position.y, DeckButton.transform.position.z + zPos), identity, DeckButton.transform); - newTopCard.name = x; + newTopCard.name = item; var s = newTopCard.GetComponent(); @@ -129,7 +101,7 @@ public void DealFromDeck() s.IsDeckPile = true; xPos += 1f; zPos -= 0.1f; - }); + } _deckLocation++; } diff --git a/Assets/Script/ManagerGame.cs b/Assets/Script/ManagerGame.cs index 539b552..af837e3 100644 --- a/Assets/Script/ManagerGame.cs +++ b/Assets/Script/ManagerGame.cs @@ -71,7 +71,6 @@ private void SetYourTime(float time) if (yourTime > time || yourTime <= 0) { SetFloat("time", time); - _ = GetYourTime(); } } diff --git a/Assets/Script/MouseInput.cs b/Assets/Script/MouseInput.cs index 0a77ed5..381788f 100644 --- a/Assets/Script/MouseInput.cs +++ b/Assets/Script/MouseInput.cs @@ -1,7 +1,6 @@ -using System.Linq; +using System.Collections; using UnityEngine; using static UnityEngine.Camera; -using static UnityEngine.GameObject; using static UnityEngine.Input; using static UnityEngine.Physics2D; using static UnityEngine.Time; @@ -10,9 +9,6 @@ public sealed class MouseInput : MonoBehaviour { public GameObject Slot; - private const float _yPos = 0.9f; - private const float _zPos = 0.01f; - private readonly float _doubleClickTime = 0.5f; private ManagerCard _managerCard; private float _times; private float _setTimes; @@ -44,7 +40,7 @@ private void Update() _clickCount = 1; } - GetMouseClick(); + _ = StartCoroutine(GetMouseClick()); } public bool StackAble(GameObject selected) @@ -52,10 +48,10 @@ 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"); + return !s2.IsDeckPile && s1.Values == s2.Values - 1 && s1.Suit is "D" or "H" != s2.Suit is "D" or "H"; } - private void GetMouseClick() + private IEnumerator GetMouseClick() { if (GetMouseButtonDown(default)) { @@ -63,11 +59,10 @@ private void GetMouseClick() if (hit) { - var s1 = Slot.GetComponent(); - if (hit.collider.CompareTag("Deck")) { - _managerCard.DealFromDeck(); + yield return _managerCard.DealFromDeck(); + Slot = gameObject; } else if (hit.collider.CompareTag("Card")) @@ -75,223 +70,45 @@ private void GetMouseClick() var selected = hit.collider.gameObject; var s2 = selected.GetComponent(); - if (!Blocked(selected)) + if (s2.FaceUp) { - if (!s2.FaceUp) + if (s2.IsDeckPile || Slot == gameObject) { - s2.FaceUp = true; - Slot = gameObject; - - if (Slot == selected && DoubleClick()) + Slot = selected; + } + else if (Slot != selected) + { + if (StackAble(selected)) { - AutoStack(selected); + Stack(selected, false); } else { Slot = selected; } } - else if (s2.IsDeckPile) - { - Slot = selected; - } - } - - if (Slot == gameObject) - { - Slot = selected; - } - else if (Slot != selected) - { - if (StackAble(selected)) - { - StackCard(selected); - } - else - { - Slot = selected; - } } } - else if (hit.collider.CompareTag("PosTop") && Slot.CompareTag("Card") && s1.FaceUp) + else if ((hit.collider.CompareTag("PosTop") || hit.collider.CompareTag("PosBot")) && Slot.CompareTag("Card")) { - for (var i = 1; i <= 13; i++) - { - if (s1.Values == i) - { - StackPos(hit.collider.gameObject); - break; - } - } - } - else if (hit.collider.CompareTag("PosBot") && Slot.CompareTag("Card") && s1.FaceUp) - { - for (var i = 1; i <= 13; i++) - { - if (s1.Values == i) - { - StackPos(hit.collider.gameObject); - break; - } - } + Stack(hit.collider.gameObject, true); } } } } - 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(); - var s1 = Slot.GetComponent(); - var s2 = selected.GetComponent(); - - if (s2.Values is 1 && stack.Values is 0) - { - Slot = selected; - StackCard(stack.gameObject); - break; - } - else if (stack.Suit == s1.Suit && stack.Values == s1.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) + private void Stack(GameObject selected, bool isPos) { var s1 = Slot.GetComponent(); var s2 = selected.GetComponent(); - - var yPos = _yPos; - var zPos = _zPos; - - if (s2.Top || s1.Values is 13) - { - yPos = default; - } + var yPos = isPos ? 0f : 0.9f; + var zPos = 0.01f; 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 ? _managerCard.TripsOnDisplay.Remove(Slot.name) : _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 StackPos(GameObject selected) - { - var s1 = Slot.GetComponent(); - var s2 = selected.GetComponent(); - - var yPos = _yPos; - var zPos = _zPos; - - for (var i = 0; i <= 13; i++) - { - if (s2.Top || s1.Values == i) - { - yPos = default; - break; - } - } - - 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 bool Blocked(GameObject selected) - { - var s2 = selected.GetComponent(); - - return s2.IsDeckPile ? s2.name != _managerCard.TripsOnDisplay.LastOrDefault() : s2.name != _managerCard.Bots[s2.Row].LastOrDefault(); - } } diff --git a/Assets/Script/PosCardUp.cs b/Assets/Script/PosCardUp.cs index 404693d..419f5a5 100644 --- a/Assets/Script/PosCardUp.cs +++ b/Assets/Script/PosCardUp.cs @@ -10,17 +10,12 @@ private void Update() { Card.Clear(); - foreach (Transform item in GlupCard.transform) - { - Card.Add(item); - } + var cardCount = GlupCard.transform.childCount; - if (Card.Count > 0) + for (var i = 0; i < cardCount; i++) { - for (var i = 0; i < Card.Count; i++) - { - Card[i].GetComponent().FaceUp = i == Card.Count - 1; - } + Card.Add(GlupCard.transform.GetChild(i)); + Card[i].GetComponent().FaceUp = i == cardCount - 1; } } } diff --git a/Assets/Script/Selectable.cs b/Assets/Script/Selectable.cs index ff3061c..4a94a90 100644 --- a/Assets/Script/Selectable.cs +++ b/Assets/Script/Selectable.cs @@ -4,7 +4,6 @@ public sealed class Selectable : MonoBehaviour { public bool FaceUp = default; - public bool Top = default; public bool IsDeckPile = default; public int Values; public int Row;