Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

432 rader
13 KiB

  1. local MJDefine=MJFramework.MJImport("mj.luaScript.MJDefine")
  2. local MJ=MJFramework.MJImport("mj.luaScript.Views.Game.MJ")
  3. local ncmajTangView = class("ncmajTangView", cc.UIView)
  4. function ncmajTangView:ctor(handCards,desktopType)
  5. ncmajTangView.super.ctor(self)
  6. --self._tingCardData = nil;
  7. self:loadUI()
  8. self.desktopType = desktopType
  9. --self:initData(handCards,desktopType)
  10. end
  11. function ncmajTangView:loadUI()
  12. local ui = loadUI("mj_ncmaj/res/ui_fangjian/mj_ncmaj_tangView.ui")
  13. self:addChild(ui)
  14. self.ui = ui
  15. self.ui.Items.Button_QueDing:registerClick(handler(self, self.onClickSure))
  16. self.ui.Items.Button_QuXiao:registerClick(handler(self, self.onClickCancel))
  17. end
  18. function ncmajTangView:initData(handCards,desktopType)
  19. self:clearTangViewData()
  20. self.handCards = self.handCards or {}
  21. local thandCards = handCards
  22. local handCardsNum = #handCards
  23. table.sort(
  24. thandCards,
  25. function(c1, c2)
  26. return c1.card < c2.card
  27. end
  28. )
  29. --self.ui.Items.Layout_card:setSize((cc.size(70*handCardsNum,115)))
  30. local startPos = cc.p(36,46)
  31. local startX = startPos.x
  32. local iWidth = 0--牌间距
  33. local iHeight = 0--牌高度
  34. for i,v in pairs(thandCards) do
  35. --mj=MJ:new(v.card,MJDefine.MJType.Stand,MJDefine.MyViewId,self.desktopType)
  36. local CardClass
  37. local mjType = MJDefine.MJConfig_2d.MJType.Stand
  38. if self.desktopType ~= MJDefine.DesktopType.ThreeD then
  39. CardClass = require(MJDefine.MJConfig_2d.MAHJONG_CARD)
  40. else
  41. CardClass = require(MJDefine.MJConfig_3d.MAHJONG_CARD)
  42. mjType = MJDefine.MJConfig_3d.MJType.Stand
  43. end
  44. local mj = CardClass:new(v.card, MJDefine.MyViewId, mjType, 1)
  45. mj.unique = i
  46. local iScale = 0.75
  47. iWidth = mj:getSize().width * iScale
  48. iHeight = mj:getSize().height * iScale
  49. --mj:setAutoSize(false)
  50. --mj:setSize(cc.size(40,65))
  51. --mj:setSize(cc.size(70,95))
  52. mj:setScale(iScale)
  53. mj:setPosition(cc.p(startX,startPos.y))
  54. startX = startX + iWidth
  55. --[[mj:registerClick(function()
  56. playBtnEffect()
  57. if not mj.isXuanZhong then
  58. mj.isXuanZhong = true
  59. --mj:setYellow()
  60. local color = cc.c3b(218, 218, 128)
  61. mj:setColor(color)
  62. elseif mj.isXuanZhong and mj.isXuanZhong == true then
  63. mj.isXuanZhong = false
  64. mj:setNormalColor()
  65. end
  66. end)--]]
  67. self.ui.Items.Layout_card:addChild(mj)
  68. table.insert(self.handCards,mj)
  69. end
  70. self.ui.Items.ImageView_bg:setSize((cc.size(iWidth*handCardsNum+20,iHeight+30)))
  71. self.ui.Items.Layout_card:setSize((cc.size(iWidth*handCardsNum+10,115)))
  72. --注册触摸事件
  73. self:registerTouch(handler(self,self.onTouchBegan), handler(self,self.onTouchMove), handler(self,self.onTouchEnd))
  74. end
  75. function ncmajTangView:checkTouches( touch )
  76. local rect = nil
  77. local node = nil
  78. local touchPos=self:convertToNodeSpace(touch:getLocation())
  79. for k,v in pairs(self.handCards) do
  80. local width = v:getContentSize().width*v:getScaleX()
  81. local height = v:getContentSize().height*v:getScaleY()
  82. local x = v:getWorldPosition().x-width/2
  83. local y = v:getWorldPosition().y-height/2
  84. local rect = cc.Rectangle:new(x,y,width,height)
  85. if rect:contains(touchPos.x,touchPos.y) then
  86. node=v
  87. break
  88. end
  89. end
  90. return node
  91. end
  92. function ncmajTangView:onTouchBegan(pt)
  93. self._selectCards = {}
  94. local node = self:checkTouches(pt)
  95. local isValidTouch = false
  96. if node and node.unique>0 then
  97. isValidTouch = true
  98. self._selectCards[node.unique] = node
  99. end
  100. self:doSelectColor()
  101. if isValidTouch == true then
  102. return true
  103. else
  104. return false
  105. end
  106. end
  107. function ncmajTangView:onTouchMove( pt )
  108. local node = self:checkTouches(pt)
  109. if node and self._selectCards[node.unique]==nil then
  110. self._selectCards[node.unique] = node
  111. end
  112. self:doSelectColor()
  113. end
  114. function ncmajTangView:onTouchEnd( pt )
  115. self:doSelect()
  116. self:checkShowQueDing()
  117. end
  118. function ncmajTangView:doSelect()
  119. local i = 0
  120. for _,mj in pairs(self._selectCards) do
  121. i = i + 1
  122. if (not tolua.isnull(mj)) and mj then
  123. if not mj.isXuanZhong then
  124. mj.isXuanZhong = true
  125. --mj:setYellow()
  126. local color = cc.c3b(218, 218, 128)
  127. mj:setColor(color)
  128. elseif mj.isXuanZhong and mj.isXuanZhong == true then
  129. mj.isXuanZhong = false
  130. mj:setNormalColor()
  131. end
  132. end
  133. end
  134. end
  135. function ncmajTangView:checkShowQueDing()
  136. local selectCards = self:getSelectCards()
  137. if selectCards and type(selectCards) == 'table' and #selectCards> 0 then
  138. self.ui.Items.Button_QueDing:setVisible(true)
  139. else
  140. self.ui.Items.Button_QueDing:setVisible(false)
  141. end
  142. end
  143. function ncmajTangView:doSelectColor()
  144. for _,mj in pairs(self._selectCards) do
  145. if not tolua.isnull(mj) and mj then
  146. if not mj.isXuanZhong then
  147. local color = cc.c3b(218, 218, 128)
  148. mj:setColor(color)
  149. elseif mj.isXuanZhong and mj.isXuanZhong == true then
  150. mj:setNormalColor()
  151. end
  152. end
  153. end
  154. end
  155. function ncmajTangView:getSelectCards()
  156. local tTable = {}
  157. for _,mj in pairs(self.handCards) do
  158. if not tolua.isnull(mj) and mj then
  159. if mj.isXuanZhong and mj.isXuanZhong == true then
  160. table.insert(tTable,mj:getValue())
  161. end
  162. end
  163. end
  164. return tTable
  165. end
  166. -- 确定
  167. function ncmajTangView:onClickSure()
  168. playBtnEffect()
  169. local selectCards = self:getSelectCards()
  170. --请求亮牌
  171. app.room:requestTangCards(selectCards,self.curSelectCard)
  172. --self:onClickClose()
  173. end
  174. -- 取消
  175. function ncmajTangView:onClickCancel()
  176. playBtnEffect()
  177. if self.quxiaoCB then
  178. self.quxiaoCB()
  179. end
  180. self:onClickClose()
  181. end
  182. -- 关闭
  183. function ncmajTangView:onClickClose()
  184. self:getEventDispatcher():removeEventListenersForTarget(self)
  185. self:removeFromParent()
  186. end
  187. function ncmajTangView:setQuXiaoCallBack(cb)
  188. if cb then
  189. self.quxiaoCB = cb
  190. end
  191. end
  192. --只显示取消按钮
  193. function ncmajTangView:showCancelBtnOnly()
  194. self:getEventDispatcher():removeEventListenersForTarget(self)
  195. self.ui.Items.ImageView_bg:setVisible(false)
  196. self.ui.Items.Layout_card:setVisible(false)
  197. self.ui.Items.Button_QueDing:setVisible(false)
  198. self.ui.Items.Button_QuXiao:setVisible(true)
  199. self.ui.Items.ImageView_bg_1:setVisible(false)
  200. end
  201. --清空躺界面牌数据
  202. function ncmajTangView:clearTangViewData()
  203. if self.handCards and #self.handCards then
  204. for i = #self.handCards,1,-1 do
  205. table.remove(self.handCards,i)
  206. end
  207. end
  208. self.handCards = nil
  209. self.ui.Items.Layout_card:removeAllChildren()
  210. end
  211. --显示躺牌界面
  212. function ncmajTangView:showTangView(handCards,sltCard,desktopType)--手牌 选中的牌 牌类型
  213. self.ui.Items.ImageView_bg:setVisible(true)
  214. self.ui.Items.Layout_card:setVisible(true)
  215. --self.ui.Items.Button_QueDing:setVisible(true)
  216. self.ui.Items.Button_QuXiao:setVisible(true)
  217. self.ui.Items.ImageView_bg_1:setVisible(true)
  218. self.curSelectCard = sltCard
  219. for i,v in pairs(handCards) do
  220. if v.card == sltCard then
  221. table.remove(handCards,i)
  222. break
  223. end
  224. end
  225. local showCards = {}--精简后可以躺后的展示牌(之前是全手牌都显示)
  226. local handCardsNum = #handCards
  227. local curTingCards = app.room:getTingCardsByCard(sltCard);
  228. local tingNums = #curTingCards
  229. local tingColorNums,tingColorsCards = self:checkCardsColorNums(curTingCards)
  230. --从手牌找出所有与听牌相同花色的牌,如听万,则找出所有的万牌,过滤掉其他花色的牌
  231. local newHandCards = self:getCardsByColor(handCards,tingColorsCards)
  232. if tingNums == 1 then
  233. local isQiDui = self:checkIsQiDui(handCards)
  234. local curTCard = curTingCards[1].card
  235. local tingCardLeftOneCard = {{card = curTCard-1}}--听牌左边的一张牌,如听三同,这里就是2同
  236. local tingCardRightOneCard = {{card = curTCard+1}}--听牌右边的一张牌,如听七同,这里就是8同
  237. local tingCardLeftTwoCards = {{card = curTCard-1},{card = curTCard-2}}--听牌左边的两张牌,如听三同,这里就是1同和2同
  238. local tingCardRightTwoCards = {{card = curTCard+1},{card = curTCard+2}}--听牌右边的两张牌,如听七同,这里就是8同和9同
  239. local tingCardLRTwoCards = {{card = curTCard-1},{card = curTCard+1}}--听牌左右边两边各一张牌,如听五同,这里就是4同和6同
  240. local isHaveTingCard = false--手牌是否有听的这张牌
  241. if self:checkIsContainCards(newHandCards,curTingCards) then
  242. local fCards = self:getContainCards(newHandCards,curTingCards)
  243. showCards = self:mergeCards(showCards,fCards)
  244. isHaveTingCard = true
  245. end
  246. local isBianZhang = false
  247. if not isQiDui and math.floor(curTCard%0x10) == 3 and self:checkIsContainCards(newHandCards,tingCardLeftTwoCards) then
  248. local fCards = self:getContainCards(newHandCards,tingCardLeftTwoCards)
  249. showCards = self:mergeCards(showCards,fCards)
  250. local fCards1 = self:getContainCards(newHandCards,tingCardRightOneCard)
  251. showCards = self:mergeCards(showCards,fCards1)
  252. isBianZhang = true
  253. end
  254. if not isQiDui and math.floor(curTCard%0x10) == 7 and self:checkIsContainCards(newHandCards,tingCardRightTwoCards) then
  255. local fCards = self:getContainCards(newHandCards,tingCardRightTwoCards)
  256. showCards = self:mergeCards(showCards,fCards)
  257. local fCards1 = self:getContainCards(newHandCards,tingCardLeftOneCard)
  258. showCards = self:mergeCards(showCards,fCards1)
  259. isBianZhang = true
  260. end
  261. if not isQiDui and (not isBianZhang) and self:checkIsContainCards(newHandCards,tingCardLRTwoCards) then
  262. local fCards = self:getContainCards(newHandCards,tingCardLRTwoCards)
  263. showCards = self:mergeCards(showCards,fCards)
  264. end
  265. elseif tingNums == 2 then
  266. local curTCard = {}
  267. for i,v in pairs(curTingCards) do
  268. table.insert(curTCard,{card = v.card})
  269. end
  270. --排序
  271. table.sort(
  272. curTCard,
  273. function(c1, c2)
  274. return c1.card < c2.card
  275. end
  276. )
  277. local tingCardMidTwoCards = {{card = curTCard[1].card+1},{card = curTCard[2].card-1}}--两张听牌中间的两张牌,如听三六同,这里就是4同和5同
  278. if self:checkIsContainCards(newHandCards,curTCard) then
  279. local fCards = self:getContainCards(newHandCards,curTCard)
  280. local firstCards = self:getContainCards(newHandCards,{curTCard[1]})
  281. local secondCards = self:getContainCards(newHandCards,{curTCard[2]})
  282. --if #firstCards == 2 and #secondCards == 2 then
  283. showCards = self:mergeCards(showCards,fCards)
  284. --end
  285. end
  286. --胡的两张牌不连续(连续的话tingCardMidTwoCards与curTCard是一样的),防止重复加入
  287. if self:checkIsContainCards(newHandCards,tingCardMidTwoCards) and curTCard[1].card+1 ~= curTCard[2].card then
  288. local fCards = self:getContainCards(newHandCards,tingCardMidTwoCards)
  289. showCards = self:mergeCards(showCards,fCards)
  290. end
  291. elseif tingNums > 2 then
  292. showCards = self:mergeCards(showCards,newHandCards)
  293. end
  294. -- 如果没找到躺牌,把过滤过一次的牌加入
  295. if #showCards == 0 then
  296. showCards = self:mergeCards(showCards,newHandCards)
  297. end
  298. self:initData(showCards,desktopType)
  299. end
  300. --检测cards里面有几种牌值,如{1,1,2,2,3,4,5},返回5,{1,2,3,4,5}
  301. function ncmajTangView:checkCardValueNums(cards)
  302. if not cards then return 0 end
  303. local tCards = {}
  304. for i,v in pairs(cards) do
  305. local isHaveCardValue = false
  306. for j,k in pairs(tCards) do
  307. if v.card == k.card then
  308. isHaveCardValue = true
  309. end
  310. end
  311. if not isHaveCardValue then
  312. table.insert(tCards,v)
  313. end
  314. end
  315. return #tCards,tCards
  316. end
  317. --检测一组牌里面是否包含了另一组牌,如果scrCards包含了cards所有的牌则返回ture,否则返回false
  318. function ncmajTangView:checkIsContainCards(scrCards,cards)
  319. for i,v in pairs(cards) do
  320. local isHave = false
  321. for j,k in pairs(scrCards) do
  322. if v.card == k.card then
  323. isHave = true
  324. break
  325. end
  326. end
  327. if not isHave then
  328. return false
  329. end
  330. end
  331. return true
  332. end
  333. --传入一组牌,检测牌有几种类型(万同条),如有万同条则返回3,{1,2,3}
  334. function ncmajTangView:checkCardsColorNums(cards)
  335. if not cards then return 0 end
  336. local tCards = {}
  337. for i,v in pairs(cards) do
  338. local color = math.floor(v.card / 0x10)
  339. local isHaveColor = false
  340. for j,k in pairs(tCards) do
  341. if k == color then
  342. isHaveColor = true
  343. end
  344. end
  345. if not isHaveColor then
  346. table.insert(tCards,color)
  347. end
  348. end
  349. return #tCards,tCards
  350. end
  351. --从scrCards把包含了cards所以的牌找出来,如scrCards={1,1,2,2,3,4,5},cards={1,2,3},则返回{1,1,2,2,3}
  352. function ncmajTangView:getContainCards(scrCards,cards)
  353. local dCards = {}
  354. for i,v in pairs(cards) do
  355. for j,k in pairs(scrCards) do
  356. if v.card == k.card then
  357. table.insert(dCards,v)
  358. end
  359. end
  360. end
  361. return dCards
  362. end
  363. --从scrCards找出指定花色的牌,cards里面是花色
  364. function ncmajTangView:getCardsByColor(scrCards,cards)
  365. if not scrCards then return nil end
  366. if not cards then return scrCards end
  367. local tCards = {}
  368. for i,v in pairs(cards) do
  369. for j,k in pairs(scrCards) do
  370. local color = math.floor(k.card / 0x10)
  371. if v == color then
  372. table.insert(tCards,k)
  373. end
  374. end
  375. end
  376. return tCards
  377. end
  378. --把dirCards里面的牌合并到scrCards,然后返回合并后的scrCards
  379. function ncmajTangView:mergeCards(scrCards,dirCards)
  380. scrCards = scrCards or {}
  381. if not dirCards then return scrCards end
  382. for i,v in pairs(dirCards) do
  383. table.insert(scrCards,v)
  384. end
  385. return scrCards
  386. end
  387. --检查一组牌是否是7对牌
  388. function ncmajTangView:checkIsQiDui(scrCards)
  389. if not scrCards or not type(scrCards) == 'table' then return false end
  390. if #scrCards < 13 then return false end--手牌小于13张,说明吃碰过了,不可能是7对了
  391. local cardNum,cards = self:checkCardValueNums(scrCards)
  392. return cardNum <= 7
  393. end
  394. return ncmajTangView