local MJDefine=MJFramework.MJImport("mj.luaScript.MJDefine") local MJ=MJFramework.MJImport("mj.luaScript.Views.Game.MJ") local ncmajTangView = class("ncmajTangView", cc.UIView) function ncmajTangView:ctor(handCards,desktopType) ncmajTangView.super.ctor(self) --self._tingCardData = nil; self:loadUI() self.desktopType = desktopType --self:initData(handCards,desktopType) end function ncmajTangView:loadUI() local ui = loadUI("mj_ncmaj/res/ui_fangjian/mj_ncmaj_tangView.ui") self:addChild(ui) self.ui = ui self.ui.Items.Button_QueDing:registerClick(handler(self, self.onClickSure)) self.ui.Items.Button_QuXiao:registerClick(handler(self, self.onClickCancel)) end function ncmajTangView:initData(handCards,desktopType) self:clearTangViewData() self.handCards = self.handCards or {} local thandCards = handCards local handCardsNum = #handCards table.sort( thandCards, function(c1, c2) return c1.card < c2.card end ) --self.ui.Items.Layout_card:setSize((cc.size(70*handCardsNum,115))) local startPos = cc.p(36,46) local startX = startPos.x local iWidth = 0--牌间距 local iHeight = 0--牌高度 for i,v in pairs(thandCards) do --mj=MJ:new(v.card,MJDefine.MJType.Stand,MJDefine.MyViewId,self.desktopType) local CardClass local mjType = MJDefine.MJConfig_2d.MJType.Stand if self.desktopType ~= MJDefine.DesktopType.ThreeD then CardClass = require(MJDefine.MJConfig_2d.MAHJONG_CARD) else CardClass = require(MJDefine.MJConfig_3d.MAHJONG_CARD) mjType = MJDefine.MJConfig_3d.MJType.Stand end local mj = CardClass:new(v.card, MJDefine.MyViewId, mjType, 1) mj.unique = i local iScale = 0.75 iWidth = mj:getSize().width * iScale iHeight = mj:getSize().height * iScale --mj:setAutoSize(false) --mj:setSize(cc.size(40,65)) --mj:setSize(cc.size(70,95)) mj:setScale(iScale) mj:setPosition(cc.p(startX,startPos.y)) startX = startX + iWidth --[[mj:registerClick(function() playBtnEffect() if not mj.isXuanZhong then mj.isXuanZhong = true --mj:setYellow() local color = cc.c3b(218, 218, 128) mj:setColor(color) elseif mj.isXuanZhong and mj.isXuanZhong == true then mj.isXuanZhong = false mj:setNormalColor() end end)--]] self.ui.Items.Layout_card:addChild(mj) table.insert(self.handCards,mj) end self.ui.Items.ImageView_bg:setSize((cc.size(iWidth*handCardsNum+20,iHeight+30))) self.ui.Items.Layout_card:setSize((cc.size(iWidth*handCardsNum+10,115))) --注册触摸事件 self:registerTouch(handler(self,self.onTouchBegan), handler(self,self.onTouchMove), handler(self,self.onTouchEnd)) end function ncmajTangView:checkTouches( touch ) local rect = nil local node = nil local touchPos=self:convertToNodeSpace(touch:getLocation()) for k,v in pairs(self.handCards) do local width = v:getContentSize().width*v:getScaleX() local height = v:getContentSize().height*v:getScaleY() local x = v:getWorldPosition().x-width/2 local y = v:getWorldPosition().y-height/2 local rect = cc.Rectangle:new(x,y,width,height) if rect:contains(touchPos.x,touchPos.y) then node=v break end end return node end function ncmajTangView:onTouchBegan(pt) self._selectCards = {} local node = self:checkTouches(pt) local isValidTouch = false if node and node.unique>0 then isValidTouch = true self._selectCards[node.unique] = node end self:doSelectColor() if isValidTouch == true then return true else return false end end function ncmajTangView:onTouchMove( pt ) local node = self:checkTouches(pt) if node and self._selectCards[node.unique]==nil then self._selectCards[node.unique] = node end self:doSelectColor() end function ncmajTangView:onTouchEnd( pt ) self:doSelect() self:checkShowQueDing() end function ncmajTangView:doSelect() local i = 0 for _,mj in pairs(self._selectCards) do i = i + 1 if (not tolua.isnull(mj)) and mj then if not mj.isXuanZhong then mj.isXuanZhong = true --mj:setYellow() local color = cc.c3b(218, 218, 128) mj:setColor(color) elseif mj.isXuanZhong and mj.isXuanZhong == true then mj.isXuanZhong = false mj:setNormalColor() end end end end function ncmajTangView:checkShowQueDing() local selectCards = self:getSelectCards() if selectCards and type(selectCards) == 'table' and #selectCards> 0 then self.ui.Items.Button_QueDing:setVisible(true) else self.ui.Items.Button_QueDing:setVisible(false) end end function ncmajTangView:doSelectColor() for _,mj in pairs(self._selectCards) do if not tolua.isnull(mj) and mj then if not mj.isXuanZhong then local color = cc.c3b(218, 218, 128) mj:setColor(color) elseif mj.isXuanZhong and mj.isXuanZhong == true then mj:setNormalColor() end end end end function ncmajTangView:getSelectCards() local tTable = {} for _,mj in pairs(self.handCards) do if not tolua.isnull(mj) and mj then if mj.isXuanZhong and mj.isXuanZhong == true then table.insert(tTable,mj:getValue()) end end end return tTable end -- 确定 function ncmajTangView:onClickSure() playBtnEffect() local selectCards = self:getSelectCards() --请求亮牌 app.room:requestTangCards(selectCards,self.curSelectCard) --self:onClickClose() end -- 取消 function ncmajTangView:onClickCancel() playBtnEffect() if self.quxiaoCB then self.quxiaoCB() end self:onClickClose() end -- 关闭 function ncmajTangView:onClickClose() self:getEventDispatcher():removeEventListenersForTarget(self) self:removeFromParent() end function ncmajTangView:setQuXiaoCallBack(cb) if cb then self.quxiaoCB = cb end end --只显示取消按钮 function ncmajTangView:showCancelBtnOnly() self:getEventDispatcher():removeEventListenersForTarget(self) self.ui.Items.ImageView_bg:setVisible(false) self.ui.Items.Layout_card:setVisible(false) self.ui.Items.Button_QueDing:setVisible(false) self.ui.Items.Button_QuXiao:setVisible(true) self.ui.Items.ImageView_bg_1:setVisible(false) end --清空躺界面牌数据 function ncmajTangView:clearTangViewData() if self.handCards and #self.handCards then for i = #self.handCards,1,-1 do table.remove(self.handCards,i) end end self.handCards = nil self.ui.Items.Layout_card:removeAllChildren() end --显示躺牌界面 function ncmajTangView:showTangView(handCards,sltCard,desktopType)--手牌 选中的牌 牌类型 self.ui.Items.ImageView_bg:setVisible(true) self.ui.Items.Layout_card:setVisible(true) --self.ui.Items.Button_QueDing:setVisible(true) self.ui.Items.Button_QuXiao:setVisible(true) self.ui.Items.ImageView_bg_1:setVisible(true) self.curSelectCard = sltCard for i,v in pairs(handCards) do if v.card == sltCard then table.remove(handCards,i) break end end local showCards = {}--精简后可以躺后的展示牌(之前是全手牌都显示) local handCardsNum = #handCards local curTingCards = app.room:getTingCardsByCard(sltCard); local tingNums = #curTingCards local tingColorNums,tingColorsCards = self:checkCardsColorNums(curTingCards) --从手牌找出所有与听牌相同花色的牌,如听万,则找出所有的万牌,过滤掉其他花色的牌 local newHandCards = self:getCardsByColor(handCards,tingColorsCards) if tingNums == 1 then local isQiDui = self:checkIsQiDui(handCards) local curTCard = curTingCards[1].card local tingCardLeftOneCard = {{card = curTCard-1}}--听牌左边的一张牌,如听三同,这里就是2同 local tingCardRightOneCard = {{card = curTCard+1}}--听牌右边的一张牌,如听七同,这里就是8同 local tingCardLeftTwoCards = {{card = curTCard-1},{card = curTCard-2}}--听牌左边的两张牌,如听三同,这里就是1同和2同 local tingCardRightTwoCards = {{card = curTCard+1},{card = curTCard+2}}--听牌右边的两张牌,如听七同,这里就是8同和9同 local tingCardLRTwoCards = {{card = curTCard-1},{card = curTCard+1}}--听牌左右边两边各一张牌,如听五同,这里就是4同和6同 local isHaveTingCard = false--手牌是否有听的这张牌 if self:checkIsContainCards(newHandCards,curTingCards) then local fCards = self:getContainCards(newHandCards,curTingCards) showCards = self:mergeCards(showCards,fCards) isHaveTingCard = true end local isBianZhang = false if not isQiDui and math.floor(curTCard%0x10) == 3 and self:checkIsContainCards(newHandCards,tingCardLeftTwoCards) then local fCards = self:getContainCards(newHandCards,tingCardLeftTwoCards) showCards = self:mergeCards(showCards,fCards) local fCards1 = self:getContainCards(newHandCards,tingCardRightOneCard) showCards = self:mergeCards(showCards,fCards1) isBianZhang = true end if not isQiDui and math.floor(curTCard%0x10) == 7 and self:checkIsContainCards(newHandCards,tingCardRightTwoCards) then local fCards = self:getContainCards(newHandCards,tingCardRightTwoCards) showCards = self:mergeCards(showCards,fCards) local fCards1 = self:getContainCards(newHandCards,tingCardLeftOneCard) showCards = self:mergeCards(showCards,fCards1) isBianZhang = true end if not isQiDui and (not isBianZhang) and self:checkIsContainCards(newHandCards,tingCardLRTwoCards) then local fCards = self:getContainCards(newHandCards,tingCardLRTwoCards) showCards = self:mergeCards(showCards,fCards) end elseif tingNums == 2 then local curTCard = {} for i,v in pairs(curTingCards) do table.insert(curTCard,{card = v.card}) end --排序 table.sort( curTCard, function(c1, c2) return c1.card < c2.card end ) local tingCardMidTwoCards = {{card = curTCard[1].card+1},{card = curTCard[2].card-1}}--两张听牌中间的两张牌,如听三六同,这里就是4同和5同 if self:checkIsContainCards(newHandCards,curTCard) then local fCards = self:getContainCards(newHandCards,curTCard) local firstCards = self:getContainCards(newHandCards,{curTCard[1]}) local secondCards = self:getContainCards(newHandCards,{curTCard[2]}) --if #firstCards == 2 and #secondCards == 2 then showCards = self:mergeCards(showCards,fCards) --end end --胡的两张牌不连续(连续的话tingCardMidTwoCards与curTCard是一样的),防止重复加入 if self:checkIsContainCards(newHandCards,tingCardMidTwoCards) and curTCard[1].card+1 ~= curTCard[2].card then local fCards = self:getContainCards(newHandCards,tingCardMidTwoCards) showCards = self:mergeCards(showCards,fCards) end elseif tingNums > 2 then showCards = self:mergeCards(showCards,newHandCards) end -- 如果没找到躺牌,把过滤过一次的牌加入 if #showCards == 0 then showCards = self:mergeCards(showCards,newHandCards) end self:initData(showCards,desktopType) end --检测cards里面有几种牌值,如{1,1,2,2,3,4,5},返回5,{1,2,3,4,5} function ncmajTangView:checkCardValueNums(cards) if not cards then return 0 end local tCards = {} for i,v in pairs(cards) do local isHaveCardValue = false for j,k in pairs(tCards) do if v.card == k.card then isHaveCardValue = true end end if not isHaveCardValue then table.insert(tCards,v) end end return #tCards,tCards end --检测一组牌里面是否包含了另一组牌,如果scrCards包含了cards所有的牌则返回ture,否则返回false function ncmajTangView:checkIsContainCards(scrCards,cards) for i,v in pairs(cards) do local isHave = false for j,k in pairs(scrCards) do if v.card == k.card then isHave = true break end end if not isHave then return false end end return true end --传入一组牌,检测牌有几种类型(万同条),如有万同条则返回3,{1,2,3} function ncmajTangView:checkCardsColorNums(cards) if not cards then return 0 end local tCards = {} for i,v in pairs(cards) do local color = math.floor(v.card / 0x10) local isHaveColor = false for j,k in pairs(tCards) do if k == color then isHaveColor = true end end if not isHaveColor then table.insert(tCards,color) end end return #tCards,tCards end --从scrCards把包含了cards所以的牌找出来,如scrCards={1,1,2,2,3,4,5},cards={1,2,3},则返回{1,1,2,2,3} function ncmajTangView:getContainCards(scrCards,cards) local dCards = {} for i,v in pairs(cards) do for j,k in pairs(scrCards) do if v.card == k.card then table.insert(dCards,v) end end end return dCards end --从scrCards找出指定花色的牌,cards里面是花色 function ncmajTangView:getCardsByColor(scrCards,cards) if not scrCards then return nil end if not cards then return scrCards end local tCards = {} for i,v in pairs(cards) do for j,k in pairs(scrCards) do local color = math.floor(k.card / 0x10) if v == color then table.insert(tCards,k) end end end return tCards end --把dirCards里面的牌合并到scrCards,然后返回合并后的scrCards function ncmajTangView:mergeCards(scrCards,dirCards) scrCards = scrCards or {} if not dirCards then return scrCards end for i,v in pairs(dirCards) do table.insert(scrCards,v) end return scrCards end --检查一组牌是否是7对牌 function ncmajTangView:checkIsQiDui(scrCards) if not scrCards or not type(scrCards) == 'table' then return false end if #scrCards < 13 then return false end--手牌小于13张,说明吃碰过了,不可能是7对了 local cardNum,cards = self:checkCardValueNums(scrCards) return cardNum <= 7 end return ncmajTangView