25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1502 lines
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. self:sortHandCards()
  409. local startIndex = table.nums(self._groupNodes) * 3
  410. for cardIndex, card in ipairs(self._handCardNodes) do
  411. local newCardIndex = startIndex + cardIndex
  412. local x, y = self:getCardPosition(card, self._viewId, newCardIndex)
  413. if (self._viewId ~= MJDefine.MyViewId and self._isReplay) or card:getMJType() == MJDefine.MJConfig_2d.MJType.Open
  414. or card:getMJType() == MJDefine.MJConfig_3d.MJType.Open then
  415. x, y = self:getOpenCardPosition(card, self._viewId, newCardIndex)
  416. end
  417. card:setPosition(cc.p(x, y))
  418. card:setMJIndex(newCardIndex)
  419. if self._viewId ~= 4 then
  420. -- 重新排序,手牌可能乱了,需要重新设置一下手牌的背景
  421. card:initBackImage()
  422. end
  423. end
  424. self:refreshHandCardZOrder()
  425. end
  426. function CommonHandCard:runOutCardAction(card, callback)
  427. if callback then
  428. callback()
  429. end
  430. end
  431. function CommonHandCard:isCanOutCard()
  432. return self.isOutCard
  433. end
  434. function CommonHandCard:setOutCardEnable(b)
  435. logD("CommonHandCard:setOutCardEnable", "是否可以出牌", b)
  436. self.isOutCard = b
  437. if b then
  438. self:moveLastCard()
  439. end
  440. -- if self._viewId==MJDefine.MyViewId and self.ui.Items.Button_Sure then
  441. -- -- self.ui.Items.Button_Sure:setVisible(b)
  442. -- end
  443. end
  444. function CommonHandCard:moveLastCard(b)
  445. end
  446. function CommonHandCard:pushTing()
  447. self:clearAllTing()
  448. local tings = app.room.roomInfo.tings
  449. if tings then
  450. for _, v in pairs(tings) do
  451. for _, mj in pairs(self._handCardNodes) do
  452. if mj:getValue() == v.outCard then
  453. mj:setTing(true)
  454. end
  455. end
  456. end
  457. end
  458. end
  459. --最后一张牌显示箭头
  460. function CommonHandCard:pushLastCardTing()
  461. --爆牌状态只给最后一张牌加听标志
  462. self:clearAllTing()
  463. local mj = self._handCardNodes[#self._handCardNodes]
  464. mj:setTing(true)
  465. end
  466. ---
  467. -- 清除所有听牌标识
  468. -- @return
  469. --
  470. function CommonHandCard:clearAllTing()
  471. for _, mj in pairs(self._handCardNodes) do
  472. mj:setTing(false)
  473. end
  474. end
  475. function CommonHandCard:setTing(isTing)
  476. local handCarsNum = self:getHandCardsLength()
  477. if not (handCarsNum%3 == 2) then--如果不是出牌阶段,不显示箭头
  478. isTing = false
  479. end
  480. for _, mjCard in pairs(self._handCardNodes or {}) do
  481. mjCard:setTing(isTing)
  482. end
  483. end
  484. function CommonHandCard:removeHandCard(arg)
  485. if type(arg) == "number" then
  486. arg = {
  487. {card = arg}}
  488. end
  489. logD("CommonHandCard:removeHandCard", "arg=", table.tostring(arg))
  490. if self._viewId == MJDefine.MyViewId or self._isReplay then
  491. for k, v in pairs(arg) do
  492. local mj = self:getMjByValue(v.card)
  493. if mj then
  494. table.removeItem(self._handCardNodes, mj)
  495. mj:removeFromParent()
  496. end
  497. end
  498. else
  499. -- local num=#self._handCardNodes-#arg
  500. -- print(num)
  501. for i = #self._handCardNodes, #self._handCardNodes - #arg + 1, -1 do
  502. if self._handCardNodes[i] then
  503. self._handCardNodes[i]:removeFromParent()
  504. table.remove(self._handCardNodes, i)
  505. end
  506. end
  507. end
  508. end
  509. function CommonHandCard:getMjByValue(card)
  510. local mj = nil
  511. for k = #self._handCardNodes, 1, -1 do
  512. local v = self._handCardNodes[k]
  513. if v:getValue() == card then
  514. mj = v
  515. break
  516. end
  517. end
  518. return mj
  519. end
  520. --玩家出牌
  521. function CommonHandCard:onOutCard(card)
  522. logD("CommonHandCard:onOutCard", "card=", card)
  523. -- self:runOutCardAction(card)
  524. if self._viewId ~= MJDefine.MyViewId then --其他玩家删除最后一个牌
  525. local mj = self:getMjByValue(card)
  526. if mj then
  527. self:removeHandCard(card)
  528. self:resetHandCards()
  529. else
  530. local last = #self._handCardNodes
  531. if last and self._handCardNodes[last] then
  532. self._handCardNodes[last]:removeFromParent()
  533. table.remove(self._handCardNodes, last)
  534. end
  535. end
  536. else
  537. self:setOutCardEnable(false)
  538. local mj = self:getMjByValue(card)
  539. --插牌
  540. if mj == self._handCardNodes[#self._handCardNodes] then
  541. table.removeItem(self._handCardNodes, mj)
  542. self:resetHandCards()
  543. else
  544. table.removeItem(self._handCardNodes, mj)
  545. self:runInsertCardAction()
  546. end
  547. if mj then
  548. mj:removeFromParent()
  549. mj = nil
  550. end
  551. end
  552. -- local memberInfo=app.room.roomInfo.memberList[data.response.nUserId]
  553. local userInfo = app.room:getUserInfoByViewId(self._viewId)
  554. local sex = 1
  555. if userInfo then
  556. sex = userInfo.sex
  557. else
  558. logD("CommonHandCard:sendOutCard", "userInfo为空,使用默认性别")
  559. end
  560. MJSound.PlayMJSound(sex, card)
  561. end
  562. ---
  563. -- 插牌动画
  564. --
  565. function CommonHandCard:runInsertCardAction()
  566. logD("CommonHandCard:runInsertCardAction")
  567. self:getEventDispatcher():removeEventListenersForTarget(self)
  568. local insertMj = self._handCardNodes[#self._handCardNodes]
  569. self:resetHandCards()
  570. local x, y = self:getCardPosition(insertMj, self._viewId, 14)
  571. insertMj:setPosition(x, y)
  572. local targetIndex = insertMj:getMJIndex()
  573. local height = insertMj:getContentSize().height
  574. local x, y = self:getCardPosition(insertMj, self._viewId, targetIndex)
  575. local actions = {
  576. cc.MoveTo:create(0, cc.p(insertMj:getPositionX(), insertMj:getPositionY())),
  577. cc.MoveTo:create(0.1, cc.p(insertMj:getPositionX(), insertMj:getPositionY() + height)),
  578. cc.MoveTo:create(0.1, cc.p(x, insertMj:getPositionY() + height)),
  579. cc.MoveTo:create(0.1, cc.p(x, y)),
  580. cc.CallFunc:create(function()
  581. self:initTouchEvent()
  582. --if self.touchMJ and not tolua.isnull(self.touchMJ) then
  583. self:resetHandCards()
  584. --end
  585. end)
  586. }
  587. insertMj:runAction(cc.Sequence:create(actions))
  588. end
  589. function CommonHandCard:removeHandCardByNum(value, num)
  590. logD("CommonHandCard:removeHandCardByNum", "value=", value, "num=", num)
  591. if self._viewId == MJDefine.MyViewId or self._isReplay then
  592. for i = 1, num do
  593. local mj = self:getMjByValue(value)
  594. if mj then
  595. table.removeItem(self._handCardNodes, mj)
  596. mj:removeFromParent()
  597. end
  598. end
  599. else
  600. for i = #self._handCardNodes, #self._handCardNodes - num + 1, -1 do
  601. self._handCardNodes[i]:removeFromParent()
  602. table.remove(self._handCardNodes, i)
  603. end
  604. end
  605. end
  606. function CommonHandCard:createOpenHandCards(cards)
  607. if not cards then
  608. return
  609. end
  610. if self._viewId == MJDefine.MyViewId and not self._isReplay then
  611. logD("CommonHandCard:createHandCards", "非回放状态,创建正常手牌")
  612. self:createHandCards(cards)
  613. return
  614. end
  615. if type(cards) == "number" then
  616. cards = {
  617. {card = cards}}
  618. end
  619. local startIndex = table.nums(self._handCardNodes) + table.nums(self._groupNodes) * 3
  620. for index, v in ipairs(cards) do
  621. local mjIndex = startIndex + index
  622. local card = self:createOpenCard(v.card, self._viewId, mjIndex)
  623. local x, y = self:getOpenCardPosition(card, self._viewId, mjIndex)
  624. card:setPosition(cc.p(x, y))
  625. self:addChild(card)
  626. table.insert(self._handCardNodes, card)
  627. end
  628. self:onAfterCreateOpenHandcards()
  629. self:refreshHandCardZOrder()
  630. end
  631. function CommonHandCard:onAfterCreateOpenHandcards()
  632. end
  633. function CommonHandCard:createOpenCard(value, viewId, mjIndex)
  634. local mjType = MJDefine.MJConfig_2d.MJType.Open
  635. local CardClass = require(MJDefine.MJConfig_2d.MAHJONG_CARD)
  636. local mjCard = CardClass:new(value, viewId, mjType, mjIndex)
  637. local x, y = self:getOpenCardPosition(mjCard, viewId, mjIndex)
  638. mjCard:setPosition(cc.p(x, y))
  639. return mjCard
  640. end
  641. function CommonHandCard:getOpenCardPosition(mjCard, viewId, mjIndex)
  642. local x = 0
  643. local y = 0
  644. local viewId = self:getViewId()
  645. local cardSize = mjCard:getContentSize()
  646. local groupCount = table.nums(self._groupNodes)
  647. local startPos = self:getHandCardStartPos(viewId)
  648. local offset = MJDefine.MJConfig_2d.OpenCardOffsetConfig[viewId]
  649. if viewId == MJDefine.PlayerViewType.My then
  650. -- 本家不用摊牌
  651. elseif viewId == MJDefine.PlayerViewType.Right then
  652. x = startPos.x + offset.x
  653. y = startPos.y + (mjIndex - 1) * (cardSize.height + offset.y)
  654. y = mjIndex == 14 and (y + 7) or y
  655. if groupCount == 4 then
  656. y = y + 22
  657. elseif groupCount == 3 then
  658. y = y + 18
  659. elseif groupCount == 2 then
  660. y = y + 13
  661. elseif groupCount == 1 then
  662. y = y + 7
  663. end
  664. elseif viewId == MJDefine.PlayerViewType.Top then
  665. x = startPos.x + (1 - mjIndex) * (cardSize.width + offset.x)
  666. y = startPos.y + offset.y
  667. x = mjIndex == 14 and (x - 10) or x
  668. if groupCount == 4 then
  669. x = x - 45
  670. elseif groupCount == 3 then
  671. x = x - 35
  672. elseif groupCount == 2 then
  673. x = x - 25
  674. elseif groupCount == 1 then
  675. x = x - 15
  676. end
  677. elseif viewId == MJDefine.PlayerViewType.Left then
  678. x = startPos.x
  679. y = startPos.y + (1 - mjIndex) * (cardSize.height + offset.y)
  680. y = mjIndex == 14 and (y - 7) or y
  681. if groupCount == 4 then
  682. y = y - 22
  683. elseif groupCount == 3 then
  684. y = y - 20
  685. elseif groupCount == 2 then
  686. y = y - 13
  687. elseif groupCount == 1 then
  688. y = y - 7
  689. end
  690. end
  691. return x, y
  692. end
  693. function CommonHandCard:createGroupCards(arg, fromViewId)
  694. local viewId = self:getViewId()
  695. for _, v in pairs(arg) do
  696. if not fromViewId then
  697. fromViewId = v.fromViewId
  698. end
  699. local groupIndex = table.nums(self._groupNodes) + 1
  700. local group = self:createGroup(v.values, v.showType, viewId, groupIndex)
  701. if group then
  702. group.fromViewId = v.fromViewId ---保留触发操作的玩家位置,补杠需要
  703. group.opType = v.opType
  704. local x, y = self:getGroupPosition(group, groupIndex)
  705. group:setPosition(cc.p(x, y))
  706. group:customRefresh(v)
  707. self:addChild(group)
  708. table.insert(self._groupNodes, group)
  709. end
  710. end
  711. self:resetHandCards()
  712. self:refreshGroupZOrder()
  713. end
  714. function CommonHandCard:getGroupPosition(group, groupIndex)
  715. local x = 0
  716. local y = 0
  717. local viewId = self:getViewId()
  718. local startPos = self:getHandCardStartPos(viewId)
  719. local offset = self:getGroupOffsetConfig(viewId)
  720. if viewId == MJDefine.PlayerViewType.My then
  721. x = startPos.x + (groupIndex - 1) * offset.x
  722. y = startPos.y + offset.y
  723. elseif viewId == MJDefine.PlayerViewType.Right then
  724. x = startPos.x + offset.x
  725. y = startPos.y + (groupIndex - 1) * offset.y
  726. elseif viewId == MJDefine.PlayerViewType.Top then
  727. x = startPos.x + (1 - groupIndex) * offset.x
  728. y = startPos.y + offset.y
  729. elseif viewId == MJDefine.PlayerViewType.Left then
  730. x = startPos.x + offset.x
  731. y = startPos.y + (1 - groupIndex) * offset.y
  732. end
  733. return x, y
  734. end
  735. function CommonHandCard:createGroup(values, showType, viewId, groupIndex)
  736. local group = nil
  737. if showType == MJDefine.MJGroupType.Chi then
  738. group = require(MJDefine.MJConfig_2d.GROUP_CHI):new(values, viewId, groupIndex)
  739. elseif showType == MJDefine.MJGroupType.Peng then
  740. group = require(MJDefine.MJConfig_2d.GROUP_PENG):new(values, viewId, groupIndex)
  741. elseif showType == MJDefine.MJGroupType.Gang then
  742. group = require(MJDefine.MJConfig_2d.GROUP_GANG):new(values, viewId, groupIndex)
  743. elseif showType == MJDefine.MJGroupType.AnGang then
  744. group = require(MJDefine.MJConfig_2d.GROUP_ANGANG):new(values, viewId, groupIndex)
  745. elseif showType == MJDefine.MJGroupType.BaGang then
  746. group = require(MJDefine.MJConfig_2d.GROUP_BAGANG):new(values, viewId, groupIndex)
  747. end
  748. return group
  749. end
  750. function CommonHandCard:refreshHandCardZOrder()
  751. logD("CommonHandCard:refreshHandCardZOrder", "调整手牌层级")
  752. local viewId = self:getViewId()
  753. local groupCount = table.nums(self._groupNodes)
  754. local handCardNodes = self:getHandCardNodes() or {}
  755. for mahjongIndex, card in ipairs(handCardNodes) do
  756. local zorder = mahjongIndex
  757. if viewId == 1 then
  758. zorder = -mahjongIndex
  759. elseif viewId == 2 then
  760. elseif viewId == MJDefine.PlayerViewType.Left then
  761. zorder = zorder + groupCount
  762. else
  763. end
  764. card:setLocalZOrder(zorder)
  765. end
  766. end
  767. function CommonHandCard:resetHandCardStatus()
  768. logD("CommonHandCard:resetHandCardStatus", "清除手牌的状态")
  769. local handCardNodes = self:getHandCardNodes() or {}
  770. for k, v in ipairs(handCardNodes) do
  771. if v then
  772. local status = v:getStatus()
  773. if status == MJDefine.MJStatus.Select then
  774. v:setStatus()
  775. end
  776. end
  777. end
  778. end
  779. function CommonHandCard:lockHandCard(isForbidTouch)
  780. logD("CommonHandCard:lockHandCard")
  781. self._isLock = true
  782. for k, v in pairs(self._handCardNodes or {}) do
  783. v:setStatus(MJDefine.MJStatus.Disable)
  784. end
  785. if isForbidTouch then
  786. self:getEventDispatcher():removeEventListenersForTarget(self)
  787. end
  788. end
  789. function CommonHandCard:lockHongZhong(lock)
  790. local disableCardNum = 0
  791. for k,v in pairs(self._handCardNodes or {}) do
  792. if v.status == MJDefine.MJStatus.Disable then
  793. disableCardNum = disableCardNum + 1
  794. end
  795. end
  796. if disableCardNum == self:getHandCardsLength() then--锁牌了,返回
  797. return
  798. end
  799. for k,mj in pairs(self._handCardNodes or {}) do
  800. if mj:getValue() == 0x41 then
  801. if lock then
  802. mj:setStatus(MJDefine.MJStatus.Disable)
  803. else
  804. mj:setStatus(MJDefine.MJStatus.Normal)
  805. end
  806. end
  807. end
  808. end
  809. function CommonHandCard:isLock()
  810. return self._isLock
  811. end
  812. --- CommonHandCard:getGroup 获取已经存在的组合牌
  813. -- @param mjValue int 麻将牌的值
  814. -- @param opTypes table 麻将牌的类型
  815. function CommonHandCard:getGroup(mjValue, opTypes)
  816. if type(opTypes) ~= "table" then
  817. opTypes = {opTypes}
  818. end
  819. for _, vv in pairs(opTypes) do
  820. opType = vv
  821. for _, v in pairs(self._groupNodes) do
  822. if v.opType == opType then
  823. local mjCards = v:getCards()
  824. if v.opType == MJDefine.MJOperateType.OPREATE_ANGANG then
  825. if
  826. (mjCards[1]:getValue() == mjValue or mjCards[2]:getValue() == mjValue or
  827. mjCards[3]:getValue() == mjValue or
  828. mjCards[4]:getValue() == mjValue)
  829. then
  830. return v
  831. end
  832. else
  833. if
  834. (mjCards[1]:getValue() == mjValue or mjCards[2]:getValue() == mjValue or
  835. mjCards[3]:getValue() == mjValue)
  836. then
  837. return v
  838. end
  839. end
  840. end
  841. end
  842. end
  843. end
  844. --- CommonHandCard:buGang 补杠
  845. function CommonHandCard:buGang(card)
  846. local oldGroup = self:getGroup(card, {MJDefine.MJOperateType.OPREATE_PENG})
  847. if not oldGroup then
  848. return
  849. end
  850. local fromViewId = oldGroup.fromViewId
  851. -- 杠牌对家,换成第四张
  852. if app.room:getMaxPlayerCount() == 2 and fromViewId ~= 0 then
  853. fromViewId = 4
  854. end
  855. if app.room:getMaxPlayerCount() == 4 and fromViewId == 2 then
  856. fromViewId = 4
  857. end
  858. local index = table.indexOf(self._groupNodes, oldGroup)
  859. table.remove(self._groupNodes, index)
  860. oldGroup:removeFromParent()
  861. local newGroup = self:createGroup({card, card, card, card}, MJDefine.MJGroupType.BaGang, self._viewId, index, cardNum)
  862. if not newGroup then
  863. return
  864. end
  865. newGroup.fromViewId = fromViewId
  866. newGroup.opType = MJDefine.MJOperateType.OPREATE_BAGANG
  867. table.insert(self._groupNodes, index, newGroup)
  868. local startPos = self:getHandCardStartPos(self._viewId)
  869. local groupOffset = self:getGroupOffsetConfig(self._viewId)
  870. local x = startPos.x + groupOffset[index].x
  871. local y = startPos.y + groupOffset[index].y
  872. newGroup:setPosition(cc.p(x, y))
  873. newGroup:customRefresh(newGroup)
  874. self:addChild(newGroup)
  875. self:refreshGroupZOrder()
  876. end
  877. --- CommonHandCard:retoreBuGang 还原补杠
  878. -- @param card 牌值
  879. -- @param cardNum 牌张数
  880. function CommonHandCard:restoreBuGang(card)
  881. local oldGroup =
  882. self:getGroup(
  883. card,
  884. {
  885. MJDefine.MJOperateType.OPREATE_BAGANG,
  886. MJDefine.MJOperateType.OPREATE_ANGANG,
  887. MJDefine.MJOperateType.OPREATE_ZHIGANG
  888. })
  889. if not oldGroup then
  890. return
  891. end
  892. local fromViewId = oldGroup.fromViewId
  893. -- 杠牌对家,换成第四张
  894. if app.room:getMaxPlayerCount() == 2 and fromViewId ~= 0 then
  895. fromViewId = 4
  896. end
  897. if app.room:getMaxPlayerCount() == 4 and fromViewId == 2 then
  898. fromViewId = 4
  899. end
  900. local index = table.indexOf(self._groupNodes, oldGroup)
  901. table.remove(self._groupNodes, index)
  902. oldGroup:removeFromParent()
  903. local newGroup = self:createGroup({card, card, card}, MJDefine.MJGroupType.Peng, self._viewId, index)
  904. if not newGroup then
  905. return
  906. end
  907. newGroup.fromViewId = fromViewId
  908. newGroup.opType = MJDefine.MJOperateType.OPREATE_PENG
  909. table.insert(self._groupNodes, index, newGroup)
  910. local startPos = self:getHandCardStartPos(viewId)
  911. local groupOffset = self:getGroupOffsetConfig(viewId)
  912. local x = startPos.x + groupOffset[index].x
  913. local y = startPos.y + groupOffset[index].y
  914. newGroup:setPosition(cc.p(x, y))
  915. self:addChild(newGroup)
  916. self:refreshGroupZOrder()
  917. end
  918. --- CommonHandCard:removeLastHandCard 移除最后一张手牌
  919. function CommonHandCard:removeLastHandCard()
  920. if #self._handCardNodes + #self._groupNodes * 3 <= 13 then
  921. -- 手牌数量不足14张,不能移除,否则会少牌
  922. return false
  923. end
  924. local lastIndex = #self._handCardNodes
  925. self._handCardNodes[lastIndex]:removeFromParent()
  926. table.remove(self._handCardNodes, lastIndex)
  927. return true
  928. end
  929. function CommonHandCard:getLastCard()
  930. return self._handCardNodes[#self._handCardNodes]
  931. end
  932. function CommonHandCard:removeAllHandCards()
  933. for k, v in ipairs(self._handCardNodes or {}) do
  934. v:removeFromParent()
  935. end
  936. self._handCardNodes = {}
  937. end
  938. function CommonHandCard:setReplay(b)
  939. self._isReplay = b
  940. end
  941. function CommonHandCard:refreshGroupZOrder()
  942. for k, v in ipairs(self._groupNodes) do
  943. if self:getViewId() == MJDefine.PlayerViewType.Right then
  944. v:setLocalZOrder(table.nums(self._groupNodes) - k)
  945. else
  946. v:setLocalZOrder(k)
  947. end
  948. end
  949. end
  950. ---
  951. -- 获取手牌起始位置,3d必须重写
  952. -- @param viewId 玩家本地座位
  953. --
  954. function CommonHandCard:getHandCardStartPos(viewId)
  955. local pos = MJDefine.MJConfig_2d.HandCardStartPos[viewId]
  956. if MJDefine.MJConfig_2d.HandCardStartPosOfCardNum then
  957. local cardNum = self:getDealHandCardNum()
  958. pos = MJDefine.MJConfig_2d.HandCardStartPosOfCardNum[cardNum][viewId]
  959. end
  960. return pos
  961. end
  962. ---
  963. -- 获取手牌偏移位置,3d必须重写
  964. -- @param viewId 玩家本地座位
  965. --
  966. function CommonHandCard:getHandCardOffestPos(viewId)
  967. return MJDefine.MJConfig_2d.HandCardOffsetPos[viewId]
  968. end
  969. ---
  970. -- 获取组合牌偏移位置,3d必须重写
  971. -- @param viewId 玩家本地座位
  972. --
  973. function CommonHandCard:getGroupOffsetConfig(viewId)
  974. return MJDefine.MJConfig_2d.GroupOffsetConfig[viewId]
  975. end
  976. --删除触摸事件only
  977. function CommonHandCard:removeTouchEventOnly()
  978. self:getEventDispatcher():removeEventListenersForTarget(self)
  979. end
  980. --初始化换牌触摸事件
  981. function CommonHandCard:initSwapCardTouchEvent()
  982. if self._isReplay then--回放不需要触摸事件
  983. self:getEventDispatcher():removeEventListenersForTarget(self)
  984. return
  985. end
  986. self._selectCards = nil
  987. self:getEventDispatcher():removeEventListenersForTarget(self)
  988. --注册交换牌时的触摸事件
  989. self:registerTouch(handler(self,self.onSwapTouchBegan))
  990. end
  991. --换三张时的触摸事件
  992. function CommonHandCard:onSwapTouchBegan(touch)
  993. self._selectCards = self._selectCards or {}
  994. local viewId = self:getViewId()
  995. local startPos = self:getHandCardStartPos(viewId)
  996. local touchPos=self:convertToNodeSpace(touch:getLocation())
  997. for k,v in pairs(self._handCardNodes) do
  998. local width = v:getContentSize().width
  999. local height = v:getContentSize().height
  1000. local x = v:getPosition().x-width/2
  1001. local y = v:getPosition().y-height/2
  1002. local rect = cc.Rectangle:new(x,y,width,height)
  1003. if rect:contains(touchPos.x,touchPos.y) then
  1004. local t_touchMJ=v--self.touchMJ
  1005. if t_touchMJ:getSelected()==MJDefine.MJStatus.Select then
  1006. t_touchMJ:runDeSelectAnimationWithTime(0.1)
  1007. t_touchMJ:setSelected(MJDefine.MJStatus.Normal)
  1008. table.removeItem(self._selectCards,t_touchMJ)
  1009. else
  1010. t_touchMJ:recordPostion(cc.p(t_touchMJ:getPositionX(), startPos.y))
  1011. t_touchMJ:runSelectAnimation()
  1012. t_touchMJ:setSelected(MJDefine.MJStatus.Select)
  1013. table.insert(self._selectCards,t_touchMJ)
  1014. end
  1015. if self:checkIsInSwapRule() then
  1016. app.room:dispatchEvent({name = MJDefine.MJEvent.CheckIsInSwapRule,canCommit=true})
  1017. else
  1018. app.room:dispatchEvent({name = MJDefine.MJEvent.CheckIsInSwapRule,canCommit=false})
  1019. end
  1020. break
  1021. end
  1022. end
  1023. end
  1024. --初始化摆牌触摸事件
  1025. function CommonHandCard:initBaiCardTouchEvent()
  1026. if self._isReplay then--回放不需要触摸事件
  1027. self:getEventDispatcher():removeEventListenersForTarget(self)
  1028. return
  1029. end
  1030. self:getEventDispatcher():removeEventListenersForTarget(self)
  1031. self._selectCards = nil
  1032. --注册交换牌时的触摸事件
  1033. self:registerTouch(handler(self,self.onBaiPaiTouchBegan))
  1034. end
  1035. --摆牌时的触摸事件
  1036. function CommonHandCard:onBaiPaiTouchBegan(touch)
  1037. self._selectCards = self._selectCards or {}
  1038. local viewId = self:getViewId()
  1039. local startPos = self:getHandCardStartPos(viewId)
  1040. local touchPos=self:convertToNodeSpace(touch:getLocation())
  1041. for k,v in pairs(self._handCardNodes) do
  1042. local width = v:getContentSize().width
  1043. local height = v:getContentSize().height
  1044. local x = v:getPosition().x-width/2
  1045. local y = v:getPosition().y-height/2
  1046. local rect = cc.Rectangle:new(x,y,width,height)
  1047. if rect:contains(touchPos.x,touchPos.y) then
  1048. local t_touchMJ=v--self.touchMJ
  1049. if t_touchMJ:getSelected()==MJDefine.MJStatus.Select then
  1050. --t_touchMJ:runDeSelectAnimationWithTime(0.1)
  1051. t_touchMJ:setNormalColor()
  1052. t_touchMJ:setSelected(MJDefine.MJStatus.Normal)
  1053. table.removeItem(self._selectCards,t_touchMJ:getValue())
  1054. else
  1055. --t_touchMJ:recordPostion(cc.p(t_touchMJ:getPositionX(), startPos.y))
  1056. --t_touchMJ:runSelectAnimation()
  1057. local color = cc.c3b(218, 218, 128)
  1058. t_touchMJ:setColor(color)
  1059. t_touchMJ:setSelected(MJDefine.MJStatus.Select)
  1060. table.insert(self._selectCards,t_touchMJ:getValue())
  1061. end
  1062. if #self._selectCards > 0 then
  1063. app.room:dispatchEvent({name = MJDefine.MJEvent.ChagneBaiCards,canCommit=true,selectCards = self._selectCards})
  1064. else
  1065. app.room:dispatchEvent({name = MJDefine.MJEvent.ChagneBaiCards,canCommit=false,selectCards = {}})
  1066. end
  1067. break
  1068. end
  1069. end
  1070. end
  1071. --判断是否符合换三张规则
  1072. function CommonHandCard:checkIsInSwapRule()
  1073. local selectNums = #self._selectCards
  1074. logD("CommonHandCard:checkIsInSwapRule1 "..selectNums)
  1075. if selectNums ~= 3 then
  1076. return false
  1077. end
  1078. local huase = self._selectCards[1]:getMJColorType()
  1079. logD("CommonHandCard:checkIsInSwapRule2 "..huase)
  1080. for i,v in pairs(self._selectCards) do
  1081. local tHuase = v:getMJColorType()
  1082. logD("CommonHandCard:checkIsInSwapRule3 "..tHuase)
  1083. if tHuase ~= huase then
  1084. return false
  1085. end
  1086. end
  1087. logD("CommonHandCard:checkIsInSwapRule4 ")
  1088. return true
  1089. end
  1090. --获取选择的牌
  1091. function CommonHandCard:getSelectedCards()
  1092. local tselCards = {}
  1093. for i,v in pairs(self._selectCards) do
  1094. table.insert(tselCards,v:getValue())
  1095. end
  1096. return tselCards
  1097. end
  1098. --通过传进来的参数cards来选出手牌里的cards,服务器推荐选牌用
  1099. function CommonHandCard:selectCardsByCards(cards)
  1100. self._selectCards = self._selectCards or {}
  1101. local viewId = self:getViewId()
  1102. local startPos = self:getHandCardStartPos(viewId)
  1103. for i,v in pairs(cards) do
  1104. for j,k in pairs(self._handCardNodes) do
  1105. if k:getValue() == v.card and (k:getSelected() ~= MJDefine.MJStatus.Select) then
  1106. k:recordPostion(cc.p(k:getPositionX(),startPos.y))
  1107. k:runSelectAnimation()
  1108. k:setSelected(MJDefine.MJStatus.Select)
  1109. table.insert(self._selectCards,k)
  1110. break
  1111. end
  1112. end
  1113. end
  1114. end
  1115. --删除选中的牌
  1116. function CommonHandCard:deleteSelectedCards()
  1117. if not self._selectCards or #self._selectCards == 0 then
  1118. return
  1119. end
  1120. local tselCards = {}
  1121. for i,v in pairs(self._selectCards) do
  1122. table.insert(tselCards,{card = v:getValue()})
  1123. end
  1124. self:removeHandCard(tselCards)
  1125. self:resetHandCards()
  1126. end
  1127. -- -- todo lwq 血流专用 胡牌后,玩家的牌要躺下
  1128. -- function CommonHandCard:setIsHupai(hu)
  1129. -- self.isHu = hu
  1130. -- end
  1131. -- function CommonHandCard:getIsHupai(hu)
  1132. -- return self.isHu
  1133. -- end
  1134. --设置定缺类型 0万,1同,2条
  1135. function CommonHandCard:setQueType(que)
  1136. logD("CommonHandCard:setQueType "..(que or "kongzhi"))
  1137. self.queType = que
  1138. end
  1139. --检查手牌是否有定缺牌
  1140. function CommonHandCard:checkHandCardHaveQue(que)
  1141. for k,v in pairs(self._handCardNodes) do
  1142. if v:getMJColorType() == self.queType then
  1143. return true
  1144. end
  1145. end
  1146. return false
  1147. end
  1148. --检测定缺牌,屏蔽缺牌以外的牌(定缺牌打完钱不能打别的牌)
  1149. function CommonHandCard:checkQueAndDisableOtherCard()
  1150. logD("CommonHandCard:checkQueAndDisableOtherCard "..(self.queType or "kongzhi"))
  1151. if not self.queType or self.queType > 2 or self.queType < 0 then
  1152. return
  1153. end
  1154. local handcardNum = #self._handCardNodes
  1155. local isMyGetCard = (handcardNum%3 == 2)--是否是自己摸牌阶段
  1156. logD("CommonHandCard:checkQueAndDisableOtherCard "..handcardNum)
  1157. if isMyGetCard and self:checkHandCardHaveQue() then
  1158. logD("CommonHandCard:checkQueAndDisableOtherCard 11111")
  1159. for k,v in pairs(self._handCardNodes) do
  1160. if v:getMJColorType() ~= self.queType then
  1161. v:setStatus(MJDefine.MJStatus.Disable)
  1162. else
  1163. v:setStatus(MJDefine.MJStatus.Normal)
  1164. end
  1165. end
  1166. else
  1167. logD("CommonHandCard:checkQueAndDisableOtherCard 22222")
  1168. for k,v in pairs(self._handCardNodes) do
  1169. v:setStatus(MJDefine.MJStatus.Normal)
  1170. end
  1171. end
  1172. end
  1173. --检测报牌,diable手牌
  1174. function CommonHandCard:checkBaoAndDisableCard()
  1175. if self.baopai and self.baopai == 1 then
  1176. self:setHandCardsDidable()
  1177. else
  1178. self:setNotTingCardDidable(false)
  1179. end
  1180. end
  1181. --设置所有手牌状态为Disable
  1182. function CommonHandCard:setHandCardsDidable()
  1183. for k,v in pairs(self._handCardNodes) do
  1184. v:setStatus(MJDefine.MJStatus.Disable)
  1185. end
  1186. end
  1187. --设置所有手牌状态为Disable
  1188. function CommonHandCard:setNotTingCardDidable(is)
  1189. if is then
  1190. for _,mjNode in pairs(self._handCardNodes) do
  1191. if not mjNode:getTing() then
  1192. mjNode:setStatus(MJDefine.MJStatus.Disable)
  1193. else
  1194. mjNode:setStatus(MJDefine.MJStatus.Normal)
  1195. end
  1196. end
  1197. else
  1198. for _,mjNode in pairs(self._handCardNodes) do
  1199. mjNode:setStatus(MJDefine.MJStatus.Normal)
  1200. end
  1201. end
  1202. end
  1203. --创建换三张的牌
  1204. function CommonHandCard:createSwapCards(cards)
  1205. local index = 1
  1206. local viewId = self:getViewId()
  1207. if viewId == MJDefine.PlayerViewType.My then
  1208. index = 4
  1209. elseif viewId == MJDefine.PlayerViewType.Left then
  1210. index = 4
  1211. elseif viewId == MJDefine.PlayerViewType.Top then
  1212. index = 4
  1213. end
  1214. local group = self:createGroup(cards, MJDefine.MJGroupType.Chi, self:getViewId(), index)
  1215. if group then
  1216. return group
  1217. end
  1218. return nil
  1219. end
  1220. -- 吃的牌不能立即打出去
  1221. function CommonHandCard:cardEnabled( cards, is )
  1222. if is then
  1223. for _,card in pairs(cards) do
  1224. for _,mjNode in pairs(self._handCardNodes) do
  1225. if mjNode:getValue() == card then
  1226. mjNode:setStatus(MJDefine.MJStatus.Disable)
  1227. end
  1228. end
  1229. end
  1230. else
  1231. for _,mjNode in pairs(self._handCardNodes) do
  1232. mjNode:setStatus(MJDefine.MJStatus.Normal)
  1233. end
  1234. end
  1235. end
  1236. --可以躺牌的时候,disable非听牌
  1237. function CommonHandCard:tangCardEnabled(is)
  1238. self.touchMJ = nil
  1239. if is then
  1240. self.isTangOp = true--正在躺操作
  1241. for _,mjNode in pairs(self._handCardNodes) do
  1242. local isTingCard = mjNode:getTing()
  1243. local a = not mjNode:getTing()
  1244. if not mjNode:getTing() then
  1245. if mjNode:getStatus() == MJDefine.MJStatus.Select then
  1246. mjNode:setStatus(MJDefine.MJStatus.Normal)
  1247. mjNode:runDeSelectAnimation(true)
  1248. end
  1249. mjNode:setStatus(MJDefine.MJStatus.Disable)
  1250. else
  1251. if mjNode:getStatus() == MJDefine.MJStatus.Select then
  1252. mjNode:runDeSelectAnimation(true)
  1253. end
  1254. mjNode:setStatus(MJDefine.MJStatus.Normal)
  1255. end
  1256. end
  1257. else
  1258. self.isTangOp = false--正在躺操作
  1259. for _,mjNode in pairs(self._handCardNodes) do
  1260. if mjNode:getStatus() == MJDefine.MJStatus.Select then
  1261. mjNode:runDeSelectAnimation(true)
  1262. end
  1263. mjNode:setSelected(MJDefine.MJStatus.Normal)
  1264. mjNode:setStatus(MJDefine.MJStatus.Normal)
  1265. end
  1266. end
  1267. end
  1268. --设置躺状态
  1269. function CommonHandCard:setIsTanging(is)
  1270. self.isTangOp = is or false
  1271. end
  1272. --设置躺牌
  1273. function CommonHandCard:setTangCard(is,cards)
  1274. if is then
  1275. for _,card in pairs(cards) do
  1276. for _,mjNode in pairs(self._handCardNodes) do
  1277. if mjNode:getValue() == card.card and (not mjNode.tang) then
  1278. local color = cc.c3b(218, 218, 128)
  1279. mjNode:setColor(color)
  1280. mjNode.tang = true
  1281. break
  1282. end
  1283. end
  1284. end
  1285. else
  1286. for _,mjNode in pairs(self._handCardNodes) do
  1287. mjNode:setNormalColor()
  1288. mjNode.tang = false
  1289. end
  1290. end
  1291. end
  1292. --设置炮牌
  1293. function CommonHandCard:setPaoCard(is,cards)
  1294. if is then
  1295. self.paoCards = self.paoCards or {}
  1296. if cards and type(cards) == 'table' and #cards > 0 then
  1297. for _,card in pairs(cards) do
  1298. table.insert(self.paoCards,card.card)
  1299. end
  1300. end
  1301. for _,card in pairs(self.paoCards) do
  1302. for _,mjNode in pairs(self._handCardNodes) do
  1303. if mjNode:getValue() == card then
  1304. mjNode:setPaoFlag(true)
  1305. end
  1306. end
  1307. end
  1308. else
  1309. self.paoCards = {}
  1310. for _,mjNode in pairs(self._handCardNodes) do
  1311. mjNode:setPaoFlag(false)
  1312. end
  1313. end
  1314. end
  1315. --显示别人的躺牌
  1316. function CommonHandCard:showTangCard(is,cards)
  1317. if is then
  1318. local index = 1
  1319. for _,card in pairs(cards) do
  1320. local mjNode = self._handCardNodes[index]
  1321. mjNode:setValue(card.card)
  1322. mjNode._newValue = string.format("%x", card.card)
  1323. mjNode:setMJType(MJDefine.MJConfig_2d.MJType.Open)
  1324. mjNode:initViews()
  1325. local color = cc.c3b(218, 218, 128)
  1326. mjNode:setColor(color)
  1327. index = index + 1
  1328. end
  1329. self:resetHandCards()
  1330. else
  1331. for _,mjNode in pairs(self._handCardNodes) do
  1332. mjNode:setNormalColor()
  1333. end
  1334. end
  1335. end
  1336. --躺成功了
  1337. function CommonHandCard:setTangSuccess(is)
  1338. self.isTangCard = is
  1339. end
  1340. function CommonHandCard:setBaoPai(flag)
  1341. self.baopai = flag
  1342. end
  1343. -- 获取手牌是否全是炮牌,全手炮牌可以出任意牌
  1344. function CommonHandCard:getHandCardsIsAllPaoCard()
  1345. --如果爆牌了,则只能出摸上来的牌
  1346. if self.baopai and self.baopai == 1 then
  1347. return true
  1348. end
  1349. for _,mjNode in pairs(self._handCardNodes) do
  1350. if mjNode:getIsPaoPai() == false then
  1351. return false
  1352. end
  1353. end
  1354. return true
  1355. end
  1356. ---
  1357. -- 获取手牌默认发牌张数
  1358. -- 默认为13张,如果有不同,子游戏重写该方法
  1359. -- @return
  1360. --
  1361. function CommonHandCard:getDealHandCardNum ()
  1362. return 13
  1363. end
  1364. --选择完摆的牌准备出牌了
  1365. function CommonHandCard:baiOverOutCards(event)
  1366. self:initTouchEvent()
  1367. self.baiOutCard = true
  1368. end
  1369. return CommonHandCard