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.

763 lines
23 KiB

  1. local MJ=MJFramework.MJImport("mj.luaScript.Views.Game.MJ")
  2. local MJDefine=MJFramework.MJImport("mj.luaScript.MJDefine")
  3. local MJGroupFactory=MJFramework.MJImport("mj.luaScript.Views.Game.MJGroupFactory")
  4. local MJPositionCompnent = MJFramework.MJImport("mj.luaScript.Views.Compnent.MJPositionCompnent")
  5. local MJMessage=MJFramework.MJImport("mj.luaScript.Protocol.MJMessage")
  6. -- local zigong7zHandCardView=MJFramework.MJFrameworkClassImprot("mj.luaScript.Views.Game.zigong7zHandCardView")
  7. local MJSound = MJFramework.MJImport("mj.luaScript.MJSound")
  8. local zigong7zHandCardView = class("zigong7zHandCardView", cc.UIView)
  9. -- 组件
  10. local HandCard_Component = 'mj_zigong7z.luaScript.Views.Component.zigong7z_HandCard_Component'
  11. local OutCard_Component = 'mj_zigong7z.luaScript.Views.Component.zigong7z_OutCard_Component'
  12. local HuCard_Component = 'mj_zigong7z.luaScript.Views.Component.zigong7z_HuCard_Component'
  13. function zigong7zHandCardView:ctor(viewId,desktopType)
  14. zigong7zHandCardView.super.ctor(self)
  15. self.viewId=viewId or MJDefine.MyViewId
  16. self.desktopType=desktopType or MJDefine.DesktopType.ThreeD
  17. --初始化视图
  18. self:initView()
  19. self.handCards={}
  20. self.handGroupCards={}
  21. self.outCards={}
  22. self.touchMJ=nil
  23. self.isOutCard=false
  24. self.isReplay=false
  25. if self.viewId == MJDefine.PlayerViewType.My then
  26. self:initTouchEvent()
  27. end
  28. end
  29. function zigong7zHandCardView:initView()
  30. self:loadUI()
  31. if self.ui.Items.Layout_Hand_Card then
  32. self.ui.Items.Layout_Hand_Card:setVisible(false)
  33. end
  34. if self.ui.Items.Button_Sure then
  35. self.ui.Items.Button_Sure:setVisible(false)
  36. end
  37. local MJConfig = MJDefine.MJConfig_2d
  38. if self.desktopType == MJDefine.DesktopType.ThreeD then
  39. MJConfig = MJDefine.MJConfig_3d
  40. end
  41. local MahjongHandCard = require(HandCard_Component)
  42. local MahjongOutCardView = require(OutCard_Component)
  43. local MahjongHuCardView = require(HuCard_Component)
  44. self._handcard = MahjongHandCard:new(self, self.viewId, self.desktopType)
  45. self:addChild(self._handcard)
  46. self._outCardView = MahjongOutCardView:new(self.viewId, self.desktopType)
  47. self:addChild(self._outCardView)
  48. self._huCardView = MahjongHuCardView:new(self.viewId, self.desktopType)
  49. self:addChild(self._huCardView)
  50. self.ui:setLocalZOrder(10)
  51. end
  52. -- 加载ui
  53. function zigong7zHandCardView:loadUI()
  54. local ui
  55. if self.desktopType==MJDefine.DesktopType.TwoD or self.desktopType==MJDefine.DesktopType.TwoDGold then
  56. ui = loadUI("mj_zigong7z/res/ui_fangjian/mj_zigong_hand_card_2d.ui");
  57. else
  58. ui = loadUI("mj_zigong7z/res/ui_fangjian/mj_zigong_hand_card.ui");
  59. end
  60. autoAdapt(ui)
  61. self.ui = ui;
  62. self:addChild(ui)
  63. end
  64. function zigong7zHandCardView:pushTing()
  65. self._handcard:pushTing()
  66. end
  67. --摸牌
  68. function zigong7zHandCardView:onGetCard(operates,card)
  69. self.touchMJ = nil
  70. self:createHandCards(card)
  71. if not operates or table.nums(operates) == 0 then
  72. self._handcard:setOutCardEnable(true)
  73. MJDefine.outCardSendGuo = false
  74. else
  75. self._handcard:setOutCardEnable(true)
  76. MJDefine.outCardSendGuo = true
  77. -- self:canOutCard(true)
  78. end
  79. if self.viewId==MJDefine.MyViewId then
  80. MJSound.PlayGetCard()
  81. end
  82. end
  83. function zigong7zHandCardView:setOutCardEnable(b)
  84. self._handcard:setOutCardEnable(b)
  85. end
  86. --不管是否自动对齐,红中始终保持放在最左边
  87. function zigong7zHandCardView:sortRedMahjong()
  88. local redNum = 0
  89. for k,mj in pairs(self.handCards) do
  90. if mj.value == 65 then
  91. redNum = redNum + 1
  92. table.remove(self.handCards,k)
  93. table.insert(self.handCards,redNum,mj)
  94. end
  95. end
  96. end
  97. function zigong7zHandCardView:removeHandCardByNum(value,num,redNum)
  98. self._handcard:removeHandCardByNum(value, num,redNum)
  99. end
  100. function zigong7zHandCardView:getGroup(card,opType)
  101. return self._handcard:getGroup(card, opTypes)
  102. end
  103. --添加杠操作按钮
  104. function zigong7zHandCardView:addOperateBtn(operates)
  105. local x=self.ui.Items.Layout_Operate:getContentSize().width
  106. local y=self.ui.Items.Layout_Operate:getContentSize().height/2
  107. local btnOperate=cc.Button:createNode()
  108. btnOperate:setDefaults()
  109. btnOperate:setText("")
  110. btnOperate:loadTextureNormal(MJDefine.MJOperateImage[operates[1].opType])
  111. btnOperate:ignoreAnchorPointForPosition(false)
  112. self.btnPosX = self.btnPosX-MJDefine.MJ_OPERATE_PADDING
  113. btnOperate:setPosition(cc.p(self.btnPosX, y))
  114. self.ui.Items.Layout_Operate:addChild(btnOperate,2)
  115. if operates and table.nums(operates) <=1 then
  116. self.btnPosX = self.btnPosX-50
  117. btnOperate:setPosition(cc.p(self.btnPosX, y))
  118. local gangBg = cc.Layout:create()
  119. gangBg:setBackGroundImageScale9Enabled(true)
  120. gangBg:setBackGroundImage("zg_room_gangBg.png",cc.TextureResType.plistType)
  121. gangBg:setPosition(cc.p(btnOperate:getPositionX()+30, y-40))
  122. gangBg:setSize(cc.size(80,80))
  123. self.ui.Items.Layout_Operate:addChild(gangBg)
  124. gangBg:registerClick(function(sender)
  125. self:sendOp({opType = operates[1].opType,opCard = operates[1].opCard,redNum = operates[1].redNum})
  126. end)
  127. local mj=MJ:new(operates[1].opCard,MJDefine.MJType.Stand,MJDefine.MyViewId,self.desktopType)
  128. mj:setScale(0.5)
  129. mj:setAnchorPoint(cc.p(0.5,0.5))
  130. gangBg:addChild(mj)
  131. mj:setPosition(cc.p(gangBg:getContentSize().width/2+10, gangBg:getContentSize().height/2))
  132. end
  133. btnOperate:registerClick(function()
  134. -- playBtnEffect()
  135. if operates and table.nums(operates) <=1 then
  136. self:sendOp({opType = operates[1].opType,opCard = operates[1].opCard,redNum = operates[1].redNum})
  137. else
  138. self:showGangItem(operates)
  139. end
  140. end)
  141. end
  142. --发送出牌消息
  143. function zigong7zHandCardView:sendOutCard()
  144. if not self.isOutCard or not self.touchMJ then return end
  145. app.room:dispatchEvent({name = MJDefine.MJEvent.HideOperateLayer})
  146. zigong7zHandCardView.super.sendOutCard(self)
  147. app.room.roomInfo.lastOutViewId = MJDefine.MyViewId
  148. end
  149. --恢复麻将位置和透明度
  150. function zigong7zHandCardView:resetHandCardsPosAndOpacity()
  151. logD("zigong7zHandCardView:resetHandCardsPosAndOpacity")
  152. local initX,initY=self:getInitHandCardPostion()
  153. local x=initX
  154. local y=initY
  155. local insertX=0
  156. local insertY=0
  157. if MJDefine.isAutomatic == true then
  158. self:sortHandCards()
  159. else
  160. end
  161. for k,mj in pairs(self.handCards) do
  162. mj:setPosition(x,y)
  163. mj:setOpacity(255)
  164. x,y=MJPositionCompnent.getHandCardPosition(self.viewId,initX,initY,mj,k,self.desktopType)
  165. end
  166. return insertX,insertY
  167. end
  168. function zigong7zHandCardView:resetHandCards(insertMj,isAnimation)
  169. self._handcard:resetHandCards(insertMj,isAnimation)
  170. end
  171. function zigong7zHandCardView:moveRestHandCards(insertMj,isAnimation)
  172. local initX,initY=self:getInitHandCardPostion()
  173. local x=initX
  174. local y=initY
  175. local insertX=0
  176. local insertY=0
  177. for k,mj in pairs(self.handCards) do
  178. --如果可以出牌 最后的麻将不重置位置
  179. -- local isLastCard = self.isOutCard and k==#self.handCards
  180. -- if isLastCard then
  181. -- break
  182. -- end
  183. if mj.status ~= MJDefine.MJStatus.Disable then
  184. mj:setStatus(MJDefine.MJStatus.Normal)
  185. mj:stopAllActions()
  186. end
  187. logD("k:"..k.." value:"..mj.value)
  188. if insertMj==mj then --记录插入麻将的坐标 动画需要
  189. insertX=x
  190. insertY=y
  191. else
  192. if isAnimation then
  193. mj:runAction(cc.MoveTo:create(0.1,cc.p(x,y)))
  194. else
  195. mj:setPosition(x,y)
  196. end
  197. end
  198. x,y=MJPositionCompnent.getHandCardPosition(self.viewId,initX,initY,mj,k,self.desktopType)
  199. end
  200. return insertX,insertY
  201. end
  202. function zigong7zHandCardView:buGang(card,newRedNum)
  203. self._handcard:buGang(card, cardNum)
  204. end
  205. function zigong7zHandCardView:retoreBuGang(card,newRedNum)
  206. self._handcard:restoreBuGang(card, cardNum)
  207. end
  208. function zigong7zHandCardView:lockHandCard(isForbidTouch)
  209. self._handcard:lockHandCard(isForbidTouch)
  210. end
  211. function zigong7zHandCardView:moveLastOpenCard()
  212. local count=#self.handCards
  213. -- 没进去判断
  214. if count>=2 then
  215. local frontMj=self.handCards[count-1]
  216. local lastMj=self.handCards[count]
  217. local margin=0
  218. local frontX=frontMj:getPositionX()
  219. local frontY=frontMj:getPositionY()
  220. local lastX=lastMj:getPositionX()
  221. local lastY=lastMj:getPositionY()
  222. local MJLastHandCardOffset=MJDefine.MJLastHandCardOffset[self.desktopType]
  223. local MJ_HAND_CARD_LEFT_PADDING_HEIGHT = MJDefine.MJ_HAND_CARD_LEFT_PADDING_HEIGHT[self.desktopType]
  224. --根据视图ID 计算坐标
  225. if self.viewId==MJDefine.PlayerViewType.My then
  226. margin=lastMj:getContentSize().width
  227. -- if math.abs(lastX-frontX)==margin then
  228. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  229. -- end
  230. elseif self.viewId==MJDefine.PlayerViewType.LeftUp then
  231. margin=MJ_HAND_CARD_LEFT_PADDING_HEIGHT
  232. -- if math.abs(lastY-frontY)==margin then
  233. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  234. lastMj:setPositionY(lastY+MJLastHandCardOffset[self.viewId].y)
  235. -- end
  236. elseif self.viewId==MJDefine.PlayerViewType.LeftDown then
  237. margin=MJ_HAND_CARD_LEFT_PADDING_HEIGHT
  238. -- if math.abs(lastY-frontY)==margin then
  239. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  240. lastMj:setPositionY(lastY+MJLastHandCardOffset[self.viewId].y)
  241. -- end
  242. elseif self.viewId==MJDefine.PlayerViewType.RightUp then
  243. margin=MJ_HAND_CARD_LEFT_PADDING_HEIGHT
  244. -- if math.abs(lastY-frontY)==margin then
  245. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  246. lastMj:setPositionY(lastY+MJLastHandCardOffset[self.viewId].y)
  247. -- end
  248. elseif self.viewId==MJDefine.PlayerViewType.RightDown then
  249. margin=MJ_HAND_CARD_LEFT_PADDING_HEIGHT
  250. -- if math.abs(lastY-frontY)==margin then
  251. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  252. lastMj:setPositionY(lastY+MJLastHandCardOffset[self.viewId].y)
  253. -- end
  254. elseif self.viewId==MJDefine.PlayerViewType.Top then
  255. margin=lastMj:getContentSize().width-MJDefine.MJ_HAND_CARD_TOP_OFFSET_X
  256. -- print("width:"..lastMj:getContentSize().width)
  257. -- if math.abs(lastX-frontX)==margin then
  258. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  259. -- end
  260. end
  261. end
  262. end
  263. function zigong7zHandCardView:onTouchMove(touch)
  264. zigong7zHandCardView.super.onTouchMove(self,touch)
  265. if self.touchMJ and self.isTouchMove==true then
  266. local pos = self.touchMJ:getPosition()
  267. local index = nil
  268. for k,v in pairs(self.handCards) do
  269. local width = v:getContentSize().width
  270. local height = v:getContentSize().height
  271. local x = v:getPosition().x-width/2
  272. local y = v:getPosition().y-height/2
  273. local rect = cc.Rectangle:new(x,y,width*0.8,height*0.8)
  274. --如果可以出牌 最后的麻将不参与移动操作
  275. -- local isLastCard = self.isOutCard and k==#self.handCards
  276. if rect:contains(pos.x,pos.y) and v ~=self.touchMJ and not isLastCard then
  277. index = k
  278. break
  279. end
  280. end
  281. if index then
  282. local touchMJIndex = table.indexOf(self.handCards,self.touchMJ)
  283. local offsetIndex = touchMJIndex-index
  284. local num = math.abs(offsetIndex)-1
  285. --大于0则往左移动 3-1:3-2 2-1 小于0右 5-8:5-6 6-7 7-8
  286. local reduce = offsetIndex>0 and -1 or 1
  287. for i = touchMJIndex,index-reduce,reduce do
  288. self.handCards[i],self.handCards[i+reduce] = self.handCards[i+reduce],self.handCards[i]
  289. end
  290. local x,y = self:moveRestHandCards(self.touchMJ)
  291. self.touchMJ:recordPostion(cc.p(x,y))
  292. end
  293. end
  294. end
  295. function zigong7zHandCardView:touchOutCard()
  296. if self.touchMJ and self.isTouchMove and (not tolua.isnull(self.touchMJ)) then
  297. if self.touchMJ:getPositionY()>MJDefine.MJ_TOUCH_OUT_CARD_Y and self.isOutCard then
  298. self:sendOutCard()
  299. else
  300. self.touchMJ:restorePostion()
  301. self.touchMJ:setStatus(MJDefine.MJStatus.Normal)
  302. app.room:dispatchEvent({name = MJDefine.MJEvent.SelectCard})
  303. app.room:dispatchEvent({name = MJDefine.MJEvent.ShowTing})
  304. if self.isOutCard then
  305. self:moveLastCard()
  306. end
  307. end
  308. end
  309. self.isTouchMove=false
  310. end
  311. function zigong7zHandCardView:outCardFalg(event)
  312. if event.viewId == MJDefine.MyViewId then
  313. app.room:dispatchEvent({name = MJDefine.MJEvent.HideOperateLayer})
  314. end
  315. self._outCardView:outCardFalg(event)
  316. end
  317. function zigong7zHandCardView:getHandCardNodes()
  318. return self._handcard:getHandCardNodes()
  319. end
  320. function zigong7zHandCardView:createHandCards(arg)
  321. self._handcard:createHandCards(arg)
  322. end
  323. function zigong7zHandCardView:createOutCards(arg)
  324. self._outCardView:createOutCards(arg)
  325. end
  326. function zigong7zHandCardView:createGroupCards(arg,fromViewId)
  327. self._handcard:createGroupCards(arg, fromViewId)
  328. end
  329. function zigong7zHandCardView:createOpenHandCards(arg)
  330. self._handcard:createOpenHandCards(arg)
  331. end
  332. function zigong7zHandCardView:initTouchEvent()
  333. if self._isReplay then--回放不需要触摸事件
  334. self:getEventDispatcher():removeEventListenersForTarget(self)
  335. return
  336. end
  337. self._handcard:initTouchEvent()
  338. end
  339. -- 获得麻将的initY坐标
  340. function zigong7zHandCardView:getHandCardPosInitY()
  341. local x, y = self._handcard:getCardPosition(self._handcard:getLastCard(), self._handcard:getViewId(), self._handcard:getHandCardsLength())
  342. return y or 0
  343. end
  344. --设置是否回放
  345. function zigong7zHandCardView:setReplay(b)
  346. self._handcard:setReplay(b)
  347. end
  348. function zigong7zHandCardView:onEnter()
  349. zigong7zHandCardView.super.onEnter(self)
  350. --绑定事件
  351. self:bindEvent(app.room , MJDefine.MJEvent.SelectCard , handler(self , self.selectCard))
  352. self:bindEvent(app.room , MJDefine.MJEvent.OutCardFalg , handler(self , self.outCardFalg))
  353. end
  354. --最后一张牌显示箭头
  355. function zigong7zHandCardView:pushLastCardTing()
  356. self._handcard:pushLastCardTing()
  357. end
  358. function zigong7zHandCardView:setTing(b)
  359. local handCarsNum = self:getHandCardsNum()
  360. if not (handCarsNum%3 == 2) then--如果不是出牌阶段,不显示箭头
  361. b = false
  362. end
  363. self._handcard:setTing(b)
  364. end
  365. function zigong7zHandCardView:selectCard(event)
  366. --if not self.touchMJ then return end
  367. if event.value then
  368. for k,v in pairs(self.outCards) do
  369. if not tolua.isnull(v) then
  370. if v.value==event.value then
  371. v:setStatus(MJDefine.MJStatus.Select)
  372. else
  373. v:setStatus(MJDefine.MJStatus.Normal)
  374. end
  375. end
  376. end
  377. else
  378. --self:setTing(false)
  379. for k,v in pairs(self.outCards) do
  380. if v.status~=MJDefine.MJStatus.Disable and (not tolua.isnull(v)) then
  381. v:setStatus(MJDefine.MJStatus.Normal)
  382. end
  383. end
  384. end
  385. -- app.room:dispatchEvent({name = "showProp",response = response});
  386. end
  387. function zigong7zHandCardView:isCanOutCard()
  388. return self._handcard:isCanOutCard(true)
  389. end
  390. function zigong7zHandCardView:moveLastCard()
  391. local count=#self.handCards
  392. if count>=2 then
  393. local frontMj=self.handCards[count-1]
  394. local lastMj=self.handCards[count]
  395. local margin=0
  396. local frontX=frontMj:getPositionX()
  397. local frontY=frontMj:getPositionY()
  398. local lastX=lastMj:getPositionX()
  399. local lastY=lastMj:getPositionY()
  400. local MJLastHandCardOffset=MJDefine.MJLastHandCardOffset[self.desktopType]
  401. local MJ_HAND_CARD_LEFT_PADDING_HEIGHT = MJDefine.MJ_HAND_CARD_LEFT_PADDING_HEIGHT[self.desktopType]
  402. --根据视图ID 计算坐标
  403. if self.viewId==MJDefine.PlayerViewType.My then
  404. margin=lastMj:getContentSize().width
  405. local space = math.ceil(math.abs(lastX-frontX))
  406. if space == margin then
  407. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  408. end
  409. elseif self.viewId==MJDefine.PlayerViewType.Left then
  410. margin=MJ_HAND_CARD_LEFT_PADDING_HEIGHT
  411. if math.abs(lastY-frontY)==margin then
  412. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  413. lastMj:setPositionY(lastY+MJLastHandCardOffset[self.viewId].y)
  414. end
  415. elseif self.viewId==MJDefine.PlayerViewType.Right then
  416. margin=MJ_HAND_CARD_LEFT_PADDING_HEIGHT
  417. if math.abs(lastY-frontY)==margin then
  418. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  419. lastMj:setPositionY(lastY+MJLastHandCardOffset[self.viewId].y)
  420. end
  421. elseif self.viewId==MJDefine.PlayerViewType.Top then
  422. margin=lastMj:getContentSize().width-MJDefine.MJ_HAND_CARD_TOP_OFFSET_X
  423. -- print("width:"..lastMj:getContentSize().width)
  424. if math.abs(lastX-frontX)==margin then
  425. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  426. end
  427. end
  428. end
  429. end
  430. --更新初始坐标
  431. function zigong7zHandCardView:getInitHandCardPostion()
  432. local initX=self.initX
  433. local initY=self.initY
  434. local MJGroupWithMJMargin=MJDefine.MJGroupWithMJMargin[self.desktopType]
  435. --如果有麻将组重新定位手牌位置
  436. if #self.handGroupCards>0 then
  437. local lastGroup=self.handGroupCards[#self.handGroupCards]
  438. if self.viewId==MJDefine.PlayerViewType.My then
  439. -- initX=initX--+MJGroupWithMJMargin[self.viewId].x
  440. elseif self.viewId==MJDefine.PlayerViewType.Left then
  441. initX=initX+MJGroupWithMJMargin[self.viewId].x
  442. initY=lastGroup:getPositionY()-lastGroup:getContentSize().height/2-MJGroupWithMJMargin[self.viewId].y
  443. elseif self.viewId==MJDefine.PlayerViewType.Right then
  444. initX=initX+MJGroupWithMJMargin[self.viewId].x
  445. initY=lastGroup:getPositionY()+lastGroup:getContentSize().height/2+MJGroupWithMJMargin[self.viewId].y
  446. elseif self.viewId==MJDefine.PlayerViewType.Top then
  447. -- initX=initX--+MJGroupWithMJMargin[self.viewId].x
  448. end
  449. end
  450. return initX,initY
  451. end
  452. function zigong7zHandCardView:removeAllHandCards()
  453. self._handcard:removeAllHandCards()
  454. end
  455. --删除触摸事件only
  456. function zigong7zHandCardView:removeTouchEventOnly()
  457. self:getEventDispatcher():removeEventListenersForTarget(self)
  458. end
  459. --初始化换牌触摸事件
  460. function zigong7zHandCardView:initSwapCardTouchEvent()
  461. if self._isReplay then--回放不需要触摸事件
  462. self:getEventDispatcher():removeEventListenersForTarget(self)
  463. return
  464. end
  465. self._handcard:initSwapCardTouchEvent()
  466. end
  467. --初始化摆牌触摸事件
  468. function zigong7zHandCardView:initBaiCardTouchEvent()
  469. if self._isReplay then--回放不需要触摸事件
  470. self:getEventDispatcher():removeEventListenersForTarget(self)
  471. return
  472. end
  473. self._handcard:initBaiCardTouchEvent()
  474. end
  475. function zigong7zHandCardView:removeTouchEvent()
  476. self:getEventDispatcher():removeEventListenersForTarget(self)
  477. if self.ui.Items.Button_Sure then
  478. self.ui.Items.Button_Sure:setVisible(false)
  479. self.ui.Items.Button_Sure:removeFromParent()
  480. self.ui.Items.Button_Sure=nil
  481. end
  482. self:setTing(false)
  483. end
  484. --换三张时的触摸事件
  485. function zigong7zHandCardView:onSwapTouchBegan(touch)
  486. self._selectCards = self._selectCards or {}
  487. local touchPos=self:convertToNodeSpace(touch:getLocation())
  488. for k,v in pairs(self.handCards) do
  489. local width = v:getContentSize().width
  490. local height = v:getContentSize().height
  491. local x = v:getPosition().x-width/2
  492. local y = v:getPosition().y-height/2
  493. local rect = cc.Rectangle:new(x,y,width,height)
  494. if rect:contains(touchPos.x,touchPos.y) then
  495. self.touchMJ=v
  496. if self.touchMJ:getSelected()==MJDefine.MJStatus.Select then
  497. self.touchMJ:runDeSelectAnimationWithTime(0.1)
  498. self.touchMJ:setSelected(MJDefine.MJStatus.Normal)
  499. table.removeItem(self._selectCards,self.touchMJ)
  500. else
  501. self.touchMJ:recordPostion(cc.p(self.touchMJ:getPositionX(),self.initY))
  502. self.touchMJ:runSelectAnimation()
  503. self.touchMJ:setSelected(MJDefine.MJStatus.Select)
  504. table.insert(self._selectCards,self.touchMJ)
  505. end
  506. if self:checkIsInSwapRule() then
  507. app.room:dispatchEvent({name = MJDefine.MJEvent.CheckIsInSwapRule,canCommit=true})
  508. else
  509. app.room:dispatchEvent({name = MJDefine.MJEvent.CheckIsInSwapRule,canCommit=false})
  510. end
  511. break
  512. end
  513. end
  514. end
  515. --判断是否符合换三张规则
  516. function zigong7zHandCardView:checkIsInSwapRule()
  517. return self._handcard:checkIsInSwapRule()
  518. end
  519. --获取选择的牌
  520. function zigong7zHandCardView:getSelectedCards()
  521. return self._handcard:getSelectedCards()
  522. end
  523. --通过传进来的参数cards来选出手牌里的cards,服务器推荐选牌用
  524. function zigong7zHandCardView:selectCardsByCards(cards)
  525. self._handcard:selectCardsByCards(cards)
  526. end
  527. --删除选中的牌
  528. function zigong7zHandCardView:deleteSelectedCards()
  529. self._handcard:deleteSelectedCards()
  530. end
  531. --设置定缺类型 0万,1同,2条
  532. function zigong7zHandCardView:setQueType(que)
  533. self._handcard:setQueType(que)
  534. end
  535. --检查手牌是否有定缺牌
  536. function zigong7zHandCardView:checkHandCardHaveQue(que)
  537. return self._handcard:checkHandCardHaveQue(que)
  538. end
  539. --检测定缺牌,屏蔽缺牌以外的牌(定缺牌打完钱不能打别的牌)
  540. function zigong7zHandCardView:checkQueAndDisableOtherCard()
  541. self._handcard:checkQueAndDisableOtherCard()
  542. end
  543. --设置所有手牌状态为Disable
  544. function zigong7zHandCardView:setHandCardsDidable()
  545. self._handcard:setHandCardsDidable()
  546. end
  547. --设置非打出去能听的牌Disable
  548. function zigong7zHandCardView:setNotTingCardDidable(is)
  549. self._handcard:setNotTingCardDidable(is)
  550. end
  551. --创建换三张的牌
  552. function zigong7zHandCardView:createSwapCards(cards)
  553. local group = self._handcard:createSwapCards(cards)
  554. if group then
  555. self.ui.Items.Layout_Animation:addChild(group,1,SWAP_CARD_TAG)
  556. local pos = self:getAnimationPostion()
  557. if self.viewId==MJDefine.MyViewId then
  558. pos.x = pos.x - 50
  559. elseif self.viewId==MJDefine.PlayerViewType.Top then
  560. pos.y = pos.y - 20
  561. end
  562. group:setPosition(pos)
  563. end
  564. end
  565. --删除换三张的牌
  566. function zigong7zHandCardView:deleteSwapCards()
  567. local swapCardsUI = self.ui.Items.Layout_Animation:getChildByTag(SWAP_CARD_TAG)
  568. if swapCardsUI and (not tolua.isnull(swapCardsUI)) then
  569. swapCardsUI:removeFromParent()
  570. swapCardsUI = nil
  571. end
  572. end
  573. --插牌动画
  574. function zigong7zHandCardView:runInsertCardAction()
  575. self:getEventDispatcher():removeEventListenersForTarget(self)
  576. local insertMj=self.handCards[#self.handCards]
  577. local iX=insertMj:getPositionX()
  578. local iY=insertMj:getPositionY()
  579. local height=insertMj:getContentSize().height
  580. local x,y=self:resetHandCards(insertMj,true)
  581. local actions={
  582. cc.MoveTo:create(0.1,cc.p(insertMj:getPositionX(),insertMj:getPositionY()+height)),
  583. cc.MoveTo:create(0.1,cc.p(x,insertMj:getPositionY()+height)),
  584. cc.MoveTo:create(0.1,cc.p(x,self.initY)),
  585. cc.CallFunc:create(function()
  586. self:initTouchEvent()
  587. if self.touchMJ and not tolua.isnull(self.touchMJ) then
  588. self:resetHandCards()
  589. end
  590. end)
  591. }
  592. insertMj:runAction(cc.Sequence:create(actions))
  593. end
  594. --出牌动画
  595. function zigong7zHandCardView:runOutCardAction(value)
  596. local outCardBgPath = 'mj_zigong7z/res/zy_fangjian/zigong_outcard_bg.png'
  597. local outCardBg = cc.ImageView:create(outCardBgPath)
  598. self:addChild(outCardBg)
  599. local showCard=MJ:new(value,MJDefine.MJType.Stand,MJDefine.MyViewId,self.desktopType)
  600. outCardBg:addChild(showCard)
  601. local outCardSize = outCardBg:getContentSize()
  602. showCard:setPosition(cc.p(outCardSize.width/2, outCardSize.height/2))
  603. outCardBg:setPosition(self:getOutCardShowPostion())
  604. local actions={
  605. cc.DelayTime:create(MJDefine.MJ_TIME_OUT_CARD_WAIT),
  606. cc.Spawn:create(
  607. cc.Spawn:create(
  608. cc.ScaleTo:create(MJDefine.MJ_TIME_OUT_CARD,MJDefine.MJ_TIME_OUT_CARD)
  609. )
  610. ),
  611. cc.CallFunc:create(function()
  612. showCard:removeFromParent()
  613. end),
  614. cc.RemoveSelf:create(),
  615. }
  616. outCardBg:runAction(cc.Sequence:create(actions))
  617. end
  618. function zigong7zHandCardView:getMjByValue(value)
  619. return self._handcard:getMjByValue(value)
  620. end
  621. --倒序遍历手牌
  622. function zigong7zHandCardView:getMjByValueReverseOrder(value)
  623. for i = #self.handCards,1,-1 do
  624. local v = self.handCards[i]
  625. if v.value==value then
  626. return v
  627. end
  628. end
  629. end
  630. function zigong7zHandCardView:removeHandCard(arg)
  631. self._handcard:removeHandCard(arg)
  632. end
  633. function zigong7zHandCardView:removeOutCard(arg)
  634. self._outCardView:removeOutCard(arg)
  635. end
  636. --玩家出牌
  637. function zigong7zHandCardView:onOutCard(card,callback)
  638. self:runOutCardAction(card)
  639. self._handcard:onOutCard(card, callback)
  640. self._outCardView:onOutCard(card, callback)
  641. end
  642. function zigong7zHandCardView:getAnimationPostion()
  643. return self.ui.Items["Layout_Aniamtion_"..self.viewId]:getPosition()
  644. end
  645. function zigong7zHandCardView:getOutCardShowPostion()
  646. return self.ui.Items["ImageView_Show_"..self.viewId]:getPosition()
  647. end
  648. --获取手牌数量
  649. function zigong7zHandCardView:getHandCardsNum()
  650. return self._handcard:getHandCardsLength()
  651. end
  652. return zigong7zHandCardView