Skip to content

Commit

Permalink
231101
Browse files Browse the repository at this point in the history
  • Loading branch information
Tynab committed Nov 1, 2023
1 parent 67308ce commit 9492cd0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 253 deletions.
50 changes: 11 additions & 39 deletions Assets/Script/ManagerCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<string>> DeckTrips = new();
public List<string>[] Tops;
Expand All @@ -55,16 +34,7 @@ public sealed class ManagerCard : MonoBehaviour
private int _trips;
private int _tripsRemainder;

private void Start() => Bots = new List<string>[]
{
_bot0,
_bot1,
_bot2,
_bot3,
_bot4,
_bot5,
_bot6
};
private void Start() => Bots = new List<string>[] { _bot0, _bot1, _bot2, _bot3, _bot4, _bot5, _bot6 };

public void DealCard()
{
Expand Down Expand Up @@ -96,7 +66,7 @@ public void ShuffleCard(List<string> list)

public static List<string> GenerateDeckCard() => SetCard.SelectMany(x => Values.Select(y => x + y)).ToList();

public void DealFromDeck()
public IEnumerator DealFromDeck()
{
foreach (Transform nameCard in DeckButton.transform)
{
Expand All @@ -115,21 +85,23 @@ 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<Selectable>();

s.FaceUp = true;
s.IsDeckPile = true;
xPos += 1f;
zPos -= 0.1f;
});
}

_deckLocation++;
}
Expand Down
1 change: 0 additions & 1 deletion Assets/Script/ManagerGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ private void SetYourTime(float time)
if (yourTime > time || yourTime <= 0)
{
SetFloat("time", time);
_ = GetYourTime();
}
}

Expand Down
223 changes: 20 additions & 203 deletions Assets/Script/MouseInput.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -44,254 +40,75 @@ private void Update()
_clickCount = 1;
}

GetMouseClick();
_ = StartCoroutine(GetMouseClick());
}

public bool StackAble(GameObject selected)
{
var s1 = Slot.GetComponent<Selectable>();
var s2 = selected.GetComponent<Selectable>();

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))
{
var hit = Raycast(main.ScreenToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, 1f)), zero);

if (hit)
{
var s1 = Slot.GetComponent<Selectable>();

if (hit.collider.CompareTag("Deck"))
{
_managerCard.DealFromDeck();
yield return _managerCard.DealFromDeck();

Slot = gameObject;
}
else if (hit.collider.CompareTag("Card"))
{
var selected = hit.collider.gameObject;
var s2 = selected.GetComponent<Selectable>();

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<Selectable>();
var s1 = Slot.GetComponent<Selectable>();
var s2 = selected.GetComponent<Selectable>();

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<Selectable>();
var s2 = selected.GetComponent<Selectable>();

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<Selectable>().Values = default;
_managerCard.PosTops[s1.Row].GetComponent<Selectable>().Suit = default;
}
else
{
_managerCard.PosTops[s1.Row].GetComponent<Selectable>().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<Selectable>().Values = s1.Values;
_managerCard.PosTops[s1.Row].GetComponent<Selectable>().Suit = s1.Suit;
s1.Top = true;
}
else
{
s1.Top = default;
}

Slot = gameObject;
}

private void StackPos(GameObject selected)
{
var s1 = Slot.GetComponent<Selectable>();
var s2 = selected.GetComponent<Selectable>();

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<Selectable>().Values = default;
_managerCard.PosTops[s1.Row].GetComponent<Selectable>().Suit = default;
}
else
{
_managerCard.PosTops[s1.Row].GetComponent<Selectable>().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<Selectable>().Values = s1.Values;
_managerCard.PosTops[s1.Row].GetComponent<Selectable>().Suit = s1.Suit;
s1.Top = true;
}
else
{
s1.Top = default;
}

Slot = gameObject;
}

private bool Blocked(GameObject selected)
{
var s2 = selected.GetComponent<Selectable>();

return s2.IsDeckPile ? s2.name != _managerCard.TripsOnDisplay.LastOrDefault() : s2.name != _managerCard.Bots[s2.Row].LastOrDefault();
}
}
Loading

0 comments on commit 9492cd0

Please sign in to comment.