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);//设置底部的按钮为可交互
    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值