2026-06-19 UI, UI로직
This commit is contained in:
199
Assets/My project/Blackjeck Scripts/UI/BlackjackUITestDriver.cs
Normal file
199
Assets/My project/Blackjeck Scripts/UI/BlackjackUITestDriver.cs
Normal file
@@ -0,0 +1,199 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class BlackjackGameManager : MonoBehaviour
|
||||
{
|
||||
[Header("UI")]
|
||||
[SerializeField] private UIController ui;
|
||||
[SerializeField] private GameObject blackjackCanvas;
|
||||
|
||||
[Header("Game Settings")]
|
||||
[SerializeField] private int maxCardCount = 4;
|
||||
[SerializeField] private int startCardCount = 2;
|
||||
|
||||
[Header("Score Settings")]
|
||||
[SerializeField] private int blackjackScore = 21;
|
||||
[SerializeField] private int successScore = 18;
|
||||
[SerializeField] private int normalScore = 15;
|
||||
|
||||
[Header("Events")]
|
||||
[SerializeField] private UnityEvent onSuccess;
|
||||
[SerializeField] private UnityEvent onFail;
|
||||
[SerializeField] private UnityEvent onExit;
|
||||
|
||||
private readonly List<int> playerCards = new List<int>();
|
||||
private int currentScore;
|
||||
private bool isGameOver;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
StartGame();
|
||||
}
|
||||
|
||||
public void StartGame()
|
||||
{
|
||||
playerCards.Clear();
|
||||
currentScore = 0;
|
||||
isGameOver = false;
|
||||
|
||||
if (ui != null)
|
||||
{
|
||||
ui.ClearUI();
|
||||
ui.SetTurn("Player Turn");
|
||||
}
|
||||
|
||||
for (int i = 0; i < startCardCount; i++)
|
||||
{
|
||||
DrawCard();
|
||||
}
|
||||
|
||||
CheckAutoResult();
|
||||
}
|
||||
|
||||
public void OnHitClicked()
|
||||
{
|
||||
if (isGameOver)
|
||||
return;
|
||||
|
||||
if (playerCards.Count >= maxCardCount)
|
||||
{
|
||||
FinishByStand();
|
||||
return;
|
||||
}
|
||||
|
||||
DrawCard();
|
||||
CheckAutoResult();
|
||||
}
|
||||
|
||||
public void OnStandClicked()
|
||||
{
|
||||
if (isGameOver)
|
||||
return;
|
||||
|
||||
FinishByStand();
|
||||
}
|
||||
|
||||
public void OnRestartClicked()
|
||||
{
|
||||
StartGame();
|
||||
}
|
||||
|
||||
public void OnExitClicked()
|
||||
{
|
||||
if (blackjackCanvas != null)
|
||||
blackjackCanvas.SetActive(false);
|
||||
|
||||
onExit?.Invoke();
|
||||
}
|
||||
|
||||
private void DrawCard()
|
||||
{
|
||||
int cardValue = Random.Range(1, 11);
|
||||
|
||||
playerCards.Add(cardValue);
|
||||
currentScore += cardValue;
|
||||
|
||||
if (ui != null)
|
||||
{
|
||||
ui.SetCardsByValues(playerCards);
|
||||
ui.SetScore(currentScore);
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckAutoResult()
|
||||
{
|
||||
if (currentScore > blackjackScore)
|
||||
{
|
||||
FinishGame(
|
||||
"BUST!",
|
||||
$"현재 점수 : {currentScore}\n21을 초과했습니다.",
|
||||
false
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentScore == blackjackScore)
|
||||
{
|
||||
FinishGame(
|
||||
"BLACKJACK!",
|
||||
$"현재 점수 : {currentScore}\n정확히 21입니다!\n보상 획득!",
|
||||
true
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (playerCards.Count >= maxCardCount)
|
||||
{
|
||||
FinishByStand();
|
||||
}
|
||||
}
|
||||
|
||||
private void FinishByStand()
|
||||
{
|
||||
if (currentScore > blackjackScore)
|
||||
{
|
||||
FinishGame(
|
||||
"BUST!",
|
||||
$"최종 점수 : {currentScore}\n21을 초과했습니다.",
|
||||
false
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentScore == blackjackScore)
|
||||
{
|
||||
FinishGame(
|
||||
"BLACKJACK!",
|
||||
$"최종 점수 : {currentScore}\n완벽합니다!\n보상 획득!",
|
||||
true
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentScore >= successScore)
|
||||
{
|
||||
FinishGame(
|
||||
"성공!",
|
||||
$"최종 점수 : {currentScore}\n21에 아주 가깝습니다.\n보상 획득!",
|
||||
true
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentScore >= normalScore)
|
||||
{
|
||||
FinishGame(
|
||||
"보통",
|
||||
$"최종 점수 : {currentScore}\n나쁘지 않은 선택입니다.",
|
||||
false
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
FinishGame(
|
||||
"실패",
|
||||
$"최종 점수 : {currentScore}\n점수가 너무 낮습니다.",
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
private void FinishGame(string title, string description, bool isSuccess)
|
||||
{
|
||||
isGameOver = true;
|
||||
|
||||
if (ui != null)
|
||||
{
|
||||
ui.ShowResult(title, description);
|
||||
}
|
||||
|
||||
if (isSuccess)
|
||||
{
|
||||
onSuccess?.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
onFail?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e1248fbbe5ee8ae44a6bd4fb6b598301
|
||||
134
Assets/My project/Blackjeck Scripts/UI/UIController.cs
Normal file
134
Assets/My project/Blackjeck Scripts/UI/UIController.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class UIController : MonoBehaviour
|
||||
{
|
||||
[Header("Card UI")]
|
||||
[SerializeField] private Image[] cardImages;
|
||||
|
||||
[Header("Card Sprites")]
|
||||
[Tooltip("Element 0 = Card 1, Element 1 = Card 2 ... Element 9 = Card 10")]
|
||||
[SerializeField] private Sprite[] cardSprites;
|
||||
|
||||
[Header("Text UI")]
|
||||
[SerializeField] private TextMeshProUGUI scoreText;
|
||||
[SerializeField] private TextMeshProUGUI turnText;
|
||||
|
||||
[Header("Action Buttons")]
|
||||
[SerializeField] private Button hitButton;
|
||||
[SerializeField] private Button standButton;
|
||||
|
||||
[Header("Result UI")]
|
||||
[SerializeField] private GameObject resultPanel;
|
||||
[SerializeField] private TextMeshProUGUI resultTitleText;
|
||||
[SerializeField] private TextMeshProUGUI resultDescriptionText;
|
||||
|
||||
public void ClearUI()
|
||||
{
|
||||
ClearCards();
|
||||
SetScore(0);
|
||||
SetTurn("Player Turn");
|
||||
HideResult();
|
||||
SetActionButtons(true);
|
||||
}
|
||||
|
||||
public void SetCardsByValues(List<int> cardValues)
|
||||
{
|
||||
ClearCards();
|
||||
|
||||
for (int i = 0; i < cardValues.Count; i++)
|
||||
{
|
||||
SetCardByValue(i, cardValues[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetCardByValue(int index, int cardValue)
|
||||
{
|
||||
if (cardImages == null || index < 0 || index >= cardImages.Length)
|
||||
{
|
||||
Debug.LogWarning($"Card Image index가 잘못되었습니다: {index}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (cardImages[index] == null)
|
||||
{
|
||||
Debug.LogWarning($"Card Image {index}가 Inspector에 연결되지 않았습니다.");
|
||||
return;
|
||||
}
|
||||
|
||||
int spriteIndex = cardValue - 1;
|
||||
|
||||
if (cardSprites == null || spriteIndex < 0 || spriteIndex >= cardSprites.Length)
|
||||
{
|
||||
Debug.LogWarning($"Card Sprite가 없습니다. cardValue: {cardValue}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (cardSprites[spriteIndex] == null)
|
||||
{
|
||||
Debug.LogWarning($"Card Sprite Element {spriteIndex}가 비어 있습니다.");
|
||||
return;
|
||||
}
|
||||
|
||||
cardImages[index].sprite = cardSprites[spriteIndex];
|
||||
cardImages[index].enabled = true;
|
||||
}
|
||||
|
||||
public void ClearCards()
|
||||
{
|
||||
if (cardImages == null)
|
||||
return;
|
||||
|
||||
foreach (Image cardImage in cardImages)
|
||||
{
|
||||
if (cardImage == null)
|
||||
continue;
|
||||
|
||||
cardImage.sprite = null;
|
||||
cardImage.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetScore(int score)
|
||||
{
|
||||
if (scoreText != null)
|
||||
scoreText.text = $"Score : {score}";
|
||||
}
|
||||
|
||||
public void SetTurn(string message)
|
||||
{
|
||||
if (turnText != null)
|
||||
turnText.text = message;
|
||||
}
|
||||
|
||||
public void SetActionButtons(bool isActive)
|
||||
{
|
||||
if (hitButton != null)
|
||||
hitButton.interactable = isActive;
|
||||
|
||||
if (standButton != null)
|
||||
standButton.interactable = isActive;
|
||||
}
|
||||
|
||||
public void ShowResult(string title, string description)
|
||||
{
|
||||
if (resultPanel != null)
|
||||
resultPanel.SetActive(true);
|
||||
|
||||
if (resultTitleText != null)
|
||||
resultTitleText.text = title;
|
||||
|
||||
if (resultDescriptionText != null)
|
||||
resultDescriptionText.text = description;
|
||||
|
||||
SetActionButtons(false);
|
||||
}
|
||||
|
||||
public void HideResult()
|
||||
{
|
||||
if (resultPanel != null)
|
||||
resultPanel.SetActive(false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7f9509c143c07dc4c839899268461ee2
|
||||
Reference in New Issue
Block a user