123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class Hand : MonoBehaviour
- {
- public int CurrentScore { get; private set; }
- public int CardNum
- {
- get
- {
- return this.m_TableCards.Count;
- }
- }
- private void Awake()
- {
- this.scoreOutput.gameObject.SetActive(false);
- this.ResetHand();
- }
- public GameObject DealCard(CardData card)
- {
- return this.InitialiseInShoe(card);
- }
- public void ResetScore()
- {
- this.CurrentScore = 0;
- this.UpdateScoreView();
- this.scoreOutput.gameObject.SetActive(false);
- }
- protected void ResetNextPosition()
- {
- this.nextTransformPosition = Vector3.zero;
- }
- public virtual void ResetPositionForSplit()
- {
- this.nextTransformPosition = this.m_CardOffset;
- }
- public virtual void ResetHand()
- {
- this.nextTransformPosition = Vector3.zero;
- if (this.m_TableCards != null && this.m_TableCards.Count > 0)
- {
- for (int i = this.m_TableCards.Count - 1; i >= 0; i--)
- {
- UnityEngine.Object.Destroy(this.m_TableCards[i]);
- this.m_TableCards.Remove(this.m_TableCards[i]);
- }
- }
- this.m_TableCards.Clear();
- this.m_HandCardData.Clear();
- this.ResetScore();
- }
- public bool IsBust()
- {
- return this.CurrentScore > 21;
- }
- protected GameObject InitialiseInShoe(CardData card)
- {
- GameObject cardObj = CardDeck.Instance.GetCardObj(card.CardSuit, card.CardRank);
- GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(cardObj);
- gameObject.transform.SetParent(base.transform, false);
- gameObject.transform.localPosition = Vector3.zero;
- Vector3 position = BlackjackGame.Instance.DeckPos();
- position.y = gameObject.transform.position.y;
- gameObject.transform.position = position;
- this.LinkCardWithData(gameObject, card);
- this.AddCardToHand(gameObject);
- gameObject.SetActive(false);
- return gameObject;
- }
- protected void AddCardToHand(GameObject card)
- {
- this.SetupPosition(card);
- this.m_TableCards.Add(card);
- this.CalculateScore();
- }
- public void UpdateScoreView()
- {
- this.scoreOutput.gameObject.SetActive(this.CurrentScore > 0);
- this.scoreOutput.text = this.CurrentScore.ToString();
- if (this.CurrentScore > 21)
- {
- this.scoreOutput.color = Color.red;
- }
- else
- {
- this.scoreOutput.color = Color.white;
- }
- }
- protected void LinkCardWithData(GameObject cardGameObject, CardData cardData)
- {
- Card component = cardGameObject.GetComponent<Card>();
- component.CardData = cardData;
- if (!this.m_HandCardData.ContainsKey(cardGameObject))
- {
- this.m_HandCardData.Add(cardGameObject, component);
- }
- }
- protected void SetupPosition(GameObject cardGameObject)
- {
- this.m_HandCardData[cardGameObject].CardSet = base.transform;
- this.m_HandCardData[cardGameObject].EndPosition = base.transform.TransformPoint(this.nextTransformPosition);
- Card.CardState cardState = this.GetCardState();
- this.m_HandCardData[cardGameObject].CurrentState = cardState;
- this.nextTransformPosition += this.m_CardOffset;
- }
- public void CalculateScore()
- {
- this.CurrentScore = 0;
- int num = 0;
- foreach (GameObject gameObject in this.m_TableCards)
- {
- Card component = gameObject.GetComponent<Card>();
- if (component.CurrentState != Card.CardState.FlipDown)
- {
- if (component.CardData.CardRank != CardData.Rank.Ace)
- {
- this.CurrentScore += component.CardData.GetValue();
- }
- else
- {
- num++;
- }
- }
- }
- for (int i = 0; i < num; i++)
- {
- if (this.CurrentScore + 11 < 22)
- {
- this.CurrentScore += 11;
- }
- else
- {
- this.CurrentScore++;
- }
- }
- }
- protected virtual Card.CardState GetCardState()
- {
- return Card.CardState.FlipUp;
- }
- public bool IsNatural()
- {
- return this.m_TableCards.Count == 2;
- }
- public Text scoreOutput;
- [SerializeField]
- [Header("カード位置のオフセット")]
- protected Vector3 m_CardOffset = Vector3.zero;
- protected List<GameObject> m_TableCards = new List<GameObject>();
- protected Dictionary<GameObject, Card> m_HandCardData = new Dictionary<GameObject, Card>();
- private Vector3 nextTransformPosition;
- }
|