--[[ 宠物捕捉弹窗 ]]-- local EventSystem = require('Event/EventSystem') local CLOSE_DURATION = DataSheetManager.ConstData:GetDataByID("PetCatchTime").IntValue local UPDATE_TIME_INTERVAL = 0.02 local OFFSET_MOVE = 10 local ITEM_CONSUME_ID = DataSheetManager.ConstData:GetDataByID("PetCatchCost").IntValue local UIPetCatchPopUp = UIViewClass() local RANDOM_BAR_MIN = 80 local RANDOM_BAR_MAX = 125 local OFFSET_RANDOM = 60 --随机范围 function UIPetCatchPopUp:OnCreated() self.moveBarTrans = self.img_CatchMoveBar.transform self.arrowTrans = self.img_Arrow.transform self.modelComponentDataHandle = UTL.UIRender.CreateModelComponentDatas(self.model_Show) end function UIPetCatchPopUp:OnOpened(context) UIManager.ActiveUIModle(false, "UIPetCatchPopUp") self:RegistSystemEvent(EventID.BagItem_Update, self.OnItemUpdate) self:RegistSystemEvent(EventID.UI_Pet_CatchReStart, self.BeginMoveExp) self:RegistSystemEvent(EventID.UI_CloseAndRecoverView, self.OnCloseAndRecoverView) self:SetPushStyle(LuaConst.PushStyleEnum.HIDE) self.variationState = context.variationState self.petTypeID = context.petTypeID self.petSex = context.petSex self.petPedigree = context.petPedigree self.generation = context.generation self.petConfig = DataSheetManager.PetData:GetDataByID_Stage(self.petTypeID, self.variationState) self:InitMoveParam() self:RefreshPanel() self:LoadModelShow() self:BeginMoveExp() end function UIPetCatchPopUp:OnBackButtonPressed() self:OnButtonClicked(self.btn_Close) end function UIPetCatchPopUp:OnButtonClicked(comp) if comp == self.btn_Sure then self:OnClickCatch() elseif comp == self.btn_Close then self:CloseView() elseif comp == self.btn_Plus then UTL.ItemTips.ShowCommonTipsById(ITEM_CONSUME_ID) EventSystem.Dispatch(EventID.UI_ItemTips_ShowItemSource) end end function UIPetCatchPopUp:OnClosed() UIManager.ActiveUIModle(true, "UIPetCatchPopUp") UTL.UIRender.DisposeModelComponentData(self.modelComponentDataHandle) MDL.Pet.StartCapturePetReq(MDL.Pet.CatchOptType.Release) end function UIPetCatchPopUp:LoadModelShow() UTL.UIRender.CreateRTSceneTrans(self.modelComponentDataHandle, self.petConfig.PetModelLight) local modelPath, modelScale, showEquipmentID, renderID, idleAnimCilp, heroEffectPath, psEffectPath, modelXYZ, modelRot = CTL.Pet.GetRenderTextureModelParam(self.petConfig, self.__viewClassPath) UTL.UIRender.CreateRTRenderModel(self.modelComponentDataHandle, modelPath, modelScale, showEquipmentID, renderID, idleAnimCilp, heroEffectPath, psEffectPath) UTL.UIRender.SetRTRootParam(self.modelComponentDataHandle, modelXYZ, modelRot) end function UIPetCatchPopUp:InitMoveParam() --总长度 self.barWidth = self.img_CatchBar.rectTransform:GetSizeDelta() --箭头移动范围 self.arrowMinPosX = 0 self.arrowMaxPosX = self.barWidth --箭头移动方向(-1左 1右) self.arrowMoveDir = 1 --箭头当前位置(减少从transform去读取) self.arrowCurPosX = 0 self.arrowTrans:SetAnchoredPosition(self.arrowCurPosX, nil) end function UIPetCatchPopUp:RandomSetMoveBarPos() self.moveBarWidth = math.random(RANDOM_BAR_MIN, RANDOM_BAR_MAX) self.moveBarTrans:SetSizeDelta(self.moveBarWidth, nil) self.moveBarMaxPosX = self.barWidth - self.moveBarWidth local randomPosX = math.random(OFFSET_RANDOM, self.moveBarMaxPosX - OFFSET_RANDOM) self.moveBarTrans:SetAnchoredPosition(randomPosX, nil) end function UIPetCatchPopUp:UpdateArrowPos() local targetPosX = self.arrowCurPosX + self.arrowMoveDir * OFFSET_MOVE targetPosX = math.max(math.min(targetPosX, self.arrowMaxPosX), self.arrowMinPosX) self.arrowTrans:SetAnchoredPosition(targetPosX, nil) self.arrowCurPosX = targetPosX if targetPosX >= self.arrowMaxPosX then self.arrowMoveDir = -1 elseif targetPosX <= self.arrowMinPosX then self.arrowMoveDir = 1 end end function UIPetCatchPopUp:GetIfCatchSuccess() local moveBarPosX = self.moveBarTrans:GetAnchoredPosition() if self.arrowCurPosX >= moveBarPosX and self.arrowCurPosX <= moveBarPosX + self.moveBarWidth then return true else return false end end function UIPetCatchPopUp:BeginMoveExp() self:RandomSetMoveBarPos() self:StartRepeatFunc(self.UpdateArrowPos, UPDATE_TIME_INTERVAL) self:StartRepeatFunc(self.CloseWhenTimeEnd, CLOSE_DURATION, 1) end function UIPetCatchPopUp:RefreshPanel() UIUtils.SetLabelText(self.group_Name.txt_Name, Const_IDS[self.petConfig.PetName]) UIUtils.SetLabel(self.group_Name.txt_Type, CTL.Pet.GetGenerationTxt(self.generation)) self:OnItemUpdate() end function UIPetCatchPopUp:OnItemUpdate() self.catchConsumeCount = MDL.Bag.GetItemCount(ITEM_CONSUME_ID) UTL.Item.SetItemIcon(self.img_Icon, ITEM_CONSUME_ID) if self.catchConsumeCount >= 1 then UIUtils.SetLabel(self.txt_Consume, UTL.Color.FormatGreenColor(self.catchConsumeCount .. "/" .. 1)) else UIUtils.SetLabel(self.txt_Consume, UTL.Color.FormatRedColor(self.catchConsumeCount .. "/" .. 1)) end end function UIPetCatchPopUp:OnClickCatch() if self.catchConsumeCount <= 0 then CTL.Tips.AddTipsMsg(Const_IDS.IDS_PetCatch11) else local isSuccess = self:GetIfCatchSuccess() if MDL.Pet.CapturePetReq(isSuccess, self.petTypeID, self.variationState) then self:StopRepeatFunc(self.UpdateArrowPos) self:StopRepeatFunc(self.CloseWhenTimeEnd) end end end function UIPetCatchPopUp:CloseWhenTimeEnd() self:CloseView() end return UIPetCatchPopUp