Zjh游戏(二十二)单机玩家比牌

萝らか妹 提交于 2019-12-20 18:58:26

单机玩家比牌

  • 在单机玩家的脚本中下注的方法中,会根据牌型,来进行决定是比牌、跟注、弃牌的操作
  • 在左右玩家的脚本基类中,创建一个比牌抽象类,在左右两个玩家中实现
  • 在下注方法中的比牌操作中,调用比牌的方法
  • 修改:在第二十骗博客中,写道,在左右玩家的脚本基类中的FixeUpdate中,调用下注的方法后,就把是否下注设置为false并且隐藏了倒计时,这里应该判断是否在比牌,如果不在比牌,则把下注设置为false,隐藏倒计时

LeftRightBaseManager_Stand
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
左右单机玩家实现抽象类

  • 在左右两个玩家的脚本中各自实现比牌的抽象类
  • 在ZhjManager_Stand中定义两个方法,一个左边玩家比牌一个右边玩家的比牌
  • 左边玩家比牌:如果自身玩家没有弃牌就和自身玩家比牌,否则就和右边玩家比牌
  • 右边同理
  • 在左右玩家的脚本中调用各自的脚本

创建比牌的界面
在这里插入图片描述
创建比牌的脚本VSPanel

  • 创建一个比牌玩家类,并进行序列化
  • 创建比牌类,设置UI,把比牌界面上的牌显示出来,
  • 实现比牌的逻辑
  • 设置比牌界面的显示以及关闭
  • 比牌界面显示后等到2s自动关闭
  • 关闭时调用比牌结果的方法。比牌结果的方法为设置下注下标,设置倒计时的显示或者隐藏
  • 监听和自身玩家比牌以及单机玩家的比牌的方法

