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.

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