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.

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