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.

1416 lines
44 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 huamajiangPositionCompnent = MJFramework.MJImport("mj_huamajiang.luaScript.Views.Compnent.huamajiangPositionCompnent")
  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 huamajiangHandCardView = class("huamajiangHandCardView", MJHandCardView)
  9. function huamajiangHandCardView:ctor(viewId,desktopType)
  10. huamajiangHandCardView.super.ctor(self,viewId,desktopType)
  11. self.huCardLayout = {};
  12. end
  13. function huamajiangHandCardView:loadUI()
  14. local ui = nil
  15. if self.desktopType == MJDefine.DesktopType.TwoD or self.desktopType == MJDefine.DesktopType.TwoDGold then
  16. ui = loadUI("mj_huamajiang/res/ui_fangjian/mj_huamajiang_hand_card_2d.ui");
  17. else
  18. ui = loadUI("mj_huamajiang/res/ui_fangjian/mj_huamajiang_hand_card.ui");
  19. end
  20. autoAdapt(ui)
  21. self.ui = ui;
  22. self:addChild(ui)
  23. end
  24. function huamajiangHandCardView:initView()
  25. if self:isUseNew3D() then
  26. self:loadUI()
  27. self.ui.Items.Layout_Hand_Card:setVisible(false)
  28. self.ui.Items.Layout_OperateItem:setVisible(false)
  29. self.ui.Items.Button_Sure:setVisible(false)
  30. local MJConfig = MJDefine.MJConfig_2d
  31. if self.desktopType == MJDefine.DesktopType.ThreeD then
  32. MJConfig = MJDefine.MJConfig_3d
  33. end
  34. local MahjongHandCard = require(MJConfig.HANDCARD_VIEW)
  35. local MahjongOutCardView = require(MJConfig.OUTCARD_VIEW)
  36. local MahjongHuCardView = require(MJConfig.HU_CARD_VIEW)
  37. self._huCardView = MahjongHuCardView:new(self.viewId)
  38. self:addChild(self._huCardView)
  39. self._outCardView = MahjongOutCardView:new(self.viewId)
  40. self:addChild(self._outCardView)
  41. self._handcard = MahjongHandCard:new(self.viewId, self.desktopType)
  42. self:addChild(self._handcard)
  43. self.ui:setLocalZOrder(10)
  44. else
  45. self:loadUI()
  46. self.ui.Items.Layout_Operate:setVisible(false)
  47. self.ui.Items.Button_Sure:setVisible(false)
  48. self.ui.Items.Button_Sure:registerClick(function()
  49. if not self.touchMJ then
  50. showTooltip("请选择要出的麻将!")
  51. end
  52. self:sendOutCard()
  53. end)
  54. print("MJHandCardView:"..self.viewId)
  55. self.initX=self.ui.Items["ImageView_Start_"..self.viewId]:getPositionX()--MJDefine.MJStartPositions[self.viewId].x
  56. self.initY=self.ui.Items["ImageView_Start_"..self.viewId]:getPositionY()--MJDefine.MJStartPositions[self.viewId].y
  57. print("手牌起始位置self.initX = "..self.initX.."self.initY"..self.initY)
  58. if self.viewId == MJDefine.PlayerViewType.My then
  59. self.initX = self.initX + (CONST_WIDTH*g_radio_x-CONST_WIDTH)/2
  60. print("调整过后我的位置self.initX = "..self.initX.."self.initY"..self.initY)
  61. else
  62. self.initX = self.initX*g_radio_x
  63. self.initY = self.initY*g_radio_y
  64. print("调整过后其他人位置self.initX = "..self.initX.."self.initY"..self.initY)
  65. end
  66. self.outX=self.ui.Items["ImageView_Out_"..self.viewId]:getPositionX()
  67. self.outY=self.ui.Items["ImageView_Out_"..self.viewId]:getPositionY()
  68. print("出牌起始位置self.outX = "..self.outX.."self.outY"..self.outY)
  69. self.outX = self.outX*g_radio_x
  70. self.outY = self.outY*g_radio_y
  71. print("出牌调整后的起始位置self.outX = "..self.outX.."self.outY"..self.outY)
  72. --如果是2人房 调整出牌坐标
  73. if app.room.roomInfo.nMaxPlayCount==2 then
  74. if self.viewId == MJDefine.PlayerViewType.My then
  75. self.outX=self.outX-285
  76. elseif self.viewId == MJDefine.PlayerViewType.Top then
  77. self.outX=self.outX+200
  78. end
  79. end
  80. -- if self.viewId~=MJDefine.MyViewId then
  81. self.showCardPostion=self.ui.Items["ImageView_Show_"..self.viewId]:getPosition()
  82. -- end
  83. print("showCard起始位置self.showCardPostion x = "..self.showCardPostion.x.."y = "..self.showCardPostion.y)
  84. self.showCardPostion.x = self.showCardPostion.x*g_radio_x
  85. self.showCardPostion.y = self.showCardPostion.y*g_radio_y
  86. print("showCard调整后起始位置self.showCardPostion x = "..self.showCardPostion.x.."y = "..self.showCardPostion.y)
  87. self.ui.Items.Layout_Hand_Card:removeAllChildren()
  88. self:hideOperateItem()
  89. self.ui.Items.Layout_OperateItem:setLocalZOrder(1);
  90. self.ui.Items.Layout_Operate:setLocalZOrder(1);
  91. end
  92. end
  93. function huamajiangHandCardView:pushTing()
  94. if self:isUseNew3D() then
  95. self._handcard:pushTing()
  96. else
  97. local tings = app.room.roomInfo:getTingCards();
  98. if tings then
  99. for k,v in pairs(tings) do
  100. for _,mj in pairs(self.handCards) do
  101. if mj.value==v.outCard then
  102. mj:setTing(true)
  103. end
  104. end
  105. end
  106. end
  107. end
  108. end
  109. --摸牌
  110. function huamajiangHandCardView:onGetCard(operates,card,isSwap)
  111. if self:isUseNew3D() then
  112. self.touchMJ = nil
  113. self:createHandCards(card, true)
  114. if not operates or table.nums(operates) == 0 then
  115. self._handcard:setOutCardEnable(true)
  116. MJDefine.outCardSendGuo = false
  117. else
  118. self._handcard:setOutCardEnable(true)
  119. MJDefine.outCardSendGuo = true
  120. -- self:canOutCard(true)
  121. end
  122. if self.viewId==MJDefine.MyViewId then
  123. MJSound.PlayGetCard()
  124. end
  125. else
  126. self.touchMJ = nil;
  127. self:createHandCards(card)
  128. if self.viewId==MJDefine.PlayerViewType.Right and not self.isReplay then
  129. -- self:resetHandCards()
  130. end
  131. if isSwap and isSwap == true then return end --如果是换牌阶段,返回
  132. if self.desktopType == MJDefine.DesktopType.ThreeD then
  133. if self.viewId == MJDefine.PlayerViewType.Right then
  134. local mj = self.handCards[#self.handCards];
  135. local pos = mj:getPosition();
  136. mj:setPositionY(pos.y + 30);
  137. elseif self.viewId == MJDefine.PlayerViewType.Top then
  138. local mj = self.handCards[#self.handCards];
  139. local pos = mj:getPosition();
  140. mj:setPositionX(pos.x - 30);
  141. end
  142. end
  143. if not operates or table.nums(operates)==0 then
  144. self:setOutCardEnable(true)
  145. MJDefine.outCardSendGuo = false
  146. else
  147. self:setOutCardEnable(true)
  148. MJDefine.outCardSendGuo = true
  149. -- self:canOutCard(true)
  150. end
  151. if self.viewId==MJDefine.MyViewId then
  152. MJSound.PlayGetCard()
  153. end
  154. -- self:moveLastCard()
  155. end
  156. end
  157. --[[
  158. 显示胡的牌
  159. @param card 胡的牌
  160. ]]
  161. function huamajiangHandCardView:onGetHuCard (huCard, isClean,data)
  162. if self:isUseNew3D() then
  163. self._huCardView:getHuCards(huCard, isClean)
  164. else
  165. self.huCards = self.huCards or {};
  166. if isClean then
  167. self.huCards = {};
  168. end
  169. for k, v in ipairs(huCard) do
  170. table.insert(self.huCards, v);
  171. end
  172. local huCards = self.huCards;
  173. local viewId = self.viewId;
  174. if not (huCards and table.nums(huCards) > 0) then
  175. logD("胡牌数据为空!")
  176. return ;
  177. end
  178. -- huCards = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8};
  179. if not self.huCardLayout[viewId] then
  180. self.huCardLayout[viewId] = cc.Layout:create();
  181. self.ui.Items.Layout_Hu_Card:addChild(self.huCardLayout[viewId]);
  182. end
  183. local k = {}
  184. for idx,v in ipairs(self.huCardLayout[viewId]:getChildren()) do
  185. if v:getOpacity() < 255 then
  186. table.insert(k,idx)
  187. end
  188. end
  189. for _, idx in ipairs(k) do
  190. huCards[idx].opacity = 125
  191. end
  192. self.huCardLayout[viewId]:removeAllChildren();
  193. local huCardsLayout = self.huCardLayout[viewId];
  194. for k, v in ipairs(huCards) do
  195. local huCard = v.huCard;
  196. local huType = v.huType;
  197. local opacity = v.opacity;
  198. local vid = viewId;
  199. local scale = 0.75;
  200. if viewId == MJDefine.PlayerViewType.MyViewId then
  201. scale = 0.7;
  202. end
  203. local card = MJ:new(huCard, MJDefine.MJType.Operate, vid, self.desktopType);
  204. local w = card:getContentSize().width * scale;
  205. local h = card:getContentSize().height * scale;
  206. card:setScale(scale);
  207. if huCard ~= 0 and huType == MJDefine.MJHuType.Zimo then
  208. -- 自摸牌,设置颜色显示
  209. card:setColor(cc.c3b(237,236,134));
  210. end
  211. --换行数量
  212. local cNum = 15
  213. local col = k % cNum
  214. col = col == 0 and cNum or col
  215. if viewId == MJDefine.PlayerViewType.My then
  216. if self.desktopType == MJDefine.DesktopType.ThreeD then
  217. local x = (1 - k) * (w - 2);
  218. card:setPositionX(x);
  219. else
  220. local x = (1 - col) * (w - 2);
  221. local y = (math.ceil(k/cNum)-1) * 12
  222. card:setPosition(cc.p(x,y));
  223. end
  224. elseif viewId == MJDefine.PlayerViewType.Right then
  225. if self.desktopType == MJDefine.DesktopType.ThreeD then
  226. local y = (1 - k) * (h - 14);
  227. local x = (k - 1) * 5;
  228. card:setPosition(cc.p(x, y));
  229. else
  230. local y = (1 - col) * (h - 9);
  231. y = y + (math.ceil(k/cNum)-1) * 8
  232. card:setPositionY(y);
  233. end
  234. elseif viewId == MJDefine.PlayerViewType.Left then
  235. if self.desktopType == MJDefine.DesktopType.ThreeD then
  236. local y = (k - 1) * (h - 14);
  237. local x = (k - 1) * 5;
  238. card:setPosition(cc.p(x, y));
  239. else
  240. local y = (col - 1) * (h - 9);
  241. y = y + (math.ceil(k/cNum)-1) * 8
  242. card:setPositionY(y);
  243. end
  244. card:setLocalZOrder(#huCards - col);
  245. elseif viewId == MJDefine.PlayerViewType.Top then
  246. if self.desktopType == MJDefine.DesktopType.ThreeD then
  247. local x = (k - 1) * (w - 2);
  248. card:setPositionX(x);
  249. else
  250. local x = (col - 1) * (w - 1);
  251. local y = (math.ceil(k/cNum)-1) * 12
  252. card:setPosition(cc.p(x,y));
  253. end
  254. end
  255. local count = app.room.roomInfo:getLuoBoCardCount(v);
  256. card:setLuoBoVisible(count > 0, count);
  257. if data and data.yipaoduoxiang and data.huPaiIndex and data.huPaiIndex > 1 or opacity then
  258. card:setOpacity(125)
  259. end
  260. huCardsLayout:addChild(card);
  261. end
  262. --local visibleSize = cc.Director:getInstance():getVisibleSize();
  263. local visibleSize = cc.size(1280,720)
  264. if viewId == MJDefine.PlayerViewType.My then
  265. if self.desktopType == MJDefine.DesktopType.ThreeD then
  266. huCardsLayout:setPosition(cc.p((visibleSize.width - 300) * g_radio_x, 155 * g_radio_y));
  267. else
  268. huCardsLayout:setPosition(cc.p((visibleSize.width - 300) * g_radio_x, 160 * g_radio_y));
  269. end
  270. elseif viewId == MJDefine.PlayerViewType.Right then
  271. if self.desktopType == MJDefine.DesktopType.ThreeD then
  272. huCardsLayout:setPosition(cc.p((visibleSize.width - 250) * g_radio_x, (visibleSize.height - 190) * g_radio_y));
  273. else
  274. huCardsLayout:setPosition(cc.p((visibleSize.width - 205) * g_radio_x, (visibleSize.height - 150) * g_radio_y));
  275. end
  276. elseif viewId == MJDefine.PlayerViewType.Left then
  277. if self.desktopType == MJDefine.DesktopType.ThreeD then
  278. huCardsLayout:setPosition(cc.p(190 * g_radio_x, 250 * g_radio_y));
  279. else
  280. huCardsLayout:setPosition(cc.p(205 * g_radio_x, 250 * g_radio_y));
  281. end
  282. elseif viewId == MJDefine.PlayerViewType.Top then
  283. huCardsLayout:setPosition(cc.p(435 * g_radio_x, (visibleSize.height - 100) * g_radio_y));
  284. end
  285. end
  286. end
  287. function huamajiangHandCardView:setOutCardEnable(b)
  288. if self:isUseNew3D() then
  289. self._handcard:setOutCardEnable(b)
  290. else
  291. self.isOutCard=b
  292. if b then
  293. self:moveLastCard()
  294. end
  295. if self.viewId==MJDefine.MyViewId and self.ui.Items.Button_Sure then
  296. -- self.ui.Items.Button_Sure:setVisible(b)
  297. end
  298. end
  299. end
  300. --不管是否自动对齐,红中始终保持放在最左边
  301. function huamajiangHandCardView:sortRedMahjong()
  302. local redNum = 0
  303. for k,mj in pairs(self.handCards) do
  304. if mj.value == 65 then
  305. redNum = redNum + 1
  306. table.remove(self.handCards,k)
  307. table.insert(self.handCards,redNum,mj)
  308. end
  309. end
  310. end
  311. function huamajiangHandCardView:removeHandCard(arg)
  312. if self:isUseNew3D() then
  313. self._handcard:removeHandCard(arg)
  314. else
  315. if type(arg)=="number" then
  316. arg={
  317. {card=arg}
  318. }
  319. end
  320. if self.viewId==MJDefine.MyViewId or self.isReplay then
  321. for k,v in pairs(arg) do
  322. local mj=self:getMjByValue(v.card)
  323. if mj then
  324. table.removeItem(self.handCards,mj)
  325. mj:removeFromParent()
  326. end
  327. end
  328. else
  329. -- local num=#self.handCards-#arg
  330. -- print(num)
  331. for i=#self.handCards,#self.handCards-#arg+1,-1 do
  332. if self.handCards[i] then
  333. self.handCards[i]:removeFromParent()
  334. table.remove(self.handCards,i)
  335. end
  336. end
  337. end
  338. end
  339. end
  340. function huamajiangHandCardView:removeHandCardByNum(value,num)
  341. if self:isUseNew3D() then
  342. self._handcard:removeHandCardByNum(value, num)
  343. else
  344. if self.viewId==MJDefine.MyViewId or self.isReplay then
  345. for i=1,num do
  346. local mj = self:getMjByValue(value)
  347. if mj then
  348. table.removeItem(self.handCards,mj)
  349. mj:removeFromParent()
  350. end
  351. end
  352. else
  353. -- local num=#self.handCards-#arg
  354. -- print(num)
  355. for i=#self.handCards,#self.handCards-num+1,-1 do
  356. self.handCards[i]:removeFromParent()
  357. table.remove(self.handCards,i)
  358. end
  359. end
  360. end
  361. end
  362. -- function huamajiangHandCardView:touchOutCard()
  363. -- huamajiangHandCardView.super.touchOutCard(self)
  364. -- app.room:dispatchEvent({name = MJDefine.MJEvent.ShowTing})
  365. -- end
  366. function huamajiangHandCardView:getGroup(card,opTypes)
  367. if self:isUseNew3D() then
  368. return self._handcard:getGroup(card, opTypes)
  369. else
  370. for kk, vv in pairs(opTypes) do
  371. opType = vv;
  372. for k,v in pairs(self.handGroupCards) do
  373. if v.opType==opType then
  374. if v.opType == MJDefine.MJOperateType.OPREATE_ANGANG then
  375. if (v.mjs[1].value==card or v.mjs[2].value == card or v.mjs[3].value == card or v.mjs[4].value == card) then
  376. return v;
  377. end
  378. else
  379. if (v.mjs[1].value==card or v.mjs[2].value == card or v.mjs[3].value == card) then
  380. return v;
  381. end
  382. end
  383. end
  384. end
  385. end
  386. end
  387. end
  388. function huamajiangHandCardView:createGroupCards(arg,fromViewId)
  389. if self:isUseNew3D() then
  390. self._handcard:createGroupCards(arg, fromViewId)
  391. else
  392. local x=self.initX
  393. local y=self.initY
  394. local MJGroupStartOffSet = MJDefine.MJGroupStartOffSet[self.desktopType]
  395. for k,v in pairs(arg) do
  396. if not fromViewId then
  397. fromViewId = v.fromViewId
  398. end
  399. local group = nil;
  400. if v.opType == MJDefine.MJOperateType.OPREATE_ANGANG then
  401. group = self:getGroup(v.values[1], {MJDefine.MJOperateType.OPREATE_ANGANG});
  402. if group then
  403. local cards = group:getCards();
  404. if cards and cards[4] then
  405. group:refreshCardNum(cards[4], v.cardNum);
  406. return ;
  407. end
  408. end
  409. end
  410. group=MJGroupFactory.CreateGroup(v.values,v.showType,self.viewId,self.desktopType, v.cardNum)
  411. if group then
  412. --设置碰杠来源
  413. --group:setDirection(self.viewId,v.fromViewId1)
  414. if group.setAnColor and (v.opType == MJDefine.MJOperateType.OPREATE_PENG
  415. or v.opType == MJDefine.MJOperateType.OPREATE_ZHIGANG
  416. or v.opType == MJDefine.MJOperateType.OPREATE_BAGANG) then
  417. --牌的阴影显示刚好和视图id相反
  418. if v.fromViewId == 1 then v.fromViewId = 3
  419. elseif v.fromViewId==3 then v.fromViewId = 1 end
  420. -- 杠牌对家,换成第四张
  421. if v.opType ~= MJDefine.MJOperateType.OPREATE_PENG then
  422. if v.fromViewId==2 then v.fromViewId = 4 end
  423. end
  424. group:setAnColor(v.opCard, v.fromViewId)
  425. end
  426. local cards = group:getCards();
  427. if cards and cards[4] then
  428. group:refreshCardNum(cards[4], v.cardNum);
  429. end
  430. if v.opType == MJDefine.MJOperateType.OPREATE_BAGANG then
  431. if cards and cards[4] then
  432. cards[4]:setOperateBlackImage(0,self.viewId)
  433. end
  434. end
  435. group.fromViewId = v.fromViewId ---保留触发操作的玩家位置,补杠需要
  436. group.opType=v.opType
  437. self.ui.Items.Layout_Hand_Card:addChild(group,-4+#self.handGroupCards)
  438. group:setPosition(cc.p(x+MJGroupStartOffSet[self.viewId].x,y+MJGroupStartOffSet[self.viewId].y))
  439. x,y=huamajiangPositionCompnent.getGroupPosition(self.viewId,x,y,group,self.desktopType)
  440. table.insert(self.handGroupCards,group)
  441. end
  442. end
  443. --更新起始位置
  444. self.initX=x
  445. self.initY=y
  446. end
  447. end
  448. --添加杠操作按钮
  449. function huamajiangHandCardView:addOperateBtn(operates)
  450. local x=self.ui.Items.Layout_Operate:getContentSize().width
  451. local y=self.ui.Items.Layout_Operate:getContentSize().height/2
  452. local btnOperate=cc.Button:createNode()
  453. btnOperate:setDefaults()
  454. btnOperate:setText("")
  455. btnOperate:loadTextureNormal(MJDefine.MJOperateImage[operates[1].opType])
  456. btnOperate:ignoreAnchorPointForPosition(false)
  457. self.btnPosX = self.btnPosX-MJDefine.MJ_OPERATE_PADDING
  458. btnOperate:setPosition(cc.p(self.btnPosX, y))
  459. self.ui.Items.Layout_Operate:addChild(btnOperate,2)
  460. -- if operates and table.nums(operates) <=1 then
  461. -- self.btnPosX = self.btnPosX-50
  462. -- btnOperate:setPosition(cc.p(self.btnPosX, y))
  463. -- local gangBg = cc.Layout:create()
  464. -- gangBg:setBackGroundImageScale9Enabled(true)
  465. -- gangBg:setBackGroundImage("huamajiang_room_gangBg.png",cc.TextureResType.plistType)
  466. -- gangBg:setPosition(cc.p(btnOperate:getPositionX()+30, y-40))
  467. -- gangBg:setSize(cc.size(80,80))
  468. -- self.ui.Items.Layout_Operate:addChild(gangBg)
  469. -- gangBg:registerClick(function(sender)
  470. -- self:sendOp({opType = operates[1].opType,opCard = operates[1].opCard,redNum = operates[1].redNum})
  471. -- end)
  472. -- local mj=MJ:new(operates[1].opCard,MJDefine.MJType.Stand,MJDefine.MyViewId,self.desktopType)
  473. -- mj:setScale(0.5)
  474. -- mj:setAnchorPoint(cc.p(0.5,0.5))
  475. -- gangBg:addChild(mj)
  476. -- mj:setPosition(cc.p(gangBg:getContentSize().width/2+10, gangBg:getContentSize().height/2))
  477. -- end
  478. btnOperate:registerClick(function()
  479. -- playBtnEffect()
  480. if operates and table.nums(operates) <=1 then
  481. self:sendOp({opType = operates[1].opType,opCard = operates[1].opCard,redNum = operates[1].redNum})
  482. else
  483. self:showGangItem(operates)
  484. end
  485. end)
  486. end
  487. function huamajiangHandCardView:showGangItem(operates)
  488. self.ui.Items.Layout_OperateItem:setVisible(true)
  489. self.ui.Items.Layout_Operate_Content:removeAllChildren()
  490. local col = 0
  491. local count = 6
  492. local temp = self.ui.Items.Layout_MJ_Item
  493. local _size = temp:getContentSize()
  494. self.ui.Items.Layout_OperateItem:setSize(cc.size(#operates*133,_size.height + 5))
  495. self.ui.Items.Layout_OperateItem:setPosition(cc.p(940,160 + 114))
  496. local _size1 = self.ui.Items.Layout_OperateItem:getContentSize()
  497. self.ui.Items.Layout_Operate_Content:setSize(cc.size(_size1.width, _size1.height))
  498. self.ui.Items.Layout_Operate_Content:setPosition(cc.p(_size1.width,0))
  499. local history = {};
  500. for k,v in pairs(operates) do
  501. if not history[v.opCard] then
  502. history[v.opCard] = true;
  503. local item = self.ui.Items.Layout_MJ_Item:getCopied()
  504. local mj=MJ:new(v.opCard,MJDefine.MJType.Stand,self.viewId,self.desktopType)
  505. item:addChild(mj)
  506. local size = item:getContentSize()
  507. mj:setPosition(cc.p(size.width/2,size.height/2))
  508. self.ui.Items.Layout_Operate_Content:addChild(item)
  509. mj:registerClick(function()
  510. self:sendOp({opType = v.opType,opCard = v.opCard,redNum = v.redNum})
  511. end)
  512. -- local col = k
  513. local row = math.ceil(k/count)
  514. -- logD(k.." :".. (k%count))
  515. -- local isChangeRow = k%count==0
  516. local x = self.ui.Items.Layout_Operate_Content:getContentSize().width-col*133
  517. local y = (row-1)*size.height
  518. item:setPositionX(x)
  519. item:setPositionY(y)
  520. col = col+1
  521. if k%count==0 then
  522. col = 0
  523. end
  524. end
  525. end
  526. -- local count = table.nums(operates)
  527. -- local padding=10
  528. -- local width=self.ui.Items.Layout_OperateItem:getContentSize().width
  529. -- local height=self.ui.Items.Layout_OperateItem:getContentSize().height
  530. -- local layoutItem = cc.Layout:create()
  531. -- layoutItem:setBackGroundImageScale9Enabled(true)
  532. -- --layoutItem:setBackGroundImage("mj/res/ui/zy_fangjian/mj_gang_bg.png")
  533. -- layoutItem:setAnchorPoint(cc.p(0.5,1))
  534. -- self.ui.Items.Layout_OperateItem:addChild(layoutItem)
  535. -- if count <= 3 then
  536. -- layoutItem:setSize(cc.size(230*count+padding*count,height+padding))
  537. -- else
  538. -- layoutItem:setSize(cc.size(230*3+padding*3,height*(math.floor(count/3)+1)+(math.floor(count/3)+1)*padding))
  539. -- --self.ui.Items.Layout_OperateItem:setSize(cc.size(230*3+padding*3,height*(math.floor(count/3)+1)+(math.floor(count/3)+1)*padding))
  540. -- end
  541. -- layoutItem:setPosition(cc.p(width/2,height))
  542. -- local layoutHeight = layoutItem:getContentSize().height
  543. -- local y = 0
  544. -- for k,v in ipairs(operates) do
  545. -- local layout=cc.Layout:create()
  546. -- layout:setBackGroundImageScale9Enabled(true)
  547. -- layout:setBackGroundImage("mj/res/ui/zy_fangjian/mj_gang_bg.png")
  548. -- self.ui.Items.Layout_OperateItem:addChild(layout)
  549. -- layout:setSize(cc.size(230,height))
  550. -- layout:setAnchorPoint(cc.p(0,1))
  551. -- if k%3 == 1 then
  552. -- y = layoutHeight -15- height*(math.floor(k/3)+1)-10*(math.floor(k/3)+1)
  553. -- end
  554. -- if count < 3 then
  555. -- layout:setPosition(cc.p(-145+padding*(k-1)+230*(k-1),layoutHeight-15))
  556. -- elseif count == 3 then
  557. -- layout:setPosition(cc.p(-260+padding*(k-1)+230*(k-1),layoutHeight-15))
  558. -- else
  559. -- layout:setPosition(cc.p(-260+padding*(k%3-1)+230*(k%3-1),y))
  560. -- if k%3 == 0 then
  561. -- layout:setPosition(cc.p(-260+padding*2+230*2,y))
  562. -- end
  563. -- end
  564. -- for i=1,4 do
  565. -- local mj
  566. -- if v.redNum > 0 and i<=v.redNum then
  567. -- mj=MJ:new(65,MJDefine.MJType.Stand,self.viewId,self.desktopType)
  568. -- else
  569. -- mj=MJ:new(v.opCard,MJDefine.MJType.Stand,self.viewId,self.desktopType)
  570. -- end
  571. -- mj:setScale(0.6)
  572. -- layout:addChild(mj)
  573. -- local mjWidth = mj:getContentSize().width*0.6
  574. -- mj:setAnchorPoint(cc.p(0.5,0.5))
  575. -- mj:setPosition(32+3*(i-1)+mjWidth*(i-1),layout:getContentSize().height/2)
  576. -- end
  577. -- layout:registerClick(function ( sender )
  578. -- self:sendOp({opType = v.opType,opCard = v.opCard,redNum = v.redNum})
  579. -- end)
  580. -- end
  581. end
  582. function huamajiangHandCardView:hideOperateItem()
  583. if self.ui.Items.Layout_OperateItem then
  584. self.ui.Items.Layout_Operate_Content:removeAllChildren()
  585. self.ui.Items.Layout_OperateItem:setVisible(false)
  586. end
  587. end
  588. --显示操作
  589. function huamajiangHandCardView:showOperate(operates,card,isMyGetCard)
  590. if not operates or table.nums(operates)==0 then return end
  591. print(table.tostring(operates))
  592. local card = card or 0
  593. self:hideOperateItem()
  594. self:hideOperate()
  595. if operates and table.nums(operates) > 0 then
  596. self.ui.Items.Button_Sure:setVisible(false)
  597. end
  598. local newOperates = {}
  599. --有操作默认添加过的操作
  600. --非忽略过默认添加
  601. if not app.room.roomInfo.passGuo then
  602. local operate=MJMessage.OperateRequest:new()
  603. operate.opType=MJDefine.MJOperateType.OPREATE_CANCEL
  604. operate.opCard=card
  605. operate.redNum = 0
  606. table.insert(newOperates,1,operate)
  607. end
  608. --table.insert(operates.Datas,1,operate)
  609. self.ui.Items.Layout_Operate:removeAllChildren()
  610. self.ui.Items.Layout_Operate:setVisible(true)
  611. local x=self.ui.Items.Layout_Operate:getContentSize().width
  612. local y=self.ui.Items.Layout_Operate:getContentSize().height/2
  613. self.btnPosX = x
  614. local gangCards ={} --所有杠組合
  615. local onceGang = false
  616. local isCanHu = false
  617. --operates.Datas包含所有碰杠组合,服务器不管是否贴鬼碰/杠,需客户端自己分离贴鬼碰和无贴鬼碰
  618. for k,v in pairs(operates.Datas) do
  619. if v.opType ~= MJDefine.MJOperateType.OPREATE_CANCEL then
  620. if MJDefine.isTieGui == true then --选择贴鬼碰/杠
  621. if (v.opType == MJDefine.MJOperateType.OPREATE_DIANPAOHU) or (v.opType == MJDefine.MJOperateType.OPREATE_ZIMOHU) or onceGang == false then
  622. if (v.opType == MJDefine.MJOperateType.OPREATE_DIANPAOHU) or (v.opType == MJDefine.MJOperateType.OPREATE_ZIMOHU) then
  623. isCanHu = true
  624. end
  625. table.insert(newOperates,v)
  626. end
  627. if (v.opType==MJDefine.MJOperateType.OPREATE_ZHIGANG) or (v.opType==MJDefine.MJOperateType.OPREATE_BAGANG) or (v.opType==MJDefine.MJOperateType.OPREATE_ANGANG) then
  628. onceGang = true
  629. table.insert(gangCards,v)
  630. end
  631. else
  632. if (v.opType == MJDefine.MJOperateType.OPREATE_DIANPAOHU) or (v.opType == MJDefine.MJOperateType.OPREATE_ZIMOHU) then
  633. isCanHu = true
  634. table.insert(newOperates,v)
  635. end
  636. --不勾选贴鬼碰/杠时,过滤掉带红中的碰杠
  637. if v.redNum == 0 and v.opType ~= MJDefine.MJOperateType.OPREATE_DIANPAOHU and (v.opType ~= MJDefine.MJOperateType.OPREATE_ZIMOHU) then
  638. if onceGang == false then
  639. table.insert(newOperates,v)
  640. end
  641. if (v.opType==MJDefine.MJOperateType.OPREATE_ZHIGANG) or (v.opType==MJDefine.MJOperateType.OPREATE_BAGANG) or (v.opType==MJDefine.MJOperateType.OPREATE_ANGANG) then
  642. onceGang = true
  643. table.insert(gangCards,v)
  644. end
  645. end
  646. end
  647. end
  648. end
  649. if table.nums(newOperates) >= 2 then
  650. local function sortOperate(opA,opB)
  651. if opA.opType<opB.opType then
  652. return true
  653. elseif opA.opType==opB.opType then
  654. if opA.opCard<opB.opCard then
  655. return true
  656. end
  657. end
  658. return false
  659. end
  660. table.sort(newOperates,sortOperate)
  661. --没有碰杠操作,此条件只有不勾选贴鬼碰/杠才可能会执行
  662. else
  663. --如果不是自己摸牌的情况下
  664. if not isMyGetCard then --app.room.roomInfo.outCardUserId~=app.room:getMyUserId() then --
  665. self:sendOp({opType = MJDefine.MJOperateType.OPREATE_CANCEL,opCard=card,redNum=0})
  666. MJDefine.isSendGuo = true
  667. return;
  668. end
  669. --是自己摸牌
  670. self:hideOperate()
  671. self:setOutCardEnable(true)
  672. logD("huamajiangHandCardView:showOperate setOutCardEnable true")
  673. end
  674. local pengCard = {}
  675. for k,v in pairs(newOperates) do
  676. if MJDefine.MJOperateImage[v.opType] then
  677. if(v.opType==MJDefine.MJOperateType.OPREATE_ZHIGANG) or (v.opType==MJDefine.MJOperateType.OPREATE_BAGANG) or (v.opType==MJDefine.MJOperateType.OPREATE_ANGANG) then
  678. self:addOperateBtn(gangCards)
  679. elseif v.opType==MJDefine.MJOperateType.OPREATE_PENG then
  680. table.insert(pengCard,v)
  681. self:addOperateBtn(pengCard)
  682. 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
  683. local btnOperate=cc.Button:createNode()
  684. btnOperate:setDefaults()
  685. btnOperate:setText("")
  686. btnOperate:loadTextureNormal(MJDefine.MJOperateImage[v.opType])
  687. btnOperate:ignoreAnchorPointForPosition(false)
  688. self.btnPosX = self.btnPosX-MJDefine.MJ_OPERATE_PADDING
  689. btnOperate:setPosition(cc.p(self.btnPosX, y))
  690. self.ui.Items.Layout_Operate:addChild(btnOperate)
  691. if isCanHu and isCanHu == true and v.opType == MJDefine.MJOperateType.OPREATE_CANCEL then
  692. local function qiHuConfirm()
  693. local content = string.format("确定放弃胡牌吗?")
  694. local okCallBack = function ()
  695. self:sendOp(v)
  696. end
  697. local cancelCallBack = function ()
  698. end
  699. showConfirmDialog(content,okCallBack,cancelCallBack)
  700. end
  701. btnOperate:registerClick(qiHuConfirm)
  702. else
  703. btnOperate:registerClick(function()
  704. -- playBtnEffect()
  705. self:sendOp(v)
  706. end)
  707. end
  708. end
  709. end
  710. end
  711. end
  712. function huamajiangHandCardView:sendOp(value)
  713. local request = MJMessage.OperateRequest:new()
  714. request.opType = value.opType
  715. request.opCard = value.opCard
  716. request.redNum = value.redNum
  717. logE("huamajiangHandCardView:sendOp"..table.tostring(request))
  718. self.ui:sendMsg(app.room, MJDefine.MJEvent.Operate, request, function(status, response)
  719. logE("MJHandCardView showOperate() response = ", table.tostring(response))
  720. end)
  721. self:hideOperateItem()
  722. self:hideOperate()
  723. if value.opType == MJDefine.MJOperateType.OPREATE_CANCEL then
  724. app.room:dispatchEvent({name = MJDefine.MJEvent.SendGuo})
  725. end
  726. --self:setTing(false)
  727. --app.room:resetTings()
  728. end
  729. --发送出牌消息
  730. function huamajiangHandCardView:sendOutCard()
  731. if not self.isOutCard or not self.touchMJ then return end
  732. self:hideOperate()
  733. huamajiangHandCardView.super.sendOutCard(self)
  734. app.room.roomInfo:setLastOutViewId(MJDefine.MyViewId)
  735. end
  736. function huamajiangHandCardView:resetHandCards(insertMj,isAnimation)
  737. if self:isUseNew3D() then
  738. self._handcard:resetHandCards(insertMj,isAnimation)
  739. else
  740. -- local initX=self.initX
  741. -- local initY=self.initY
  742. local initX,initY=self:getInitHandCardPostion()
  743. local x=initX
  744. local y=initY
  745. local insertX=0
  746. local insertY=0
  747. if MJDefine.isAutomatic == true then
  748. self:sortHandCards()
  749. else
  750. -- self:sortRedMahjong()
  751. end
  752. for k,mj in pairs(self.handCards) do
  753. if mj.status ~= MJDefine.MJStatus.Disable then
  754. mj:setStatus(MJDefine.MJStatus.Normal)
  755. mj:stopAllActions()
  756. end
  757. logD("k:"..k.." value:"..mj.value)
  758. if insertMj==mj then --记录插入麻将的坐标 动画需要
  759. insertX=x
  760. insertY=y
  761. else
  762. if isAnimation then
  763. mj:runAction(cc.MoveTo:create(0.1,cc.p(x,y)))
  764. else
  765. mj:setPosition(x,y)
  766. end
  767. end
  768. x,y=huamajiangPositionCompnent.getHandCardPosition(self.viewId,initX,initY,mj,k,self.desktopType)
  769. end
  770. return insertX,insertY
  771. end
  772. end
  773. function huamajiangHandCardView:moveRestHandCards(insertMj,isAnimation)
  774. local initX,initY=self:getInitHandCardPostion()
  775. local x=initX
  776. local y=initY
  777. local insertX=0
  778. local insertY=0
  779. for k,mj in pairs(self.handCards) do
  780. --如果可以出牌 最后的麻将不重置位置
  781. -- local isLastCard = self.isOutCard and k==#self.handCards
  782. -- if isLastCard then
  783. -- break
  784. -- end
  785. if mj.status ~= MJDefine.MJStatus.Disable then
  786. mj:setStatus(MJDefine.MJStatus.Normal)
  787. mj:stopAllActions()
  788. end
  789. logD("k:"..k.." value:"..mj.value)
  790. if insertMj==mj then --记录插入麻将的坐标 动画需要
  791. insertX=x
  792. insertY=y
  793. else
  794. if isAnimation then
  795. mj:runAction(cc.MoveTo:create(0.1,cc.p(x,y)))
  796. else
  797. mj:setPosition(x,y)
  798. end
  799. end
  800. x,y=huamajiangPositionCompnent.getHandCardPosition(self.viewId,initX,initY,mj,k,self.desktopType)
  801. end
  802. return insertX,insertY
  803. end
  804. function huamajiangHandCardView:onTouchEnd( ... )
  805. huamajiangHandCardView.super.onTouchEnd(self, ...);
  806. MJSound.PlayClickCard();
  807. end
  808. --[[function huamajiangHandCardView:onTouchEnd(touch)
  809. if MJDefine.isAutomatic == false then
  810. local touchPos=self:convertToNodeSpace(touch:getLocation())
  811. local newHandCards = {}
  812. for k,mj in pairs(self.handCards) do
  813. table.insert(newHandCards,k,mj)
  814. local width = mj:getContentSize().width
  815. local height = mj:getContentSize().height
  816. local x = mj:getPosition().x-width/2
  817. local y = mj:getPosition().y-height/2
  818. local rect = cc.Rectangle:new(x,y,width,height)
  819. if rect:contains(touchPos.x,touchPos.y) then
  820. if self.handCards[k] then
  821. table.remove(newHandCards,k)
  822. end
  823. if self.touchMJ and not tolua.isnull(self.touchMJ) then
  824. table.insert(newHandCards,k,self.touchMJ)
  825. end
  826. end
  827. end
  828. self.handCards = nil
  829. self.handCards = DeepCopy(newHandCards)
  830. end
  831. self:touchOutCard()
  832. end
  833. --]]
  834. function huamajiangHandCardView:buGang(card, cardNum)
  835. if self:isUseNew3D() then
  836. self._handcard:buGang(card, cardNum)
  837. else
  838. local oldGroup = self:getGroup(card, {MJDefine.MJOperateType.OPREATE_BAGANG, MJDefine.MJOperateType.OPREATE_ZHIGANG, MJDefine.MJOperateType.OPREATE_ANGANG});
  839. if oldGroup then
  840. -- 如果没有碰牌,则查看是否已经有补杠
  841. local cards = oldGroup:getCards();
  842. if cards and cards[4] then
  843. oldGroup:refreshCardNum(cards[4], cardNum);
  844. cards[4]:setOutBlackImage(0, self.viewId);
  845. end
  846. return ;
  847. else
  848. oldGroup=self:getGroup(card,{MJDefine.MJOperateType.OPREATE_PENG})
  849. end
  850. if not oldGroup then
  851. return ;
  852. end
  853. local fromViewId = oldGroup.fromViewId
  854. -- 杠牌对家,换成第四张
  855. if app.room:getMaxPlayerCount()==2 and fromViewId~=0 then
  856. fromViewId = 4
  857. end
  858. if app.room:getMaxPlayerCount()==4 and fromViewId == 2 then
  859. fromViewId = 4
  860. end
  861. local newGroup=MJGroupFactory.CreateGroup({card,card,card,card},MJDefine.MJGroupType.Gang,self.viewId,self.desktopType,cardNum)
  862. if newGroup.setAnColor then
  863. newGroup:setAnColor(card, fromViewId)
  864. end
  865. newGroup.fromViewId = fromViewId
  866. newGroup.opType=MJDefine.MJOperateType.OPREATE_BAGANG
  867. newGroup:setPosition(oldGroup:getPosition())
  868. local cards = newGroup:getCards();
  869. if cards and cards[4] then
  870. cards[4]:setOutBlackImage(0,self.viewId)
  871. newGroup:refreshCardNum(cards[4], cardNum);
  872. end
  873. self.ui.Items.Layout_Hand_Card:addChild(newGroup)
  874. local index=table.indexOf(self.handGroupCards,oldGroup)
  875. table.remove(self.handGroupCards,index)
  876. table.insert(self.handGroupCards,index,newGroup)
  877. oldGroup:removeFromParent()
  878. end
  879. end
  880. function huamajiangHandCardView:retoreBuGang(card,cardNum)
  881. if self:isUseNew3D() then
  882. self._handcard:restoreBuGang(card, cardNum)
  883. else
  884. local oldGroup=self:getGroup(card, {MJDefine.MJOperateType.OPREATE_BAGANG, MJDefine.MJOperateType.OPREATE_ANGANG, MJDefine.MJOperateType.OPREATE_ZHIGANG})
  885. if not oldGroup then return end
  886. local oldCardNum = oldGroup:getCardNum();
  887. if oldCardNum > 4 then
  888. oldCardNum = oldCardNum - 1;
  889. local cards = oldGroup:getCards();
  890. if cards and cards[4] then
  891. oldGroup:refreshCardNum(cards[4], oldCardNum);
  892. return ;
  893. end
  894. end
  895. local fromViewId = oldGroup.fromViewId
  896. -- 杠牌对家,换成第四张
  897. if app.room:getMaxPlayerCount()==2 and fromViewId~=0 then
  898. fromViewId = 4
  899. end
  900. if app.room:getMaxPlayerCount()==4 and fromViewId == 2 then
  901. fromViewId = 4
  902. end
  903. --local redNum = newRedNum + oldRedNum
  904. local newGroup=MJGroupFactory.CreateGroup({card,card,card},MJDefine.MJGroupType.Peng,self.viewId,self.desktopType,cardNum)
  905. if newGroup.setAnColor then
  906. newGroup:setAnColor(card, fromViewId)
  907. end
  908. newGroup.fromViewId = fromViewId
  909. newGroup.opType=MJDefine.MJOperateType.OPREATE_PENG
  910. newGroup:setPosition(oldGroup:getPosition())
  911. self.ui.Items.Layout_Hand_Card:addChild(newGroup)
  912. local index=table.indexOf(self.handGroupCards,oldGroup)
  913. table.remove(self.handGroupCards,index)
  914. table.insert(self.handGroupCards,index,newGroup)
  915. oldGroup:removeFromParent()
  916. end
  917. end
  918. function huamajiangHandCardView:lockHandCard(isForbidTouch)
  919. if self:isUseNew3D() then
  920. self._handcard:lockHandCard(isForbidTouch)
  921. else
  922. self._isLock = true;
  923. for k,v in pairs(self.handCards) do
  924. v:setStatus(MJDefine.MJStatus.Disable)
  925. end
  926. if isForbidTouch then
  927. self:getEventDispatcher():removeEventListenersForTarget(self)
  928. end
  929. end
  930. end
  931. function huamajiangHandCardView:moveLastOpenCard()
  932. local count=#self.handCards
  933. if count>=2 then
  934. local frontMj=self.handCards[count-1]
  935. local lastMj=self.handCards[count]
  936. local margin=0
  937. local frontX=frontMj:getPositionX()
  938. local frontY=frontMj:getPositionY()
  939. local lastX=lastMj:getPositionX()
  940. local lastY=lastMj:getPositionY()
  941. local MJLastHandCardOffset=MJDefine.MJLastHandCardOffset[self.desktopType]
  942. local MJ_HAND_CARD_LEFT_PADDING_HEIGHT = MJDefine.MJ_HAND_CARD_LEFT_PADDING_HEIGHT[self.desktopType]
  943. --根据视图ID 计算坐标
  944. if self.viewId==MJDefine.PlayerViewType.My then
  945. margin=lastMj:getContentSize().width
  946. -- if math.abs(lastX-frontX)==margin then
  947. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  948. -- end
  949. elseif self.viewId==MJDefine.PlayerViewType.Left then
  950. margin=MJ_HAND_CARD_LEFT_PADDING_HEIGHT
  951. -- if math.abs(lastY-frontY)==margin then
  952. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  953. lastMj:setPositionY(lastY+MJLastHandCardOffset[self.viewId].y)
  954. -- end
  955. elseif self.viewId==MJDefine.PlayerViewType.Right then
  956. margin=MJ_HAND_CARD_LEFT_PADDING_HEIGHT
  957. -- if math.abs(lastY-frontY)==margin then
  958. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  959. lastMj:setPositionY(lastY+MJLastHandCardOffset[self.viewId].y)
  960. -- end
  961. elseif self.viewId==MJDefine.PlayerViewType.Top then
  962. margin=lastMj:getContentSize().width-MJDefine.MJ_HAND_CARD_TOP_OFFSET_X
  963. -- print("width:"..lastMj:getContentSize().width)
  964. -- if math.abs(lastX-frontX)==margin then
  965. lastMj:setPositionX(lastX+MJLastHandCardOffset[self.viewId].x)
  966. -- end
  967. end
  968. end
  969. end
  970. function huamajiangHandCardView:onTouchBegan(touch)
  971. logD("huamajiangHandCardView:onTouchBegan")
  972. local lastMj=self.touchMJ
  973. self.touchMJ=nil
  974. self.isTouchMove=false
  975. -- if self.touchMJ then
  976. -- self.touchMJ:setPositionY(self.initY)
  977. -- self.touchMJ:setStatus(MJDefine.MJStatus.Normal)
  978. -- self.touchMJ=nil
  979. -- end
  980. local index = -1;
  981. local touchPos=self:convertToNodeSpace(touch:getLocation())
  982. for k,v in pairs(self.handCards) do
  983. local width = v:getContentSize().width
  984. local height = v:getContentSize().height
  985. local x = v:getPosition().x-width/2
  986. local y = v:getPosition().y-height/2
  987. local rect = cc.Rectangle:new(x,y,width,height)
  988. if rect:contains(touchPos.x,touchPos.y) then
  989. if #self.handCards + #self.handGroupCards * 3 ~= 14 then
  990. -- 非抓牌状态
  991. self.touchMJ = nil;
  992. break
  993. else
  994. -- 抓牌状态
  995. if self._isLock and k ~= #self.handCards then
  996. self.touchMJ = nil;
  997. break
  998. end
  999. end
  1000. self.touchMJ=v
  1001. logD("huamajiangHandCardView:onTouchBegan touchMJ", self.touchMJ.value)
  1002. if self.touchMJ.status==MJDefine.MJStatus.Select then --双击出牌
  1003. logD("huamajiangHandCardView:onTouchBegan outCard")
  1004. self:sendOutCard()
  1005. return false
  1006. else
  1007. logD("huamajiangHandCardView:onTouchBegan selectCard")
  1008. self.touchMJ:recordPostion(cc.p(self.touchMJ:getPositionX(),self.initY))
  1009. self.touchMJ:runSelectAnimation()
  1010. self.touchMJ:setStatus(MJDefine.MJStatus.Select)
  1011. app.room:dispatchEvent({name = MJDefine.MJEvent.ShowTing,card=self.touchMJ.value})
  1012. end
  1013. index = k;
  1014. break
  1015. end
  1016. end
  1017. --lastMj 有可能被移除 必须 tolua.isnull 判断一次
  1018. if lastMj and not tolua.isnull(lastMj) and lastMj~=self.touchMJ then
  1019. lastMj:runDeSelectAnimation(true)
  1020. lastMj:setStatus(MJDefine.MJStatus.Normal)
  1021. end
  1022. if self.touchMJ then
  1023. app.room:dispatchEvent({name = MJDefine.MJEvent.SelectCard,value = self.touchMJ.value})
  1024. else
  1025. app.room:dispatchEvent({name = MJDefine.MJEvent.SelectCard})
  1026. app.room:dispatchEvent({name = MJDefine.MJEvent.ShowTing})
  1027. return false
  1028. end
  1029. return true
  1030. end
  1031. function huamajiangHandCardView:onTouchMove(touch)
  1032. local touchPos=self:convertToNodeSpace(touch:getLocation())
  1033. local prePos=self:convertToNodeSpace(touch:getPreviousLocation())
  1034. local x=math.abs(prePos.x-touchPos.x)
  1035. local y=math.abs(prePos.y-touchPos.y)
  1036. if x>5 or y>5 then
  1037. logD("onTouchMove : ------------2")
  1038. self.isTouchMove=true
  1039. end
  1040. if not tolua.isnull(self.touchMJ) and self.isTouchMove==true then
  1041. self.touchMJ:setPosition(touchPos)
  1042. self.touchMJ:setLocalZOrder(1)
  1043. end
  1044. end
  1045. function huamajiangHandCardView:touchOutCard()
  1046. if self.touchMJ and self.isTouchMove and not tolua.isnull(self.touchMJ) then
  1047. logD("huamajiangHandCardView:touchOutCard", "touchMJ", self.touchMJ.value)
  1048. if self.touchMJ:getPositionY()>MJDefine.MJ_TOUCH_OUT_CARD_Y and self.isOutCard then
  1049. self:sendOutCard()
  1050. else
  1051. self.touchMJ:restorePostion()
  1052. self.touchMJ:setStatus(MJDefine.MJStatus.Normal)
  1053. app.room:dispatchEvent({name = MJDefine.MJEvent.SelectCard})
  1054. app.room:dispatchEvent({name = MJDefine.MJEvent.ShowTing})
  1055. if self.isOutCard then
  1056. self:moveLastCard()
  1057. end
  1058. end
  1059. end
  1060. self.isTouchMove=false
  1061. end
  1062. --[[
  1063. 移除最后一张手牌
  1064. ]]
  1065. function huamajiangHandCardView:removeLastHandCard ()
  1066. if self:isUseNew3D() then
  1067. return self._handcard:removeLastHandCard()
  1068. else
  1069. if #self.handCards + #self.handGroupCards * 3 <= 13 then
  1070. return false;
  1071. end
  1072. local last= #self.handCards;
  1073. self.handCards[last]:removeFromParent();
  1074. table.remove(self.handCards,last);
  1075. return true;
  1076. end
  1077. end
  1078. --[[
  1079. 创建手牌
  1080. ]]
  1081. function huamajiangHandCardView:createHandCards (arg)
  1082. if self:isUseNew3D() then
  1083. self._handcard:createHandCards(arg)
  1084. else
  1085. huamajiangHandCardView.super.createHandCards(self, arg);
  1086. self:refreshLuoBoCards();
  1087. end
  1088. end
  1089. --[[
  1090. 刷新手牌中萝卜牌的显示
  1091. ]]
  1092. function huamajiangHandCardView:refreshLuoBoCards ()
  1093. if self:isUseNew3D() then
  1094. self._handcard:refreshLuoBoCards()
  1095. else
  1096. for k, card in ipairs(self.handCards or {}) do
  1097. local count = app.room:getLuoBoCardCount(card:getValue()) or 0;
  1098. card:setLuoBoVisible(count > 0, count)
  1099. end
  1100. for k,v in pairs(self.handGroupCards) do
  1101. local cards = v:getCards();
  1102. for _, card in pairs(cards) do
  1103. local cardValue = card:getValue();
  1104. if cardValue > 0 then
  1105. local count = app.room:getLuoBoCardCount(cardValue) or 0;
  1106. cards[1]:setLuoBoVisible(count > 0, count)
  1107. break;
  1108. end
  1109. end
  1110. end
  1111. end
  1112. end
  1113. --[[
  1114. 获取最后一张牌
  1115. ]]
  1116. function huamajiangHandCardView:getLastCard( )
  1117. if self:isUseNew3D() then
  1118. return self._handcard:getLastCard()
  1119. else
  1120. return self.handCards[#self.handCards];
  1121. end
  1122. end
  1123. function huamajiangHandCardView:isLock ()
  1124. if self:isUseNew3D() then
  1125. return self._handcard:isLock()
  1126. else
  1127. return self._isLock;
  1128. end
  1129. end
  1130. function huamajiangHandCardView:createOutCards(arg)
  1131. if self:isUseNew3D() then
  1132. self._outCardView:createOutCards(arg)
  1133. else
  1134. if type(arg)=="number" then
  1135. arg={
  1136. {card=arg}
  1137. }
  1138. end
  1139. for k,v in pairs(arg) do
  1140. print("测试:"..v.card)
  1141. local mj=MJ:new(tonumber(v.card),MJDefine.MJType.Out,self.viewId,self.desktopType)
  1142. self.ui.Items.Layout_Hand_Card:addChild(mj)
  1143. table.insert(self.outCards,mj)
  1144. local x,y=huamajiangPositionCompnent.getOutCardPosition(self.viewId,self.outX,self.outY,mj,#self.outCards,self.desktopType,app.room.roomInfo.nMaxPlayCount)
  1145. mj:setPosition(x,y)
  1146. end
  1147. end
  1148. end
  1149. function huamajiangHandCardView:outCardFalg(event)
  1150. if event.viewId == MJDefine.MyViewId then
  1151. self:hideOperateItem()
  1152. self:hideOperate()
  1153. end
  1154. if self:isUseNew3D() then
  1155. event.pointerPath = "mj_huamajiang/res/zy_fangjian/room/huamajiang_pointer.png"
  1156. self._outCardView:outCardFalg(event)
  1157. else
  1158. for k,v in pairs(self.outCards) do
  1159. for i,child in pairs(v:getChildren()) do
  1160. local tag = child:getTag()
  1161. if tag ~= MJ.MJ_HAND_CARD_FLAG_LAIZI then
  1162. child:removeFromParent()
  1163. end
  1164. end
  1165. end
  1166. local mj=self.outCards[#self.outCards]
  1167. if event.value and mj and mj.value==event.value and event.viewId==self.viewId then
  1168. local flag = cc.Sprite:create()
  1169. flag:setTexture("mj_huamajiang/res/zy_fangjian/room/huamajiang_pointer.png");
  1170. -- flag:setScale(0.8)
  1171. mj:addChild(flag)
  1172. flag:setPosition(mj:getContentSize().width/2,mj:getContentSize().height)
  1173. local moveBy=cc.MoveBy:create(0.5,cc.p(0, 30))
  1174. flag:runAction(cc.RepeatForever:create(cc.Sequence:create(moveBy,moveBy:reverse())))
  1175. end
  1176. end
  1177. end
  1178. --- huamajiangHandCardView:runOutCardAction 出牌动画
  1179. -- @param value 牌值
  1180. -- @param callback 播放成功回调
  1181. function huamajiangHandCardView:runOutCardAction(value,callback)
  1182. if self:isUseNew3D() then
  1183. self._handcard:runOutCardAction(value, callback)
  1184. else
  1185. MJSound.PlayOutCard()
  1186. self:createOutCards(value)
  1187. local outCard=self.outCards[#self.outCards]
  1188. local showCard=MJ:new(value,MJDefine.MJType.Stand,MJDefine.MyViewId,self.desktopType)
  1189. self:addChild(showCard)
  1190. showCard:setScale(MJDefine.MJ_OUT_CARD_SCALE)
  1191. showCard:setPosition(self.showCardPostion)
  1192. local rotate=0
  1193. if self.viewId==MJDefine.PlayerViewType.Left then
  1194. rotate=-45
  1195. elseif self.viewId==MJDefine.PlayerViewType.Right then
  1196. rotate=45
  1197. end
  1198. local actions={
  1199. cc.ScaleTo:create(MJDefine.MJ_TIME_OUT_CARD_SCALE,1),
  1200. cc.DelayTime:create(MJDefine.MJ_TIME_OUT_CARD_WAIT),
  1201. cc.Spawn:create(
  1202. cc.Spawn:create(
  1203. cc.MoveTo:create(MJDefine.MJ_TIME_OUT_CARD,outCard:getPosition()),
  1204. cc.ScaleTo:create(MJDefine.MJ_TIME_OUT_CARD,MJDefine.MJ_OUT_CARD_SCALE)
  1205. ),
  1206. cc.RotateBy:create(MJDefine.MJ_TIME_OUT_CARD,rotate)
  1207. ),
  1208. cc.CallFunc:create(function()
  1209. -- MJSound.PlayOutCard()
  1210. if outCard and not tolua.isnull(outCard) then
  1211. outCard:setVisible(true)
  1212. end
  1213. if callback then callback() end
  1214. end),
  1215. cc.RemoveSelf:create(),
  1216. }
  1217. showCard:runAction(cc.Sequence:create(actions))
  1218. end
  1219. end
  1220. function huamajiangHandCardView:getHandCardNodes()
  1221. if self:isUseNew3D() then
  1222. return self._handcard:getHandCardNodes()
  1223. else
  1224. return self.handCards
  1225. end
  1226. end
  1227. --判断是否符合换三张规则
  1228. function huamajiangHandCardView:checkIsInSwapRule()
  1229. local roomInfo=app.room.roomInfo
  1230. local gameInfo=json.decode(roomInfo.strGameInfo)
  1231. local swapcolorcard = getNumBand(gameInfo.specRule, 0x80000)
  1232. local selectNums = #self._selectCards
  1233. logD("huamajiangHandCardView:checkIsInSwapRule1 "..selectNums)
  1234. if selectNums ~= 3 then
  1235. return false
  1236. end
  1237. --如果勾选了可换不同花色则不需要花色判断 只判断数量
  1238. if swapcolorcard > 0 and selectNums == 3 then
  1239. return true
  1240. end
  1241. if swapcolorcard==0 then
  1242. local huase = self._selectCards[1]:getMJColorType()
  1243. logD("huamajiangHandCardView:checkIsInSwapRule2 "..huase)
  1244. for i,v in pairs(self._selectCards) do
  1245. local tHuase = v:getMJColorType()
  1246. logD("huamajiangHandCardView:checkIsInSwapRule3 "..tHuase)
  1247. if tHuase ~= huase then
  1248. return false
  1249. end
  1250. end
  1251. logD("huamajiangHandCardView:checkIsInSwapRule4 ")
  1252. return true
  1253. end
  1254. end
  1255. return huamajiangHandCardView