Zjh游戏(十六)搭建房间选择界面

笑着哭i 提交于 2019-12-11 20:25:01

UI图:
在这里插入图片描述


选择房间脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using DG.Tweening;

/// <summary>
/// 游戏类型 单机 联网
/// </summary>
public enum GameType
{
    Net,
    StandAlone
}

public class RoomChoosePanel : MonoBehaviour
{
    private Button btn_EnterRoom1;
    private Button btn_EnterRoom2;
    private Button btn_EnterRoom3;
    private Button btn_Close;
    private GameType gameType;

    private void Awake()
    {
        EventCenter.AddListener<GameType>(EventDefine.ShowRoomChoosePanel,Show);
        Init();
    }

    void Init()
    {
        btn_EnterRoom1 = transform.Find("Room_1/btn_EnterRoom").GetComponent<Button>();
        btn_EnterRoom1.onClick.AddListener(delegate { EnterRoom(10, 100); });
        btn_EnterRoom2 = transform.Find("Room_2/btn_EnterRoom").GetComponent<Button>();
        btn_EnterRoom2.onClick.AddListener(delegate { EnterRoom(20, 200); });
        btn_EnterRoom3 = transform.Find("Room_3/btn_EnterRoom").GetComponent<Button>();
        btn_EnterRoom3.onClick.AddListener(delegate { EnterRoom(50, 500); });
        btn_Close = transform.Find("btn_Close").GetComponent<Button>();
        btn_Close.onClick.AddListener(()=> 
        {
            transform.DOScale(Vector3.zero, 0.3f);
        });
    }

    void Show(GameType gameType)
    {
        this.gameType = gameType;
        transform.DOScale(Vector3.one,0.3f);
    }

    /// <summary>
    /// 进入房间按钮的点击
    /// </summary>
    private void EnterRoom(int bottomStakes, int topStakes)
    {
        //保存底注和顶注
        Models.GameModel.BottomStakes = bottomStakes;
        Models.GameModel.TopStakes = topStakes;

        switch (gameType)
        {
            case GameType.Net:
                //进入联网场景
                break;
            case GameType.StandAlone:
                //进入单机场景
                SceneManager.LoadScene(2);
              break;
        }
    }

    private void OnDestroy()
    {
        EventCenter.RemoveListener<GameType>(EventDefine.ShowRoomChoosePanel, Show);
    }
}


单机游戏场景搭建

UI图

在这里插入图片描述


自身玩家脚本

