Roulette/Assets/Scripts/RouletteManager.cs
2023-03-05 15:28:01 +05:30

205 lines
5.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
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; instance.vcurrencyTxt.text = value.ToString();}}
public static UnityEvent<float> OnMoneyAvailablChanged = new UnityEvent<float>();
public static Dictionary<float, Chip> chipsBoard {get; private set;}
public static bool RegisterChip(Chip chip){
if(chipsBoard == null){chipsBoard = new Dictionary<float, Chip>();}
if(chipsBoard.ContainsKey(chip.value)){return false;}
chipsBoard.Add(chip.value, chip);
selectedChipChanged.AddListener(chip.OnSelectedChanged);
chip.OnSelectedChanged(SelectedChip);
return true;
}
public static UnityEvent<float> selectedChipChanged = new UnityEvent<float>();
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<int> Reds = new List<int>(){1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36};
public static Dictionary<int, BettingSpace> bettingSpaces = new Dictionary<int, BettingSpace>();
public static List<int> HighlightedBettingSpaces = new List<int>();
private static List<Bet> bets = new List<Bet>();
public static List<Bet> PlacedBets {get{return bets;}}
public static bool spinning;
public TMP_Text betsvalue;
public TMP_Text vcurrencyTxt;
public static void RegisterBettingSpace(BettingSpace space){
if(bettingSpaces.ContainsKey(space.Number)){return;}
bettingSpaces.Add(space.Number, space);
}
public static void HighlightBettingSpace(List<int> numbers){
HighlightedBettingSpaces = numbers;
UpdateHightlightedSpaces();
}
public static void ClearHighlightedBettingSpaces(){ HighlightedBettingSpaces = new List<int>(); UpdateHightlightedSpaces();}
static void UpdateHightlightedSpaces(){
foreach(KeyValuePair<int,BettingSpace> 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<int> _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<Bet>();
RefreshUI();
}
void Awake(){
instance =this;
}
void Start()
{
Spinner.OnSpinStopped.AddListener(OnSpinStopped);
}
void OnSpinStopped(int landedValue){
foreach(Bet bet in bets){
if(bet.Numbers.Contains(landedValue)){
MoneyAvailable += bet.BetAmount * bet.PayoutRatio;
MoneyAvailable += bet.BetAmount;
}
}
ClearBets();
}
void BetsChanged(){
}
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<int> Numbers;
public GameObject chipObj;
}