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
25 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 MJHandCardView=MJFramework.MJFrameworkClassImprot("mj.luaScript.Views.Game.MJHandCardView")
  7. local MJSound = MJFramework.MJImport("mj.luaScript.MJSound")
  8. local NeiJiangMJHandCardView = class("NeiJiangMJHandCardView", MJHandCardView)
  9. function NeiJiangMJHandCardView:ctor(viewId,desktopType)
  10. NeiJiangMJHandCardView.super.ctor(self,viewId,desktopType)
  11. end
  12. function NeiJiangMJHandCardView:loadUI()
  13. local ui
  14. if self.desktopType==MJDefine.DesktopType.TwoD or self.desktopType==MJDefine.DesktopType.TwoDGold then
  15. ui = loadUI("mj_neijiang/res/ui_fangjian/mj_neijiang_hand_card_2d.ui");
  16. else
  17. ui = loadUI("mj_neijiang/res/ui_fangjian/mj_neijiang_hand_card.ui");
  18. end
  19. autoAdapt(ui)
  20. self.ui = ui;
  21. cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("mj_neijiang/res/zy_fangjian/mj_neijiang_operation_btn.plist");
  22. self:addChild(ui)
  23. end
  24. function NeiJiangMJHandCardView:initView()
  25. NeiJiangMJHandCardView.super.initView(self)
  26. self:hideOperateItem()
  27. end
  28. function NeiJiangMJHandCardView:pushTing()
  29. local tings=app.room.roomInfo.tings
  30. --local temp=table.tostring(tings)
  31. if tings then
  32. for k,v in pairs(tings) do
  33. for _,mj in pairs(self.handCards) do
  34. if mj.value==v.outCard then
  35. mj:setTing(true)
  36. end
  37. end
  38. end
  39. end
  40. end
  41. --摸牌
  42. function NeiJiangMJHandCardView:onGetCard(operates,card)
  43. self:createHandCards(card)
  44. if self.viewId==MJDefine.PlayerViewType.Right and not self.isReplay then
  45. self:resetHandCards()
  46. end
  47. if not operates or table.nums(operates)==0 then
  48. self:setOutCardEnable(true)
  49. MJDefine.outCardSendGuo = false
  50. else
  51. self:setOutCardEnable(true)
  52. MJDefine.outCardSendGuo = true
  53. -- self:canOutCard(true)
  54. end
  55. if self.viewId==MJDefine.MyViewId then
  56. MJSound.PlayGetCard()
  57. end
  58. -- self:moveLastCard()
  59. end
  60. function NeiJiangMJHandCardView:setOutCardEnable(b)
  61. self.isOutCard=b
  62. if b then
  63. self:moveLastCard()
  64. end
  65. if self.viewId==MJDefine.MyViewId and self.ui.Items.Button_Sure then
  66. -- self.ui.Items.Button_Sure:setVisible(b)
  67. end
  68. end
  69. --不管是否自动对齐,红中始终保持放在最左边
  70. function NeiJiangMJHandCardView:sortRedMahjong()
  71. local redNum = 0
  72. for k,mj in pairs(self.handCards) do
  73. if mj.value == 65 then
  74. redNum = redNum + 1
  75. table.remove(self.handCards,k)
  76. table.insert(self.handCards,redNum,mj)
  77. end
  78. end
  79. end
  80. function NeiJiangMJHandCardView:removeHandCardByNum(value,num)
  81. if self.viewId==MJDefine.MyViewId or self.isReplay then
  82. for i=1,num do
  83. local mj =self:getMjByValue(value)
  84. if mj then
  85. table.removeItem(self.handCards,mj)
  86. mj:removeFromParent()
  87. end
  88. end
  89. else
  90. -- local num=#self.handCards-#arg
  91. -- print(num)
  92. for i=#self.handCards,#self.handCards-num+1,-1 do
  93. self.handCards[i]:removeFromParent()
  94. table.remove(self.handCards,i)
  95. end
  96. end
  97. end
  98. -- function NeiJiangMJHandCardView:touchOutCard()
  99. -- NeiJiangMJHandCardView.super.touchOutCard(self)
  100. -- app.room:dispatchEvent({name = MJDefine.MJEvent.ShowTing})
  101. -- end
  102. function NeiJiangMJHandCardView:getGroup(card,opType)
  103. for k,v in pairs(self.handGroupCards) do
  104. if v.opType==opType and (v.mjs[1].value==card or v.mjs[2].value == card or v.mjs[3].value == card) then
  105. local redNum = 0
  106. for i,mj in pairs(v.mjs) do
  107. --先取出原组合的红中个数
  108. if mj.value == 65 then
  109. redNum = redNum + 1
  110. end
  111. end
  112. return v,redNum
  113. end
  114. end
  115. end
  116. function NeiJiangMJHandCardView:createGroupCards(arg,fromViewId)
  117. local x=self.initX
  118. local y=self.initY
  119. local MJGroupStartOffSet = MJDefine.MJGroupStartOffSet[self.desktopType]
  120. for k,v in pairs(arg) do
  121. if not fromViewId then
  122. fromViewId = v.fromViewId
  123. end
  124. local group=MJGroupFactory.CreateGroup(v.values,v.showType,self.viewId,self.desktopType,v.redNum)
  125. if group then
  126. --设置碰杠来源
  127. --group:setDirection(self.viewId,v.fromViewId1)
  128. if group.setAnColor and (v.opType == MJDefine.MJOperateType.OPREATE_PENG
  129. or v.opType == MJDefine.MJOperateType.OPREATE_ZHIGANG
  130. or v.opType == MJDefine.MJOperateType.OPREATE_BAGANG) then
  131. --牌的阴影显示刚好和视图id相反
  132. if v.fromViewId == 1 then v.fromViewId = 3
  133. elseif v.fromViewId==3 then v.fromViewId = 1 end
  134. -- 杠牌对家,换成第四张
  135. if v.opType ~= MJDefine.MJOperateType.OPREATE_PENG then
  136. if v.fromViewId==2 then v.fromViewId = 4 end
  137. end
  138. group:setAnColor(v.opCard, v.fromViewId)
  139. end
  140. group.fromViewId = v.fromViewId ---保留触发操作的玩家位置,补杠需要
  141. group.opType=v.opType
  142. self.ui.Items.Layout_Hand_Card:addChild(group,-4+#self.handGroupCards)
  143. group:setPosition(cc.p(x+MJGroupStartOffSet[self.viewId].x,y+MJGroupStartOffSet[self.viewId].y))
  144. x,y=MJPositionCompnent.getGroupPosition(self.viewId,x,y,group,self.desktopType)
  145. table.insert(self.handGroupCards,group)
  146. end
  147. end
  148. --更新起始位置
  149. self.initX=x
  150. self.initY=y
  151. end
  152. --添加杠操作按钮
  153. function NeiJiangMJHandCardView:addOperateBtn(operates)
  154. local x=self.ui.Items.Layout_Operate:getContentSize().width
  155. local y=self.ui.Items.Layout_Operate:getContentSize().height/2
  156. local btnOperate=cc.Button:createNode()
  157. btnOperate:setDefaults()
  158. btnOperate:setText("")
  159. btnOperate:loadTextureNormal(MJDefine.MJOperateImage[operates[1].opType])
  160. btnOperate:ignoreAnchorPointForPosition(false)
  161. self.btnPosX = self.btnPosX-MJDefine.MJ_OPERATE_PADDING
  162. btnOperate:setPosition(cc.p(self.btnPosX, y))
  163. self.ui.Items.Layout_Operate:addChild(btnOperate,2)
  164. if operates and table.nums(operates) <=1 then
  165. self.btnPosX = self.btnPosX-50
  166. btnOperate:setPosition(cc.p(self.btnPosX, y))
  167. local gangBg = cc.Layout:create()
  168. gangBg:setBackGroundImageScale9Enabled(true)
  169. gangBg:setBackGroundImage("mj_neijiang_operate_bg.png", 1)
  170. gangBg:setPosition(cc.p(btnOperate:getPositionX()+30, y-40))
  171. gangBg:setSize(cc.size(80,80))
  172. self.ui.Items.Layout_Operate:addChild(gangBg)
  173. gangBg:registerClick(function(sender)
  174. self:sendOp({opType = operates[1].opType,opCard = operates[1].opCard,redNum = operates[1].redNum})
  175. end)
  176. local mj=MJ:new(operates[1].opCard,MJDefine.MJType.Stand,MJDefine.MyViewId,self.desktopType)
  177. mj:setScale(0.5)
  178. mj:setAnchorPoint(cc.p(0.5,0.5))
  179. gangBg:addChild(mj)
  180. mj:setPosition(cc.p(gangBg:getContentSize().width/2+10, gangBg:getContentSize().height/2))
  181. end
  182. btnOperate:registerClick(function()
  183. -- playBtnEffect()
  184. if operates and table.nums(operates) <=1 then
  185. self:sendOp({opType = operates[1].opType,opCard = operates[1].opCard,redNum = operates[1].redNum})
  186. else
  187. self:showGangItem(operates)
  188. end
  189. end)
  190. end
  191. function NeiJiangMJHandCardView:showGangItem(operates)
  192. self.ui.Items.Layout_OperateItem:setVisible(true)
  193. self.ui.Items.Layout_Operate_Content:removeAllChildren()
  194. local col = 0
  195. local count = 6
  196. for k,v in pairs(operates) do
  197. local item = self.ui.Items.Layout_MJ_Item:getCopied()
  198. local mj=MJ:new(v.opCard,MJDefine.MJType.Stand,self.viewId,self.desktopType)
  199. item:addChild(mj)
  200. local size = item:getContentSize()
  201. mj:setPosition(cc.p(size.width/2,size.height/2))
  202. self.ui.Items.Layout_Operate_Content:addChild(item)
  203. mj:registerClick(function()
  204. self:sendOp({opType = v.opType,opCard = v.opCard,redNum = v.redNum})
  205. end)
  206. -- local col = k
  207. local row = math.ceil(k/count)
  208. -- logD(k.." :".. (k%count))
  209. -- local isChangeRow = k%count==0
  210. local x = self.ui.Items.Layout_Operate_Content:getContentSize().width-col*133
  211. local y = (row-1)*size.height
  212. item:setPositionX(x)
  213. item:setPositionY(y)
  214. col = col+1
  215. if k%count==0 then
  216. col = 0
  217. end
  218. end
  219. -- local count = table.nums(operates)
  220. -- local padding=10
  221. -- local width=self.ui.Items.Layout_OperateItem:getContentSize().width
  222. -- local height=self.ui.Items.Layout_OperateItem:getContentSize().height
  223. -- local layoutItem = cc.Layout:create()
  224. -- layoutItem:setBackGroundImageScale9Enabled(true)
  225. -- --layoutItem:setBackGroundImage("mj/res/ui/zy_fangjian/mj_gang_bg.png")
  226. -- layoutItem:setAnchorPoint(cc.p(0.5,1))
  227. -- self.ui.Items.Layout_OperateItem:addChild(layoutItem)
  228. -- if count <= 3 then
  229. -- layoutItem:setSize(cc.size(230*count+padding*count,height+padding))
  230. -- else
  231. -- layoutItem:setSize(cc.size(230*3+padding*3,height*(math.floor(count/3)+1)+(math.floor(count/3)+1)*padding))
  232. -- --self.ui.Items.Layout_OperateItem:setSize(cc.size(230*3+padding*3,height*(math.floor(count/3)+1)+(math.floor(count/3)+1)*padding))
  233. -- end
  234. -- layoutItem:setPosition(cc.p(width/2,height))
  235. -- local layoutHeight = layoutItem:getContentSize().height
  236. -- local y = 0
  237. -- for k,v in ipairs(operates) do
  238. -- local layout=cc.Layout:create()
  239. -- layout:setBackGroundImageScale9Enabled(true)
  240. -- layout:setBackGroundImage("mj/res/ui/zy_fangjian/mj_gang_bg.png")
  241. -- self.ui.Items.Layout_OperateItem:addChild(layout)
  242. -- layout:setSize(cc.size(230,height))
  243. -- layout:setAnchorPoint(cc.p(0,1))
  244. -- if k%3 == 1 then
  245. -- y = layoutHeight -15- height*(math.floor(k/3)+1)-10*(math.floor(k/3)+1)
  246. -- end
  247. -- if count < 3 then
  248. -- layout:setPosition(cc.p(-145+padding*(k-1)+230*(k-1),layoutHeight-15))
  249. -- elseif count == 3 then
  250. -- layout:setPosition(cc.p(-260+padding*(k-1)+230*(k-1),layoutHeight-15))
  251. -- else
  252. -- layout:setPosition(cc.p(-260+padding*(k%3-1)+230*(k%3-1),y))
  253. -- if k%3 == 0 then
  254. -- layout:setPosition(cc.p(-260+padding*2+230*2,y))
  255. -- end
  256. -- end
  257. -- for i=1,4 do
  258. -- local mj
  259. -- if v.redNum > 0 and i<=v.redNum then
  260. -- mj=MJ:new(65,MJDefine.MJType.Stand,self.viewId,self.desktopType)
  261. -- else
  262. -- mj=MJ:new(v.opCard,MJDefine.MJType.Stand,self.viewId,self.desktopType)
  263. -- end
  264. -- mj:setScale(0.6)
  265. -- layout:addChild(mj)
  266. -- local mjWidth = mj:getContentSize().width*0.6
  267. -- mj:setAnchorPoint(cc.p(0.5,0.5))
  268. -- mj:setPosition(32+3*(i-1)+mjWidth*(i-1),layout:getContentSize().height/2)
  269. -- end
  270. -- layout:registerClick(function ( sender )
  271. -- self:sendOp({opType = v.opType,opCard = v.opCard,redNum = v.redNum})
  272. -- end)
  273. -- end
  274. end
  275. function NeiJiangMJHandCardView:hideOperateItem()
  276. if self.ui.Items.Layout_OperateItem then
  277. self.ui.Items.Layout_Operate_Content:removeAllChildren()
  278. self.ui.Items.Layout_OperateItem:setVisible(false)
  279. end
  280. end
  281. --显示操作
  282. function NeiJiangMJHandCardView:showOperate(operates,card,isMyGetCard)
  283. if not operates or table.nums(operates)==0 then return end
  284. print(table.tostring(operates))
  285. local card = card or 0
  286. self:hideOperateItem()
  287. self:hideOperate()
  288. if operates and table.nums(operates) > 0 then
  289. self.ui.Items.Button_Sure:setVisible(false)
  290. end
  291. local newOperates = {}
  292. --有操作默认添加过的操作
  293. local operate=MJMessage.OperateRequest:new()
  294. operate.opType=MJDefine.MJOperateType.OPREATE_CANCEL
  295. operate.opCard=card
  296. operate.redNum = 0
  297. --table.insert(operates.Datas,1,operate)
  298. table.insert(newOperates,1,operate)
  299. self.ui.Items.Layout_Operate:removeAllChildren()
  300. self.ui.Items.Layout_Operate:setVisible(true)
  301. local x=self.ui.Items.Layout_Operate:getContentSize().width
  302. local y=self.ui.Items.Layout_Operate:getContentSize().height/2
  303. self.btnPosX = x
  304. local gangCards ={} --所有杠組合
  305. local onceGang = false
  306. --operates.Datas包含所有碰杠组合,服务器不管是否贴鬼碰/杠,需客户端自己分离贴鬼碰和无贴鬼碰
  307. for k,v in pairs(operates.Datas) do
  308. if v.opType ~= MJDefine.MJOperateType.OPREATE_CANCEL then
  309. if MJDefine.isTieGui == true then --选择贴鬼碰/杠
  310. if (v.opType == MJDefine.MJOperateType.OPREATE_DIANPAOHU) or (v.opType == MJDefine.MJOperateType.OPREATE_ZIMOHU) or onceGang == false then
  311. table.insert(newOperates,v)
  312. end
  313. if (v.opType==MJDefine.MJOperateType.OPREATE_ZHIGANG) or (v.opType==MJDefine.MJOperateType.OPREATE_BAGANG) or (v.opType==MJDefine.MJOperateType.OPREATE_ANGANG) then
  314. onceGang = true
  315. table.insert(gangCards,v)
  316. end
  317. else
  318. if (v.opType == MJDefine.MJOperateType.OPREATE_DIANPAOHU) or (v.opType == MJDefine.MJOperateType.OPREATE_ZIMOHU) then
  319. table.insert(newOperates,v)
  320. end
  321. --不勾选贴鬼碰/杠时,过滤掉带红中的碰杠
  322. if v.redNum == 0 and v.opType ~= MJDefine.MJOperateType.OPREATE_DIANPAOHU and (v.opType ~= MJDefine.MJOperateType.OPREATE_ZIMOHU) then
  323. if onceGang == false then
  324. table.insert(newOperates,v)
  325. end
  326. if (v.opType==MJDefine.MJOperateType.OPREATE_ZHIGANG) or (v.opType==MJDefine.MJOperateType.OPREATE_BAGANG) or (v.opType==MJDefine.MJOperateType.OPREATE_ANGANG) then
  327. onceGang = true
  328. table.insert(gangCards,v)
  329. end
  330. end
  331. end
  332. end
  333. end
  334. if table.nums(newOperates) >= 2 then
  335. local function sortOperate(opA,opB)
  336. if opA.opType<opB.opType then
  337. return true
  338. elseif opA.opType==opB.opType then
  339. if opA.opCard<opB.opCard then
  340. return true
  341. end
  342. end
  343. return false
  344. end
  345. table.sort(newOperates,sortOperate)
  346. --没有碰杠操作,此条件只有不勾选贴鬼碰/杠才可能会执行
  347. else
  348. --如果不是自己摸牌的情况下
  349. if not isMyGetCard then --app.room.roomInfo.outCardUserId~=app.room:getMyUserId() then --
  350. self:sendOp({opType = MJDefine.MJOperateType.OPREATE_CANCEL,opCard=card,redNum=0})
  351. MJDefine.isSendGuo = true
  352. return;
  353. end
  354. --是自己摸牌
  355. self:hideOperate()
  356. self:setOutCardEnable(true)
  357. logD("NeiJiangMJHandCardView:showOperate setOutCardEnable true")
  358. end
  359. local pengCard = {}
  360. for k,v in pairs(newOperates) do
  361. if MJDefine.MJOperateImage[v.opType] then
  362. if(v.opType==MJDefine.MJOperateType.OPREATE_ZHIGANG) or (v.opType==MJDefine.MJOperateType.OPREATE_BAGANG) or (v.opType==MJDefine.MJOperateType.OPREATE_ANGANG) then
  363. self:addOperateBtn(gangCards)
  364. elseif v.opType==MJDefine.MJOperateType.OPREATE_PENG then
  365. table.insert(pengCard,v)
  366. self:addOperateBtn(pengCard)
  367. elseif (v.opType~=MJDefine.MJOperateType.OPREATE_ZHIGANG) or (v.opType~=MJDefine.MJOperateType.OPREATE_BAGANG) or (v.opType~=MJDefine.MJOperateType.OPREATE_ANGANG) or (v.opType~=MJDefine.MJOperateType.OPREATE_PENG) then
  368. local btnOperate=cc.Button:createNode()
  369. btnOperate:setDefaults()
  370. btnOperate:setText("")
  371. btnOperate:loadTextureNormal(MJDefine.MJOperateImage[v.opType])
  372. btnOperate:ignoreAnchorPointForPosition(false)
  373. self.btnPosX = self.btnPosX-MJDefine.MJ_OPERATE_PADDING
  374. btnOperate:setPosition(cc.p(self.btnPosX, y))
  375. self.ui.Items.Layout_Operate:addChild(btnOperate)
  376. btnOperate:registerClick(function()
  377. -- playBtnEffect()
  378. self:sendOp(v)
  379. end)
  380. end
  381. end
  382. end
  383. end
  384. function NeiJiangMJHandCardView:sendOp(value)
  385. local request = MJMessage.OperateRequest:new()
  386. request.opType = value.opType
  387. request.opCard = value.opCard
  388. request.redNum = value.redNum
  389. -- logE("LHQRecordView:setOutCardVisible"..table.tostring(request))
  390. self.ui:sendMsg(app.room, MJDefine.MJEvent.Operate, request, function(status, response)
  391. logE("MJHandCardView showOperate() response = ", table.tostring(response))
  392. end)
  393. self:hideOperateItem()
  394. self:hideOperate()
  395. if value.opType == MJDefine.MJOperateType.OPREATE_CANCEL then
  396. app.room:dispatchEvent({name = MJDefine.MJEvent.SendGuo})
  397. end
  398. --self:setTing(false)
  399. --app.room:resetTings()
  400. end
  401. --发送出牌消息
  402. function NeiJiangMJHandCardView:sendOutCard()
  403. if not self.isOutCard or not self.touchMJ then return end
  404. self:hideOperate()
  405. -- if MJDefine.outCardSendGuo == true then
  406. -- self:sendOp({opType = MJDefine.MJOperateType.OPREATE_CANCEL,opCard=0,redNum=0})
  407. -- -- self:runDelay(0.1,function()
  408. -- NeiJiangMJHandCardView.super.sendOutCard(self)
  409. -- -- end)
  410. -- else
  411. NeiJiangMJHandCardView.super.sendOutCard(self)
  412. -- end
  413. app.room.roomInfo.lastOutViewId = MJDefine.MyViewId
  414. -- local request = MJMessage.Card:new()
  415. -- request.card = self.touchMJ.value
  416. -- -- logE("LHQRecordView:setOutCardVisible"..table.tostring(request))
  417. -- self.ui:sendMsg(app.room, MJDefine.MJEvent.OutCard, request, function(status, response)
  418. -- logE("MJHandCardView sendOutCard() response = ", table.tostring(response))
  419. -- end)
  420. -- --音效
  421. -- local userInfo=app.room:getUserInfoByViewId(self.viewId)
  422. -- if userInfo then
  423. -- MJSound.PlayMJSound(userInfo.sex,request.card)
  424. -- end
  425. -- app.room:dispatchEvent({name = MJDefine.MJEvent.OutCardFalg,value=self.touchMJ.value,viewId=self.viewId})
  426. -- self:setOutCardEnable(false)
  427. -- app.room:dispatchEvent({name = MJDefine.MJEvent.SelectCard})
  428. -- app.room:dispatchEvent({name = MJDefine.MJEvent.ShowTing})
  429. -- self:setTing(false)
  430. -- app.room:resetTings()
  431. -- self.touchMJ=nil
  432. end
  433. function NeiJiangMJHandCardView:resetHandCards(insertMj,isAnimation)
  434. -- local initX=self.initX
  435. -- local initY=self.initY
  436. local initX,initY=self:getInitHandCardPostion()
  437. local x=initX
  438. local y=initY
  439. local insertX=0
  440. local insertY=0
  441. if MJDefine.isAutomatic == true then
  442. self:sortHandCards()
  443. else
  444. -- self:sortRedMahjong()
  445. end
  446. for k,mj in pairs(self.handCards) do
  447. if mj.status ~= MJDefine.MJStatus.Disable then
  448. mj:setStatus(MJDefine.MJStatus.Normal)
  449. mj:stopAllActions()
  450. end
  451. logD("k:"..k.." value:"..mj.value)
  452. if insertMj==mj then --记录插入麻将的坐标 动画需要
  453. insertX=x
  454. insertY=y
  455. else
  456. if isAnimation then
  457. mj:runAction(cc.MoveTo:create(0.1,cc.p(x,y)))
  458. else
  459. mj:setPosition(x,y)
  460. end
  461. end
  462. x,y=MJPositionCompnent.getHandCardPosition(self.viewId,initX,initY,mj,k,self.desktopType)
  463. end
  464. return insertX,insertY
  465. end
  466. function NeiJiangMJHandCardView:moveRestHandCards(insertMj,isAnimation)
  467. local initX,initY=self:getInitHandCardPostion()
  468. local x=initX
  469. local y=initY
  470. local insertX=0
  471. local insertY=0
  472. for k,mj in pairs(self.handCards) do
  473. --如果可以出牌 最后的麻将不重置位置
  474. -- local isLastCard = self.isOutCard and k==#self.handCards
  475. -- if isLastCard then
  476. -- break
  477. -- end
  478. if mj.status ~= MJDefine.MJStatus.Disable then
  479. mj:setStatus(MJDefine.MJStatus.Normal)
  480. mj:stopAllActions()
  481. end
  482. logD("k:"..k.." value:"..mj.value)
  483. if insertMj==mj then --记录插入麻将的坐标 动画需要
  484. insertX=x
  485. insertY=y
  486. else
  487. if isAnimation then
  488. mj:runAction(cc.MoveTo:create(0.1,cc.p(x,y)))
  489. else
  490. mj:setPosition(x,y)
  491. end
  492. end
  493. x,y=MJPositionCompnent.getHandCardPosition(self.viewId,initX,initY,mj,k,self.desktopType)
  494. end
  495. return insertX,insertY
  496. end
  497. --[[function NeiJiangMJHandCardView:onTouchEnd(touch)
  498. if MJDefine.isAutomatic == false then
  499. local touchPos=self:convertToNodeSpace(touch:getLocation())
  500. local newHandCards = {}
  501. for k,mj in pairs(self.handCards) do
  502. table.insert(newHandCards,k,mj)
  503. local width = mj:getContentSize().width
  504. local height = mj:getContentSize().height
  505. local x = mj:getPosition().x-width/2
  506. local y = mj:getPosition().y-height/2
  507. local rect = cc.Rectangle:new(x,y,width,height)
  508. if rect:contains(touchPos.x,touchPos.y) then
  509. if self.handCards[k] then
  510. table.remove(newHandCards,k)
  511. end
  512. if self.touchMJ and not tolua.isnull(self.touchMJ) then
  513. table.insert(newHandCards,k,self.touchMJ)
  514. end
  515. end
  516. end
  517. self.handCards = nil
  518. self.handCards = DeepCopy(newHandCards)
  519. end
  520. self:touchOutCard()
  521. end
  522. --]]
  523. function NeiJiangMJHandCardView:buGang(card,newRedNum)
  524. local oldGroup,oldRedNum=self:getGroup(card,MJDefine.MJOperateType.OPREATE_PENG)
  525. if not oldGroup then return end
  526. local fromViewId = oldGroup.fromViewId
  527. -- 杠牌对家,换成第四张
  528. if app.room:getMaxPlayerCount()==2 and fromViewId~=0 then
  529. fromViewId = 4
  530. end
  531. if app.room:getMaxPlayerCount()==4 and fromViewId == 2 then
  532. fromViewId = 4
  533. end
  534. local redNum = newRedNum + oldRedNum
  535. local newGroup=MJGroupFactory.CreateGroup({card,card,card,card},MJDefine.MJGroupType.Gang,self.viewId,self.desktopType,redNum)
  536. if newGroup.setAnColor then
  537. newGroup:setAnColor(card, fromViewId)
  538. end
  539. newGroup.fromViewId = fromViewId
  540. newGroup.opType=MJDefine.MJOperateType.OPREATE_BAGANG
  541. newGroup:setPosition(oldGroup:getPosition())
  542. self.ui.Items.Layout_Hand_Card:addChild(newGroup)
  543. local index=table.indexOf(self.handGroupCards,oldGroup)
  544. table.remove(self.handGroupCards,index)
  545. table.insert(self.handGroupCards,index,newGroup)
  546. oldGroup:removeFromParent()
  547. end
  548. function NeiJiangMJHandCardView:retoreBuGang(card,newRedNum)
  549. local oldGroup,oldRedNum=self:getGroup(card,MJDefine.MJOperateType.OPREATE_BAGANG)
  550. if not oldGroup then return end
  551. local fromViewId = oldGroup.fromViewId
  552. -- 杠牌对家,换成第四张
  553. if app.room:getMaxPlayerCount()==2 and fromViewId~=0 then
  554. fromViewId = 4
  555. end
  556. if app.room:getMaxPlayerCount()==4 and fromViewId == 2 then
  557. fromViewId = 4
  558. end
  559. --local redNum = newRedNum + oldRedNum
  560. local redNum = oldRedNum
  561. local newGroup=MJGroupFactory.CreateGroup({card,card,card},MJDefine.MJGroupType.Peng,self.viewId,self.desktopType,redNum)
  562. if newGroup.setAnColor then
  563. newGroup:setAnColor(card, fromViewId)
  564. end
  565. newGroup.fromViewId = fromViewId
  566. newGroup.opType=MJDefine.MJOperateType.OPREATE_PENG
  567. newGroup:setPosition(oldGroup:getPosition())
  568. self.ui.Items.Layout_Hand_Card:addChild(newGroup)
  569. local index=table.indexOf(self.handGroupCards,oldGroup)
  570. table.remove(self.handGroupCards,index)
  571. table.insert(self.handGroupCards,index,newGroup)
  572. oldGroup:removeFromParent()
  573. end
  574. function NeiJiangMJHandCardView:lockHandCard()
  575. for k,v in pairs(self.handCards) do
  576. v:setStatus(MJDefine.MJStatus.Disable)
  577. end
  578. self:getEventDispatcher():removeEventListenersForTarget(self)
  579. end
  580. function NeiJiangMJHandCardView:moveLastOpenCard()
  581. local count=#self.handCards
  582. if count>=2 then
  583. local frontMj=self.handCards[count-1]
  584. local lastMj=self.handCards[count]
  585. local margin=0
  586. local frontX=frontMj:getPositionX()
  587. local frontY=frontMj:getPositionY()
  588. local lastX=lastMj:getPositionX()
  589. local lastY=lastMj:getPositionY()
  590. local MJLastHandCardOffset=MJDefine.MJLastHandCardOffset[self.desktopType]
  591. local MJ_HAND_CARD_LEFT_PADDING_HEIGHT = MJDefine.MJ_HAND_CARD_LEFT_PADDING_HEIGHT[self.desktopType]
  592. --根据视图ID 计算坐标
  593. if self.viewId==MJDefine.PlayerViewType.My then
  594. margin=lastMj:getContentSize().width
  595. -- if math.abs(lastX-frontX)==margin then
  596. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  597. -- end
  598. elseif self.viewId==MJDefine.PlayerViewType.Left then
  599. margin=MJ_HAND_CARD_LEFT_PADDING_HEIGHT
  600. -- if math.abs(lastY-frontY)==margin then
  601. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  602. lastMj:setPositionY(lastY+MJLastHandCardOffset[self.viewId].y)
  603. -- end
  604. elseif self.viewId==MJDefine.PlayerViewType.Right then
  605. margin=MJ_HAND_CARD_LEFT_PADDING_HEIGHT
  606. -- if math.abs(lastY-frontY)==margin then
  607. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  608. lastMj:setPositionY(lastY+MJLastHandCardOffset[self.viewId].y)
  609. -- end
  610. elseif self.viewId==MJDefine.PlayerViewType.Top then
  611. margin=lastMj:getContentSize().width-MJDefine.MJ_HAND_CARD_TOP_OFFSET_X
  612. -- print("width:"..lastMj:getContentSize().width)
  613. -- if math.abs(lastX-frontX)==margin then
  614. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  615. -- end
  616. end
  617. end
  618. end
  619. function NeiJiangMJHandCardView:onTouchMove(touch)
  620. NeiJiangMJHandCardView.super.onTouchMove(self,touch)
  621. if self.touchMJ and self.isTouchMove==true then
  622. local pos = self.touchMJ:getPosition()
  623. local index = nil
  624. for k,v in pairs(self.handCards) do
  625. local width = v:getContentSize().width
  626. local height = v:getContentSize().height
  627. local x = v:getPosition().x-width/2
  628. local y = v:getPosition().y-height/2
  629. local rect = cc.Rectangle:new(x,y,width*0.8,height*0.8)
  630. --如果可以出牌 最后的麻将不参与移动操作
  631. -- local isLastCard = self.isOutCard and k==#self.handCards
  632. if rect:contains(pos.x,pos.y) and v ~=self.touchMJ and not isLastCard then
  633. index = k
  634. break
  635. end
  636. end
  637. if index then
  638. local touchMJIndex = table.indexOf(self.handCards,self.touchMJ)
  639. local offsetIndex = touchMJIndex-index
  640. local num = math.abs(offsetIndex)-1
  641. --大于0则往左移动 3-1:3-2 2-1 小于0右 5-8:5-6 6-7 7-8
  642. local reduce = offsetIndex>0 and -1 or 1
  643. for i = touchMJIndex,index-reduce,reduce do
  644. self.handCards[i],self.handCards[i+reduce] = self.handCards[i+reduce],self.handCards[i]
  645. end
  646. local x,y = self:moveRestHandCards(self.touchMJ)
  647. self.touchMJ:recordPostion(cc.p(x,y))
  648. end
  649. end
  650. end
  651. function NeiJiangMJHandCardView:touchOutCard()
  652. if self.touchMJ and self.isTouchMove then
  653. if self.touchMJ:getPositionY()>MJDefine.MJ_TOUCH_OUT_CARD_Y and self.isOutCard then
  654. self:sendOutCard()
  655. else
  656. self.touchMJ:restorePostion()
  657. self.touchMJ:setStatus(MJDefine.MJStatus.Normal)
  658. app.room:dispatchEvent({name = MJDefine.MJEvent.SelectCard})
  659. app.room:dispatchEvent({name = MJDefine.MJEvent.ShowTing})
  660. if self.isOutCard then
  661. self:moveLastCard()
  662. end
  663. end
  664. end
  665. self.isTouchMove=false
  666. end
  667. return NeiJiangMJHandCardView