Commit 566fea8e by alsunj

Upload New File

parent ce885967
Showing with 295 additions and 0 deletions
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;
using UnityEngine.UI;
public class GameMananger : MonoBehaviour
{
public Button dealBtn;
public Button hitBtn;
public Button standBtn;
public Button bet500Btn;
public Button bet100Btn;
public Button bet50Btn;
public Button bet10Btn;
public Button bet1Btn;
// Kasuta teisi scripte
public PlayerScript playerScript;
public PlayerScript dealerScript;
// public text, et nha betti/jki/ktt
public Text playerScoreText;
public Text dealerScoreText;
public Text betsText;
public Text balanceText;
public Text mainText;
public Text warningText;
public Text money;
// Hiden card for dealer's 2nd card
public GameObject hideCard;
// bet amount
int bet = 0;
void Start()
{
Debug.Log(playerScript.GetMoney().ToString());
hitBtn.interactable = false;
standBtn.interactable = false;
if (DBmanager.LoggedIn)
{
money.text = DBmanager.bank.ToString();
}
money.text = "BALANCE: " + playerScript.GetMoney().ToString();
// Add on click listeners to the buttons
dealBtn.onClick.AddListener(() => DealClicked());
hitBtn.onClick.AddListener(() => HitClicked());
standBtn.onClick.AddListener(() => StandClicked());
// Bet buttons
bet500Btn.onClick.AddListener(() => BetClicked(500));
bet100Btn.onClick.AddListener(() => BetClicked(100));
bet50Btn.onClick.AddListener(() => BetClicked(50));
bet10Btn.onClick.AddListener(() => BetClicked(10));
bet1Btn.onClick.AddListener(() => BetClicked(1));
mainText.gameObject.SetActive(true);
//ResetTableCheckBet();
}
// Function for deal button
private void DealClicked()
{
// checking if the bet is made
if (bet <= 0)
{
StartCoroutine(ShowWarning("Make a bet!", 1.5f));
return;
}
// Adjust buttons visibility and remove total cards score
mainText.gameObject.SetActive(false);
dealerScoreText.gameObject.SetActive(false);
bet500Btn.interactable = false;
bet100Btn.interactable = false;
bet50Btn.interactable = false;
bet10Btn.interactable = false;
bet1Btn.interactable = false;
//dealerScoreText.text = "XX";
GameObject.Find("Deck").GetComponent<DeckScript>().Shuffle();
playerScript.StartHand();
dealerScript.StartHand();
// Update the socre displayed
playerScoreText.text = playerScript.handValue.ToString();
dealerScoreText.text = dealerScript.handValue.ToString();
//Make player's score visible
playerScoreText.gameObject.SetActive(true);
//
if (playerScript.handValue == 21)
{
RoundOver();
}
// Adjust buttons visibility
dealBtn.interactable = false;
hitBtn.interactable = true;
standBtn.interactable = true;
betsText.text = bet.ToString();
playerScript.AdjustMoney(0);
money.text = "BALANCE: " + playerScript.GetMoney().ToString();
}
private void HitClicked()
{
//check that there is still room on the table
if (playerScript.cardIndex <= 10)
{
playerScript.GetCard();
playerScoreText.text = playerScript.handValue.ToString();
if (playerScript.handValue > 20) RoundOver();
}
}
// Function for stand button
private void StandClicked()
{
dealBtn.interactable = false;
hitBtn.interactable = false;
standBtn.interactable = false;
hideCard.GetComponent<Renderer>().enabled = false;
dealerScoreText.gameObject.SetActive(true);
// Give dealer cards until one of the gameover the condition is met
HitDealer();
}
private void HitDealer()
{
StartCoroutine(DisplayCards());
}
// Check for winner or loser, hand is over
void RoundOver()
{
bool playerBust = playerScript.handValue > 21;
bool dealerBust = dealerScript.handValue > 21;
bool player21 = playerScript.handValue == 21;
bool dealer21 = dealerScript.handValue == 21;
bool roundOver = true;
// Does a player have a BlackJack
if (player21 && playerScript.cardIndex == 2)
{
mainText.text = "BlackJack! Win: " + Mathf.FloorToInt(bet * 2.25f);
playerScript.AdjustMoney(Mathf.FloorToInt(bet * 2.25f));
DBmanager.wins += 1;
}
// if player busts, dealer didnt, or if dealer has more points, dealer wins
else if (playerBust || (!dealerBust && dealerScript.handValue > playerScript.handValue))
{
mainText.text = "Dealer wins!";
DBmanager.loses += 1;
}
// if dealer busts, player didnt, or player has more points, player wins
else if (dealerBust || playerScript.handValue > dealerScript.handValue)
{
mainText.text = "You win: " + bet * 2;
playerScript.AdjustMoney(bet * 2);
DBmanager.wins += 1;
}
//Check for tie, return bets
else if (playerScript.handValue == dealerScript.handValue)
{
mainText.text = "Push: Bets returned";
playerScript.AdjustMoney(bet);
}
else
{
roundOver = false;
}
// Set ui up for next move / hand / turn
if (roundOver)
{
hitBtn.interactable = false;
standBtn.interactable = false;
mainText.gameObject.SetActive(true);
dealerScoreText.gameObject.SetActive(true);
bet500Btn.interactable = true;
bet100Btn.interactable = true;
bet50Btn.interactable = true;
bet10Btn.interactable = true;
bet1Btn.interactable = true;
hideCard.GetComponent<Renderer>().enabled = false;
money.text = "BALANCE: " + playerScript.GetMoney().ToString();
DBmanager.bank = playerScript.GetMoney();
bet = 0;
betsText.text = bet.ToString();
Invoke("ResetTable", 2);
}
return;
}
// Add money to bet if bet clicked
void BetClicked(int value)
{
int intBet = value;
playerScript.AdjustMoney(-intBet);
// Can bet be added
if (playerScript.GetMoney() < 0)
{
StartCoroutine(ShowWarning("No free balance!", 1.5f));
playerScript.AdjustMoney(intBet);
}
else
{
bet += intBet;
money.text = "BALANCE: " + playerScript.GetMoney().ToString();
betsText.text = bet.ToString();
}
}
private void ResetTable()
{
hideCard.GetComponent<Renderer>().enabled = true;
mainText.gameObject.SetActive(false);
playerScript.ResetHand();
dealerScript.ResetHand();
playerScoreText.gameObject.SetActive(false);
dealerScoreText.gameObject.SetActive(false);
dealBtn.interactable = true;
}
// Function for showing warnings on screen for given time
IEnumerator ShowWarning(string message, float delay)
{
warningText.text = message;
warningText.enabled = true;
yield return new WaitForSeconds(delay);
warningText.enabled = false;
}
// Show dealer cards with delay until cards value condition is met
IEnumerator DisplayCards()
{
while (dealerScript.handValue <= 16 && dealerScript.cardIndex < 10)
{
yield return new WaitForSeconds(1);
dealerScript.GetCard();
dealerScoreText.text = dealerScript.handValue.ToString();
}
RoundOver();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment