Hand.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class Hand : MonoBehaviour
  6. {
  7. public int CurrentScore { get; private set; }
  8. public int CardNum
  9. {
  10. get
  11. {
  12. return this.m_TableCards.Count;
  13. }
  14. }
  15. private void Awake()
  16. {
  17. this.scoreOutput.gameObject.SetActive(false);
  18. this.ResetHand();
  19. }
  20. public GameObject DealCard(CardData card)
  21. {
  22. return this.InitialiseInShoe(card);
  23. }
  24. public void ResetScore()
  25. {
  26. this.CurrentScore = 0;
  27. this.UpdateScoreView();
  28. this.scoreOutput.gameObject.SetActive(false);
  29. }
  30. protected void ResetNextPosition()
  31. {
  32. this.nextTransformPosition = Vector3.zero;
  33. }
  34. public virtual void ResetPositionForSplit()
  35. {
  36. this.nextTransformPosition = this.m_CardOffset;
  37. }
  38. public virtual void ResetHand()
  39. {
  40. this.nextTransformPosition = Vector3.zero;
  41. if (this.m_TableCards != null && this.m_TableCards.Count > 0)
  42. {
  43. for (int i = this.m_TableCards.Count - 1; i >= 0; i--)
  44. {
  45. UnityEngine.Object.Destroy(this.m_TableCards[i]);
  46. this.m_TableCards.Remove(this.m_TableCards[i]);
  47. }
  48. }
  49. this.m_TableCards.Clear();
  50. this.m_HandCardData.Clear();
  51. this.ResetScore();
  52. }
  53. public bool IsBust()
  54. {
  55. return this.CurrentScore > 21;
  56. }
  57. protected GameObject InitialiseInShoe(CardData card)
  58. {
  59. GameObject cardObj = CardDeck.Instance.GetCardObj(card.CardSuit, card.CardRank);
  60. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(cardObj);
  61. gameObject.transform.SetParent(base.transform, false);
  62. gameObject.transform.localPosition = Vector3.zero;
  63. Vector3 position = BlackjackGame.Instance.DeckPos();
  64. position.y = gameObject.transform.position.y;
  65. gameObject.transform.position = position;
  66. this.LinkCardWithData(gameObject, card);
  67. this.AddCardToHand(gameObject);
  68. gameObject.SetActive(false);
  69. return gameObject;
  70. }
  71. protected void AddCardToHand(GameObject card)
  72. {
  73. this.SetupPosition(card);
  74. this.m_TableCards.Add(card);
  75. this.CalculateScore();
  76. }
  77. public void UpdateScoreView()
  78. {
  79. this.scoreOutput.gameObject.SetActive(this.CurrentScore > 0);
  80. this.scoreOutput.text = this.CurrentScore.ToString();
  81. if (this.CurrentScore > 21)
  82. {
  83. this.scoreOutput.color = Color.red;
  84. }
  85. else
  86. {
  87. this.scoreOutput.color = Color.white;
  88. }
  89. }
  90. protected void LinkCardWithData(GameObject cardGameObject, CardData cardData)
  91. {
  92. Card component = cardGameObject.GetComponent<Card>();
  93. component.CardData = cardData;
  94. if (!this.m_HandCardData.ContainsKey(cardGameObject))
  95. {
  96. this.m_HandCardData.Add(cardGameObject, component);
  97. }
  98. }
  99. protected void SetupPosition(GameObject cardGameObject)
  100. {
  101. this.m_HandCardData[cardGameObject].CardSet = base.transform;
  102. this.m_HandCardData[cardGameObject].EndPosition = base.transform.TransformPoint(this.nextTransformPosition);
  103. Card.CardState cardState = this.GetCardState();
  104. this.m_HandCardData[cardGameObject].CurrentState = cardState;
  105. this.nextTransformPosition += this.m_CardOffset;
  106. }
  107. public void CalculateScore()
  108. {
  109. this.CurrentScore = 0;
  110. int num = 0;
  111. foreach (GameObject gameObject in this.m_TableCards)
  112. {
  113. Card component = gameObject.GetComponent<Card>();
  114. if (component.CurrentState != Card.CardState.FlipDown)
  115. {
  116. if (component.CardData.CardRank != CardData.Rank.Ace)
  117. {
  118. this.CurrentScore += component.CardData.GetValue();
  119. }
  120. else
  121. {
  122. num++;
  123. }
  124. }
  125. }
  126. for (int i = 0; i < num; i++)
  127. {
  128. if (this.CurrentScore + 11 < 22)
  129. {
  130. this.CurrentScore += 11;
  131. }
  132. else
  133. {
  134. this.CurrentScore++;
  135. }
  136. }
  137. }
  138. protected virtual Card.CardState GetCardState()
  139. {
  140. return Card.CardState.FlipUp;
  141. }
  142. public bool IsNatural()
  143. {
  144. return this.m_TableCards.Count == 2;
  145. }
  146. public Text scoreOutput;
  147. [SerializeField]
  148. [Header("カード位置のオフセット")]
  149. protected Vector3 m_CardOffset = Vector3.zero;
  150. protected List<GameObject> m_TableCards = new List<GameObject>();
  151. protected Dictionary<GameObject, Card> m_HandCardData = new Dictionary<GameObject, Card>();
  152. private Vector3 nextTransformPosition;
  153. }