using Protocol.Code;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SelfManager_Stand : MonoBehaviour
{
    private GameObject go_BottomButton;
    private Image img_HeadIcon;
    private Text txt_UserName;
    private Text txt_CoinCount;
    private Image img_Backer;
    private Text txt_StakesSum;
    private GameObject go_CountDown;
    private Text txt_CountDown;
    private Button btn_Ready;
    private GameObject txt_GiveUp;
    private Transform CardPoints;
    private ZjhManager_Stand m_ZjhManager;
    private Button btn_LookCard;
    private Button btn_FollowStakes;
    private Button btn_AddStakes;
    private Button btn_ComapreCard;
    private Button btn_GiveUp;
    private Toggle tgo_2;
    private Toggle tgo_5;
    private Toggle tgo_10;


    //总的下注数
    private int m_StakesNum=0;
    private void Awake()
    {
        Init();
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener<int>(EventDefine.SendRechargePanel, UpdateCoinCount);
    }
    void Init()
    {
        EventCenter.AddListener<int>(EventDefine.SendRechargePanel, UpdateCoinCount);
        m_ZjhManager = GetComponentInParent<ZjhManager_Stand>();
        go_BottomButton = transform.Find("BottomButtons").gameObject;
        img_HeadIcon = transform.Find("img_HeadIcon").GetComponent<Image>();
        txt_UserName = transform.Find("txt_UserName").GetComponent<Text>();
        txt_CoinCount = transform.Find("Coin/txt_CoinCount").GetComponent<Text>();
        img_Backer = transform.Find("img_Backer").GetComponent<Image>();
        txt_StakesSum=transform.Find("StakesSum/txt_StakesSum").GetComponent<Text>();
        go_CountDown = transform.Find("CountDown").gameObject;
        txt_CountDown = transform.Find("CountDown/txt_CountDowm").GetComponent<Text>();
        btn_Ready = transform.Find("btn_Ready").GetComponent<Button>();
        btn_Ready.onClick.AddListener(OnReadyButtonClick);
        txt_GiveUp = transform.Find("txt_GiveUp").gameObject;
        CardPoints = transform.Find("CardPosition");

        btn_LookCard = go_BottomButton.transform.Find("btn_LockCard").GetComponent<Button>();
        btn_FollowStakes = go_BottomButton.transform.Find("btn_FollowStakes").GetComponent<Button>();
        btn_AddStakes = go_BottomButton.transform.Find("btn_AddStakes").GetComponent<Button>();
        btn_ComapreCard = go_BottomButton.transform.Find("btn_ComapreCard").GetComponent<Button>();
        btn_GiveUp = go_BottomButton.transform.Find("btn_GiveUp").GetComponent<Button>();
        tgo_2 = go_BottomButton.transform.Find("tgo_2").GetComponent<Toggle>();
        tgo_5 = go_BottomButton.transform.Find("tgo_5").GetComponent<Toggle>();
        tgo_10 = go_BottomButton.transform.Find("tgo_10").GetComponent<Toggle>();
       
        go_BottomButton.SetActive(false);
        go_CountDown.SetActive(false);
        txt_GiveUp.SetActive(false);
        img_Backer.gameObject.SetActive(false);

        txt_StakesSum.text = "0";
        img_HeadIcon.sprite = ResourcesManager.GetSprite(Models.GameModel.userDto.IconName);
        txt_UserName.text = Models.GameModel.userDto.UserName;
        txt_CoinCount.text = Models.GameModel.userDto.Coin.ToString();
  
    }


    private void OnReadyButtonClick()
    {
        m_StakesNum += Models.GameModel.BottomStakes;
        txt_StakesSum.text = m_StakesNum.ToString();
        if (NetMsgCenter.Instance!=null)//每次下注后,发送请求更新金币的请求
        {
            NetMsgCenter.Instance.SendMsg(OpCode.Account, AccountCode.GetRecharge_CREQ, -Models.GameModel.BottomStakes);
        }   
        
        btn_Ready.gameObject.SetActive(false);
        m_ZjhManager.ChooseBanker();     
        go_BottomButton.SetActive(true);
        SetBottomButtoninteractable(false);
    }

    private void SetBottomButtoninteractable(bool value)

    {
        btn_LookCard.interactable = value;
        btn_FollowStakes.interactable = value;
        btn_AddStakes.interactable = value;
        btn_ComapreCard.interactable = value;
        btn_GiveUp.interactable = value;
        tgo_2.interactable = value;
        tgo_5.interactable = value;
        tgo_10.interactable = value;
    }

    /// <summary>
    /// 更新金币后的调用
    /// </summary>
    private void UpdateCoinCount(int value)
    {
        Models.GameModel.userDto.Coin = value;
        txt_CoinCount.text = value.ToString();
    }

    /// <summary>
    /// 成为庄家
    /// </summary>
    public void BecomeBanker()
    {
        img_Backer.gameObject.SetActive(true);
    }
}


左边玩家脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LeftManager_Stand : MonoBehaviour
{

    private Image img_Banker;
    private Text txt_StakesSum;
    private GameObject go_CountDown;
    private Text txt_CountDown;
    private GameObject txt_Ready;
    private Transform CardPoints;
    private Image img_HeadIcon;

    private int m_StakesSum;
         
    private void Awake()
    {
        Init();
    }

    private void Init()
    {
        img_Banker = transform.Find("img_Backer").GetComponent<Image>();
        txt_StakesSum = transform.Find("StakesSum/txt_StakesSum").GetComponent<Text>();
        go_CountDown = transform.Find("CountDown").gameObject;
        txt_CountDown = transform.Find("CountDown/txt_CountDowm").GetComponent<Text>();
        txt_Ready = transform.Find("txt_Ready").gameObject;
        CardPoints = transform.Find("CardPosition").transform;
        img_HeadIcon = transform.Find("img_HeadIcon").GetComponent<Image>();

        int headIndex = Random.Range(0, 19);
        string name = "headIcon_" + headIndex;
        img_HeadIcon.sprite = ResourcesManager.GetSprite(name);

        txt_StakesSum.text = "0";
        img_Banker.gameObject.SetActive(false);
        go_CountDown.SetActive(false);

    }

    /// <summary>
    /// 开始选择庄家
    /// </summary>
    public void ChooseBanker()
    {
        m_StakesSum += Models.GameModel.BottomStakes;
        txt_StakesSum.text = m_StakesSum.ToString();
        txt_Ready.SetActive(false);
    }
    /// <summary>
    /// 成为庄家
    /// </summary>
    public void BecomeBanker()
    {
        img_Banker.gameObject.SetActive(true);
    }
}