VSPanel

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class VSPanel : MonoBehaviour
{
    [System.Serializable]
    public class Player
    {
        public Text txt_Name;
        public Image[] cardsArray;
        public Image img_Loser;
        public Image img_Win;
    }
    public float m_DelayTime = 2;
    public Player m_CamparePlayer;
    public Player m_CamparedPlayer;

    private BaseManager_Stand compare;
    private BaseManager_Stand compareD;

    private void Awake()
    {
        EventCenter.AddListener<BaseManager_Stand, BaseManager_Stand, string, string>(EventDefine.VSWithSelf, VSWithSelf);

        EventCenter.AddListener<BaseManager_Stand, BaseManager_Stand>(EventDefine.VSAI, CompareCard);
    }
    private void OnDestroy()
    {
        EventCenter.RemoveListener<BaseManager_Stand, BaseManager_Stand, string, string>(EventDefine.VSWithSelf, VSWithSelf);

        EventCenter.RemoveListener<BaseManager_Stand, BaseManager_Stand>(EventDefine.VSAI, CompareCard);
    }

    //延迟关闭比牌面板
    IEnumerator Delay()
    {
        yield return new WaitForSeconds(m_DelayTime);
        transform.DOScale(Vector3.zero,0.3f);
    }
    /// <summary>
    /// 延迟比较者胜利
    /// 比牌面板关闭后在继续执行
    /// </summary>
    IEnumerator CompareWin()
    {
        yield return new WaitForSeconds(m_DelayTime);
        compare.Win();
        compareD.Lose();
    }   
    /// <summary>
    /// 延迟被比较者胜利
    /// </summary>
    IEnumerator CompareDWin()
    {
        yield return new WaitForSeconds(m_DelayTime);
        compare.Lose();
        compareD.Win();
    }

    private void VSWithSelf(BaseManager_Stand compare,BaseManager_Stand compared,string campareName,string camparedName)
    {
        //设置为比牌界面的缩放为1,0.3f后延迟2s关闭比牌界面
        transform.DOScale(Vector3.one, 0.3f).OnComplete(()=> 
        {
            StartCoroutine(Delay());
        });
        //设置UI
        m_CamparePlayer.img_Loser.gameObject.SetActive(false);
        m_CamparePlayer.img_Win.gameObject.SetActive(false);
        m_CamparedPlayer.img_Loser.gameObject.SetActive(false);
        m_CamparedPlayer.img_Win.gameObject.SetActive(false);
        m_CamparePlayer.txt_Name.text = campareName;
        m_CamparedPlayer.txt_Name.text = camparedName;
        //显示比牌玩家的牌
        for (int i = 0; i < compare.m_CardList.Count; i++)
        {
            string name = "card_" + compare.m_CardList[i].Color + "_" + compare.m_CardList[i].Weight;
            m_CamparePlayer.cardsArray[i].sprite = ResourcesManager.LoadCardSprite(name);
        }
        for (int i = 0; i < compared.m_CardList.Count; i++)
        {
            string name = "card_" + compared.m_CardList[i].Color + "_" + compared.m_CardList[i].Weight;
            m_CamparedPlayer.cardsArray[i].sprite = ResourcesManager.LoadCardSprite(name);
        }
        //调用比牌的方法
        CompareCard(compare, compared);
    }
    /// <summary>
    /// 比牌的逻辑算法
    /// </summary>
    private void CompareCard(BaseManager_Stand compare, BaseManager_Stand compareD)
    {
        this.compare = compare;
        this.compareD = compareD;
        //比较者的牌型大于被比较这的牌型
        if (compare.m_CardType > compareD.m_CardType)
        {
            //比较者赢
            StartCoroutine(CompareWin());
            m_CamparePlayer.img_Win.gameObject.SetActive(true);
            m_CamparedPlayer.img_Loser.gameObject.SetActive(true);
        }
        //两者牌型相同
        else if (compare.m_CardType==compareD.m_CardType)
        {
            if (compare.m_CardType==CardType.Min)
            {
                CompareCardWhenMin(compare,compareD);
            }
            if (compare.m_CardType==CardType.Duizi)
            {
                //663 662 766 866
                //记录对子和单张的大小
                int CompareDuiziValue = 0, CompareDanValue = 0, CompareDduiziValue = 0, CompareDdanValue = 0;
                //比较者  663的情况
                if (compare.m_CardList[0]==compare.m_CardList[1])
                {
                    CompareDuiziValue = compare.m_CardList[0].Weight;
                    CompareDanValue= compare.m_CardList[2].Weight;
                }
                //766的情况
                if (compare.m_CardList[1]==compare.m_CardList[2])
                {
                    CompareDuiziValue = compare.m_CardList[1].Weight;
                    CompareDanValue= compare.m_CardList[0].Weight;
                }

                 //被比较者  663的情况
                if (compareD.m_CardList[0]== compareD.m_CardList[1])
                {
                    CompareDduiziValue = compareD.m_CardList[0].Weight;
                    CompareDdanValue = compareD.m_CardList[2].Weight;
                }
                //766的情况
                if (compare.m_CardList[1]== compareD.m_CardList[2])
                {
                    CompareDduiziValue = compareD.m_CardList[1].Weight;
                    CompareDdanValue = compareD.m_CardList[0].Weight;
                }

                if (CompareDuiziValue> CompareDduiziValue)
                {
                    //比较者赢
                    StartCoroutine(CompareWin());
                    m_CamparePlayer.img_Win.gameObject.SetActive(true);
                    m_CamparedPlayer.img_Loser.gameObject.SetActive(true);
                }
                else if (CompareDuiziValue ==CompareDduiziValue)
                {
                    if (CompareDanValue== CompareDdanValue)
                    {
                        //比较者赢
                        StartCoroutine(CompareWin());
                        m_CamparePlayer.img_Win.gameObject.SetActive(true);
                        m_CamparedPlayer.img_Loser.gameObject.SetActive(true);
                    }
                    else
                    {
                        //被比较者赢
                        StartCoroutine(CompareDWin());
                        m_CamparePlayer.img_Loser.gameObject.SetActive(true);
                        m_CamparedPlayer.img_Win.gameObject.SetActive(true);

                    }
                }
                else
                {
                    //被比较者赢
                    StartCoroutine(CompareDWin());
                    m_CamparePlayer.img_Loser.gameObject.SetActive(true);
                    m_CamparedPlayer.img_Win.gameObject.SetActive(true);
                }
            }
            if (compare.m_CardType==CardType.Shunzi|| compare.m_CardType == CardType.Shunjin|| compare.m_CardType == CardType.Baozi)
            {
                //三个牌值加起来进行比较
                int compareSum = 0, compareDSum = 0;
                for (int i = 0; i < compare.m_CardList.Count; i++)
                {
                    compareSum += compare.m_CardList[i].Weight;
                }
                for (int i = 0; i < compareD.m_CardList.Count; i++)
                {
                    compareDSum += compareD.m_CardList[i].Weight;
                }
                if (compareSum>compareDSum)
                {
                    //比较者赢
                    StartCoroutine(CompareWin());
                    m_CamparePlayer.img_Win.gameObject.SetActive(true);
                    m_CamparedPlayer.img_Loser.gameObject.SetActive(true);
                }
                else
                {
                    //被比较者赢
                    StartCoroutine(CompareDWin());
                    m_CamparePlayer.img_Loser.gameObject.SetActive(true);
                    m_CamparedPlayer.img_Win.gameObject.SetActive(true);
                }
            }
            if (compare.m_CardType==CardType.Jinhua)
            {
                CompareCardWhenMin(compare, compareD);
            }
            if (compare.m_CardType==CardType.Max)
            {
                //被比较者赢
                StartCoroutine(CompareDWin());
                m_CamparePlayer.img_Loser.gameObject.SetActive(true);
                m_CamparedPlayer.img_Win.gameObject.SetActive(true);
            }
        }
        //小于
        else
        {
            //被比较者赢
            StartCoroutine(CompareDWin());
            m_CamparePlayer.img_Loser.gameObject.SetActive(true);
            m_CamparedPlayer.img_Win.gameObject.SetActive(true);
        }
    }
    //最小和金花的比较方法
    private void CompareCardWhenMin(BaseManager_Stand compare, BaseManager_Stand compareD)
    {
        //比较第一张牌
        if (compare.m_CardList[0].Weight > compareD.m_CardList[0].Weight)
        {
            //比较者赢
            StartCoroutine(CompareWin());
            m_CamparePlayer.img_Win.gameObject.SetActive(true);
            m_CamparedPlayer.img_Loser.gameObject.SetActive(true);
        }
        //第一张牌相同
        else if (compare.m_CardList[0].Weight == compareD.m_CardList[0].Weight)
        {
            //比较第二张牌
            if (compare.m_CardList[1].Weight > compareD.m_CardList[1].Weight)
            {
                //比较者赢
                StartCoroutine(CompareWin());
                m_CamparePlayer.img_Win.gameObject.SetActive(true);
                m_CamparedPlayer.img_Loser.gameObject.SetActive(true);
            }
            else if (compare.m_CardList[1].Weight == compareD.m_CardList[1].Weight)
            {
                //比较第三张
                if (compare.m_CardList[2].Weight == compareD.m_CardList[2].Weight)
                {
                    //比较者赢
                    StartCoroutine(CompareWin());
                    m_CamparePlayer.img_Win.gameObject.SetActive(true);
                    m_CamparedPlayer.img_Loser.gameObject.SetActive(true);
                }
                else
                {
                    //被比较者赢
                    StartCoroutine(CompareDWin());
                    m_CamparePlayer.img_Loser.gameObject.SetActive(true);
                    m_CamparedPlayer.img_Win.gameObject.SetActive(true);
                }
            }
            else
            {
                //被比较者赢
                StartCoroutine(CompareDWin());
                m_CamparePlayer.img_Loser.gameObject.SetActive(true);
                m_CamparedPlayer.img_Win.gameObject.SetActive(true);
            }
        }
        else
        {
            //被比较者赢
            StartCoroutine(CompareDWin());
            m_CamparePlayer.img_Loser.gameObject.SetActive(true);
            m_CamparedPlayer.img_Win.gameObject.SetActive(true);
        }
    }
}


实现玩家的比牌结果的方法

  • 在BaseManager_Stand中定义两个抽象类,Win和Lose,LeftRightBaseManager_Stand和SelfManager_Stand中实现
  • Lose为调用GiveUp的方法
  • 在三个脚本各自重新Win方法

在这里插入图片描述

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


在ZhjManager_Stand中创建的左右玩家的比牌方法中,广播VSPanel中的监听的方法

在这里插入图片描述

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