天天看點

Zjh遊戲(二十一)精簡代碼

精簡三個玩家的代碼,三個玩家的基類

BaseMangaer_Stand

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

public class BaseManager_Stand : MonoBehaviour
{
    public GameObject go_CardPre;//牌的預制體

    protected Transform CardPoints;
    protected Image img_Backer;
    protected GameObject go_CountDown;
    protected Text txt_CountDown;
    protected StakesCountHint m_StakesCountHint;
    protected ZjhManager_Stand m_ZjhManager;
    protected Image img_HeadIcon;
    protected Text txt_StakesSum;
    /// <summary>
    /// 牌的位置
    /// </summary>
    protected float m_CardPoints = -40f;
    /// <summary>
    ///自身的三張牌
    /// </summary>
    protected List<Card> m_CardList = new List<Card>();
    /// <summary>
    /// 牌型
    /// </summary>
    protected CardType m_CardType;
    /// <summary>
    /// 生成的三張牌的集合
    /// </summary>
    protected List<GameObject> go_SpawnCardList = new List<GameObject>();
    /// <summary>
    /// 是否開始下注
    /// </summary>
    protected bool m_IsStartStakes = false;
    /// <summary>
    /// 倒計時
    /// </summary>
    protected float m_Time = 60;
    /// <summary>
    /// 計時器
    /// </summary>
    protected float m_Tiemr = 0.0f;
    //總的下注數
    protected int m_StakesNum = 0;
    /// <summary>
    /// 是否棄牌
    /// </summary>
    public bool m_IsGaveUp = false;
    /// <summary>
    /// 開始下注
    /// </summary>
    public virtual void StartStakes()
    {
      
        m_IsStartStakes = true;//開始下注設定為true
        go_CountDown.SetActive(true);//顯示倒計時
        txt_CountDown.text = "60";//設定倒計時文本
        m_Time = 60f;//設定倒計時的時間
    }
 
    /// <summary>
    /// 下注以後
    /// </summary>
    protected virtual void StakesAffter(int coin, string str)
    {
        m_StakesCountHint.Show(coin + str);//提示
        m_StakesNum += coin;//更新下注總數
        txt_StakesSum.text = m_StakesNum.ToString();//更新總的下注數
    }

    /// <summary>
    /// 成為莊家
    /// </summary>
    public void BecomeBanker()
    {
        img_Backer.gameObject.SetActive(true);
    }
    /// <summary>
    /// 發牌
    /// </summary>
    public void DealCord(Card card, float duration, Vector3 initPos)
    {
        m_CardList.Add(card);
        GameObject go = Instantiate(go_CardPre, CardPoints);
        go.GetComponent<RectTransform>().localPosition = initPos;
        go.GetComponent<RectTransform>().DOLocalMove(new Vector3(m_CardPoints, 0, 0), duration);
        m_CardPoints += 40;
        go_SpawnCardList.Add(go);
    }

    /// <summary>
    /// 手牌排序
    /// </summary>
    protected void SordCard()
    {
        //冒泡排序
        for (int i = 0; i < m_CardList.Count - 1; i++)
        {
            for (int j = 0; j < m_CardList.Count - 1 - i; j++)
            {
                if (m_CardList[j].Weight < m_CardList[j + 1].Weight)
                {
                    Card temp = m_CardList[j];
                    m_CardList[j] = m_CardList[j + 1];
                    m_CardList[j + 1] = temp;
                }
            }
        }
    }
    /// <summary>
    /// 擷取手牌類型
    /// </summary>
    protected void GetCardType()
    {
        //532 最大
        if (m_CardList[0].Weight == 5 && m_CardList[1].Weight == 3 && m_CardList[2].Weight == 2)
        {
            m_CardType = CardType.Max;
        }
        //666 豹子
        else if (m_CardList[0].Weight == m_CardList[1].Weight && m_CardList[0].Weight == m_CardList[2].Weight)
        {
            m_CardType = CardType.Baozi;
        }
        ///765 顔色相同 順金 
        else if (m_CardList[0].Color == m_CardList[1].Color && m_CardList[0].Color == m_CardList[2].Color
                && m_CardList[0].Weight == m_CardList[1].Weight + 1 && m_CardList[0].Weight == m_CardList[2].Weight + 2)
        {
            m_CardType = CardType.Shunjin;
        }
        /// 金花 顔色相同
        else if (m_CardList[0].Color == m_CardList[1].Color && m_CardList[0].Color == m_CardList[2].Color)
        {
            m_CardType = CardType.Jinhua;
        }
        //順子 765
        else if (m_CardList[0].Weight == m_CardList[1].Weight + 1 && m_CardList[0].Weight == m_CardList[2].Weight + 2)
        {
            m_CardType = CardType.Shunzi;
        }
        //對子 665 688
        else if (m_CardList[0].Weight == m_CardList[1].Weight || m_CardList[1].Weight == m_CardList[2].Weight)
        {
            m_CardType = CardType.Duizi;
        }
        else
        {
            m_CardType = CardType.Min;
        }
    }
}

           