右边玩家脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RightManager_Stand : MonoBehaviour
{

    private Image img_Banker;
    private Text txt_StakesSum;
    private GameObject go_CountDown;
    private Text txt_CountDown;
    private GameObject txt_Ready;
    private Transform CardPoints;
    private Image img_HeadIcon;

    private int m_StakesSum;
         
    private void Awake()
    {
        Init();
    }

    private void Init()
    {
        img_Banker = transform.Find("img_Backer").GetComponent<Image>();
        txt_StakesSum = transform.Find("StakesSum/txt_StakesSum").GetComponent<Text>();
        go_CountDown = transform.Find("CountDown").gameObject;
        txt_CountDown = transform.Find("CountDown/txt_CountDowm").GetComponent<Text>();
        txt_Ready = transform.Find("txt_Ready").gameObject;
        CardPoints = transform.Find("CardPosition").transform;
        img_HeadIcon = transform.Find("img_HeadIcon").GetComponent<Image>();

        int headIndex = Random.Range(0, 19);
        string name = "headIcon_" + headIndex;
        img_HeadIcon.sprite = ResourcesManager.GetSprite(name);

        txt_StakesSum.text = "0";
        img_Banker.gameObject.SetActive(false);
        go_CountDown.SetActive(false);

    }

    /// <summary>
    /// 开始选择庄家
    /// </summary>
    public void ChooseBanker()
    {
        m_StakesSum += Models.GameModel.BottomStakes;
        txt_StakesSum.text = m_StakesSum.ToString();
        txt_Ready.SetActive(false);
    }
    /// <summary>
    /// 成为庄家
    /// </summary>
    public void BecomeBanker()
    {
        img_Banker.gameObject.SetActive(true);
    }
}

单机模式游戏管理器

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class ZjhManager_Stand : MonoBehaviour
{
    private Text txt_ButtomStackes;
    private Text txt_TopStackes;
    private Button btn_Back;
    private LeftManager_Stand m_LeftManager;
    private RightManager_Stand m_RightManager;
    private SelfManager_Stand m_SelfManager;
    /// <summary>
    /// 当前发牌的游标
    /// </summary>
    private int m_CurrentCardSendIndex=0;

    /// <summary>
    /// 当前下注的游标
    /// </summary>
    private int m_CurrentStakesIndex = 0;

    /// <summary>
    /// 牌库
    /// </summary>
    private List<Card> m_CardList = new List<Card>();
    private void Awake()
    {
        Init();
    }

    private void Init()
    {
        m_SelfManager = GetComponentInChildren<SelfManager_Stand>();
        m_LeftManager = GetComponentInChildren<LeftManager_Stand>();
        m_RightManager = GetComponentInChildren<RightManager_Stand>();
        txt_ButtomStackes = transform.Find("Main/txt_BottomStakes").GetComponent<Text>();
        txt_TopStackes = transform.Find("Main/txt_topStakes").GetComponent<Text>();
        btn_Back = transform.Find("Main/btn_Back").GetComponent<Button>();
        btn_Back.onClick.AddListener(()=>
        {
            SceneManager.LoadScene(1);
        });

        txt_ButtomStackes.text = Models.GameModel.BottomStakes.ToString();
        txt_TopStackes.text = Models.GameModel.TopStakes.ToString();      
    }

    /// <summary>
    /// 开始选择庄家
    /// </summary>
    public void ChooseBanker()
    {
        //告诉左右两边玩家开始选择庄家
        m_LeftManager.ChooseBanker();
        m_RightManager.ChooseBanker();

        //随机成为庄家
        int ran = Random.Range(0,3);
        switch (ran)
        {
            case 0:
                m_SelfManager.BecomeBanker();
                m_CurrentCardSendIndex = 0;
                m_CurrentStakesIndex = 1;
                break;
            case 1:
                m_LeftManager.BecomeBanker();
                m_CurrentCardSendIndex = 1;
                m_CurrentStakesIndex = 2;
                break;
            case 2:
                m_RightManager.BecomeBanker();
                m_CurrentCardSendIndex = 2;
                m_CurrentStakesIndex = 0;
                break;
        }
        EventCenter.Broadcast(EventDefine.Hint,"开始发牌!");
        //开始发牌
        StartCoroutine(DealCard());
    }

    /// <summary>
    /// 延迟发牌
    /// </summary>
    /// <returns></returns>
    private IEnumerator DealCard()
    {
        //初始化牌
        if (m_CardList.Count == 0 || m_CardList == null || m_CardList.Count < 9)
        {
            InitCard(); 
        }
        //洗牌

        //发牌
        yield return 0;

    }

    /// <summary>
    /// 初始化牌库
    /// </summary>
    private void InitCard()
    {
        for (int weight = 0; weight < 15; weight++)
        {
            for (int color = 0; color < 4; color++)
            {
                Card card = new Card(weight,color);
                m_CardList.Add(card);
            }
        }
    }
}

未完结,后面继续更新,当前写到初始化牌库


有需要学习视频的欢迎关注微信公众号:
在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!