-- <* -- @Author: Saber -- @Description: 圣物系统数据寻宝界面 -- *> PsionicDrawView = PsionicDrawView or BaseClass(BaseItem) local PsionicDrawView = PsionicDrawView local math_random = math.random local cc = cc local SetAnchoredPositionY = SetAnchoredPositionY function PsionicDrawView:__init(parent_wnd,prefab_asset,layer_name) self.base_file = "psionic" self.layout_file = "PsionicDrawView" self.parent_wnd = parent_wnd self.layer_name = layer_name self.model = PsionicModel:getInstance() self.goods_model = GoodsModel:getInstance() -- 抽卡消耗缓存 self.cost_cache = { [PsionicConst.DrawPoolType.Normal] = {}, [PsionicConst.DrawPoolType.Deep] = {} } -- 简化代码用 self.pool_type_str = { [PsionicConst.DrawPoolType.Normal] = "normal", [PsionicConst.DrawPoolType.Deep] = "deep" } self.anim_item_list = {} -- 参与动画的节点 self:Load() end function PsionicDrawView:SetVisible(state, force_hide) local function visible_callback() BaseItem.SetVisible(self, state, force_hide) end if state then visible_callback() self:StartTransCGAlphaAnim(true) else lua_resM:clearRoleMode(self) self:StartTransCGAlphaAnim(false, visible_callback) end end function PsionicDrawView:Load_callback() local nodes = { -- 普通寻宝 "normal_cost_icon1:img", "normal_cost_icon2:img", "normal_one_btn:obj", "normal_ten_btn:obj", "normal_cost1:tmp", "normal_cost2:tmp", "normal_free_time:tmp", "normal_one_red:obj", "normal_ten_red:obj", "normal_model_con:obj:raw", "normal_draw_tip:tmp", "normal_tip_btn:obj", -- 深度寻宝 "deep_cost_icon1:img", "deep_cost_icon2:img", "deep_one_btn:obj", "deep_ten_btn:obj", "deep_cost1:tmp", "deep_cost2:tmp", "deep_free_time:tmp", "deep_one_red:obj", "deep_ten_red:obj", "deep_first_tip:obj", "deep_model_con:obj:raw", "deep_draw_tip:tmp", "deep_tip_btn:obj", } self:GetChildren(nodes) self.transform_cg = self.transform:GetComponent(typeof(UnityEngine.CanvasGroup)) self.normal_draw_tip_tmp.text = string.format("必得极品紫色品质圣物", ColorUtil.PURPLE_DARK) self.deep_draw_tip_tmp.text = string.format("必得极品橙色品质圣物", ColorUtil.ORANGE_DARK) SetLocalPositionZ(self.normal_tip_btn, -2000) SetLocalPositionZ(self.deep_tip_btn, -2000) -- 获取浮动节点 for i = 1, 8 do self.anim_item_list[i] = self:GetChild("anim_con/anim_item"..i) end self:AddEvents() self:StartTransCGAlphaAnim(true) self:UpdateView() self:CreateRewardsAnim() self:UpdateRedByPoolType(PsionicConst.DrawPoolType.Normal) self:UpdateRedByPoolType(PsionicConst.DrawPoolType.Deep) end function PsionicDrawView:AddEvents( ) local function click_event(target) if target == self.normal_one_btn_obj then -- 普通单抽 self:OnDrawBtnClick(PsionicConst.DrawPoolType.Normal, PsionicConst.DrawNumType.OneDraw) elseif target == self.normal_ten_btn_obj then -- 普通十连 self:OnDrawBtnClick(PsionicConst.DrawPoolType.Normal, PsionicConst.DrawNumType.TenDraw) elseif target == self.deep_one_btn_obj then -- 深度单抽 self:OnDrawBtnClick(PsionicConst.DrawPoolType.Deep, PsionicConst.DrawNumType.OneDraw) elseif target == self.deep_ten_btn_obj then -- 深度十连抽 self:OnDrawBtnClick(PsionicConst.DrawPoolType.Deep, PsionicConst.DrawNumType.TenDraw) elseif target == self.normal_tip_btn_obj then -- 普通池奖励展示 self.model:Fire(PsionicConst.OPEN_DRAW_PREVIEW_VIEW, true, PsionicConst.DrawPoolType.Normal) elseif target == self.deep_tip_btn_obj then -- 深度池奖励展示 self.model:Fire(PsionicConst.OPEN_DRAW_PREVIEW_VIEW, true, PsionicConst.DrawPoolType.Deep) end end AddClickEvent(self.normal_one_btn_obj, click_event, LuaSoundManager.SOUND_UI.NONE) AddClickEvent(self.normal_ten_btn_obj, click_event, LuaSoundManager.SOUND_UI.NONE) AddClickEvent(self.deep_one_btn_obj, click_event, LuaSoundManager.SOUND_UI.NONE) AddClickEvent(self.deep_ten_btn_obj, click_event, LuaSoundManager.SOUND_UI.NONE) AddClickEvent(self.normal_tip_btn_obj, click_event, LuaSoundManager.SOUND_UI.NONE) AddClickEvent(self.deep_tip_btn_obj, click_event, LuaSoundManager.SOUND_UI.NONE) local function update_dig_data(pool_type, opty_type) -- 抽卡结果需要指定类型,其他的情况则刷新整个界面 if pool_type and opty_type then if pool_type == PsionicConst.DrawPoolType.Normal then self:UpdateDrawCostDataByType(PsionicConst.DrawPoolType.Normal) self:UpdateNormalFreeTimer(PsionicConst.DrawPoolType.Normal) elseif pool_type == PsionicConst.DrawPoolType.Deep then self:UpdateDrawCostDataByType(PsionicConst.DrawPoolType.Deep) self:UpdateNormalFreeTimer(PsionicConst.DrawPoolType.Deep) end else self:UpdateView() end end self:BindEvent(self.model, PsionicConst.UPDATE_DIG_DATA, update_dig_data) local function update_draw_red(pool_type) self:UpdateRedByPoolType(pool_type) end self:BindEvent(self.model, PsionicConst.UPDATE_DATADIG_RED_BY_TYPE, update_draw_red) -- 通过更新红点的方式更新抽奖道具表现 local function update_draw_items(tab_id) if tab_id and tab_id == PsionicConst.TabId.PDataDig then self:UpdateDrawCostDataByType(PsionicConst.DrawPoolType.Normal) self:UpdateDrawCostDataByType(PsionicConst.DrawPoolType.Deep) end end self:BindEvent(self.model, PsionicConst.UPDATE_RED_BY_TABID, update_draw_items) end function PsionicDrawView:StartTransCGAlphaAnim(show, close_callback) if not self.is_loaded then return end self:StopTransCFAlphaAnim() local target_alpha = show and 1 or 0 self.transform_cg.interactable = false if show then self:UpdateDrawModel(PsionicConst.DrawPoolType.Normal) self:UpdateDrawModel(PsionicConst.DrawPoolType.Deep) end local function alpha_anim_callback() if show then self.transform_cg.interactable = true end end -- 关闭回调覆盖 if not show and close_callback then alpha_anim_callback = close_callback end self.alpha_anim_id = TweenLite.to(self, self.transform_cg, TweenLite.UiAnimationType.ALPHA_ALL, target_alpha, 0.2, alpha_anim_callback) end function PsionicDrawView:StopTransCFAlphaAnim( ) if self.alpha_anim_id then TweenLite.Stop(self.alpha_anim_id) self.alpha_anim_id = nil end end function PsionicDrawView:UpdateView( ) -- 加载普通寻宝配置 self.normal_cfg = Config.Nucleonpool[1] -- 加载深度寻宝配置 self.deep_cfg = Config.Nucleonpool[2] self:UpdateDrawCostDataByType(PsionicConst.DrawPoolType.Normal) self:UpdateDrawCostDataByType(PsionicConst.DrawPoolType.Deep) self:UpdateNormalFreeTimer(PsionicConst.DrawPoolType.Normal) self:UpdateNormalFreeTimer(PsionicConst.DrawPoolType.Deep) end function PsionicDrawView:UpdateDrawModel(pool_type) local pt = self.pool_type_str[pool_type] local data = { father_node = self, transform = self[pt.."_model_con"], fashion_type = FuncOpenModel.TypeId.OtherGoods, figure_id = PsionicConst.DrawShowModel[pool_type], scale = 300, can_rotate = false, action_name_list = {"idle"}, position = Vector3(0, -130, 0), rotate = Vector3(15,180,0), ui_model_type = UIModelCommon.ModelType.Model, } FuncOpenModel:getInstance():SetModelRes(data) end -- 新版调整 根据池子类型不同,直接更新对应池子的消耗物品信息,并缓存每种情况所需要的花费 function PsionicDrawView:UpdateDrawCostDataByType(pool_type) local cfg_data = pool_type == PsionicConst.DrawPoolType.Normal and self.normal_cfg or self.deep_cfg local pt = self.pool_type_str[pool_type] local pool_data = self.model:GetPsionicDigData(pool_type) -- 当前是否能免费 local can_free = pool_data and pool_data.remain > 0 and pool_data.rtime - TimeUtil:getServerTime() < 0 or false -- 获取抽奖券数 local single_ticket_num = self.goods_model:GetTypeGoodsNum(cfg_data.one) local ten_ticket_num = self.goods_model:GetTypeGoodsNum(cfg_data.ten) local one_cost = stringtotable(cfg_data.cost)[1] local ten_cost = stringtotable(cfg_data.pay)[1] -- 单抽券价格 local single_ticket_price = tonumber(one_cost[3]) local single_ticket_price_type = tonumber(one_cost[1]) local one_cost_icon_asset, one_cost_icon_name = WordManager:GetCommonMoneyIcon(single_ticket_price_type) local ten_cost_icon_asset, ten_cost_icon_name = WordManager:GetCommonMoneyIcon(ten_cost[1]) if pool_type == PsionicConst.DrawPoolType.Normal then -- 普通池子默认展示红钻,有单抽券时显示单抽券 -- 单抽 -- 没有抽奖券的情况 if can_free then lua_resM:setImageSprite(self, self[pt .. "_cost_icon1_img"], "psionic_asset", "psionic_dig_icon"..pool_type.."_1", true) self[pt .. "_cost1_tmp"].text = "免费" self.cost_cache[pool_type][PsionicConst.DrawNumType.OneDraw] = {free = true} -- 免费,可以直接抽 -- 有抽奖券,则加载为抽奖券的图标 elseif single_ticket_num > 0 then lua_resM:setImageSprite(self, self[pt .. "_cost_icon1_img"], "psionic_asset", "psionic_dig_icon"..pool_type.."_1", true) self[pt .. "_cost1_tmp"].text = string.format("%s/1", single_ticket_num > 0 and ColorUtil.GREEN_DARK or ColorUtil.RED_DARK, single_ticket_num) self.cost_cache[pool_type][PsionicConst.DrawNumType.OneDraw] = {goods_id = cfg_data.one} -- 构造数据,扣除道具 else -- 显示为钻石付费 lua_resM:setImageSprite(self, self[pt .. "_cost_icon1_img"], one_cost_icon_asset, one_cost_icon_name, true) self[pt .. "_cost1_tmp"].text = single_ticket_price self.cost_cache[pool_type][PsionicConst.DrawNumType.OneDraw] = {price_type = single_ticket_price_type, cost = single_ticket_price} -- 构造数据,扣除金额 end -- 十连 -- 有十连券,则加载为十连券的图标 if ten_ticket_num > 0 then lua_resM:setImageSprite(self, self[pt .. "_cost_icon2_img"], "psionic_asset", "psionic_dig_icon"..pool_type.."_2", true) self[pt .. "_cost2_tmp"].text = string.format("%s/1", ColorUtil.GREEN_DARK, ten_ticket_num) self.cost_cache[pool_type][PsionicConst.DrawNumType.TenDraw] = {goods_id = cfg_data.ten} -- 构造数据,表现为扣除道具 -- 有单抽券,则加载为单抽券,并显示出所需个数相关表现 elseif single_ticket_num > 0 then lua_resM:setImageSprite(self, self[pt .. "_cost_icon2_img"], "psionic_asset", "psionic_dig_icon"..pool_type.."_1", true) self[pt .. "_cost2_tmp"].text = string.format("%s/9", single_ticket_num >= 9 and ColorUtil.GREEN_DARK or ColorUtil.RED_DARK, single_ticket_num) if single_ticket_num >= 9 then -- 单抽券数量足够,表现为扣除道具 self.cost_cache[pool_type][PsionicConst.DrawNumType.TenDraw] = {goods_id = cfg_data.one} -- 构造数据,表现为扣除道具 else -- 单抽不足,算出差价,表现为补齐单抽 local need_cost = single_ticket_price * (9 - single_ticket_num) self.cost_cache[pool_type][PsionicConst.DrawNumType.TenDraw] = {price_type = single_ticket_price_type, cost = need_cost} -- 构造数据,扣除金额 end -- 普通池子没抽奖券展示红钻 else lua_resM:setImageSprite(self, self[pt .. "_cost_icon2_img"], ten_cost_icon_asset, ten_cost_icon_name, true) local need_cost = single_ticket_price * 9 self[pt .. "_cost2_tmp"].text = need_cost self.cost_cache[pool_type][PsionicConst.DrawNumType.TenDraw] = {price_type = single_ticket_price_type, cost = need_cost} -- 构造数据,表现为扣除金额 end else -- 高级池子默认展示道具 -- 单抽 lua_resM:setImageSprite(self, self[pt .. "_cost_icon1_img"], "psionic_asset", "psionic_dig_icon"..pool_type.."_1", true) -- 免费单抽 if can_free then self[pt .. "_cost1_tmp"].text = "免费" self.cost_cache[pool_type][PsionicConst.DrawNumType.OneDraw] = {free = true} -- 构造数据,表现为扣除金额 -- 有抽奖券,则加载为抽奖券的图标 else self[pt .. "_cost1_tmp"].text = string.format("%s/1", single_ticket_num > 0 and ColorUtil.GREEN_DARK or ColorUtil.RED_DARK, single_ticket_num) if single_ticket_num > 0 then self.cost_cache[pool_type][PsionicConst.DrawNumType.OneDraw] = {goods_id = cfg_data.one} -- 构造数据,扣除道具 else self.cost_cache[pool_type][PsionicConst.DrawNumType.OneDraw] = {price_type = single_ticket_price_type, cost = single_ticket_price} -- 构造数据,扣除金额 end end -- 十连 -- 有十连券,就展示十连券的表现 if ten_ticket_num > 0 then lua_resM:setImageSprite(self, self[pt .. "_cost_icon2_img"], "psionic_asset", "psionic_dig_icon"..pool_type.."_2", true) self[pt .. "_cost2_tmp"].text = string.format("%s/1", ColorUtil.GREEN_DARK, ten_ticket_num) self.cost_cache[pool_type][PsionicConst.DrawNumType.TenDraw] = {goods_id = cfg_data.ten} -- 构造数据,表现为扣除道具 -- 有单抽券,则加载为单抽券,并显示出所需个数相关表现 else lua_resM:setImageSprite(self, self[pt .. "_cost_icon2_img"], "psionic_asset", "psionic_dig_icon"..pool_type.."_1", true) self[pt .. "_cost2_tmp"].text = string.format("%s/9", single_ticket_num >= 9 and ColorUtil.GREEN_DARK or ColorUtil.RED_DARK, single_ticket_num) if single_ticket_num >= 9 then -- 单抽券满足 self.cost_cache[pool_type][PsionicConst.DrawNumType.TenDraw] = {goods_id = cfg_data.one} -- 构造数据,表现为扣除道具 else local need_cost = single_ticket_price * (9 - single_ticket_num) self.cost_cache[pool_type][PsionicConst.DrawNumType.TenDraw] = {price_type = single_ticket_price_type, cost = need_cost} -- 构造数据,扣除金额 end end end end function PsionicDrawView:UpdateNormalFreeTimer(pool_type) local pool_data = self.model:GetPsionicDigData(pool_type) local cfg_data = pool_type == PsionicConst.DrawPoolType.Normal and self.normal_cfg or self.deep_cfg local tmp_component = pool_type == PsionicConst.DrawPoolType.Normal and self.normal_free_time_tmp or self.deep_free_time_tmp if pool_data and cfg_data then self:ClearFreeTimerByType(pool_type) -- 今日免费次数 if pool_data.remain > 0 then -- 免费文本额外内容 local extra_str = string.format("(%s/%s)", ColorUtil.GREEN_DARK, pool_data.remain, cfg_data.free) if pool_data.rtime <= TimeUtil:getServerTime() then -- 已经过了冷却时间 tmp_component.text = string.format("本日免费%s", extra_str) else local function free_timer() local left_time = pool_data.rtime - TimeUtil:getServerTime() if left_time > 0 then tmp_component.text = string.format("%s后免费%s", TimeUtil:convertTime(left_time), extra_str) else tmp_component.text = string.format("本日免费%s", extra_str) self:UpdateDrawCostDataByType(pool_type) -- 更新单抽花费信息 self:ClearFreeTimerByType(pool_type) end end self["free_timer_id" .. pool_type] = GlobalTimerQuest:AddPeriodQuest(free_timer, 0.5, -1) free_timer() end else tmp_component.text = "今日免费次数已用完" end -- 终生免费相关 if pool_type == PsionicConst.DrawPoolType.Deep then self.deep_first_tip_obj:SetActive(pool_data.isfirst == 0) end end end function PsionicDrawView:ClearFreeTimerByType(pool_type) if self["free_timer_id" .. pool_type] then GlobalTimerQuest:CancelQuest(self["free_timer_id" .. pool_type]) self["free_timer_id" .. pool_type] = nil end end -- 创建画面中的奖励浮动动画 function PsionicDrawView:CreateRewardsAnim( ) local function create_anim(index, node) cc.ActionManager:getInstance():removeAllActionsFromTarget(node) local node_pos = node.anchoredPosition -- 获取配置 local anim_cfg = PsionicConst.DrawAnimData[index] -- 随机抽取一个进度百分数和方向,然后从这个百分数开始播动画,制造错乱的感觉 local random_percentage = math_random(200, 800) * 0.001 local dir = math_random(0, 10) > 5 -- true为初始朝上,否则朝下 local pos_offset = (anim_cfg.end_pos - anim_cfg.start_pos) local pos_offset_per = pos_offset * random_percentage -- 设置起点 SetAnchoredPositionY(node, (dir and anim_cfg.start_pos or anim_cfg.end_pos) + (dir and pos_offset_per or -pos_offset_per)) local function begin_action_up_func(percent) SetAnchoredPositionY(node, anim_cfg.start_pos + (percent * (1 - random_percentage) + random_percentage) * pos_offset) end local function begin_action_down_func(percent) SetAnchoredPositionY(node, anim_cfg.end_pos - (percent * (1 - random_percentage) + random_percentage) * pos_offset) end -- 从随机点到目标点的动画 local action1 = cc.CustomUpdate.New(anim_cfg.anim_time * (1 - random_percentage), dir and begin_action_up_func or begin_action_down_func) -- 循环动画 local function action_begin_func() local function action_up_func(percent) SetAnchoredPositionY(node, anim_cfg.start_pos + percent * pos_offset) end local function action_down_func(percent) SetAnchoredPositionY(node, anim_cfg.end_pos - percent * pos_offset) end cc.ActionManager:getInstance():removeAllActionsFromTarget(node) local action_up = cc.CustomUpdate.New(anim_cfg.anim_time, action_up_func) local action_down = cc.CustomUpdate.New(anim_cfg.anim_time, action_down_func) local action_rep if dir then -- 初始方向是朝上的,循环动画就要从向下开始 action_rep = cc.RepeatForever.New(cc.Sequence.New(action_down, action_up)) else action_rep = cc.RepeatForever.New(cc.Sequence.New(action_up, action_down)) end cc.ActionManager:getInstance():addAction(action_rep, node) end local action_begin = cc.Sequence.New(action1, cc.CallFunc.New(action_begin_func)) cc.ActionManager:getInstance():addAction(action_begin, node) end for k, v in ipairs(self.anim_item_list) do create_anim(k, v) end end -- 抽奖按钮点击逻辑 pool_type:奖池类型 opty:次数类型 -- 消耗顺序:免费次数(单抽)> 单抽/十连券 > 红钻/彩钻 function PsionicDrawView:OnDrawBtnClick(pool_type, opty) local cost_data = self.cost_cache[pool_type][opty] if not cost_data then return end local price_asset, price_icon, price_color local price_type, cost_price local priceText = "" local insufficientText = nil local special_icon_data = nil local cfg_data = pool_type == PsionicConst.DrawPoolType.Normal and self.normal_cfg or self.deep_cfg local function ok_callback() self.model:Fire(PsionicConst.REQUEST_CCMD_EVENT, 13911, pool_type, opty) end if cost_data.free then -- 免费抽,直接冲 ok_callback() return end local cost_goods_name = self.goods_model:getGoodsName(cfg_data.one, true) -- 使用抽奖券的情况,提示后直接使用 if cost_data.goods_id then special_icon_data = true price_asset = "psionic_asset" price_icon = "psionic_dig_icon" .. pool_type .. "_" .. opty price_type = 0 if cost_data.goods_id == cfg_data.one then -- 单抽券需要区分抽奖次数,十连券每次消耗1张 cost_price = opty == PsionicConst.DrawNumType.TenDraw and 9 or 1 else cost_price = 1 end price_color = self.goods_model:GetGoodsColor(cost_data.goods_id) priceText = string.format("%s %s %s寻宝%s次?", price_color, cost_price, cost_goods_name, pool_type == PsionicConst.DrawPoolType.Deep and "深度" or "普通", opty == PsionicConst.DrawNumType.TenDraw and 10 or 1) else -- 需要使用钻石补足的情况 price_type = cost_data.price_type cost_price = cost_data.cost price_asset, price_icon = WordManager:GetCommonMoneyIcon(price_type) -- price_color = WordManager.GetGoodsColor(pool_type == PsionicConst.DrawPoolType.Deep and 4 or 3) price_color = ColorUtil.YELLOW_DARK if pool_type == PsionicConst.DrawPoolType.Deep then -- 统一用抽奖券表现 insufficientText = string.format("%s 不足", cost_goods_name) priceText = string.format("%s 补齐 %s %s寻宝%s次?", price_color, cost_price, cost_goods_name, pool_type == PsionicConst.DrawPoolType.Deep and "深度" or "普通", opty == PsionicConst.DrawNumType.TenDraw and 10 or 1) else priceText = string.format("%s %s寻宝%s次?", price_color, cost_price, pool_type == PsionicConst.DrawPoolType.Deep and "深度" or "普通", opty == PsionicConst.DrawNumType.TenDraw and 10 or 1) end end local function use_function( toggle_tip_data, call_fun_sum ) if not self.model._dont_check_psionic_draw_tip[pool_type] then GlobalEventSystem:Fire(EventName.OPEN_COM_TOGGLE_TIP_VIEW, toggle_tip_data) else call_fun_sum() end end local data = { insufficientText = insufficientText, special_icon_data = special_icon_data, gold_type = price_type, cost_price = cost_price, ok_callback = ok_callback, togglePriceStr = priceText, use_function = use_function, toggle_function = function(flag) self.model._dont_check_psionic_draw_tip[pool_type] = flag end, } CustomActivityModel:getInstance():BuyTips(data) end function PsionicDrawView:UpdateRedByPoolType(pool_type) local pt = self.pool_type_str[pool_type] local red_cache = self.model:GetPsionicDataDigPoolRedCache(pool_type) local single_red, ten_red if red_cache then single_red = red_cache.free_red or red_cache.single_red or false ten_red = red_cache.ten_red or false end self[pt.."_one_red_obj"]:SetActive(single_red) self[pt.."_ten_red_obj"]:SetActive(ten_red) end function PsionicDrawView:__delete( ) self:ClearFreeTimerByType(PsionicConst.DrawPoolType.Normal) self:ClearFreeTimerByType(PsionicConst.DrawPoolType.Deep) self:StopTransCFAlphaAnim() -- 销毁动画 for k, v in pairs(self.anim_item_list) do cc.ActionManager:getInstance():removeAllActionsFromTarget(v) end end