hengyang_client/lua_probject/main_project/main/majiang/MJPlayerCardInfoView.lua

515 lines
17 KiB
Lua

AreaOderType = {
left_right = 'left_right',
right_left = 'right_left',
up_down = 'up_down',
down_up = 'down_up'
}
local PlayerCardInfoView = {
_view = nil,
_mainView = nil,
_mask_liangpai = nil,
_mask_data = nil,
_area_handcard_list = nil,
_src_fz_list = nil,
_current_card_type = -1
}
local CardView = {
card = nil,
-- 牌序号
card_item = 0,
-- 索引
index = 0,
-- 原始位置
old_postion = Vector2.zero
}
local function NewCardView(card, cardItem)
local self = {}
setmetatable(self, { __index = CardView })
self.card = card
self.card_item = cardItem
return self
end
local M = PlayerCardInfoView
--- Create a new PlayerCardInfoView
function M.new(view, mainView, record, direction)
local self = {}
setmetatable(self, { __index = M })
self._view = view
self._mainView = mainView
self._flag_record = record
self.direction = direction
self:init()
return self
end
function M:init()
local CardInfo = self._view:GetChild('Text_CardInfo')
self._viewText_cardInfo = json.decode(CardInfo.text)
self._view_handCardList = self._view:GetChild('List_HandCard')
self._view_FZList = self._view:GetChild('List_FZ')
self._view_outCardList = self._view:GetChild('List_OutCard')
self._viewList_HuEffect = self._view:GetChild("list_HuEffect")
self._viewLoader_selfHuCardEffect = self._view:GetChild("loader_selfHuCardEffect")
self._view_getCard = self._view:GetChild('Btn_HandCard')
self._ctr_getCard = self._view:GetController('getCard')
self._ctr_record = self._view:GetController('record')
if self._flag_record and self._flag_record == 1 and self._ctr_record then
self._ctr_record.selectedIndex = self._flag_record or 0
self._view_handCardList = self._view:GetChild('List_HandCard2')
self._view_getCard = self._view:GetChild('Btn_HandCard2')
end
if self.direction == "S" then
self._view_outCardList.layout = FairyGUI.ListLayoutType.FlowHorizontal_left2right_bottom2top
elseif self.direction == "N" then
self._view_outCardList.layout = FairyGUI.ListLayoutType.FlowHorizontal_right2left_top2bottom
elseif self.direction == "E" then
self._view_outCardList.layout = FairyGUI.ListLayoutType.FlowVertical_right2left_bottom2top
end
end
function M:SetPlayer(p)
self._player = p
end
function M:FillData(begin)
if (begin) then
print("lingmengResetFzList2")
pt(self._player)
for i = 1, #self._player.fz_list do
self:UpdateFzList(self._player.fz_list[i], -1)
end
self:UpdateOutCardList()
else
self._current_card_type = DataManager.CurrenRoom.card_type
if self._current_card_type == 2 then
local c3d = self._view:GetController('3d')
if c3d ~= nil then
c3d.selectedIndex = 1
end
else
local c3d = self._view:GetController('3d')
if c3d ~= nil then
c3d.selectedIndex = 0
end
end
end
end
function M:Clear()
--self._ctr_state.selectedIndex = 0
self._view_handCardList:RemoveChildren(0, -1, true)
self._view_outCardList:RemoveChildren(0, -1, true)
self._view_FZList:RemoveChildren(0, -1, true)
self._view_getCard:RemoveChildren(0, -1, true)
if self._view:GetChild('List_HandCard2') then
self._view:GetChild('List_HandCard2'):RemoveChildren(0, -1, true)
end
self._viewList_HuEffect:RemoveChildren(0, -1, true)
self._view_handCardList.selectedIndex = -1
self._click_index = self._view_handCardList.selectedIndex
end
-- 获取麻将资源前缀
function M:GetPrefix()
-- local card_type = DataManager.CurrenRoom.card_type
-- local prefix = card_type == 1 and "a" or ""
-- return prefix
return get_majiang_prefix(DataManager.CurrenRoom.game_id)
end
function M:fillCard(obj, pos_str, card, use3d)
if DataManager.CurrenRoom.jing and card == DataManager.CurrenRoom.jing and obj:GetController('jing') then
obj:GetController('jing').selectedIndex = 1
end
if self._current_card_type == 2 and (use3d == nil or use3d == true) then
obj.icon = 'ui://MajiangCard3d/' .. 'b' .. pos_str .. card
else
obj.icon = 'ui://Main_Majiang/' .. self:GetPrefix() .. pos_str .. card
end
end
function M:fillCard2(obj, pos_str, card, out)
if DataManager.CurrenRoom.jing and card == DataManager.CurrenRoom.jing and obj:GetController('jing') then
obj:GetController('jing').selectedIndex = 1
if obj:GetController('out') then
obj:GetController('out').selectedIndex = out or 0
end
end
obj:GetChild("icon").url = 'ui://Main_Majiang/' .. pos_str .. card
end
function M:getBackCard(card)
return 'ui://Main_Majiang/' .. card
end
function M:GetCard(btn)
local pic_name = split(btn.icon, '/')[4]
local lst = string.split(pic_name, '_')
return tonumber(lst[2] or 0)
end
function M:UpdateHandCard(getcard, mp)
getcard = getcard or false
mp = mp or false
self._view_handCardList:RemoveChildren()
self._view_getCard:RemoveChildren()
local btn_card
for i = 0, self._player.hand_left_count - 1 do
if getcard and i == self._player.hand_left_count - 1 then
btn_card = self._view_getCard:AddItemFromPool()
else
btn_card = self._view_handCardList:AddItemFromPool()
end
if mp then
local tem_card = self._player.card_list[i + 1]
self:FillHandCard(i, btn_card, tem_card, true)
end
end
self._ctr_getCard.selectedIndex = getcard and 1 or 0
print("lingmenghand", self._ctr_getCard.selectedIndex, self._view_getCard.numItems, self._view_getCard.visible)
end
function M:UpdateHandCardWitness(getcard, isSelf)
print("lingmeng UpdateHandCardWitness")
getcard = getcard or false
isSelf = isSelf or false
self._view_handCardList:RemoveChildren()
self._view_getCard:RemoveChildren()
print("lingmeng UpdateHandCardWitness1")
for i = 0, self._player.hand_left_count - 1 do
local btn_card
if getcard and i == self._player.hand_left_count - 1 then
btn_card = self._view_getCard:AddItemFromPool()
else
btn_card = self._view_handCardList:AddItemFromPool()
end
if isSelf then
self:FillWitnessCard(btn_card)
end
end
print("lingmeng UpdateHandCardWitness2")
self._view_handCardList.touchable = false
self._view_getCard.touchable = false
print("lingmeng UpdateHandCardWitness3")
self._ctr_getCard.selectedIndex = getcard and 1 or 0
end
function M:FillWitnessCard(btn_card)
btn_card:GetChild("icon").url = 'ui://Main_Majiang/b201_0'
end
-- 获取麻将图片资源位置,可以在扩展中复写
function M:GetCardPicPack()
-- if DataManager.CurrenRoom.card_type == 2 then
-- return "MajiangCard3d"
-- else
return 'Main_Majiang'
-- end
end
function M:adjust3dOutPut(obj, area, oder, num, index)
if index >= num then
local row = 1 + math.floor(((index - num) / (num + 2)))
local col = ((index - num) % (num + 2))
if oder == AreaOderType.left_right then
obj.x = obj.x + math.floor((area.width - obj.width * (num + 2)) / 2)
elseif oder == AreaOderType.right_left then
obj.x = obj.x - math.floor((area.width - obj.width * (num + 2)) / 2)
elseif oder == AreaOderType.up_down then
obj.y = obj.y + math.floor((area.height - obj.height * (num + 2)) / 2)
obj.x = obj.x - col * 7 + 7 * (row + 1)
elseif oder == AreaOderType.down_up then
obj.y = obj.y - math.floor((area.height - obj.height * (num + 2)) / 2)
obj.x = obj.x - col * 7 - 7 * (row - 1)
end
else
if oder == AreaOderType.left_right then
obj.x = obj.x + math.floor((area.width - obj.width * num) / 2)
elseif oder == AreaOderType.right_left then
obj.x = obj.x - math.floor((area.width - obj.width * num) / 2)
elseif oder == AreaOderType.up_down then
obj.y = obj.y + math.floor((area.height - obj.height * num) / 2)
obj.x = obj.x - index * 7
elseif oder == AreaOderType.down_up then
obj.y = obj.y - math.floor((area.height - obj.height * num) / 2)
obj.x = obj.x - index * 7
end
end
end
function M:FillHandCard(i, btn_card, tem_card, event)
local handCardName = self._viewText_cardInfo['Hand_Card']
self:fillCard2(btn_card, handCardName, tem_card)
local c_v = NewCardView(btn_card, tem_card)
c_v.index = i
c_v.old_postion = btn_card.xy
c_v.touch_pos = Vector2.New(btn_card.width / 2, btn_card.height / 2)
btn_card.data = c_v
if event then
btn_card.onTouchBegin:Set(handler(self, self.onTouchBegin))
btn_card.onTouchMove:Set(handler(self, self.onTouchMove))
btn_card.onTouchEnd:Set(handler(self, self.__OnDragEnd))
end
end
function M:UpdateOutCardList(outcard, card_item, cursor)
outcard = outcard or nil
card_item = card_item or 0
cursor = cursor or nil
local outCardName = self._viewText_cardInfo['Out_Card']
local outlist = self._player.outcard_list
self._view_outCardList:RemoveChildrenToPool()
for i = 0, #outlist - 1 do
local outcard = self._view_outCardList:AddItemFromPool()
self:fillCard2(outcard, outCardName, outlist[i + 1])
if cursor and i == #outlist - 1 then
Timer.New(function()
self._view:AddChild(cursor)
-- GRoot.inst:AddChild(cursor)
cursor.xy = self._view_outCardList.xy +
self._view_outCardList:GlobalToLocal(outcard:LocalToGlobal(Vector2.New(outcard.width * 0.5,
outcard.height * 0.5)))
end, Time.deltaTime, 1, false):Start()
end
end
end
-- 设置添加角标的方法
function M:SetAddFlag(cb)
self.__addFlag = cb
end
-- 给麻将牌添加flag
function M:AddFlag(index, card, btn)
if not self.__addFlag then
return
end
local str_flag = self.__addFlag(index, card, btn)
btn:GetController('hun').selectedIndex = str_flag == '' and 0 or 1
btn:GetChild('flag').icon = str_flag
end
local function getPos(my_seat, other_seat, total)
local pos = 0
pos = other_seat - my_seat + 1
if pos <= 0 then
pos = pos + total
end
if total ~= 4 and pos == total then
pos = total + 1
end
return pos
end
function M:UpdateFzList(fz, index, show_card)
local room = DataManager.CurrenRoom
-- local seat = (room.room_config.people_num == 2 and fz.from_seat == 2) and 2 or fz.from_seat - 1
local seat = ViewUtil.GetPos(room.self_player.seat, fz.from_seat or 1, room.room_config.people_num)
seat = (room.room_config.people_num == 2 and seat == 2) and 2 or seat - 1
print("lingmengUpdateFzList", fz, index, show_card, seat)
local FZame = self._viewText_cardInfo['FZ_Card']
index = index or 0
show_card = show_card or nil
local outCard = self._view_FZList:AddItemFromPool()
outCard:GetController("seat").selectedIndex = seat or 0
if fz.type == FZType.Chi then
for i = 1, 3 do
local FZCard = outCard:GetChild(string.format("Comp_Card%d", i))
self:fillCard2(FZCard, FZame, fz.opcard[i])
end
else
local ctr_Four = outCard:GetController("isFour")
outCard.data = fz.card
for i = 1, 3 do
local FZCard = outCard:GetChild(string.format("Comp_Card%d", i))
self:fillCard2(FZCard, FZame, fz.card)
end
if fz.type == FZType.Peng then
ctr_Four.selectedIndex = 0
else
ctr_Four.selectedIndex = 1
local FZCard = outCard:GetChild(string.format("Comp_Card%d", 4))
self:fillCard2(FZCard, FZame, fz.card)
if fz.type == FZType.Gang_An then
outCard:GetController("not_arrow").selectedIndex = 1
self:fillCard2(FZCard, FZame, '00')
if FZCard:GetController('jing') then
FZCard:GetController('jing').selectedIndex = 0
end
elseif fz.type == FZType.Gang_Peng then
for i = 1, self._view_FZList.numItems do
if self._view_FZList:GetChildAt(i - 1).data and self._view_FZList:GetChildAt(i - 1).data == fz.card then
outCard:GetController("seat").selectedIndex = self._view_FZList:GetChildAt(i - 1):GetController(
"seat").selectedIndex
self._view_FZList:RemoveChildrenToPool(i - 1, i - 1)
return
end
end
end
end
end
-- self:fillCard2(outcard, outCardName, card_item)
end
function M:GetOutCardByIndex(index)
local outcard_list = self._mask_data['outcard_list']
local oder = outcard_list['oder']
local multi_draw_oder = 0
if (outcard_list['multi_draw_oder']) then
multi_draw_oder = outcard_list['multi_draw_oder']
end
if (oder == AreaOderType.down_up or (multi_draw_oder == 1)) then
return self._area_outcard_list:GetChildAt(self._area_outcard_list.numChildren - index)
else
return self._area_outcard_list:GetChildAt(index - 1)
end
end
function M:ResetFzList()
for i = 1, #self._player.fz_list do
print("lingmengResetFzList")
pt(self._player)
local fz = self._player.fz_list[i]
-- if fz.type ~= FZType.Gang_Peng then
self:UpdateFzList(fz, -1)
-- else
-- self:UpdateFzList(fz, i)
-- end
end
end
local function replace_card(obj, prefix)
local url = obj.icon
if url then
local len = string.len(url)
local pos = string.len('ui://Main_Majiang/') + 1
local head_char = string.sub(url, pos, pos)
local card = string.sub(url, pos, len)
if head_char >= 'a' then
card = string.sub(card, 2, len)
end
obj.icon = 'ui://Main_Majiang/' .. prefix .. card
end
end
function M:ResetCardType()
-- local old_card_type = self._current_card_type
-- self._current_card_type = DataManager.CurrenRoom.card_type
-- --设置3d标志
-- if self._current_card_type == 2 then
-- local c3d = self._view:GetController('3d')
-- if c3d ~= nil then
-- c3d.selectedIndex = 1
-- self._area_fz_list.x = self._src_fz_list_3d.x
-- self._area_fz_list.y = self._src_fz_list_3d.y
-- self._area_fz_list.width = self._src_fz_list_3d.z
-- self._area_fz_list.height = self._src_fz_list_3d.w
-- end
-- else
-- local c3d = self._view:GetController('3d')
-- if c3d ~= nil then
-- c3d.selectedIndex = 0
-- self._area_fz_list.x = self._src_fz_list.x
-- self._area_fz_list.y = self._src_fz_list.y
-- self._area_fz_list.width = self._src_fz_list.z
-- self._area_fz_list.height = self._src_fz_list.w
-- end
-- end
-- local change2d = false
-- if old_card_type == 2 and self._current_card_type ~= 2 then
-- change2d = true
-- end
-- local change3d = false
-- if old_card_type ~= 2 and self._current_card_type == 2 then
-- change3d = true
-- end
-- if change2d == false and change3d == false and self._current_card_type ~= 2 then
-- local prefix = self:GetPrefix()
-- --更新已出牌
-- for i = 1, self._area_outcard_list.numChildren do
-- local obj = self._area_outcard_list:GetChildAt(i - 1)
-- replace_card(obj, prefix)
-- end
-- --更新手牌
-- for i = 1, self._area_handcard_list.numChildren do
-- local obj = self._area_handcard_list:GetChildAt(i - 1)
-- replace_card(obj, prefix)
-- end
-- --更新放子牌
-- for i = 1, self._area_fz_list.numChildren do
-- local com = self._area_fz_list:GetChildAt(i - 1)
-- for j = 1, 4 do
-- local obj = com:GetChild('card_' .. j)
-- if obj then
-- replace_card(obj, prefix)
-- end
-- end
-- end
-- end
end
function M:ShowHand(cards)
local list = self._view_handCardList
local cardType = self._viewText_cardInfo["Out_Card"]
if self._viewText_cardInfo['IS_SIDE'] == "true" then
list = self._view:GetChild('List_HandCard2')
cardType = self._viewText_cardInfo["Hand_Card"]
end
list:RemoveChildren()
--list:RemoveChildren()
local passcard = false
for _, card in pairs(cards) do
if self.winCard == cards and passcard == false then
passcard = true
else
local obj = list:AddItemFromPool()
self:fillCard2(obj, cardType, card, 1)
end
end
end
function M:ShowHuCard(card)
self.winCard = card
self._view_getCard:RemoveChildrenToPool()
local btn_card = self._view_getCard:AddItemFromPool()
self:fillCard2(btn_card, self._viewText_cardInfo['Out_Card'], card, 1)
self._ctr_getCard.selectedIndex = 1
end
return M