|
- local MJDefine = MJFramework.MJImport("mj.luaScript.MJDefine")
- local MJSound = MJFramework.MJImport("mj.luaScript.MJSound")
- local CommonOutCardView = class("CommonOutCardView", cc.UIView)
-
- local TAG_OUTCARD_POINTER = 1000
-
- function CommonOutCardView:ctor(viewId)
- CommonOutCardView.super.ctor(self)
-
- self._viewId = viewId
- self._outCardNodes = {}
-
- self:initOutCardArea()
- end
-
- function CommonOutCardView:onEnter()
- self:bindEvent(app.room, MJDefine.MJEvent.LOCAL_OUT_CARD, handler(self, self.onLocalOutCard));
- self:bindEvent(app.room, MJDefine.MJEvent.SelectCard, handler(self, self.onSelectCard));
- end
-
- function CommonOutCardView:initOutCardArea()
-
- end
-
- function CommonOutCardView:createOutCards(arg)
- if type(arg)=="number" then
- arg={
- {card=arg}
- }
- end
-
- for k,v in pairs(arg) do
- self:addCard(v.card)
- end
-
- self:refreshZOrder()
- end
-
- function CommonOutCardView:getViewId()
- return self._viewId
- end
-
- function CommonOutCardView:addCard(value)
- local cardIndex = #self._outCardNodes + 1
- local card = self:createCard(value, self._viewId, cardIndex)
- local x, y = self:getOutCardPosition(card, cardIndex)
- card:setPosition(cc.p(x, y))
- self:addChild(card)
-
- table.insert(self._outCardNodes, card)
- end
-
- function CommonOutCardView:createCard(value, viewId, mjIndex)
- local CardClass = require(MJDefine.MJConfig_2d.MAHJONG_CARD)
- local mjCard = CardClass:new(value, viewId, MJDefine.MJConfig_2d.MJType.Out, mjIndex)
- local x, y = self:getOutCardPosition(mjCard, viewId, mjIndex)
- mjCard:setPosition(cc.p(x, y))
- return mjCard
- end
-
- function CommonOutCardView:getOutCardPosition(mjCard, mjIndex)
- local x = 0
- local y = 0
- local viewId = self:getViewId()
- local playerCount = app.room:getMaxPlayerCount()
- local mjRowCount = MJDefine.MJConfig_2d.OutCardRowCount[playerCount][viewId]
- local col = mjIndex % mjRowCount
- col = col == 0 and mjRowCount or col
- local row = math.ceil(mjIndex / mjRowCount)
-
- local startPos = MJDefine.MJConfig_2d.OutCardStartPos[viewId]
- local offset = MJDefine.MJConfig_2d.OutCardOffsetConfig[viewId]
- local cardSize = mjCard:getContentSize()
-
- if playerCount == 2 then
- startPos = MJDefine.MJConfig_2d.OutCardStartPos2[viewId]
- offset = MJDefine.MJConfig_2d.OutCardOffsetConfig2[viewId]
- end
-
- if viewId == MJDefine.PlayerViewType.My then
- x = startPos.x + (col - 1) * (cardSize.width + offset.x)
- y = startPos.y + (1 - row) * (cardSize.height + offset.y)
- elseif viewId == MJDefine.PlayerViewType.Right then
- x = startPos.x + (row - 1) * (cardSize.width + offset.x)
- y = startPos.y + (col - 1) * (cardSize.height + offset.y)
- elseif viewId == MJDefine.PlayerViewType.Top then
- x = startPos.x + (1 - col) * (cardSize.width + offset.x)
- y = startPos.y + (row - 1) * (cardSize.height + offset.y)
- elseif viewId == MJDefine.PlayerViewType.Left then
- x = startPos.x + (1 - row) * (cardSize.width + offset.x)
- y = startPos.y + (1 - col) * (cardSize.height + offset.y)
- end
-
- return x, y
- end
-
- function CommonOutCardView:onOutCard(card, callback)
- MJSound.PlayOutCard()
- self:addCard(card)
- self:refreshZOrder()
- if callback then
- callback()
- end
- end
-
- function CommonOutCardView:onLocalOutCard(event)
- if self._viewId ~= 4 then
- return
- end
- self:onOutCard(event.card)
- end
-
- function CommonOutCardView:outCardFalg( event )
- self:clearOutCardPointer()
-
- local viewId = event.viewId
- local mjValue = event.value
- local pointerPath = event.pointerPath
- if mjValue and viewId == self:getViewId() then
- self:showOutCardPointer(mjValue, pointerPath)
- end
- end
- --- CommonOutCardView:showOutCardPointer 显示出牌指示箭头
- function CommonOutCardView:showOutCardPointer(mjValue, pointerPath)
- local mjCard = self._outCardNodes[#self._outCardNodes]
- if not mjCard then
- return
- end
-
- if mjCard:getValue() ~= mjValue then
- return
- end
-
- local imgPointer = nil
- pointerPath = "mj/res/ui/zy_fangjian/room/mj_pointer.png"
- if not pointerPath then
- imgPointer = cc.Sprite:createWithSpriteFrameName("mj_room_out_falg.png")
- else
- imgPointer = cc.Sprite:create()
- imgPointer:setTexture(pointerPath);
- end
-
- if not imgPointer then
- return
- end
-
- local size = mjCard:getContentSize()
- imgPointer:setPosition(size.width / 2, size.height)
- imgPointer:setTag(TAG_OUTCARD_POINTER)
- mjCard:addChild(imgPointer)
-
- local moveBy=cc.MoveBy:create(0.5,cc.p(0, 30))
- imgPointer:runAction(cc.RepeatForever:create(cc.Sequence:create(moveBy,moveBy:reverse())))
- end
- --- CommonOutCardView:clearOutCardPointer 移除出牌箭头
- function CommonOutCardView:clearOutCardPointer()
- for _, v in pairs(self._outCardNodes or {}) do
- local children = v:getChildren()
- for _, child in pairs(children) do
- if child:getTag() == TAG_OUTCARD_POINTER then
- child:removeFromParent()
- end
- end
- end
- end
-
- function CommonOutCardView:removeOutCard(arg)
- if type(arg)=="number" then
- arg={
- {card=arg}
- }
- end
-
- local function getMjByValue(value)
- for i = #self._outCardNodes, 1, -1 do
- if self._outCardNodes[i] then
- if self._outCardNodes[i]:getValue() == value then
- return self._outCardNodes[i]
- end
- end
- end
- end
-
- for k,v in pairs(arg) do
- local mj = getMjByValue(v.card)
- if mj then
- table.removeItem(self._outCardNodes, mj)
- mj:removeFromParent()
- end
- end
- end
-
- function CommonOutCardView:refreshZOrder()
- local viewId = self:getViewId()
- if viewId == MJDefine.PlayerViewType.Top or viewId == MJDefine.PlayerViewType.Right then
- local count = table.nums(self._outCardNodes)
- for k, v in ipairs(self._outCardNodes or {}) do
- v:setLocalZOrder(count - k)
- end
- else
-
- end
- end
-
- function CommonOutCardView:onSelectCard(event)
- if not event then
- return
- end
- local value = event.value;
- for k, v in ipairs(self._outCardNodes or {}) do
- if (not value) or value <= 0 then
- v:setNormalColor()
- else
- if v:getValue() == value then
- v:setColor(cc.c3b(150, 150, 150))
- else
- v:setNormalColor()
- end
- end
- end
- end
- ---
- -- 返回打出的所有牌
- -- @return table {node1, node2, ...}
- --
- function CommonOutCardView:getOutCardNodes()
- return self._outCardNodes or {}
- end
-
- return CommonOutCardView
|