using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.SceneManagement; public class RouletteManager : MonoBehaviour { public static RouletteManager instance { get; private set;} private static float moneyAvailable = 1000; public static float MoneyAvailable {get{ return moneyAvailable;} set{OnMoneyAvailablChanged.Invoke(value); moneyAvailable = value; foreach(TMP_Text txt in instance.vcurrencyTxt){txt.text = "$ " +value.ToString();}}} public static UnityEvent OnMoneyAvailablChanged = new UnityEvent(); public static Dictionary chipsBoard {get; private set;} public static bool RegisterChip(Chip chip){ if(chipsBoard == null){chipsBoard = new Dictionary();} if(chipsBoard.ContainsKey(chip.value)){return false;} chipsBoard.Add(chip.value, chip); selectedChipChanged.AddListener(chip.OnSelectedChanged); chip.OnSelectedChanged(SelectedChip); return true; } public static UnityEvent selectedChipChanged = new UnityEvent(); private static float m_selectedChip = 1f; public static float SelectedChip { get{ return m_selectedChip; } set{ m_selectedChip = value; selectedChipChanged.Invoke(value); }} public static Chip SelectedChipItem{get{return chipsBoard[SelectedChip];}} public static int[] RouletteNumbers = {-1,27,10,25,29,12,8,19,31,18,6,21,33,16,4,23,35,14,2,0,28,9,26,30,11,7,20,32,17,5,22,34,15,3,24,36,13,1}; public static List Reds = new List(){1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36}; public static Dictionary bettingSpaces = new Dictionary(); public static List HighlightedBettingSpaces = new List(); private static List bets = new List(); public static List PlacedBets {get{return bets;}} public static bool spinning; public TMP_Text betsvalue; public TMP_Text[] vcurrencyTxt; public TMP_Text walletMoneyTxt; public float walletMoney; public Transform betsHistoryParent; public GameObject gameOverPanel; public static void RegisterBettingSpace(BettingSpace space){ if(bettingSpaces.ContainsKey(space.Number)){return;} bettingSpaces.Add(space.Number, space); } public static void HighlightBettingSpace(List numbers){ HighlightedBettingSpaces = numbers; UpdateHightlightedSpaces(); } public static void ClearHighlightedBettingSpaces(){ HighlightedBettingSpaces = new List(); UpdateHightlightedSpaces();} static void UpdateHightlightedSpaces(){ foreach(KeyValuePair bettingSpace in bettingSpaces){ if(HighlightedBettingSpaces.Contains(bettingSpace.Key)){ bettingSpace.Value.SetHighlighted(true); }else{ bettingSpace.Value.SetHighlighted(false); } } } public static void RefreshUI(){ instance.Refresh(); } public void Refresh(){ betsvalue.text = "$"+ GetBetsValue(); } public static float GetBetsValue(){ float betsVal = 0; foreach(Bet bet in bets){ betsVal += bet.BetAmount; } return betsVal; } public static void AddBet(float value, CombinerType type, List _numbers, GameObject newChip){ // bets += value; if(value > MoneyAvailable){Debug.LogError("Not enough money for this"); return;} Bet newBet = new Bet(){BetAmount = value, PayoutRatio = GetRatioFromType(type), Numbers = _numbers, chipObj= newChip}; bets.Add(newBet); MoneyAvailable-=value; instance.BetsChanged(); RefreshUI(); } public static void ClearBets(){ foreach(Bet bet in bets){ Destroy(bet.chipObj); } bets = new List(); RefreshUI(); } public static void ClearBets(List space){ List betsToRemove = new List(); foreach(Bet bet in bets){ if(space.Count == 1 && bet.Numbers.Count == 1){ if(space[0] == bet.Numbers[0]){ betsToRemove.Add(bet); continue; } } if(bet.Numbers == space){ // Destroy(bet.chipObj); betsToRemove.Add(bet); } } foreach(Bet bet in betsToRemove){ Destroy(bet.chipObj); MoneyAvailable += bet.BetAmount; bets.Remove(bet); } RefreshUI(); } void Awake(){ instance =this; } void Start() { MoneyAvailable = 1000; walletMoney = MoneyAvailable; walletMoneyTxt.text = "$ " +MoneyAvailable; Spinner.OnSpinStopped.AddListener(OnSpinStopped); } List betsHistory = new List(); void OnSpinStopped(int landedValue){ float prevMoney=walletMoney; foreach(Bet bet in bets){ if(bet.Numbers.Contains(landedValue)){ float addition = (bet.BetAmount * bet.PayoutRatio) + (bet.BetAmount); MoneyAvailable += addition; // profit += addition; }else{ // profit -= bet.BetAmount; } } Debug.Log("Bet profitted : " + (MoneyAvailable - prevMoney)); betsHistory.Add((MoneyAvailable - prevMoney)); walletMoneyTxt.text = "$ " +MoneyAvailable; walletMoney = MoneyAvailable; if(walletMoney <= 0){ gameOverPanel.SetActive(true); } UpdateBetsHistory(); ClearBets(); } void UpdateBetsHistory(){ int diff = betsHistory.Count - betsHistoryParent.childCount; for(int i=0; i < diff; i++){ betsHistory.RemoveAt(i); } for(int i =0; i < betsHistoryParent.childCount; i++){ if(betsHistory.Count <= i){ betsHistoryParent.GetChild(i).GetChild(0).GetComponent().text = "-"; betsHistoryParent.GetChild(i).GetChild(1).GetComponent().text = "-"; betsHistoryParent.GetChild(i).GetChild(0).GetComponent().color = Color.white; betsHistoryParent.GetChild(i).GetChild(1).GetComponent().color = Color.white; }else{ Color txtColor = betsHistory[i] > 0 ? Color.green : Color.red; betsHistoryParent.GetChild(i).GetChild(0).GetComponent().text = "$ " + Mathf.Abs(betsHistory[i]).ToString(); betsHistoryParent.GetChild(i).GetChild(1).GetComponent().text = betsHistory[i] > 0 ? "Win" : "Lose"; betsHistoryParent.GetChild(i).GetChild(0).GetComponent().color = txtColor; betsHistoryParent.GetChild(i).GetChild(1).GetComponent().color = txtColor; } } } void BetsChanged(){ } public void Restart(){ // SceneManager.LoadScene(0); MoneyAvailable = 1000; walletMoney = MoneyAvailable; walletMoneyTxt.text = "$ 1000"; betsHistory = new List(); UpdateBetsHistory(); Spinner.instance.wheel.eulerAngles = Vector3.zero; Spinner.instance.numText.text = ""; gameOverPanel.SetActive(false); } public static float GetRatioFromType(CombinerType type){ switch(type){ case CombinerType.Single: return 35; break; case CombinerType.Split: return 17; break; case CombinerType.Street: return 11; break; case CombinerType.Corner: return 8; break; case CombinerType.Five: return 6; break; case CombinerType.Line: return 5; break; case CombinerType.Dozen: return 2; break; case CombinerType.Row: return 2; break; case CombinerType.Eighteen: return 1; break; case CombinerType.Red: return 1; break; case CombinerType.Black: return 1; break; case CombinerType.Even: return 1; break; case CombinerType.Odd: return 1; break; } return 1; } } [System.Serializable] public class Bet{ public float BetAmount; public float PayoutRatio = 1; public List Numbers; public GameObject chipObj; }