精簡左右兩個玩家的代碼,左右兩個玩家的基類

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

public class LeftRightBaseManager_Stand : BaseManager_Stand
{
    private GameObject txt_Ready;
    private GameObject txt_GiveUp;
   
    /// <summary>
    /// 下注的總數
    /// </summary>
    private int m_StakesSum=0;
    /// <summary>
    /// 下注的等待時間
    /// </summary>
    private float m_RandomWaitStakesTime = 0;
    /// <summary>
    /// 是否有下注次數
    /// </summary>
    private bool m_IsHasStakesNum = false;


    private List<GameObject> m_SpawnCardList = new List<GameObject>();

    private void Awake()
    {
        Init();
    }
    protected void FixedUpdate()
    {
        if (m_IsStartStakes)
        {
            if (m_RandomWaitStakesTime <= 0)//等待時間結束,開始下注
            {
                //開始下注
                PutStakes();
                m_IsStartStakes = false;
                go_CountDown.SetActive(false);
                m_ZjhManager.SetNextPlayerStakes();
                return;
            }
            m_Tiemr += Time.deltaTime;
            if (m_Tiemr >= 1)
            {
                m_RandomWaitStakesTime--;
                m_Tiemr = 0;
                m_Time--;
                txt_CountDown.text = m_Time.ToString();

            }
        }
    }
    private void Init()
    {
        txt_GiveUp = transform.Find("txt_GiveUp").gameObject;
        m_StakesCountHint = transform.Find("StakesCountHint").GetComponent<StakesCountHint>();
      
        m_ZjhManager = GetComponentInParent<ZjhManager_Stand>();
        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>();
        txt_Ready = transform.Find("txt_Ready").gameObject;
        CardPoints = transform.Find("CardPosition");
        img_HeadIcon = transform.Find("img_HeadIcon").GetComponent<Image>();


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

        img_Backer.gameObject.SetActive(false);
        go_CountDown.SetActive(false);

        txt_StakesSum.text = "0";
    }

    /// <summary>
    /// 下注
    /// </summary>
    private void PutStakes()
    {
        if (m_IsHasStakesNum)
        {
            m_StakesNum--;
            if (m_StakesSum <= 0)
            {
                GetPutStakesNum();//擷取下注次數
                //比牌 TODO
                return;
            }
            //調用zjhmanager的下注方法
            int stakesNum = m_ZjhManager.Stakes(Random.Range(3, 6));
            StakesAffter(stakesNum, "不看");
        }
        else if (m_CardType == CardType.Duizi)
        {
            int rand = Random.Range(0, 10);      
            if (rand < 5)//跟注
            {
                StakesAffter(m_ZjhManager.Stakes(Random.Range(3, 6)), "不看");
            }
            else//比牌
            {

            }
        }
        else if (m_CardType == CardType.Min)
        {
            int rand = Random.Range(0, 15);
            if (rand < 5)//跟注
            {
                StakesAffter(m_ZjhManager.Stakes(Random.Range(3, 6)), "不看");
            }
            else if (rand > 5 && rand < 10)//比牌
            {

            }
            else//棄牌
            {

            }
        }
        else if (m_CardType == CardType.Max || m_CardType == CardType.Baozi)
        {
            StakesAffter(m_ZjhManager.Stakes(Random.Range(5, 8)), "不看");
        }
    }
    /// <summary>
    /// 棄牌
    /// </summary>
    private void GiveUpCard()
    {
        m_IsStartStakes = false;
        txt_GiveUp.SetActive(true);
        go_CountDown.SetActive(false);
        m_ZjhManager.SetNextPlayerStakes();
        m_IsGaveUp = true;

        foreach (var item in m_SpawnCardList)
        {
            Destroy(item);
        }
    }

    /// <summary>
    /// 擷取下注次數
    /// </summary>
    private void GetPutStakesNum()
    {
        if ((int)m_CardType >= 2 && (int)m_CardType <= 4)
        {
            m_IsHasStakesNum = true;
            m_StakesNum = (int)m_CardType * 6;
        }
        Debug.Log(m_StakesNum);
    }

