local UIInteractView = UIViewClass() local EventSystem = require('Event/EventSystem') local ItemIconPath = ConstAssetPathConfig.ItemIconPath1 local DEFAULT_PROGRESSBAR_PATH = ConstAssetPathConfig.DEFAULT_PROGRESSBAR_PATH local DEFAULT_BACKGROUND_PATH = ConstAssetPathConfig.DEFAULT_BACKGROUND_PATH local UIBtnDefaultPos = { [1] = {-8,30}, [2] = {-208,30}, [3] = {-108,-110}, [4] = {-108, 1.9}, [5] = {350, -100}, } function UIInteractView:OnOpened(context) self:RegistSystemEvent(EventID.UI_Interact_ShowUI,self.ShowBtn) self:RegistSystemEvent(EventID.UI_Interact_Break,self.BreakInteract) self:RegistSystemEvent(EventID.UI_Interact_Start,self.StartInteract) self:RegistSystemEvent(EventID.UI_Interact_Over,self.OnInteractFinish) self:RegistSystemEvent(EventID.UI_Interact_ClickFirstInteract,self.OnFirstButtonClick) self:RegistSystemEvent(EventID.UI_Interact_Fail,self.BreakInteract) self:InitBtn() if context then self:ShowBtn(context[1] ,context[2] ,context[3], context[4], context[5]) end self:SetPushStyle(LuaConst.PushStyleEnum.HIDE); end function UIInteractView:InitBtn() self.interactBtns = { [1] = { btnCollection = self.obj_Interact_1, interactIndex = nil, interactData = nil, animation = "UIAnimationBtn1", animationID = nil, active = false, }, [2] = { btnCollection = self.obj_Interact_2, interactIndex = nil, interactData = nil, animation = "UIAnimationBtn2", animationID = nil, active = false, }, [3] = { btnCollection = self.obj_Interact_3, interactIndex = nil, interactData = nil, animation = "UIAnimationBtn3", animationID = nil, active = false, }, [4] = { btnCollection = self.obj_Interact_4, interactIndex = nil, interactData = nil, animation = "UIAnimationBtn4", animationID = nil, active = false, }, [5] = { btnCollection = self.obj_Interact_5, interactIndex = nil, interactData = nil, animation = "UIAnimationBtn5", animationID = nil, active = false, }, } UIUtils.SetActive(self.obj_Interact_1.gameObject,false) UIUtils.SetActive(self.obj_Interact_2.gameObject,false) UIUtils.SetActive(self.obj_Interact_3.gameObject,false) UIUtils.SetActive(self.obj_Interact_4.gameObject,false) UIUtils.SetActive(self.obj_Interact_5.gameObject,false) end function UIInteractView:ShowBtn(isShow , interactData ,triggerEntity ,interactEntity, interactIndex) if not isShow then if self.interactEntity ~= interactEntity then if BattleDataGetter.GetCurEntityCurCmdInteractEntity() ~= self.interactEntity then local curEntityCurCmdInteractEntity = BattleDataGetter.GetCurEntityCurCmdInteractEntity() logging.crit(LTAG.CRIT, "关闭交互按钮时 ,发现交互物和界面记录的交互物不一致,则强制设置界面记录的交互物entity为当前执行cmd的交互物entity。交互物id:", interactEntity.EntityID,", 界面EntityID:", self.interactEntity and self.interactEntity.EntityID or nil, ", 逻辑ID :", curEntityCurCmdInteractEntity and curEntityCurCmdInteractEntity.EntityID or nil ) -- 逻辑里面有控制当前的交互物了 ,这个 self.interactEntity 已经没必要留着了,但是为了减少工作量,冗余代码就先不动了。 self.interactEntity = BattleDataGetter.GetCurEntityCurCmdInteractEntity() end --return end if interactIndex == nil then -- interactIndex 空 ,关闭所有按钮,按钮信息恢复到初始化状态 logging.crit(LTAG.CRIT, "[gws]: 关闭交互按钮时 ,要关闭的按钮index = nil ,强制关闭所有按钮!") self:InitBtn() return end local btn = self.interactBtns[interactData.BtnPostion] -- 举例:打开按钮的条件为false未打开,但是关闭依然要执行。这样做的目的:防止打开时条件为true,要关闭时条件变为false,则按钮会关不掉。故打开又判断,关闭一定尝试执行。 if btn.active and btn.interactData.ID == interactData.ID then -- 如果不相等 ,说明关闭这个交互按钮时,这按钮已经被其他的交互功能使用了即覆盖了原有交互,则不要关这个按钮 if btn.interactData and btn.interactData.ShowInteractIconEffect then self:StopUIAnimation(btn.animationID) if btn.btnCollection.effect_Icon then UIUtils.SetActive(btn.btnCollection.effect_Icon, false) end end UIUtils.SetActive(btn.btnCollection.gameObject,false) btn.interactData = nil btn.interactIndex = nil UIUtils.SetAtlasImageFillAmount(btn.btnCollection.sprite_InteractProgress,0) btn.active = false end if interactIndex == self.interactingIndex then -- 关闭的按钮是正在交互中的,则停止交互 self.duration = nil self.interactTimer = nil self.interactingIndex = nil self.curInteractingBtn = nil end --如果所有按钮关闭 ,则可显示与其他交互物的按钮 local allDisActive = true for _ ,btn in ipairs(self.interactBtns) do if btn.active then allDisActive = false break end end if allDisActive then self.interactEntity = nil end else if self.interactEntity and self.interactEntity ~= interactEntity then -- 如果已经有显示交互的信息后 ,且不是同一个交互物,就不显示其他交互 。以后策划有需求 ,要同时交互多个entity的话 ,需要修改。 self:InitBtn() -- 关闭之前的所有交互 end self.interactEntity = interactEntity local btn = self.interactBtns[interactData.BtnPostion] UIUtils.SetActive(btn.btnCollection.gameObject,true) btn.interactData = interactData btn.interactIndex = interactIndex btn.active = true --5 是捕捉宠物 当按钮出现 播放声音 if interactData.BtnPostion == 5 then CTL.Sound.PlayGameSound("UI_flower") end -- 交互按钮 UIUtils.SetImageByAssetBundle(btn.btnCollection.sprite_Interact , string.format(ItemIconPath,interactData.InteractIcon)) -- 进度条 local progressBarPath = string.isNilOrEmpty(interactData.InteractingBar) and DEFAULT_PROGRESSBAR_PATH or string.format(ItemIconPath, interactData.InteractingBar) UIUtils.SetImageByAssetBundle(btn.btnCollection.sprite_InteractProgress , progressBarPath) UIUtils.SetAtlasImageFillAmount(btn.btnCollection.sprite_InteractProgress,0) -- 背景底图 local interactBg = string.isNilOrEmpty(interactData.ShowInteractBg) and DEFAULT_BACKGROUND_PATH or string.format(ItemIconPath, interactData.ShowInteractBg) UIUtils.SetImageByAssetBundle(btn.btnCollection.bg, interactBg) -- 4\5号自定义btn装饰显示 if btn.state_Marks then btn.state_Marks:SetState(0) end btn.btnCollection.state:SetState(0) UIUtils.SetLabel(btn.btnCollection.desc,string.FormatByIdsId(Const_IDS[btn.interactData.InteractIconTipsIDS], BattleDataGetter.GetEntityName(self.interactEntity))) if btn.interactData.ShowInteractIconEffect then if btn.animationID then self:PlayUIAnimation(btn.animationID) else btn.animationID = self:PlayUIAnimation(btn.animation) end if btn.btnCollection.effect_Icon then UIUtils.SetActive(btn.btnCollection.effect_Icon, true) end end -- 设置btn位置和scale if interactData.BtnUISetting and #interactData.BtnUISetting >= 3 then local posX = interactData.BtnUISetting[1] == 0 and UIBtnDefaultPos[interactData.BtnPostion][1] or interactData.BtnUISetting[1] local posY = interactData.BtnUISetting[2] == 0 and UIBtnDefaultPos[interactData.BtnPostion][2] or interactData.BtnUISetting[2] local scale = interactData.BtnUISetting[3] == 0 and 1 or interactData.BtnUISetting[3] UIUtils.SetLocalPosition(btn.btnCollection.gameObject,posX,posY,0) btn.btnCollection.gameObject.transform:SetLocalScale(scale, scale, 1) end end if interactData.InteractEffect == LuaConst.InteractSpecialID.Drive then EventSystem.Dispatch(EventID.UI_ShowGuildDriveBtn, isShow) end end function UIInteractView:OnButtonClicked(comp) if self.duration or self.interactingIndex then -- 交互中禁止再次点击 return end local btn = self:GetBtnByComp(comp) if comp == btn.btnCollection.btn_Interact then LuaInputManager.SetSpecialCommand(LuaConst.SpecialCommandType.Interact) LuaInputManager.SetMoveType(btn.interactIndex + LuaConst.CmdMoveTypeBaseEnum.InteractBase) self.interactingIndex = btn.interactIndex self.curInteractingBtn = btn end end function UIInteractView:OnFirstButtonClick() if self.duration or self.interactingIndex then -- 交互中禁止再次点击 return end local btn = self.interactBtns[1] if btn and btn.interactIndex ~= nil then LuaInputManager.SetSpecialCommand(LuaConst.SpecialCommandType.Interact) LuaInputManager.SetMoveType(btn.interactIndex + LuaConst.CmdMoveTypeBaseEnum.InteractBase) self.interactingIndex = btn.interactIndex self.curInteractingBtn = btn end end function UIInteractView:GetBtnByComp(comp) for _, btn in ipairs(self.interactBtns) do if btn.btnCollection.btn_Interact == comp then return btn end end end function UIInteractView:StartInteract(entity, interactIndex) local btn = self.curInteractingBtn if btn == nil then -- 当时AI自动采集时,由于未点击按钮,所以这个btn为空 for _, bt in ipairs(self.interactBtns) do if bt.interactIndex == interactIndex then btn = bt self.curInteractingBtn = btn self.interactingIndex = interactIndex break; end end end if btn and btn.interactData.InteractTime > 0 then self.duration = btn.interactData.InteractTime / 1000 self.interactTimer = 0 -- 交互中图片 UIUtils.SetImageByAssetBundle(btn.btnCollection.sprite_Interacting, string.format(ItemIconPath,btn.interactData.InteractingIcon)) -- 交互中的背景 local interactBg = string.isNilOrEmpty(btn.interactData.InteractingBg) and DEFAULT_BACKGROUND_PATH or string.format(ItemIconPath, btn.interactData.InteractingBg) UIUtils.SetImageByAssetBundle(btn.btnCollection.bg, interactBg) if btn.interactData.BtnPostion == 4 then -- 4号自定义btn装饰显示 btn.btnCollection.state_Marks:SetState(1) end btn.btnCollection.state:SetState(1) self:StartRepeatFunc(self.OnUpdateFun, 0) UIUtils.SetLabel(btn.btnCollection.desc,string.FormatByIdsId(Const_IDS[btn.interactData.InteractTipsIDS], BattleDataGetter.GetEntityName(self.interactEntity))) if btn.interactData.ShowInteractIconEffect then self:StopUIAnimation(btn.animationID) if btn.btnCollection.effect_Icon then UIUtils.SetActive(btn.btnCollection.effect_Icon, false) end end end end --gws: 交互行为完成后 ,发送过来的事件(此时逻辑中的交互数据已经清空) function UIInteractView:OnInteractFinish() self.duration = nil self.interactTimer = nil local curBtn = self.curInteractingBtn if curBtn then UIUtils.SetAtlasImageFillAmount(curBtn.btnCollection.sprite_InteractProgress,0) curBtn.btnCollection.state:SetState(0) if curBtn.interactData.ShowInteractIconEffect then if curBtn.animationID then self:PlayUIAnimation(curBtn.animationID) else curBtn.animationID = self:PlayUIAnimation(curBtn.animation) end if curBtn.btnCollection.effect_Icon then UIUtils.SetActive(curBtn.btnCollection.effect_Icon, true) end end end self:StopRepeatFunc(self.OnUpdateFun) self.interactingIndex = nil self.curInteractingBtn = nil end function UIInteractView:OnUpdateFun(deltaTime, timeSinceUpdate) if not self.interactTimer then return end if self.interactTimer <= self.duration then self.interactTimer = timeSinceUpdate UIUtils.SetAtlasImageFillAmount(self.curInteractingBtn.btnCollection.sprite_InteractProgress,self.interactTimer/self.duration) end end -- 中断交互行为(在最后交互成功也会执行这代码) function UIInteractView:BreakInteract(entity) if entity ~= LogicUpdateManager._curEntity then return end self.duration = nil self.interactTimer = nil if self.curInteractingBtn then -- 如果最后成功了 ,这当前正在执行按钮就是空,不用再设置 UIUtils.SetAtlasImageFillAmount(self.curInteractingBtn.btnCollection.sprite_InteractProgress,0) self.curInteractingBtn.btnCollection.state:SetState(0) UIUtils.SetLabel(self.curInteractingBtn.btnCollection.desc,string.FormatByIdsId(Const_IDS[self.curInteractingBtn.interactData.InteractIconTipsIDS], BattleDataGetter.GetEntityName(self.interactEntity))) if self.curInteractingBtn.interactData.ShowInteractIconEffect then if self.curInteractingBtn.animationID then self:PlayUIAnimation(self.curInteractingBtn.animationID) else self.curInteractingBtn.animationID = self:PlayUIAnimation(self.curInteractingBtn.animation) end if self.curInteractingBtn.btnCollection.effect_Icon then UIUtils.SetActive(self.curInteractingBtn.btnCollection.effect_Icon, true) end end end self:StopRepeatFunc(self.OnUpdateFun) self.interactingIndex = nil self.curInteractingBtn = nil end function UIInteractView:OnFocusChanged(value) UIUtils.SetActive(self.gameObject, value) end return UIInteractView