您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

230 行
6.5 KiB

  1. local MJDefine = MJFramework.MJImport("mj.luaScript.MJDefine")
  2. local MJSound = MJFramework.MJImport("mj.luaScript.MJSound")
  3. local CommonOutCardView = class("CommonOutCardView", cc.UIView)
  4. local TAG_OUTCARD_POINTER = 1000
  5. function CommonOutCardView:ctor(viewId)
  6. CommonOutCardView.super.ctor(self)
  7. self._viewId = viewId
  8. self._outCardNodes = {}
  9. self:initOutCardArea()
  10. end
  11. function CommonOutCardView:onEnter()
  12. self:bindEvent(app.room, MJDefine.MJEvent.LOCAL_OUT_CARD, handler(self, self.onLocalOutCard));
  13. self:bindEvent(app.room, MJDefine.MJEvent.SelectCard, handler(self, self.onSelectCard));
  14. end
  15. function CommonOutCardView:initOutCardArea()
  16. end
  17. function CommonOutCardView:createOutCards(arg)
  18. if type(arg)=="number" then
  19. arg={
  20. {card=arg}
  21. }
  22. end
  23. for k,v in pairs(arg) do
  24. self:addCard(v.card)
  25. end
  26. self:refreshZOrder()
  27. end
  28. function CommonOutCardView:getViewId()
  29. return self._viewId
  30. end
  31. function CommonOutCardView:addCard(value)
  32. local cardIndex = #self._outCardNodes + 1
  33. local card = self:createCard(value, self._viewId, cardIndex)
  34. local x, y = self:getOutCardPosition(card, cardIndex)
  35. card:setPosition(cc.p(x, y))
  36. self:addChild(card)
  37. table.insert(self._outCardNodes, card)
  38. end
  39. function CommonOutCardView:createCard(value, viewId, mjIndex)
  40. local CardClass = require(MJDefine.MJConfig_2d.MAHJONG_CARD)
  41. local mjCard = CardClass:new(value, viewId, MJDefine.MJConfig_2d.MJType.Out, mjIndex)
  42. local x, y = self:getOutCardPosition(mjCard, viewId, mjIndex)
  43. mjCard:setPosition(cc.p(x, y))
  44. return mjCard
  45. end
  46. function CommonOutCardView:getOutCardPosition(mjCard, mjIndex)
  47. local x = 0
  48. local y = 0
  49. local viewId = self:getViewId()
  50. local playerCount = app.room:getMaxPlayerCount()
  51. local mjRowCount = MJDefine.MJConfig_2d.OutCardRowCount[playerCount][viewId]
  52. local col = mjIndex % mjRowCount
  53. col = col == 0 and mjRowCount or col
  54. local row = math.ceil(mjIndex / mjRowCount)
  55. local startPos = MJDefine.MJConfig_2d.OutCardStartPos[viewId]
  56. local offset = MJDefine.MJConfig_2d.OutCardOffsetConfig[viewId]
  57. local cardSize = mjCard:getContentSize()
  58. if playerCount == 2 then
  59. startPos = MJDefine.MJConfig_2d.OutCardStartPos2[viewId]
  60. offset = MJDefine.MJConfig_2d.OutCardOffsetConfig2[viewId]
  61. end
  62. if viewId == MJDefine.PlayerViewType.My then
  63. x = startPos.x + (col - 1) * (cardSize.width + offset.x)
  64. y = startPos.y + (1 - row) * (cardSize.height + offset.y)
  65. elseif viewId == MJDefine.PlayerViewType.Right then
  66. x = startPos.x + (row - 1) * (cardSize.width + offset.x)
  67. y = startPos.y + (col - 1) * (cardSize.height + offset.y)
  68. elseif viewId == MJDefine.PlayerViewType.Top then
  69. x = startPos.x + (1 - col) * (cardSize.width + offset.x)
  70. y = startPos.y + (row - 1) * (cardSize.height + offset.y)
  71. elseif viewId == MJDefine.PlayerViewType.Left then
  72. x = startPos.x + (1 - row) * (cardSize.width + offset.x)
  73. y = startPos.y + (1 - col) * (cardSize.height + offset.y)
  74. end
  75. return x, y
  76. end
  77. function CommonOutCardView:onOutCard(card, callback)
  78. MJSound.PlayOutCard()
  79. self:addCard(card)
  80. self:refreshZOrder()
  81. if callback then
  82. callback()
  83. end
  84. end
  85. function CommonOutCardView:onLocalOutCard(event)
  86. if self._viewId ~= 4 then
  87. return
  88. end
  89. self:onOutCard(event.card)
  90. end
  91. function CommonOutCardView:outCardFalg( event )
  92. self:clearOutCardPointer()
  93. local viewId = event.viewId
  94. local mjValue = event.value
  95. local pointerPath = event.pointerPath
  96. if mjValue and viewId == self:getViewId() then
  97. self:showOutCardPointer(mjValue, pointerPath)
  98. end
  99. end
  100. --- CommonOutCardView:showOutCardPointer 显示出牌指示箭头
  101. function CommonOutCardView:showOutCardPointer(mjValue, pointerPath)
  102. local mjCard = self._outCardNodes[#self._outCardNodes]
  103. if not mjCard then
  104. return
  105. end
  106. if mjCard:getValue() ~= mjValue then
  107. return
  108. end
  109. local imgPointer = nil
  110. pointerPath = "mj/res/ui/zy_fangjian/room/mj_pointer.png"
  111. if not pointerPath then
  112. imgPointer = cc.Sprite:createWithSpriteFrameName("mj_room_out_falg.png")
  113. else
  114. imgPointer = cc.Sprite:create()
  115. imgPointer:setTexture(pointerPath);
  116. end
  117. if not imgPointer then
  118. return
  119. end
  120. local size = mjCard:getContentSize()
  121. imgPointer:setPosition(size.width / 2, size.height)
  122. imgPointer:setTag(TAG_OUTCARD_POINTER)
  123. mjCard:addChild(imgPointer)
  124. local moveBy=cc.MoveBy:create(0.5,cc.p(0, 30))
  125. imgPointer:runAction(cc.RepeatForever:create(cc.Sequence:create(moveBy,moveBy:reverse())))
  126. end
  127. --- CommonOutCardView:clearOutCardPointer 移除出牌箭头
  128. function CommonOutCardView:clearOutCardPointer()
  129. for _, v in pairs(self._outCardNodes or {}) do
  130. local children = v:getChildren()
  131. for _, child in pairs(children) do
  132. if child:getTag() == TAG_OUTCARD_POINTER then
  133. child:removeFromParent()
  134. end
  135. end
  136. end
  137. end
  138. function CommonOutCardView:removeOutCard(arg)
  139. if type(arg)=="number" then
  140. arg={
  141. {card=arg}
  142. }
  143. end
  144. local function getMjByValue(value)
  145. for i = #self._outCardNodes, 1, -1 do
  146. if self._outCardNodes[i] then
  147. if self._outCardNodes[i]:getValue() == value then
  148. return self._outCardNodes[i]
  149. end
  150. end
  151. end
  152. end
  153. for k,v in pairs(arg) do
  154. local mj = getMjByValue(v.card)
  155. if mj then
  156. table.removeItem(self._outCardNodes, mj)
  157. mj:removeFromParent()
  158. end
  159. end
  160. end
  161. function CommonOutCardView:refreshZOrder()
  162. local viewId = self:getViewId()
  163. if viewId == MJDefine.PlayerViewType.Top or viewId == MJDefine.PlayerViewType.Right then
  164. local count = table.nums(self._outCardNodes)
  165. for k, v in ipairs(self._outCardNodes or {}) do
  166. v:setLocalZOrder(count - k)
  167. end
  168. else
  169. end
  170. end
  171. function CommonOutCardView:onSelectCard(event)
  172. if not event then
  173. return
  174. end
  175. local value = event.value;
  176. for k, v in ipairs(self._outCardNodes or {}) do
  177. if (not value) or value <= 0 then
  178. v:setNormalColor()
  179. else
  180. if v:getValue() == value then
  181. v:setColor(cc.c3b(150, 150, 150))
  182. else
  183. v:setNormalColor()
  184. end
  185. end
  186. end
  187. end
  188. ---
  189. -- 返回打出的所有牌
  190. -- @return table {node1, node2, ...}
  191. --
  192. function CommonOutCardView:getOutCardNodes()
  193. return self._outCardNodes or {}
  194. end
  195. return CommonOutCardView