    /// <summary>
    /// 開始選擇莊家
    /// </summary>
    public void ChooseBanker()
    {
        m_StakesSum += Models.GameModel.BottomStakes;
        txt_StakesSum.text = m_StakesSum.ToString();
        txt_Ready.SetActive(false);
    }


    /// <summary>
    /// 發牌結束
    /// </summary>
    public void DealCardFinished()
    {
        SordCard();
        GetCardType();
        print("右邊玩家的牌型是:" + m_CardType);
    }

    /// <summary>
    ///開始下注
    /// </summary>
    public override void StartStakes()
    {
        base.StartStakes();
        m_RandomWaitStakesTime = Random.Range(3, 6);
    }

}

           

左右玩家的腳本

左右玩家的腳本相同

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

public class RightManager_Stand : LeftRightBaseManager_Stand
{
   
}

           

自身玩家的腳本

using Protocol.Code;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class SelfManager_Stand : BaseManager_Stand
{
    #region 字段  
    private GameObject go_BottomButton;
    private Text txt_UserName;
    private Text txt_CoinCount;
    private Button btn_Ready;
    private GameObject txt_GiveUp; 
    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;  
   
    #endregion

    #region Unity回調
    private void Awake()
    {
        Init();
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener<int>(EventDefine.SendRechargePanel, UpdateCoinCount);
    }

    private void FixedUpdate()
    {
        if (m_IsStartStakes)
        {
            if (m_Time <= 0)
            {
                //倒計時結束,
                //預設跟注
                OnFollowStakesButtonClick();
                m_Time = 60;
            }
            m_Tiemr += Time.deltaTime;
            if (m_Tiemr>=1)
            {
                m_Tiemr = 0;
                m_Time--;
                txt_CountDown.text = m_Time.ToString();

            }
        }
    }
    #endregion


    void Init()
    {
        m_StakesCountHint = transform.Find("StakesCountHint").GetComponent<StakesCountHint>();
        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_LookCard.onClick.AddListener(OnLockCardButtonClick);

        btn_FollowStakes = go_BottomButton.transform.Find("btn_FollowStakes").GetComponent<Button>();
        btn_FollowStakes.onClick.AddListener(OnFollowStakesButtonClick);

        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";
        if (Models.GameModel.userDto!=null)
        {
            img_HeadIcon.sprite = ResourcesManager.GetSprite(Models.GameModel.userDto.IconName);
            txt_UserName.text = Models.GameModel.userDto.UserName;
            txt_CoinCount.text = Models.GameModel.userDto.Coin.ToString();
        }      
    }

    /// <summary>
    /// 跟注按鈕的點選
    /// </summary>
    private void OnFollowStakesButtonClick()
    {
        int stakes = m_ZjhManager.Stakes(0);
        m_ZjhManager.SetNextPlayerStakes();
        m_IsStartStakes = false;//是否下注
        go_CountDown.SetActive(false);//倒計時
        SetBottomButtoninteractable(false);//設定底部按鈕
        StakesAffter(stakes,"不看");//跟注提示
    }
    /// <summary>
    /// 下注以後的事
    /// </summary>
    /// <param name="coin"></param>
    /// <param name="str"></param>
    protected override void StakesAffter(int coin, string str)
    {
        base.StakesAffter(coin, str);
        if (NetMsgCenter.Instance != null)//更新金币
        {
            NetMsgCenter.Instance.SendMsg(OpCode.Account, AccountCode.GetRecharge_CREQ, -coin);
        }
    }

    /// <summary>
    /// 準備按鈕的點選
    /// </summary>
    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();
    }

    /// <summary>
    /// 設定底部的按鈕
    /// </summary>
    private void SetBottomButtoninteractable(bool 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 OnLockCardButtonClick()
    {
        btn_LookCard.interactable = false;
        for (int i = 0; i < m_CardList.Count; i++)
        {
            string name = "card_" + m_CardList[i].Color + "_" + m_CardList[i].Weight;
            go_SpawnCardList[i].GetComponent<Image>().sprite = ResourcesManager.LoadCardSprite(name);
        }
    }

    /// <summary>
    /// 更新金币後的調用
    /// </summary>
    private void UpdateCoinCount(int value)
    {
        Models.GameModel.userDto.Coin = value;
        txt_CoinCount.text = value.ToString();
    }
    /// <summary>
    /// 發牌結束
    /// </summary>
    public void DealCardFinished()
    {
        go_BottomButton.SetActive(true);
        SetBottomButtoninteractable(false);
        SordCard();
        GetCardType();
        print("自身玩家的牌型是:"+m_CardType);
    }

    public override void StartStakes()
    {
        base.StartStakes();
        SetBottomButtoninteractable(true);//設定底部的按鈕為可互動
    }

}