_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Battle_Royale/BettingBtn.cs
2024-11-16 16:01:27 +08:00

157 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Unity.VisualScripting.Antlr3.Runtime;
using UnityEngine;
using UnityEngine.UI;
using static BettingBtn;
using static UnityEditor.PlayerSettings;
public class BettingBtn : MonoBehaviour
{
public static BettingBtn instance;
public GameObject BetList;
public Button NumBtn;
public Text BetText;
public float BetValue;//投注的值
public float AllBetValue;//投注总值
public Button BetButton;//投注的按钮
public GameObject text1;
public GameObject text2;
public GameObject text3;
public GameObject text4;
public GameObject text5;
public GameObject text6;
public List<GameObject> list=new List<GameObject>();
// Start is called before the first frame update
void Start()
{
instance = this;
BetList.SetActive(false);
NumBtn.onClick.AddListener(OnClickNumBtn);
BetValue = 50;//默认50
SetBet();
}
void OnClickNumBtn()
{
BetList.SetActive(true);
}
[System.Serializable]
public class RequestData
{
public int userId;
public int escapeId;
public float bet;
public int roomNo;
}
public GameObject bg;
public async void OnClickBetBtnAsync()
{
RequestData body = new RequestData();
body.escapeId = bg.GetComponentInChildren<AllHouseContro>().escapeId;
body.bet = BetValue;
body.roomNo = bg.GetComponentInChildren<AllHouseContro>().roomNo;
body.userId = Global.global.serverResponse.data.userId;
bg.GetComponentInChildren<AllHouseContro>().house.text.GetComponentInChildren< TextBox >().AddText(BetValue);
string response = await web.SendRequest(web.URL + "/snail/gameEscape/userBet", "POST", JsonUtility.ToJson(body), Global.global.CreateHeaders());
Debug.Log("用户下注响应: " + response);
bg.GetComponentInChildren<AllHouseContro>().battleRoyaleGameDetails();
}
void SetBet()
{
// 获取所有的Button组件
Button[] buttons = BetList.GetComponentsInChildren<Button>();
// 遍历每一个Button添加点击事件
foreach (Button button in buttons)
{
button.onClick.AddListener(() => {
BetText.text = button.transform.GetComponentInChildren<Text>().text;
BetValue = float.Parse(BetText.text);
BetList.SetActive(false);
});
}
}
public void GiveMoney(string room_id)
{
float AllMoney = text1.GetComponent<TextBox>().ReturnText() + text2.GetComponent<TextBox>().ReturnText() + text3.GetComponent<TextBox>().ReturnText()
+ text4.GetComponent<TextBox>().ReturnText() + text5.GetComponent<TextBox>().ReturnText() + text6.GetComponent<TextBox>().ReturnText();
foreach (GameObject item in list)
{
if (item.GetComponent<TextBox>().ParentHouse.GetComponent<HouseBtn>().roomNo == int.Parse(room_id))
{
//item.GetComponent<TextBox>().ParentHouse.GetComponent<HouseBtn>().wn.GetComponent<wuniusj>().yingchang();
item.GetComponent<TextBox>().PlayAni();
ControMoney(item);
}
}
}
public void ControMoney(GameObject text)
{
float giveMoney= text.GetComponent<TextBox>().ReturnText();
text.GetComponent<TextBox>().SetText(-giveMoney);
float AllMoney = text1.GetComponent<TextBox>().ReturnText() + text2.GetComponent<TextBox>().ReturnText() + text3.GetComponent<TextBox>().ReturnText()
+ text4.GetComponent<TextBox>().ReturnText() + text5.GetComponent<TextBox>().ReturnText() + text6.GetComponent<TextBox>().ReturnText();
for (int i=0;i<list.Count;i++)
{
if (list[i]==text)
{
continue;
}
list[i].GetComponent<TextBox>().SetText(giveMoney*(list[i].GetComponent<TextBox>().ReturnText()/AllMoney));
}
}
public void GiveMoneyToPlayer()
{
switch (PlayerMovePos.instance.HouseId)
{
case 1:
PlayerInfo.instance.SetMoney(text1.GetComponent<TextBox>().ReturnText()/2);
break;
case 2:
PlayerInfo.instance.SetMoney(text2.GetComponent<TextBox>().ReturnText()/2);
break;
case 3:
PlayerInfo.instance.SetMoney(text3.GetComponent<TextBox>().ReturnText() / 2);
break;
case 4:
PlayerInfo.instance.SetMoney(text4.GetComponent<TextBox>().ReturnText()/2);
break;
case 5:
PlayerInfo.instance.SetMoney(text5.GetComponent<TextBox>().ReturnText() / 2);
break;
case 6:
PlayerInfo.instance.SetMoney(text6.GetComponent<TextBox>().ReturnText() / 2);
break;
default:
Debug.Log("未选择房间");
break;
}
}
}