Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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