예제 #1
0
    /// <summary>
    /// 同步数据,玩家在网络不好,或是断线重连时会直接显示当前数据
    /// </summary>
    /// <param name="battle"></param>
    public void SynchronousData(Battle battle)
    {
        SendCommand.BattleCode = battle.Code;
        //if (battle.CratorID == SendCommand.UserID)
        //{
        //    BattleUI.BtnJSRoom.gameObject.SetActive(true);
        //}
        //else
        //{
        //    BattleUI.BtnJSRoom.gameObject.SetActive(false);
        //}
        if (battle.Step != BattleCommand.CreateBattleBack &&
            (int)battle.Step > (int)BattleCommand.RollDice)
        {
            IsBattleing = true;
        }
        BattleUI.MatchLoading.gameObject.SetActive(false);
        BattleUI.EnableOperation();

        //同步玩家信息
        OneSide mySide = battle.Sides.FirstOrDefault(c => c.AccountID == SendCommand.UserID);

        if (mySide == null)
        {
            return;
        }
        if (GlobalVariable.IsBattleRecordPlay && battle.CurrentSide != null)
        {
            mySide = battle.CurrentSide;
        }
        SetCurrentPlayer(mySide.Order);
        int orderOffset = mySide.Order - 1;

        BattleUI.BattleCode.text   = battle.Code;
        BattleUI.LeaveCardNum.text = battle.LibraryCardNum.ToString();

        if (ZhuoXinRenderer != null && battle.CurrentSide != null)
        {
            if (battle.CurrentSide.Order == 1)
            {
                ZhuoXinRenderer.sharedMaterial.SetTexture("_MainTex", MJZhuoTextureN);
            }
            else if (battle.CurrentSide.Order == 2)
            {
                ZhuoXinRenderer.sharedMaterial.SetTexture("_MainTex", MJZhuoTextureX);
            }
            else if (battle.CurrentSide.Order == 3)
            {
                ZhuoXinRenderer.sharedMaterial.SetTexture("_MainTex", MJZhuoTextureB);
            }
            else if (battle.CurrentSide.Order == 4)
            {
                ZhuoXinRenderer.sharedMaterial.SetTexture("_MainTex", MJZhuoTextureD);
            }
        }

        foreach (var p in BattleUI.PlayerUIList)
        {
            int newOrder = p.Order + orderOffset;
            if (newOrder > 4)
            {
                newOrder = newOrder % 4;
            }
            OneSide side = battle.Sides.FirstOrDefault(c => c.Order == newOrder);
            if (side != null)
            {
                p.Show(side.NickName, side.Face, side.TotalScore, side.Vip, System.Convert.ToInt32(side.AccountID), battle);
            }
            else
            {
                p.gameObject.SetActive(false);
            }
            if (IsBattleing)
            {
                p.GoOut.gameObject.SetActive(false);
            }
        }
        //同步卡牌信息
        foreach (var p in PlayerList)
        {
            if (battle.Sides == null)
            {
                continue;
            }
            OneSide oneSide = battle.Sides.FirstOrDefault(c => c.Order == p.Order);
            if (oneSide == null)
            {
                continue;
            }
            p.UserID = oneSide.AccountID;
            //同步手牌区
            foreach (var c in oneSide.Cards)
            {
                CardCtr cardCtr = p.HandCards.GetCard(c.ID);
                if (cardCtr == null)
                {
                    CardCtr newCard = CardLibrary.GetOrCreateCard(c.ID, c.Type, c.Num);
                    newCard.IsFront   = c.IsFront;
                    newCard.PlayerCtr = p;
                    p.AddCardToHandCards(newCard, newCard.IsNew);
                }
                else
                {
                    cardCtr.IsFront = c.IsFront;
                }
            }
            List <CardCtr> handCards = new List <CardCtr>(p.HandCards.ListCards());
            foreach (var cc in handCards)
            {
                if (!oneSide.Cards.Any(c => c.ID == cc.ID))
                {
                    p.HandCards.RemoveCard(cc);
                }
            }
            //同步出牌区
            foreach (var c in oneSide.OutCards)
            {
                CardCtr cardCtr = p.OutCards1.GetCard(c.ID);
                if (cardCtr == null)
                {
                    cardCtr = p.OutCards2.GetCard(c.ID);
                }
                if (cardCtr == null)
                {
                    cardCtr = p.OutCards3.GetCard(c.ID);
                }
                if (cardCtr == null)
                {
                    CardCtr newCard = CardLibrary.GetOrCreateCard(c.ID, c.Type, c.Num);
                    p.AddCardToOutCards(newCard);
                }
                else
                {
                    cardCtr.IsFront = c.IsFront;
                }
            }
            List <CardCtr> outCards = new List <CardCtr>(p.OutCards1.ListCards());
            outCards.AddRange(p.OutCards2.ListCards());
            outCards.AddRange(p.OutCards3.ListCards());
            foreach (var cc in outCards)
            {
                if (!oneSide.OutCards.Any(c => c.ID == cc.ID))
                {
                    p.OutCards1.RemoveCard(cc);
                    p.OutCards2.RemoveCard(cc);
                }
            }
        }
        //获取听牌列表
        List <Card> listenCards = ChessHelper.CanListenCardList(mySide.Cards);
        PlayerCtr   myPlayerCtr = PlayerList.FirstOrDefault(c => c.UserID == mySide.AccountID);

        foreach (var c in myPlayerCtr.HandCards.CardList)
        {
            if (IsCanOutCard && listenCards.Any(b => b.ID == c.ID))
            {
                ShowTing(c);
            }
            else
            {
                HideTing(c);
            }
        }
        //重新计算位置
        if ((battle.Step != BattleCommand.TakeCard && battle.Step != BattleCommand.HandOutCard) || GlobalVariable.IsBattleRecordPlay)
        {
            foreach (var p in PlayerList)
            {
                p.HandCards.AutoSettleCards();
            }
        }
        //排除可能要播动画的,其它的直接设置位置
        if (battle.Step == BattleCommand.Licensing)
        {
        }
        else if (battle.Step == BattleCommand.TakeCard)
        {
            foreach (var c in CardLibrary.ListAllShowCards())
            {
                if (c.ID == battle.CurrentSide.GetACard.ID)
                {
                    continue;
                }
                c.transform.localPosition = c.TargetLocalPosition;
            }
        }
        else if (battle.Step == BattleCommand.HandOutCard)
        {
            foreach (var c in CardLibrary.ListAllShowCards())
            {
                if (battle.CurrentSide.GetACard != null && c.ID == battle.CurrentSide.GetACard.ID)
                {
                    continue;
                }
                c.transform.localPosition = c.TargetLocalPosition;
            }
        }
        else if (battle.Step == BattleCommand.NoticeOutCard)
        {
            foreach (var c in CardLibrary.ListAllShowCards())
            {
                if (c.ID == battle.CurrentSide.TakeOutCard.ID)
                {
                    continue;
                }
                c.transform.localPosition = c.TargetLocalPosition;
            }
        }
        else if (battle.Step == BattleCommand.NoticeTouchCard)
        {
            foreach (var c in CardLibrary.ListAllShowCards())
            {
                if (battle.CurrentSide.TakeOutCard == null)
                {
                    continue;
                }
                if (c.IsFront && c.CardType == battle.CurrentSide.TakeOutCard.Type && c.Num == battle.CurrentSide.TakeOutCard.Num)
                {
                    continue;
                }
                c.transform.localPosition = c.TargetLocalPosition;
            }
        }
        else
        {
            foreach (var c in CardLibrary.ListAllShowCards())
            {
                c.transform.localPosition = c.TargetLocalPosition;
            }
        }
    }