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.

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