You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1505 line
46 KiB

  1. local MJDefine = MJFramework.MJImport("mj.luaScript.MJDefine")
  2. local MJMessage = MJFramework.MJImport("mj.luaScript.Protocol.MJMessage")
  3. local MJSound = MJFramework.MJImport("mj.luaScript.MJSound")
  4. local CommonHandCard = class("CommonHandCard", cc.UIView)
  5. function CommonHandCard:ctor(viewId)
  6. CommonHandCard.super.ctor(self)
  7. self._viewId = viewId
  8. self._handCards = {}
  9. self._groupNodes = {}
  10. self._handCardNodes = {}
  11. self:initHandCardConfig()
  12. end
  13. function CommonHandCard:initHandCardConfig()
  14. --[[
  15. if not self._viewId then
  16. return
  17. end
  18. if not MJDefine.MJConfig then
  19. return
  20. end
  21. self._startPos = self:getHandCardStartPos(viewId)
  22. self._offsetPos = MJDefine.MJConfig_2d.HandCardOffsetPos[self._viewId]
  23. ]]
  24. end
  25. function CommonHandCard:onEnter()
  26. CommonHandCard.super.onEnter(self)
  27. --绑定事件
  28. self:bindEvent(app.room , MJDefine.MJEvent.BaiOverOutCards , handler(self , self.baiOverOutCards))
  29. end
  30. function CommonHandCard:createHandCards(cards, isGetCard)
  31. logD("CommonHandCard:createHandCards", table.tostring(cards))
  32. if self._viewId ~= MJDefine.MyViewId and self._isReplay then
  33. logD("CommonHandCard:createHandCards", "回放状态,摊开手牌")
  34. self:createOpenHandCards(cards)
  35. return
  36. end
  37. if not cards then
  38. return
  39. end
  40. if isGetCard then
  41. self.touchMJ = nil
  42. end
  43. -- 先整理手牌,再抓牌,避免手牌出现空牌位置
  44. logD("CommonHandCard:createHandCards", "创建手牌前,先整理一下手牌")
  45. self:resetHandCards()
  46. if type(cards) == "number" then
  47. cards = {
  48. {card = cards}}
  49. end
  50. local startIndex = table.nums(self._handCardNodes) + table.nums(self._groupNodes) * 3
  51. for mahjongIndex, v in ipairs(cards) do
  52. local card = self:createCard(v.card, self._viewId, startIndex + mahjongIndex)
  53. self:addChild(card)
  54. table.insert(self._handCardNodes, card)
  55. end
  56. self:initTouchEvent()
  57. self:onAfterCreateHandcards()
  58. self:refreshHandCardZOrder()
  59. -- 创建完手牌之后,清除所有手牌的选中状态
  60. self:resetHandCardStatus()
  61. end
  62. --- CommonHandCard:onAfterCreateHandcards 创建手牌之后的处理
  63. function CommonHandCard:onAfterCreateHandcards()
  64. -- logD("CommonHandCard:onAfterCreateHandcards")
  65. end
  66. function CommonHandCard:createCard(value, viewId, mjIndex)
  67. local mjType = MJDefine.MJConfig_2d.MJType.Stand
  68. local CardClass = require(MJDefine.MJConfig_2d.MAHJONG_CARD)
  69. local mjCard = CardClass:new(value, viewId, mjType, mjIndex)
  70. local x, y = self:getCardPosition(mjCard, viewId, mjIndex)
  71. mjCard:setPosition(cc.p(x, y))
  72. return mjCard
  73. end
  74. function CommonHandCard:getCardPosition(mjCard, viewId, mjIndex)
  75. local x = 0
  76. local y = 0
  77. local cardSize = mjCard:getContentSize()
  78. local offset = self:getHandCardOffestPos(viewId)
  79. local startPos = self:getHandCardStartPos(viewId)
  80. local groupCount = table.nums(self._groupNodes)
  81. local dealCardNum = self:getDealHandCardNum()
  82. if viewId == MJDefine.PlayerViewType.My then
  83. x = startPos.x + (mjIndex - 1) * (cardSize.width + offset.x)
  84. y = startPos.y + offset.y
  85. x = mjIndex == (dealCardNum + 1) and (x + 10) or x
  86. if groupCount > 0 then
  87. x = x - 180 + (4 - groupCount) * 50
  88. end
  89. elseif viewId == MJDefine.PlayerViewType.Right then
  90. x = startPos.x + offset.x
  91. y = startPos.y + (mjIndex - 1) * (cardSize.height + offset.y)
  92. y = mjIndex == (dealCardNum + 1) and (y + 10) or y
  93. if groupCount == 4 then
  94. y = y + 100
  95. elseif groupCount == 3 then
  96. y = y + 70
  97. elseif groupCount == 2 then
  98. y = y + 45
  99. elseif groupCount == 1 then
  100. y = y + 15
  101. end
  102. elseif viewId == MJDefine.PlayerViewType.Top then
  103. x = startPos.x + (1 - mjIndex) * (cardSize.width + offset.x)
  104. y = startPos.y + offset.y
  105. x = mjIndex == (dealCardNum + 1) and (x - 10) or x
  106. if groupCount == 4 then
  107. x = x - 45
  108. elseif groupCount == 3 then
  109. x = x - 35
  110. elseif groupCount == 2 then
  111. x = x - 25
  112. elseif groupCount == 1 then
  113. x = x - 15
  114. end
  115. elseif viewId == MJDefine.PlayerViewType.Left then
  116. x = startPos.x
  117. y = startPos.y + (1 - mjIndex) * (cardSize.height + offset.y)
  118. y = mjIndex == (dealCardNum + 1) and (y - 10) or y
  119. if groupCount == 4 then
  120. y = y - 75
  121. elseif groupCount == 3 then
  122. y = y - 55
  123. elseif groupCount == 2 then
  124. y = y - 30
  125. elseif groupCount == 1 then
  126. y = y - 10
  127. end
  128. end
  129. return x, y
  130. end
  131. function CommonHandCard:createMahongOpenCard()
  132. end
  133. function CommonHandCard:clearHandCardNodes()
  134. self:removeAllChildren()
  135. end
  136. function CommonHandCard:sortHandCards()
  137. table.sort(
  138. self._handCardNodes,
  139. function(c1, c2)
  140. return c1:getValue() < c2:getValue()
  141. end
  142. )
  143. end
  144. function CommonHandCard:getGroups()
  145. end
  146. function CommonHandCard:getGroupsLength()
  147. return table.nums(self._groupNodes)
  148. end
  149. function CommonHandCard:getHandCardsLength()
  150. return table.nums(self._handCardNodes)
  151. end
  152. function CommonHandCard:getHandCardNodes()
  153. return self._handCardNodes
  154. end
  155. function CommonHandCard:getViewId()
  156. return self._viewId
  157. end
  158. function CommonHandCard:isHandCardValid()
  159. local groupLength = self:getGroupsLength()
  160. local handCardLength = self:getHandCardsLength()
  161. local dealCardNum = self:getDealHandCardNum()
  162. local isValid = (groupLength * 3 + handCardLength) == (dealCardNum + 1)
  163. return isValid
  164. end
  165. function CommonHandCard:initTouchEvent()
  166. if self._isReplay then--回放不需要触摸事件
  167. self:getEventDispatcher():removeEventListenersForTarget(self)
  168. return
  169. end
  170. if self._viewId ~= 4 then
  171. return
  172. end
  173. self:getEventDispatcher():removeEventListenersForTarget(self)
  174. self:registerTouch(
  175. handler(self, self.onTouchBegan),
  176. handler(self, self.onTouchMove),
  177. handler(self, self.onTouchEnd),
  178. handler(self, self.onTouchCancel))
  179. end
  180. function CommonHandCard:onTouchBegan(touch)
  181. logD("CommonHandCard:onTouchBegan")
  182. if self.isTangCard and self.isTangCard == true then--躺牌状态,自动出牌,屏蔽触摸时间
  183. return false
  184. end
  185. local viewId = self:getViewId()
  186. local startPos = self:getHandCardStartPos(viewId)
  187. local offsetPos = self:getHandCardOffestPos(viewId)
  188. if self.touchMJ and self.isTouchMove then
  189. logD("onTouchBegan : ------------2")
  190. self:touchOutCard()
  191. return false
  192. end
  193. logD("onTouchBegan : ------------3")
  194. local lastMj = self.touchMJ
  195. self.touchMJ = nil
  196. self.isTouchMove = false
  197. --[[
  198. -- if self.touchMJ then
  199. -- self.touchMJ:setPositionY(self.initY)
  200. -- self.touchMJ:setStatus(MJDefine.MJStatus.Normal)
  201. -- self.touchMJ=nil
  202. -- end
  203. ]]
  204. local dealCardNum = self:getDealHandCardNum()
  205. local touchPos = self:convertToNodeSpace(touch:getLocation())
  206. for k, v in pairs(self._handCardNodes) do
  207. local width = v:getContentSize().width
  208. local height = v:getContentSize().height
  209. local x = v:getPosition().x - width / 2
  210. local y = v:getPosition().y - height / 2
  211. local rect = cc.Rectangle:new(x, y, width, height)
  212. if rect:contains(touchPos.x, touchPos.y) then-- 抓牌状态
  213. if v:getStatus() == MJDefine.MJStatus.Disable then
  214. self.touchMJ = lastMj--防止选牌出牌时,点击了一下diaable状态的麻将,再选牌的时候,之前选的那张牌下不来
  215. return
  216. end
  217. if #self._handCardNodes + #self._groupNodes * 3 ~= (dealCardNum + 1) then
  218. -- 非抓牌状态
  219. self.touchMJ = nil;
  220. break
  221. else
  222. -- 抓牌状态
  223. if self._isLock and k ~= #self._handCardNodes then
  224. self.touchMJ = nil;
  225. break
  226. end
  227. end
  228. if self.baiOutCard and self.baiOutCard == true and v:getSelected()==MJDefine.MJStatus.Select then
  229. self.touchMJ = lastMj
  230. return--摆牌准备出牌阶段,被选做为摆的牌,不可以被选来打牌
  231. end
  232. self.touchMJ = v
  233. logD("CommonHandCard:onTouchBegan touchMJ", self.touchMJ:getValue())
  234. if self.touchMJ.status == MJDefine.MJStatus.Select then --双击出牌
  235. if self.isTangOp and self.isTangOp == true then--如果正在躺牌
  236. return false
  237. else
  238. self:sendOutCard()
  239. return false
  240. end
  241. else
  242. if self.isTangOp and self.isTangOp == true then--如果正在躺牌
  243. for _,mjNode in pairs(self._handCardNodes) do
  244. if mjNode:getStatus() == MJDefine.MJStatus.Select then
  245. mjNode:runDeSelectAnimation(true)
  246. mjNode:setStatus(MJDefine.MJStatus.Normal)
  247. end
  248. end
  249. self.touchMJ:recordPostion(cc.p(self.touchMJ:getPositionX(), startPos.y))
  250. self.touchMJ:runSelectAnimation()
  251. self.touchMJ:setStatus(MJDefine.MJStatus.Select)
  252. app.room:dispatchEvent({name = MJDefine.MJEvent.ShowTangView, card = self.touchMJ:getValue()})
  253. return false
  254. else
  255. self.touchMJ:recordPostion(cc.p(self.touchMJ:getPositionX(), startPos.y))
  256. self.touchMJ:runSelectAnimation()
  257. self.touchMJ:setStatus(MJDefine.MJStatus.Select)
  258. app.room:dispatchEvent({name = MJDefine.MJEvent.ShowTing, card = self.touchMJ:getValue()})
  259. end
  260. end
  261. break
  262. end
  263. end
  264. if self.isTangOp and self.isTangOp == true then--如果正在躺牌
  265. self.touchMJ = lastMj
  266. return false
  267. end
  268. --lastMj 有可能被移除 必须 tolua.isnull 判断一次
  269. if lastMj and not tolua.isnull(lastMj) and lastMj ~= self.touchMJ then
  270. lastMj:runDeSelectAnimation(true)
  271. lastMj:setStatus(MJDefine.MJStatus.Normal)
  272. end
  273. if self.baiOutCard and self.baiOutCard == true then
  274. --app.room:dispatchEvent({name = MJDefine.MJEvent.ClearBaiView, showValue = 2})
  275. else
  276. if self.touchMJ then
  277. app.room:dispatchEvent({name = MJDefine.MJEvent.ClearBaiView,value = self.touchMJ:getValue(),showValue = 0})
  278. else
  279. app.room:dispatchEvent({name = MJDefine.MJEvent.ClearBaiView})
  280. end
  281. end
  282. if self.touchMJ then
  283. app.room:dispatchEvent({name = MJDefine.MJEvent.SelectCard, value = self.touchMJ:getValue()})
  284. else
  285. app.room:dispatchEvent({name = MJDefine.MJEvent.SelectCard})
  286. app.room:dispatchEvent({name = MJDefine.MJEvent.ShowTing})
  287. return false
  288. end
  289. return true
  290. end
  291. function CommonHandCard:onTouchMove(touch)
  292. logD("CommonHandCard:onTouchMove")
  293. local touchPos = self:convertToNodeSpace(touch:getLocation())
  294. local prePos = self:convertToNodeSpace(touch:getPreviousLocation())
  295. local x = math.abs(prePos.x - touchPos.x)
  296. local y = math.abs(prePos.y - touchPos.y)
  297. if x > 5 or y > 5 then
  298. logD("onTouchMove : ------------2")
  299. self.isTouchMove = true
  300. end
  301. if not tolua.isnull(self.touchMJ) and self.isTouchMove == true then
  302. self.touchMJ:setPosition(touchPos)
  303. self.touchMJ:setLocalZOrder(1)
  304. end
  305. end
  306. function CommonHandCard:onTouchEnd(touch)
  307. logD("CommonHandCard:onTouchEnd")
  308. self:touchOutCard()
  309. end
  310. function CommonHandCard:onTouchCancel(touch)
  311. logD("onTouchCancel : ------------1")
  312. self:touchOutCard()
  313. end
  314. function CommonHandCard:touchOutCard()
  315. logD("touchOutCard : ------------1")
  316. if not tolua.isnull(self.touchMJ) and self.isTouchMove then
  317. logD("touchOutCard : ------------2")
  318. if self.touchMJ:getPositionY() > MJDefine.MJ_TOUCH_OUT_CARD_Y and self.isOutCard then
  319. logD("touchOutCard : ------------3")
  320. self:sendOutCard()
  321. else
  322. logD("touchOutCard : ------------4")
  323. self.touchMJ:restorePostion()
  324. self.touchMJ:setStatus(MJDefine.MJStatus.Normal)
  325. app.room:dispatchEvent({name = MJDefine.MJEvent.SelectCard})
  326. app.room:dispatchEvent({name = MJDefine.MJEvent.ShowTing})
  327. self.touchMJ = nil
  328. end
  329. end
  330. logD("touchOutCard : ------------5")
  331. self.isTouchMove = false
  332. end
  333. --发送出牌消息
  334. function CommonHandCard:sendOutCard()
  335. logD("CommonHandCard:sendOutCard")
  336. if not (self.isOutCard and self.touchMJ) then
  337. logD("CommonHandCard:sendOutCard", "不是本家出牌")
  338. return
  339. end
  340. if self.touchMJ:getIsPaoPai() and (not self:getHandCardsIsAllPaoCard()) then
  341. self.touchMJ:restorePostion()
  342. self.touchMJ:setStatus(MJDefine.MJStatus.Normal)
  343. showTooltip("不能打出别人要胡的牌!");
  344. return
  345. end
  346. if self.baiOutCard and self.baiOutCard == true then
  347. --请求亮牌
  348. self.baiOutCard = nil
  349. app.room:requestTangCards(self._selectCards,self.touchMJ:getValue())
  350. return
  351. end
  352. logD("CommonHandCard:sendOutCard", "touchMJ", self.touchMJ:getValue())
  353. local request = MJMessage.Card:new()
  354. request.card = self.touchMJ:getValue()
  355. -- logE("LHQRecordView:setOutCardVisible"..table.tostring(request))
  356. self:sendMsg(
  357. app.room,
  358. MJDefine.MJEvent.OutCard,
  359. request,
  360. function(status, response)
  361. logE("CommonHandCard:sendOutCard", "response = ", table.tostring(response))
  362. end
  363. )
  364. --音效
  365. local userInfo = app.room:getUserInfoByViewId(self._viewId)
  366. local sex = 1
  367. if userInfo then
  368. sex = userInfo.sex
  369. else
  370. logD("CommonHandCard:sendOutCard", "userInfo为空,使用默认性别")
  371. end
  372. MJSound.PlayMJSound(sex, request.card)
  373. self:runOutCardAction(self.touchMJ:getValue())
  374. app.room:dispatchEvent({name = MJDefine.MJEvent.LOCAL_OUT_CARD, card = self.touchMJ:getValue()})
  375. -- app.room:dispatchEvent({name = MJDefine.MJEvent.OutHuCardFalg, card = self.touchMJ:getValue()})
  376. app.room:dispatchEvent({name = MJDefine.MJEvent.OutCardFalg, value = self.touchMJ:getValue(), viewId = self._viewId})
  377. self:setOutCardEnable(false)
  378. --插牌
  379. if self.touchMJ == self._handCardNodes[#self._handCardNodes] then
  380. table.removeItem(self._handCardNodes, self.touchMJ)
  381. else
  382. table.removeItem(self._handCardNodes, self.touchMJ)
  383. self:runInsertCardAction()
  384. end
  385. self.touchMJ:removeFromParent()
  386. self.touchMJ = nil
  387. app.room:dispatchEvent({name = MJDefine.MJEvent.SelectCard})
  388. app.room:dispatchEvent({name = MJDefine.MJEvent.ShowTing})
  389. app.room:dispatchEvent({name = MJDefine.MJEvent.ClearBaiView,showValue = 0})
  390. self:setTing(false)
  391. app.room:resetTings()
  392. app.room.roomInfo.lastOutViewId = MJDefine.MyViewId
  393. self:checkQueAndDisableOtherCard()
  394. self:checkBaoAndDisableCard()
  395. end
  396. function CommonHandCard:getInsertIndex(mjCard)
  397. local targetIndex = -1
  398. for k, v in ipairs(self._handCardNodes or {}) do
  399. if v == mjCard then
  400. targetIndex = mjCard:getMJIndex()
  401. break
  402. end
  403. end
  404. return targetIndex
  405. end
  406. function CommonHandCard:resetHandCards()
  407. logD("CommonHandCard:resetHandCards", "重置手牌")
  408. if self.isHu and self.isHu == 1 then --todo lwq
  409. return
  410. end
  411. self:sortHandCards()
  412. local startIndex = table.nums(self._groupNodes) * 3
  413. for cardIndex, card in ipairs(self._handCardNodes) do
  414. local newCardIndex = startIndex + cardIndex
  415. local x, y = self:getCardPosition(card, self._viewId, newCardIndex)
  416. if (self._viewId ~= MJDefine.MyViewId and self._isReplay) or card:getMJType() == MJDefine.MJConfig_2d.MJType.Open
  417. or card:getMJType() == MJDefine.MJConfig_3d.MJType.Open then
  418. x, y = self:getOpenCardPosition(card, self._viewId, newCardIndex)
  419. end
  420. card:setPosition(cc.p(x, y))
  421. card:setMJIndex(newCardIndex)
  422. if self._viewId ~= 4 then
  423. -- 重新排序,手牌可能乱了,需要重新设置一下手牌的背景
  424. card:initBackImage()
  425. end
  426. end
  427. self:refreshHandCardZOrder()
  428. end
  429. function CommonHandCard:runOutCardAction(card, callback)
  430. if callback then
  431. callback()
  432. end
  433. end
  434. function CommonHandCard:isCanOutCard()
  435. return self.isOutCard
  436. end
  437. function CommonHandCard:setOutCardEnable(b)
  438. logD("CommonHandCard:setOutCardEnable", "是否可以出牌", b)
  439. self.isOutCard = b
  440. if b then
  441. self:moveLastCard()
  442. end
  443. -- if self._viewId==MJDefine.MyViewId and self.ui.Items.Button_Sure then
  444. -- -- self.ui.Items.Button_Sure:setVisible(b)
  445. -- end
  446. end
  447. function CommonHandCard:moveLastCard(b)
  448. end
  449. function CommonHandCard:pushTing()
  450. self:clearAllTing()
  451. local tings = app.room.roomInfo.tings
  452. if tings then
  453. for _, v in pairs(tings) do
  454. for _, mj in pairs(self._handCardNodes) do
  455. if mj:getValue() == v.outCard then
  456. mj:setTing(true)
  457. end
  458. end
  459. end
  460. end
  461. end
  462. --最后一张牌显示箭头
  463. function CommonHandCard:pushLastCardTing()
  464. --爆牌状态只给最后一张牌加听标志
  465. self:clearAllTing()
  466. local mj = self._handCardNodes[#self._handCardNodes]
  467. mj:setTing(true)
  468. end
  469. ---
  470. -- 清除所有听牌标识
  471. -- @return
  472. --
  473. function CommonHandCard:clearAllTing()
  474. for _, mj in pairs(self._handCardNodes) do
  475. mj:setTing(false)
  476. end
  477. end
  478. function CommonHandCard:setTing(isTing)
  479. local handCarsNum = self:getHandCardsLength()
  480. if not (handCarsNum%3 == 2) then--如果不是出牌阶段,不显示箭头
  481. isTing = false
  482. end
  483. for _, mjCard in pairs(self._handCardNodes or {}) do
  484. mjCard:setTing(isTing)
  485. end
  486. end
  487. function CommonHandCard:removeHandCard(arg)
  488. if type(arg) == "number" then
  489. arg = {
  490. {card = arg}}
  491. end
  492. logD("CommonHandCard:removeHandCard", "arg=", table.tostring(arg))
  493. if self._viewId == MJDefine.MyViewId or self._isReplay then
  494. for k, v in pairs(arg) do
  495. local mj = self:getMjByValue(v.card)
  496. if mj then
  497. table.removeItem(self._handCardNodes, mj)
  498. mj:removeFromParent()
  499. end
  500. end
  501. else
  502. -- local num=#self._handCardNodes-#arg
  503. -- print(num)
  504. for i = #self._handCardNodes, #self._handCardNodes - #arg + 1, -1 do
  505. if self._handCardNodes[i] then
  506. self._handCardNodes[i]:removeFromParent()
  507. table.remove(self._handCardNodes, i)
  508. end
  509. end
  510. end
  511. end
  512. function CommonHandCard:getMjByValue(card)
  513. local mj = nil
  514. for k = #self._handCardNodes, 1, -1 do
  515. local v = self._handCardNodes[k]
  516. if v:getValue() == card then
  517. mj = v
  518. break
  519. end
  520. end
  521. return mj
  522. end
  523. --玩家出牌
  524. function CommonHandCard:onOutCard(card)
  525. logD("CommonHandCard:onOutCard", "card=", card)
  526. -- self:runOutCardAction(card)
  527. if self._viewId ~= MJDefine.MyViewId then --其他玩家删除最后一个牌
  528. local mj = self:getMjByValue(card)
  529. if mj then
  530. self:removeHandCard(card)
  531. self:resetHandCards()
  532. else
  533. local last = #self._handCardNodes
  534. if last and self._handCardNodes[last] then
  535. self._handCardNodes[last]:removeFromParent()
  536. table.remove(self._handCardNodes, last)
  537. end
  538. end
  539. else
  540. self:setOutCardEnable(false)
  541. local mj = self:getMjByValue(card)
  542. --插牌
  543. if mj == self._handCardNodes[#self._handCardNodes] then
  544. table.removeItem(self._handCardNodes, mj)
  545. self:resetHandCards()
  546. else
  547. table.removeItem(self._handCardNodes, mj)
  548. self:runInsertCardAction()
  549. end
  550. if mj then
  551. mj:removeFromParent()
  552. mj = nil
  553. end
  554. end
  555. -- local memberInfo=app.room.roomInfo.memberList[data.response.nUserId]
  556. local userInfo = app.room:getUserInfoByViewId(self._viewId)
  557. local sex = 1
  558. if userInfo then
  559. sex = userInfo.sex
  560. else
  561. logD("CommonHandCard:sendOutCard", "userInfo为空,使用默认性别")
  562. end
  563. MJSound.PlayMJSound(sex, card)
  564. end
  565. ---
  566. -- 插牌动画
  567. --
  568. function CommonHandCard:runInsertCardAction()
  569. logD("CommonHandCard:runInsertCardAction")
  570. self:getEventDispatcher():removeEventListenersForTarget(self)
  571. local insertMj = self._handCardNodes[#self._handCardNodes]
  572. self:resetHandCards()
  573. local x, y = self:getCardPosition(insertMj, self._viewId, 14)
  574. insertMj:setPosition(x, y)
  575. local targetIndex = insertMj:getMJIndex()
  576. local height = insertMj:getContentSize().height
  577. local x, y = self:getCardPosition(insertMj, self._viewId, targetIndex)
  578. local actions = {
  579. cc.MoveTo:create(0, cc.p(insertMj:getPositionX(), insertMj:getPositionY())),
  580. cc.MoveTo:create(0.1, cc.p(insertMj:getPositionX(), insertMj:getPositionY() + height)),
  581. cc.MoveTo:create(0.1, cc.p(x, insertMj:getPositionY() + height)),
  582. cc.MoveTo:create(0.1, cc.p(x, y)),
  583. cc.CallFunc:create(function()
  584. self:initTouchEvent()
  585. --if self.touchMJ and not tolua.isnull(self.touchMJ) then
  586. self:resetHandCards()
  587. --end
  588. end)
  589. }
  590. insertMj:runAction(cc.Sequence:create(actions))
  591. end
  592. function CommonHandCard:removeHandCardByNum(value, num)
  593. logD("CommonHandCard:removeHandCardByNum", "value=", value, "num=", num)
  594. if self._viewId == MJDefine.MyViewId or self._isReplay then
  595. for i = 1, num do
  596. local mj = self:getMjByValue(value)
  597. if mj then
  598. table.removeItem(self._handCardNodes, mj)
  599. mj:removeFromParent()
  600. end
  601. end
  602. else
  603. for i = #self._handCardNodes, #self._handCardNodes - num + 1, -1 do
  604. self._handCardNodes[i]:removeFromParent()
  605. table.remove(self._handCardNodes, i)
  606. end
  607. end
  608. end
  609. function CommonHandCard:createOpenHandCards(cards)
  610. if not cards then
  611. return
  612. end
  613. if self._viewId == MJDefine.MyViewId and not self._isReplay then
  614. logD("CommonHandCard:createHandCards", "非回放状态,创建正常手牌")
  615. self:createHandCards(cards)
  616. return
  617. end
  618. if type(cards) == "number" then
  619. cards = {
  620. {card = cards}}
  621. end
  622. local startIndex = table.nums(self._handCardNodes) + table.nums(self._groupNodes) * 3
  623. for index, v in ipairs(cards) do
  624. local mjIndex = startIndex + index
  625. local card = self:createOpenCard(v.card, self._viewId, mjIndex)
  626. local x, y = self:getOpenCardPosition(card, self._viewId, mjIndex)
  627. card:setPosition(cc.p(x, y))
  628. self:addChild(card)
  629. table.insert(self._handCardNodes, card)
  630. end
  631. self:onAfterCreateOpenHandcards()
  632. self:refreshHandCardZOrder()
  633. end
  634. function CommonHandCard:onAfterCreateOpenHandcards()
  635. end
  636. function CommonHandCard:createOpenCard(value, viewId, mjIndex)
  637. local mjType = MJDefine.MJConfig_2d.MJType.Open
  638. local CardClass = require(MJDefine.MJConfig_2d.MAHJONG_CARD)
  639. local mjCard = CardClass:new(value, viewId, mjType, mjIndex)
  640. local x, y = self:getOpenCardPosition(mjCard, viewId, mjIndex)
  641. mjCard:setPosition(cc.p(x, y))
  642. return mjCard
  643. end
  644. function CommonHandCard:getOpenCardPosition(mjCard, viewId, mjIndex)
  645. local x = 0
  646. local y = 0
  647. local viewId = self:getViewId()
  648. local cardSize = mjCard:getContentSize()
  649. local groupCount = table.nums(self._groupNodes)
  650. local startPos = self:getHandCardStartPos(viewId)
  651. local offset = MJDefine.MJConfig_2d.OpenCardOffsetConfig[viewId]
  652. if viewId == MJDefine.PlayerViewType.My then
  653. -- 本家不用摊牌
  654. elseif viewId == MJDefine.PlayerViewType.Right then
  655. x = startPos.x + offset.x
  656. y = startPos.y + (mjIndex - 1) * (cardSize.height + offset.y)
  657. y = mjIndex == 14 and (y + 7) or y
  658. if groupCount == 4 then
  659. y = y + 22
  660. elseif groupCount == 3 then
  661. y = y + 18
  662. elseif groupCount == 2 then
  663. y = y + 13
  664. elseif groupCount == 1 then
  665. y = y + 7
  666. end
  667. elseif viewId == MJDefine.PlayerViewType.Top then
  668. x = startPos.x + (1 - mjIndex) * (cardSize.width + offset.x)
  669. y = startPos.y + offset.y
  670. x = mjIndex == 14 and (x - 10) or x
  671. if groupCount == 4 then
  672. x = x - 45
  673. elseif groupCount == 3 then
  674. x = x - 35
  675. elseif groupCount == 2 then
  676. x = x - 25
  677. elseif groupCount == 1 then
  678. x = x - 15
  679. end
  680. elseif viewId == MJDefine.PlayerViewType.Left then
  681. x = startPos.x
  682. y = startPos.y + (1 - mjIndex) * (cardSize.height + offset.y)
  683. y = mjIndex == 14 and (y - 7) or y
  684. if groupCount == 4 then
  685. y = y - 22
  686. elseif groupCount == 3 then
  687. y = y - 20
  688. elseif groupCount == 2 then
  689. y = y - 13
  690. elseif groupCount == 1 then
  691. y = y - 7
  692. end
  693. end
  694. return x, y
  695. end
  696. function CommonHandCard:createGroupCards(arg, fromViewId)
  697. local viewId = self:getViewId()
  698. for _, v in pairs(arg) do
  699. if not fromViewId then
  700. fromViewId = v.fromViewId
  701. end
  702. local groupIndex = table.nums(self._groupNodes) + 1
  703. local group = self:createGroup(v.values, v.showType, viewId, groupIndex)
  704. if group then
  705. group.fromViewId = v.fromViewId ---保留触发操作的玩家位置,补杠需要
  706. group.opType = v.opType
  707. local x, y = self:getGroupPosition(group, groupIndex)
  708. group:setPosition(cc.p(x, y))
  709. group:customRefresh(v)
  710. self:addChild(group)
  711. table.insert(self._groupNodes, group)
  712. end
  713. end
  714. self:resetHandCards()
  715. self:refreshGroupZOrder()
  716. end
  717. function CommonHandCard:getGroupPosition(group, groupIndex)
  718. local x = 0
  719. local y = 0
  720. local viewId = self:getViewId()
  721. local startPos = self:getHandCardStartPos(viewId)
  722. local offset = self:getGroupOffsetConfig(viewId)
  723. if viewId == MJDefine.PlayerViewType.My then
  724. x = startPos.x + (groupIndex - 1) * offset.x
  725. y = startPos.y + offset.y
  726. elseif viewId == MJDefine.PlayerViewType.Right then
  727. x = startPos.x + offset.x
  728. y = startPos.y + (groupIndex - 1) * offset.y
  729. elseif viewId == MJDefine.PlayerViewType.Top then
  730. x = startPos.x + (1 - groupIndex) * offset.x
  731. y = startPos.y + offset.y
  732. elseif viewId == MJDefine.PlayerViewType.Left then
  733. x = startPos.x + offset.x
  734. y = startPos.y + (1 - groupIndex) * offset.y
  735. end
  736. return x, y
  737. end
  738. function CommonHandCard:createGroup(values, showType, viewId, groupIndex)
  739. local group = nil
  740. if showType == MJDefine.MJGroupType.Chi then
  741. group = require(MJDefine.MJConfig_2d.GROUP_CHI):new(values, viewId, groupIndex)
  742. elseif showType == MJDefine.MJGroupType.Peng then
  743. group = require(MJDefine.MJConfig_2d.GROUP_PENG):new(values, viewId, groupIndex)
  744. elseif showType == MJDefine.MJGroupType.Gang then
  745. group = require(MJDefine.MJConfig_2d.GROUP_GANG):new(values, viewId, groupIndex)
  746. elseif showType == MJDefine.MJGroupType.AnGang then
  747. group = require(MJDefine.MJConfig_2d.GROUP_ANGANG):new(values, viewId, groupIndex)
  748. elseif showType == MJDefine.MJGroupType.BaGang then
  749. group = require(MJDefine.MJConfig_2d.GROUP_BAGANG):new(values, viewId, groupIndex)
  750. end
  751. return group
  752. end
  753. function CommonHandCard:refreshHandCardZOrder()
  754. logD("CommonHandCard:refreshHandCardZOrder", "调整手牌层级")
  755. local viewId = self:getViewId()
  756. local groupCount = table.nums(self._groupNodes)
  757. local handCardNodes = self:getHandCardNodes() or {}
  758. for mahjongIndex, card in ipairs(handCardNodes) do
  759. local zorder = mahjongIndex
  760. if viewId == 1 then
  761. zorder = -mahjongIndex
  762. elseif viewId == 2 then
  763. elseif viewId == MJDefine.PlayerViewType.Left then
  764. zorder = zorder + groupCount
  765. else
  766. end
  767. card:setLocalZOrder(zorder)
  768. end
  769. end
  770. function CommonHandCard:resetHandCardStatus()
  771. logD("CommonHandCard:resetHandCardStatus", "清除手牌的状态")
  772. local handCardNodes = self:getHandCardNodes() or {}
  773. for k, v in ipairs(handCardNodes) do
  774. if v then
  775. local status = v:getStatus()
  776. if status == MJDefine.MJStatus.Select then
  777. v:setStatus()
  778. end
  779. end
  780. end
  781. end
  782. function CommonHandCard:lockHandCard(isForbidTouch)
  783. logD("CommonHandCard:lockHandCard")
  784. self._isLock = true
  785. for k, v in pairs(self._handCardNodes or {}) do
  786. v:setStatus(MJDefine.MJStatus.Disable)
  787. end
  788. if isForbidTouch then
  789. self:getEventDispatcher():removeEventListenersForTarget(self)
  790. end
  791. end
  792. function CommonHandCard:lockHongZhong(lock)
  793. local disableCardNum = 0
  794. for k,v in pairs(self._handCardNodes or {}) do
  795. if v.status == MJDefine.MJStatus.Disable then
  796. disableCardNum = disableCardNum + 1
  797. end
  798. end
  799. if disableCardNum == self:getHandCardsLength() then--锁牌了,返回
  800. return
  801. end
  802. for k,mj in pairs(self._handCardNodes or {}) do
  803. if mj:getValue() == 0x41 then
  804. if lock then
  805. mj:setStatus(MJDefine.MJStatus.Disable)
  806. else
  807. mj:setStatus(MJDefine.MJStatus.Normal)
  808. end
  809. end
  810. end
  811. end
  812. function CommonHandCard:isLock()
  813. return self._isLock
  814. end
  815. --- CommonHandCard:getGroup 获取已经存在的组合牌
  816. -- @param mjValue int 麻将牌的值
  817. -- @param opTypes table 麻将牌的类型
  818. function CommonHandCard:getGroup(mjValue, opTypes)
  819. if type(opTypes) ~= "table" then
  820. opTypes = {opTypes}
  821. end
  822. for _, vv in pairs(opTypes) do
  823. opType = vv
  824. for _, v in pairs(self._groupNodes) do
  825. if v.opType == opType then
  826. local mjCards = v:getCards()
  827. if v.opType == MJDefine.MJOperateType.OPREATE_ANGANG then
  828. if
  829. (mjCards[1]:getValue() == mjValue or mjCards[2]:getValue() == mjValue or
  830. mjCards[3]:getValue() == mjValue or
  831. mjCards[4]:getValue() == mjValue)
  832. then
  833. return v
  834. end
  835. else
  836. if
  837. (mjCards[1]:getValue() == mjValue or mjCards[2]:getValue() == mjValue or
  838. mjCards[3]:getValue() == mjValue)
  839. then
  840. return v
  841. end
  842. end
  843. end
  844. end
  845. end
  846. end
  847. --- CommonHandCard:buGang 补杠
  848. function CommonHandCard:buGang(card)
  849. local oldGroup = self:getGroup(card, {MJDefine.MJOperateType.OPREATE_PENG})
  850. if not oldGroup then
  851. return
  852. end
  853. local fromViewId = oldGroup.fromViewId
  854. -- 杠牌对家,换成第四张
  855. if app.room:getMaxPlayerCount() == 2 and fromViewId ~= 0 then
  856. fromViewId = 4
  857. end
  858. if app.room:getMaxPlayerCount() == 4 and fromViewId == 2 then
  859. fromViewId = 4
  860. end
  861. local index = table.indexOf(self._groupNodes, oldGroup)
  862. table.remove(self._groupNodes, index)
  863. oldGroup:removeFromParent()
  864. local newGroup = self:createGroup({card, card, card, card}, MJDefine.MJGroupType.BaGang, self._viewId, index, cardNum)
  865. if not newGroup then
  866. return
  867. end
  868. newGroup.fromViewId = fromViewId
  869. newGroup.opType = MJDefine.MJOperateType.OPREATE_BAGANG
  870. table.insert(self._groupNodes, index, newGroup)
  871. local startPos = self:getHandCardStartPos(self._viewId)
  872. local groupOffset = self:getGroupOffsetConfig(self._viewId)
  873. local x = startPos.x + groupOffset[index].x
  874. local y = startPos.y + groupOffset[index].y
  875. newGroup:setPosition(cc.p(x, y))
  876. newGroup:customRefresh(newGroup)
  877. self:addChild(newGroup)
  878. self:refreshGroupZOrder()
  879. end
  880. --- CommonHandCard:retoreBuGang 还原补杠
  881. -- @param card 牌值
  882. -- @param cardNum 牌张数
  883. function CommonHandCard:restoreBuGang(card)
  884. local oldGroup =
  885. self:getGroup(
  886. card,
  887. {
  888. MJDefine.MJOperateType.OPREATE_BAGANG,
  889. MJDefine.MJOperateType.OPREATE_ANGANG,
  890. MJDefine.MJOperateType.OPREATE_ZHIGANG
  891. })
  892. if not oldGroup then
  893. return
  894. end
  895. local fromViewId = oldGroup.fromViewId
  896. -- 杠牌对家,换成第四张
  897. if app.room:getMaxPlayerCount() == 2 and fromViewId ~= 0 then
  898. fromViewId = 4
  899. end
  900. if app.room:getMaxPlayerCount() == 4 and fromViewId == 2 then
  901. fromViewId = 4
  902. end
  903. local index = table.indexOf(self._groupNodes, oldGroup)
  904. table.remove(self._groupNodes, index)
  905. oldGroup:removeFromParent()
  906. local newGroup = self:createGroup({card, card, card}, MJDefine.MJGroupType.Peng, self._viewId, index)
  907. if not newGroup then
  908. return
  909. end
  910. newGroup.fromViewId = fromViewId
  911. newGroup.opType = MJDefine.MJOperateType.OPREATE_PENG
  912. table.insert(self._groupNodes, index, newGroup)
  913. local startPos = self:getHandCardStartPos(viewId)
  914. local groupOffset = self:getGroupOffsetConfig(viewId)
  915. local x = startPos.x + groupOffset[index].x
  916. local y = startPos.y + groupOffset[index].y
  917. newGroup:setPosition(cc.p(x, y))
  918. self:addChild(newGroup)
  919. self:refreshGroupZOrder()
  920. end
  921. --- CommonHandCard:removeLastHandCard 移除最后一张手牌
  922. function CommonHandCard:removeLastHandCard()
  923. if #self._handCardNodes + #self._groupNodes * 3 <= 13 then
  924. -- 手牌数量不足14张,不能移除,否则会少牌
  925. return false
  926. end
  927. local lastIndex = #self._handCardNodes
  928. self._handCardNodes[lastIndex]:removeFromParent()
  929. table.remove(self._handCardNodes, lastIndex)
  930. return true
  931. end
  932. function CommonHandCard:getLastCard()
  933. return self._handCardNodes[#self._handCardNodes]
  934. end
  935. function CommonHandCard:removeAllHandCards()
  936. for k, v in ipairs(self._handCardNodes or {}) do
  937. v:removeFromParent()
  938. end
  939. self._handCardNodes = {}
  940. end
  941. function CommonHandCard:setReplay(b)
  942. self._isReplay = b
  943. end
  944. function CommonHandCard:refreshGroupZOrder()
  945. for k, v in ipairs(self._groupNodes) do
  946. if self:getViewId() == MJDefine.PlayerViewType.Right then
  947. v:setLocalZOrder(table.nums(self._groupNodes) - k)
  948. else
  949. v:setLocalZOrder(k)
  950. end
  951. end
  952. end
  953. ---
  954. -- 获取手牌起始位置,3d必须重写
  955. -- @param viewId 玩家本地座位
  956. --
  957. function CommonHandCard:getHandCardStartPos(viewId)
  958. local pos = MJDefine.MJConfig_2d.HandCardStartPos[viewId]
  959. if MJDefine.MJConfig_2d.HandCardStartPosOfCardNum then
  960. local cardNum = self:getDealHandCardNum()
  961. pos = MJDefine.MJConfig_2d.HandCardStartPosOfCardNum[cardNum][viewId]
  962. end
  963. return pos
  964. end
  965. ---
  966. -- 获取手牌偏移位置,3d必须重写
  967. -- @param viewId 玩家本地座位
  968. --
  969. function CommonHandCard:getHandCardOffestPos(viewId)
  970. return MJDefine.MJConfig_2d.HandCardOffsetPos[viewId]
  971. end
  972. ---
  973. -- 获取组合牌偏移位置,3d必须重写
  974. -- @param viewId 玩家本地座位
  975. --
  976. function CommonHandCard:getGroupOffsetConfig(viewId)
  977. return MJDefine.MJConfig_2d.GroupOffsetConfig[viewId]
  978. end
  979. --删除触摸事件only
  980. function CommonHandCard:removeTouchEventOnly()
  981. self:getEventDispatcher():removeEventListenersForTarget(self)
  982. end
  983. --初始化换牌触摸事件
  984. function CommonHandCard:initSwapCardTouchEvent()
  985. if self._isReplay then--回放不需要触摸事件
  986. self:getEventDispatcher():removeEventListenersForTarget(self)
  987. return
  988. end
  989. self._selectCards = nil
  990. self:getEventDispatcher():removeEventListenersForTarget(self)
  991. --注册交换牌时的触摸事件
  992. self:registerTouch(handler(self,self.onSwapTouchBegan))
  993. end
  994. --换三张时的触摸事件
  995. function CommonHandCard:onSwapTouchBegan(touch)
  996. self._selectCards = self._selectCards or {}
  997. local viewId = self:getViewId()
  998. local startPos = self:getHandCardStartPos(viewId)
  999. local touchPos=self:convertToNodeSpace(touch:getLocation())
  1000. for k,v in pairs(self._handCardNodes) do
  1001. local width = v:getContentSize().width
  1002. local height = v:getContentSize().height
  1003. local x = v:getPosition().x-width/2
  1004. local y = v:getPosition().y-height/2
  1005. local rect = cc.Rectangle:new(x,y,width,height)
  1006. if rect:contains(touchPos.x,touchPos.y) then
  1007. local t_touchMJ=v--self.touchMJ
  1008. if t_touchMJ:getSelected()==MJDefine.MJStatus.Select then
  1009. t_touchMJ:runDeSelectAnimationWithTime(0.1)
  1010. t_touchMJ:setSelected(MJDefine.MJStatus.Normal)
  1011. table.removeItem(self._selectCards,t_touchMJ)
  1012. else
  1013. t_touchMJ:recordPostion(cc.p(t_touchMJ:getPositionX(), startPos.y))
  1014. t_touchMJ:runSelectAnimation()
  1015. t_touchMJ:setSelected(MJDefine.MJStatus.Select)
  1016. table.insert(self._selectCards,t_touchMJ)
  1017. end
  1018. if self:checkIsInSwapRule() then
  1019. app.room:dispatchEvent({name = MJDefine.MJEvent.CheckIsInSwapRule,canCommit=true})
  1020. else
  1021. app.room:dispatchEvent({name = MJDefine.MJEvent.CheckIsInSwapRule,canCommit=false})
  1022. end
  1023. break
  1024. end
  1025. end
  1026. end
  1027. --初始化摆牌触摸事件
  1028. function CommonHandCard:initBaiCardTouchEvent()
  1029. if self._isReplay then--回放不需要触摸事件
  1030. self:getEventDispatcher():removeEventListenersForTarget(self)
  1031. return
  1032. end
  1033. self:getEventDispatcher():removeEventListenersForTarget(self)
  1034. self._selectCards = nil
  1035. --注册交换牌时的触摸事件
  1036. self:registerTouch(handler(self,self.onBaiPaiTouchBegan))
  1037. end
  1038. --摆牌时的触摸事件
  1039. function CommonHandCard:onBaiPaiTouchBegan(touch)
  1040. self._selectCards = self._selectCards or {}
  1041. local viewId = self:getViewId()
  1042. local startPos = self:getHandCardStartPos(viewId)
  1043. local touchPos=self:convertToNodeSpace(touch:getLocation())
  1044. for k,v in pairs(self._handCardNodes) do
  1045. local width = v:getContentSize().width
  1046. local height = v:getContentSize().height
  1047. local x = v:getPosition().x-width/2
  1048. local y = v:getPosition().y-height/2
  1049. local rect = cc.Rectangle:new(x,y,width,height)
  1050. if rect:contains(touchPos.x,touchPos.y) then
  1051. local t_touchMJ=v--self.touchMJ
  1052. if t_touchMJ:getSelected()==MJDefine.MJStatus.Select then
  1053. --t_touchMJ:runDeSelectAnimationWithTime(0.1)
  1054. t_touchMJ:setNormalColor()
  1055. t_touchMJ:setSelected(MJDefine.MJStatus.Normal)
  1056. table.removeItem(self._selectCards,t_touchMJ:getValue())
  1057. else
  1058. --t_touchMJ:recordPostion(cc.p(t_touchMJ:getPositionX(), startPos.y))
  1059. --t_touchMJ:runSelectAnimation()
  1060. local color = cc.c3b(218, 218, 128)
  1061. t_touchMJ:setColor(color)
  1062. t_touchMJ:setSelected(MJDefine.MJStatus.Select)
  1063. table.insert(self._selectCards,t_touchMJ:getValue())
  1064. end
  1065. if #self._selectCards > 0 then
  1066. app.room:dispatchEvent({name = MJDefine.MJEvent.ChagneBaiCards,canCommit=true,selectCards = self._selectCards})
  1067. else
  1068. app.room:dispatchEvent({name = MJDefine.MJEvent.ChagneBaiCards,canCommit=false,selectCards = {}})
  1069. end
  1070. break
  1071. end
  1072. end
  1073. end
  1074. --判断是否符合换三张规则
  1075. function CommonHandCard:checkIsInSwapRule()
  1076. local selectNums = #self._selectCards
  1077. logD("CommonHandCard:checkIsInSwapRule1 "..selectNums)
  1078. if selectNums ~= 3 then
  1079. return false
  1080. end
  1081. local huase = self._selectCards[1]:getMJColorType()
  1082. logD("CommonHandCard:checkIsInSwapRule2 "..huase)
  1083. for i,v in pairs(self._selectCards) do
  1084. local tHuase = v:getMJColorType()
  1085. logD("CommonHandCard:checkIsInSwapRule3 "..tHuase)
  1086. if tHuase ~= huase then
  1087. return false
  1088. end
  1089. end
  1090. logD("CommonHandCard:checkIsInSwapRule4 ")
  1091. return true
  1092. end
  1093. --获取选择的牌
  1094. function CommonHandCard:getSelectedCards()
  1095. local tselCards = {}
  1096. for i,v in pairs(self._selectCards) do
  1097. table.insert(tselCards,v:getValue())
  1098. end
  1099. return tselCards
  1100. end
  1101. --通过传进来的参数cards来选出手牌里的cards,服务器推荐选牌用
  1102. function CommonHandCard:selectCardsByCards(cards)
  1103. self._selectCards = self._selectCards or {}
  1104. local viewId = self:getViewId()
  1105. local startPos = self:getHandCardStartPos(viewId)
  1106. for i,v in pairs(cards) do
  1107. for j,k in pairs(self._handCardNodes) do
  1108. if k:getValue() == v.card and (k:getSelected() ~= MJDefine.MJStatus.Select) then
  1109. k:recordPostion(cc.p(k:getPositionX(),startPos.y))
  1110. k:runSelectAnimation()
  1111. k:setSelected(MJDefine.MJStatus.Select)
  1112. table.insert(self._selectCards,k)
  1113. break
  1114. end
  1115. end
  1116. end
  1117. end
  1118. --删除选中的牌
  1119. function CommonHandCard:deleteSelectedCards()
  1120. if not self._selectCards or #self._selectCards == 0 then
  1121. return
  1122. end
  1123. local tselCards = {}
  1124. for i,v in pairs(self._selectCards) do
  1125. table.insert(tselCards,{card = v:getValue()})
  1126. end
  1127. self:removeHandCard(tselCards)
  1128. self:resetHandCards()
  1129. end
  1130. -- todo lwq 血流专用 胡牌后,玩家的牌要躺下
  1131. function CommonHandCard:setIsHupai(hu)
  1132. self.isHu = hu
  1133. end
  1134. function CommonHandCard:getIsHupai(hu)
  1135. return self.isHu
  1136. end
  1137. --设置定缺类型 0万,1同,2条
  1138. function CommonHandCard:setQueType(que)
  1139. logD("CommonHandCard:setQueType "..(que or "kongzhi"))
  1140. self.queType = que
  1141. end
  1142. --检查手牌是否有定缺牌
  1143. function CommonHandCard:checkHandCardHaveQue(que)
  1144. for k,v in pairs(self._handCardNodes) do
  1145. if v:getMJColorType() == self.queType then
  1146. return true
  1147. end
  1148. end
  1149. return false
  1150. end
  1151. --检测定缺牌,屏蔽缺牌以外的牌(定缺牌打完钱不能打别的牌)
  1152. function CommonHandCard:checkQueAndDisableOtherCard()
  1153. logD("CommonHandCard:checkQueAndDisableOtherCard "..(self.queType or "kongzhi"))
  1154. if not self.queType or self.queType > 2 or self.queType < 0 then
  1155. return
  1156. end
  1157. local handcardNum = #self._handCardNodes
  1158. local isMyGetCard = (handcardNum%3 == 2)--是否是自己摸牌阶段
  1159. logD("CommonHandCard:checkQueAndDisableOtherCard "..handcardNum)
  1160. if isMyGetCard and self:checkHandCardHaveQue() then
  1161. logD("CommonHandCard:checkQueAndDisableOtherCard 11111")
  1162. for k,v in pairs(self._handCardNodes) do
  1163. if v:getMJColorType() ~= self.queType then
  1164. v:setStatus(MJDefine.MJStatus.Disable)
  1165. else
  1166. v:setStatus(MJDefine.MJStatus.Normal)
  1167. end
  1168. end
  1169. else
  1170. logD("CommonHandCard:checkQueAndDisableOtherCard 22222")
  1171. for k,v in pairs(self._handCardNodes) do
  1172. v:setStatus(MJDefine.MJStatus.Normal)
  1173. end
  1174. end
  1175. end
  1176. --检测报牌,diable手牌
  1177. function CommonHandCard:checkBaoAndDisableCard()
  1178. if self.baopai and self.baopai == 1 then
  1179. self:setHandCardsDidable()
  1180. else
  1181. self:setNotTingCardDidable(false)
  1182. end
  1183. end
  1184. --设置所有手牌状态为Disable
  1185. function CommonHandCard:setHandCardsDidable()
  1186. for k,v in pairs(self._handCardNodes) do
  1187. v:setStatus(MJDefine.MJStatus.Disable)
  1188. end
  1189. end
  1190. --设置所有手牌状态为Disable
  1191. function CommonHandCard:setNotTingCardDidable(is)
  1192. if is then
  1193. for _,mjNode in pairs(self._handCardNodes) do
  1194. if not mjNode:getTing() then
  1195. mjNode:setStatus(MJDefine.MJStatus.Disable)
  1196. else
  1197. mjNode:setStatus(MJDefine.MJStatus.Normal)
  1198. end
  1199. end
  1200. else
  1201. for _,mjNode in pairs(self._handCardNodes) do
  1202. mjNode:setStatus(MJDefine.MJStatus.Normal)
  1203. end
  1204. end
  1205. end
  1206. --创建换三张的牌
  1207. function CommonHandCard:createSwapCards(cards)
  1208. local index = 1
  1209. local viewId = self:getViewId()
  1210. if viewId == MJDefine.PlayerViewType.My then
  1211. index = 4
  1212. elseif viewId == MJDefine.PlayerViewType.Left then
  1213. index = 4
  1214. elseif viewId == MJDefine.PlayerViewType.Top then
  1215. index = 4
  1216. end
  1217. local group = self:createGroup(cards, MJDefine.MJGroupType.Chi, self:getViewId(), index)
  1218. if group then
  1219. return group
  1220. end
  1221. return nil
  1222. end
  1223. -- 吃的牌不能立即打出去
  1224. function CommonHandCard:cardEnabled( cards, is )
  1225. if is then
  1226. for _,card in pairs(cards) do
  1227. for _,mjNode in pairs(self._handCardNodes) do
  1228. if mjNode:getValue() == card then
  1229. mjNode:setStatus(MJDefine.MJStatus.Disable)
  1230. end
  1231. end
  1232. end
  1233. else
  1234. for _,mjNode in pairs(self._handCardNodes) do
  1235. mjNode:setStatus(MJDefine.MJStatus.Normal)
  1236. end
  1237. end
  1238. end
  1239. --可以躺牌的时候,disable非听牌
  1240. function CommonHandCard:tangCardEnabled(is)
  1241. self.touchMJ = nil
  1242. if is then
  1243. self.isTangOp = true--正在躺操作
  1244. for _,mjNode in pairs(self._handCardNodes) do
  1245. local isTingCard = mjNode:getTing()
  1246. local a = not mjNode:getTing()
  1247. if not mjNode:getTing() then
  1248. if mjNode:getStatus() == MJDefine.MJStatus.Select then
  1249. mjNode:setStatus(MJDefine.MJStatus.Normal)
  1250. mjNode:runDeSelectAnimation(true)
  1251. end
  1252. mjNode:setStatus(MJDefine.MJStatus.Disable)
  1253. else
  1254. if mjNode:getStatus() == MJDefine.MJStatus.Select then
  1255. mjNode:runDeSelectAnimation(true)
  1256. end
  1257. mjNode:setStatus(MJDefine.MJStatus.Normal)
  1258. end
  1259. end
  1260. else
  1261. self.isTangOp = false--正在躺操作
  1262. for _,mjNode in pairs(self._handCardNodes) do
  1263. if mjNode:getStatus() == MJDefine.MJStatus.Select then
  1264. mjNode:runDeSelectAnimation(true)
  1265. end
  1266. mjNode:setSelected(MJDefine.MJStatus.Normal)
  1267. mjNode:setStatus(MJDefine.MJStatus.Normal)
  1268. end
  1269. end
  1270. end
  1271. --设置躺状态
  1272. function CommonHandCard:setIsTanging(is)
  1273. self.isTangOp = is or false
  1274. end
  1275. --设置躺牌
  1276. function CommonHandCard:setTangCard(is,cards)
  1277. if is then
  1278. for _,card in pairs(cards) do
  1279. for _,mjNode in pairs(self._handCardNodes) do
  1280. if mjNode:getValue() == card.card and (not mjNode.tang) then
  1281. local color = cc.c3b(218, 218, 128)
  1282. mjNode:setColor(color)
  1283. mjNode.tang = true
  1284. break
  1285. end
  1286. end
  1287. end
  1288. else
  1289. for _,mjNode in pairs(self._handCardNodes) do
  1290. mjNode:setNormalColor()
  1291. mjNode.tang = false
  1292. end
  1293. end
  1294. end
  1295. --设置炮牌
  1296. function CommonHandCard:setPaoCard(is,cards)
  1297. if is then
  1298. self.paoCards = self.paoCards or {}
  1299. if cards and type(cards) == 'table' and #cards > 0 then
  1300. for _,card in pairs(cards) do
  1301. table.insert(self.paoCards,card.card)
  1302. end
  1303. end
  1304. for _,card in pairs(self.paoCards) do
  1305. for _,mjNode in pairs(self._handCardNodes) do
  1306. if mjNode:getValue() == card then
  1307. mjNode:setPaoFlag(true)
  1308. end
  1309. end
  1310. end
  1311. else
  1312. self.paoCards = {}
  1313. for _,mjNode in pairs(self._handCardNodes) do
  1314. mjNode:setPaoFlag(false)
  1315. end
  1316. end
  1317. end
  1318. --显示别人的躺牌
  1319. function CommonHandCard:showTangCard(is,cards)
  1320. if is then
  1321. local index = 1
  1322. for _,card in pairs(cards) do
  1323. local mjNode = self._handCardNodes[index]
  1324. mjNode:setValue(card.card)
  1325. mjNode._newValue = string.format("%x", card.card)
  1326. mjNode:setMJType(MJDefine.MJConfig_2d.MJType.Open)
  1327. mjNode:initViews()
  1328. local color = cc.c3b(218, 218, 128)
  1329. mjNode:setColor(color)
  1330. index = index + 1
  1331. end
  1332. self:resetHandCards()
  1333. else
  1334. for _,mjNode in pairs(self._handCardNodes) do
  1335. mjNode:setNormalColor()
  1336. end
  1337. end
  1338. end
  1339. --躺成功了
  1340. function CommonHandCard:setTangSuccess(is)
  1341. self.isTangCard = is
  1342. end
  1343. function CommonHandCard:setBaoPai(flag)
  1344. self.baopai = flag
  1345. end
  1346. -- 获取手牌是否全是炮牌,全手炮牌可以出任意牌
  1347. function CommonHandCard:getHandCardsIsAllPaoCard()
  1348. --如果爆牌了,则只能出摸上来的牌
  1349. if self.baopai and self.baopai == 1 then
  1350. return true
  1351. end
  1352. for _,mjNode in pairs(self._handCardNodes) do
  1353. if mjNode:getIsPaoPai() == false then
  1354. return false
  1355. end
  1356. end
  1357. return true
  1358. end
  1359. ---
  1360. -- 获取手牌默认发牌张数
  1361. -- 默认为13张,如果有不同,子游戏重写该方法
  1362. -- @return
  1363. --
  1364. function CommonHandCard:getDealHandCardNum ()
  1365. return 13
  1366. end
  1367. --选择完摆的牌准备出牌了
  1368. function CommonHandCard:baiOverOutCards(event)
  1369. self:initTouchEvent()
  1370. self.baiOutCard = true
  1371. end
  1372. return CommonHandCard