local UIVirtualController = UIViewClass() local EventSystem = require('Event/EventSystem') local LuaConst=LuaConst local VECTORFAR = Vector3.New(9999, 9999, 0) local MaxRadius = 110 local HalfMaxRadius = 80 local Vector3Up = Vector3.up local Vector3Zero = Vector3.New(0, 0, 0) local ColorHalf = Color.New(1,1,1,0.5) local ColorOne = Color.New(1,1,1,1) local StartPntX = 0 local StartPntY = 0 local StartPntZ = 0 local StartTouchRelativePos = Vector3.New(0, 0, 0) local Vector3TempDir = Vector3.New(0, 0, 0) local Quaternion=Quaternion local KeyBoardDragRadius=40 local VECTORINIT = { x = 400,y = 250,z = 0 } --初始拖动距离跟MaxRadius的比值小于这个值,不移动 local torleranceRatio = 0.08 function UIVirtualController:OnCreated() self.thisTrans=self.gameObject.transform --以下为变量 self.sprite_Fore_Transform=self.sprite_Fore.transform self.sprite_Back_Transform=self.sprite_Back.transform self.sprite_Direction_Transform=self.sprite_Direction.transform self.curEventPos=Vector3.New(0,0,0) self.KeyboardEventData= { position = Vector3.New(0,0,0) } self.isCheatModel = UIModelComponent.IsCheatModel self.skillJoyStickData = nil -- 控制技能摇杆的技能摇杆数据 self.CameraCtrl = CameraController.Instance self.isShowCameraChange = true self.callBack_PatternChange = function(show,free) self:SetCameraChangeButton(show,free) end self.isFocus = false end function UIVirtualController:OnOpened() self.Vector3Temp = Vector3.New(0, 0, 0) self.moving = false self.alwaysShowStick = false self.needToWorldDir = true -- 需要转换为世界朝向 if self.alwaysShowStick then self:ControllerSetActive(true); UIUtils.SetImageColor(self.sprite_Back , ColorHalf) UIUtils.SetImageColor(self.sprite_Fore , ColorHalf) else self:ControllerSetActive(false); UIUtils.SetImageColor(self.sprite_Back , ColorOne) UIUtils.SetImageColor(self.sprite_Fore , ColorOne) end self.sprite_Direction_Transform:SetLocalPosition(VECTORFAR.x, VECTORFAR.y, VECTORFAR.z) if LogicUpdateManager._curEntity then local carEntity = LogicUpdateManager._curEntity.components.drive:GetDrivedEntity() if carEntity then self:DriveCar(carEntity, LogicUpdateManager._curEntity) end end self:UpdateHorseBtnState() --event self:RegistSystemEvent(EventID.TimeLine_Update, self.OnTimeLine) self:RegistTriggerEvent(self.event_Collider,UIEventTriggerType.PointerDown) self:RegistTriggerEvent(self.event_Collider, UIEventTriggerType.Drag) self:RegistTriggerEvent(self.event_Collider, UIEventTriggerType.EndDrag) self:RegistTriggerEvent(self.event_Collider,UIEventTriggerType.PointerUp) self:RegistSystemEvent(EventID.UI_OnHorse, self.OnHorse) self:RegistSystemEvent(EventID.Player_DriveEntity, self.DriveCar) self:RegistSystemEvent(EventID.Player_OffDriveEntity, self.OffCar) self:RegistSystemEvent(EventID.UI_VirtualJoystick_Show, self.ShowJoystick) self:RegistSystemEvent(EventID.FunctionUnlockUpdate, self.UpdateHorseBtnState) self:RegistSystemEvent(EventID.Level_HideHorseInput, self.HideHorseBtn) self:RegistSystemEvent(EventID.UI_KeyboardControl_MoveDir, self.SetMovingDir) self:RegistSystemEvent(EventID.UI_KeyboardControl_MoveRelease, self.EndMovingDir) self:RegistTriggerEvent(self.event_Accelerator,UIEventTriggerType.PointerDown) self:RegistTriggerEvent(self.event_Accelerator,UIEventTriggerType.PointerUp) self:RegistSystemEvent(EventID.UI_EnterCheatModel, self.EnterCheatModel) self:RegistSystemEvent(EventID.UIBattle_MainPlayerAutoFightChanged, self.OnAutoFightStateChanged) self:RegistSystemEvent(EventID.UIBattle_SkillBtn_InitStaticJoy,self.ShowStaticJoySkillRange) self:RegistSystemEvent(EventID.Camera_TargetChangePattern, self.OnCameraTargetPatternChange) self:RegistSystemEvent(EventID.Camera_TargetEnd, self.OnCameraTargetEnd) self.CameraCtrl:AddPatternCallBack(self.callBack_PatternChange) self.CameraCtrl:RefreshPatternSystem() end function UIVirtualController:UpdateHorseBtnState() if LevelManager.IsMonsterTest() then -- 实验室强制开骑马 UIUtils.SetActive(self.event_Horse,true) UIUtils.SetActive(self.sprite_HorseOn,true) UIUtils.SetActive(self.sprite_HorseOff,false) return end if LevelManager.CurLevelCanRideHorse() and MDL.Unlock.IsUnlock(MDL.Unlock.FunctionType.WarHorse) then UIUtils.SetActive(self.event_Horse,true) local isRide = LogicUpdateManager._curEntity and LogicUpdateManager._curEntity.components.buff:CheckHasBuffState(LuaConst.SpecialBuffs.InRiding) or false if isRide then UIUtils.SetActive(self.sprite_HorseOn,false) UIUtils.SetActive(self.sprite_HorseOff,true) else UIUtils.SetActive(self.sprite_HorseOn,true) UIUtils.SetActive(self.sprite_HorseOff,false) end else UIUtils.SetActive(self.event_Horse,false) end end function UIVirtualController:HideHorseBtn(hide) if hide then UIUtils.SetActive(self.event_Horse,false) else self:UpdateHorseBtnState() end end function UIVirtualController:OnTimeLine(value) if value then self:EnableInput() else self:DisableInput() end end function UIVirtualController:OnClosed() self:Stop() self.CameraCtrl:RemovePatternCallBack(self.callBack_PatternChange) end function UIVirtualController:OnFocusChanged(value) if value then self.isFocus=true else self:Stop() self.isFocus=false end end --region --************** Move **************-- function UIVirtualController:Move(eventDataPos) --region 拦截条件 --判断界面isFocus if not self.isFocus and not self.isCheatModel then return end --判断角色存活 local curEntity = BattleDataGetter.GetCurEntity() if not curEntity or not curEntity:IsAlive() then return end --判断组队跟随中 if MDL.Team.IsFollowing() then CTL.Tips.AddTipsMsg(Const_IDS["IDS_Team_FollowCanNotDoThis"]) return end --endregion 拦截条件 if BattleDataGetter.IsOnlineTownLevel() and BattleDataGetter.GetAutoFindPathInTownState() then NotSyncBattleSetter.StopAutoFightPathInTown() end EventSystem.Dispatch(EventID.UI_VirtualController_Move) if not self.moving then self.moving = true if self.alwaysShowStick then UIUtils.SetImageColor(self.sprite_Fore,ColorOne) UIUtils.SetImageColor(self.sprite_Back,ColorOne) end self:StartRepeatFunc(self.OnLateUpdateFun, 0, -1, false) else self:Moving(eventDataPos) end end function UIVirtualController:ShowStick(eventDataPos) --判断界面isFocus if not self.isFocus and not self.isCheatModel then return end --判断角色存活 local curEntity = BattleDataGetter.GetCurEntity() if not curEntity or not curEntity:IsAlive() then return end local touchPos if eventDataPos then touchPos = eventDataPos else touchPos = UnityEngine.Input.mousePosition end StartPntX,StartPntY,StartPntZ = self:GetPositionBasedOnWidth(touchPos) self.sprite_Back_Transform:SetLocalPosition(StartPntX,StartPntY,StartPntZ) self.sprite_Fore_Transform:SetLocalPosition(Vector3Zero.x,Vector3Zero.y,Vector3Zero.z) if self.skillJoyStickData then -- 控制技能时 EventSystem.Dispatch(EventID.UIBattle_StaticSkillJoy_PointerDown) EventSystem.Dispatch(EventID.UIBattle_StaticSkillJoy_Drag, self.event_Collider, {position = touchPos}, self.skillJoyStickData, curEntity:GetSkillData(self.skillJoyStickData.ID)) end end function UIVirtualController:Stop() local curEntity = BattleDataGetter.GetCurEntity() if not curEntity or not curEntity:IsAlive() then return end LuaInputManager.SetVirtualMoveDir(0,0); LuaInputManager.SetVirtualMoveHalf(false) self.moving = false self.moved = false self.sprite_Direction_Transform:SetLocalPosition(VECTORFAR.x, VECTORFAR.y, VECTORFAR.z) if self.alwaysShowStick then self.sprite_Back_Transform:SetAnchoredPosition(VECTORINIT.x, VECTORINIT.y) self.sprite_Fore_Transform:SetLocalPosition(Vector3Zero.x,Vector3Zero.y,Vector3Zero.z) UIUtils.SetImageColor(self.sprite_Back ,ColorHalf) UIUtils.SetImageColor(self.sprite_Fore , ColorHalf) else self:ControllerSetActive(false) end self:StopRepeatFunc(self.OnLateUpdateFun) if self.skillJoyStickData then -- 控制技能时 EventSystem.Dispatch(EventID.UIBattle_StaticSkillJoy_EndDrag, nil, nil, self.skillJoyStickData) EventSystem.Dispatch(EventID.UIBattle_StaticSkillJoy_PointerUp) end end function UIVirtualController:GetPositionBasedOnWidth(screenPosition) local vector3TempX,vector3TempY,vector3TempZ = UIUtils.GetPositionBasedOnWidth(self.thisTrans,screenPosition,nil,nil,nil) self.Vector3Temp:Set(vector3TempX, vector3TempY, vector3TempZ) return vector3TempX,vector3TempY,vector3TempZ end function UIVirtualController:Moving(touchPos) if self.moving then local curPosX,curPosY,curPosZ = self:GetPositionBasedOnWidth(touchPos) Vector3TempDir:Set(curPosX - StartPntX, curPosY - StartPntY, curPosZ - StartPntZ) local dist = (Vector3TempDir - StartTouchRelativePos).magnitude self.Vector3Temp:Set(Vector3TempDir.x,Vector3TempDir.y,Vector3TempDir.z) self.Vector3Temp:SetNormalize():Mul(math.min(dist, MaxRadius)) self.sprite_Fore_Transform:SetLocalPosition(self.Vector3Temp.x, self.Vector3Temp.y, self.Vector3Temp.z) local halfDis = dist < HalfMaxRadius if self.antiShake then local tan = Vector3TempDir.x / Vector3TempDir.y if math.abs(tan) < 0.06 then -- 0.176 = tan(10) --0.06 =tan(3.5) Vector3TempDir.x = 0 end end if self.needToWorldDir then local vector3TempX, vector3TempY,vector3TempZ = UIUtils.CameraTransformDirection(Vector3TempDir.x,0,Vector3TempDir.y,nil,nil,nil) self.Vector3Temp:Set(vector3TempX, vector3TempY,vector3TempZ) if self.moved or dist >= MaxRadius * torleranceRatio then self.moved = true LuaInputManager.SetVirtualMoveDir(self.Vector3Temp.normalized.x, self.Vector3Temp.normalized.z) LuaInputManager.SetVirtualMoveHalf(halfDis) self:UpdateDirectionSprite(Vector3TempDir) end else if self.moved or dist >= MaxRadius * torleranceRatio then self.moved = true LuaInputManager.SetVirtualMoveDir(self.Vector3Temp.normalized.x, self.Vector3Temp.normalized.y) LuaInputManager.SetVirtualMoveHalf(halfDis) self:UpdateDirectionSprite(Vector3TempDir) end end end end function UIVirtualController:SetMovingDir(dx,dy) if self.skillJoyStickData then if not self.moving then self.KeyboardEventData.position.x = 200 self.KeyboardEventData.position.y = 200 self:ShowStick(self.KeyboardEventData.position) else self.KeyboardEventData.position.x = dx * KeyBoardDragRadius * 2 + 200 self.KeyboardEventData.position.y = dy * KeyBoardDragRadius * 2 + 200 EventSystem.Dispatch(EventID.UIBattle_StaticSkillJoy_Drag, self.event_Collider, self.KeyboardEventData, self.skillJoyStickData, BattleDataGetter.GetCurSkillUIEntity():GetSkillData(self.skillJoyStickData.ID)) end else self.KeyboardEventData.position.x = 200 self.KeyboardEventData.position.y = 200 self:ShowStick(self.KeyboardEventData.position) EventSystem.Dispatch(EventID.UI_Guide_VirtualJoystick_PointerDown) end self.curEventPos:Set(dx * KeyBoardDragRadius + 200, dy * KeyBoardDragRadius + 200, 0) self:Move(self.curEventPos) end function UIVirtualController:EndMovingDir() self:Stop() end function UIVirtualController:UpdateDirectionSprite(dir) if dir.x == 0 and dir.y == 0 then self.sprite_Direction_Transform:SetLocalPosition(VECTORFAR.x, VECTORFAR.y, VECTORFAR.z) return end self.Vector3Temp:Set(dir.x,dir.y,0) local q = Quaternion.FromToRotation(Vector3Up, self.Vector3Temp):ToEulerAngles() self.sprite_Direction_Transform:SetLocalEulerAngles(q.x,q.y,q.z) self.sprite_Direction_Transform:SetLocalPosition(0,0,0) end function UIVirtualController:ControllerSetActive(isActive) local vector3 = isActive and VECTORINIT or VECTORFAR self.sprite_Back_Transform:SetAnchoredPosition(vector3.x, vector3.y, vector3.z) end --endregion --************** Move **************-- function UIVirtualController:OnButtonClicked(comp) if comp==self.event_CameraChange then self.CameraCtrl:ChangeFreePattern(false) self.CameraCtrl:SetRecordFreeInfo() elseif comp == self.event_Horse then self.OnClickHorse() end end function UIVirtualController:OnPointerDown(comp, eventData) if comp == self.event_Collider then self:ShowStick(eventData.position) EventSystem.Dispatch(EventID.UI_Guide_VirtualJoystick_PointerDown) elseif comp == self.event_Accelerator then LuaInputManager.StepOnAccelerator() end end function UIVirtualController:OnDrag(comp, eventData) if comp == self.event_Collider then self.curEventPos:Set(eventData.position.x, eventData.position.y, eventData.position.z) self:Move(eventData.position) if self.skillJoyStickData then EventSystem.Dispatch(EventID.UIBattle_StaticSkillJoy_Drag, comp, eventData, self.skillJoyStickData, BattleDataGetter.GetCurSkillUIEntity():GetSkillData(self.skillJoyStickData.ID)) end end end function UIVirtualController:OnLateUpdateFun(deltaTime, timeSinceUpdate) if self.curEventPos then self:Move(self.curEventPos) end end function UIVirtualController:OnEndDrag(comp, eventData) if comp == self.event_Collider then self:Stop() end end function UIVirtualController:OnPointerUp(comp) if comp == self.event_Collider then self:Stop() EventSystem.Dispatch(EventID.UI_Guide_VirtualJoystick_PointerUp) elseif comp == self.event_Accelerator then LuaInputManager.ReleaseAccelerator() end end function UIVirtualController:EnableInput() UIUtils.SetActive(self.event_Collider,true) end function UIVirtualController:DisableInput() UIUtils.SetActive(self.event_Collider,false) self:Stop(nil) end function UIVirtualController:OnHorse(entity,isOn) if not entity:IsPlayerSelf() then return end --关卡判断 if not LevelManager.CurLevelCanRideHorse() then return end if isOn then UIUtils.SetActive(self.sprite_HorseOn,false) UIUtils.SetActive(self.sprite_HorseOff,true) else UIUtils.SetActive(self.sprite_HorseOn,true) UIUtils.SetActive(self.sprite_HorseOff,false) end end --骑马 function UIVirtualController:OnClickHorse() if MDL.Team.IsFollowing() then --组队跟随中不可操作 CTL.Tips.AddTipsMsg(Const_IDS["IDS_Team_FollowCanNotDoThis"]) return end if MDL.WarHorse.GetIsActiveHorse() == false then CTL.Tips.AddTipsMsg(Const_IDS["IDS_Horse_Town_2"]) return end if LogicUpdateManager._curEntity._logicInitData.HorseModelData == nil then CTL.Tips.AddTipsMsg(Const_IDS["IDS_Horse_Town_5"]) return end if BattleDataGetter.CheckMainPlayerHasBuffState(LuaConst.SpecialBuffs.InRiding) then CTL.Sound.PlayGameSound("UI_close") else if not BattleDataGetter.CheckMainPlayerHasBuffState(LuaConst.SpecialBuffs.CanRide) then CTL.Tips.AddTipsMsg(Const_IDS["IDS_CanNotRideHorse"]) return end CTL.Sound.PlayGameSound("UI_horse_call") end LuaInputManager.SetSpecialCommand(LuaConst.SpecialCommandType.Ride) end --上车 function UIVirtualController:DriveCar(carEntity,driverEntity) if driverEntity ~= LogicUpdateManager._curEntity then return end local driveData = carEntity.components.drive:GetDriveData() if driveData and driveData.IsAntiShake then self.antiShake = true end if not carEntity.components.drive.IsFirstPerspective then return end self.alwaysShowStick = true self.needToWorldDir = false UIUtils.SetImageColor(self.sprite_Back, ColorHalf) UIUtils.SetImageColor(self.sprite_Fore, ColorHalf) self.sprite_Back_Transform:SetAnchoredPosition(VECTORINIT.x, VECTORINIT.y) self.sprite_Fore_Transform:SetLocalPosition(Vector3Zero.x,Vector3Zero.y,Vector3Zero.z) if LevelManager.CurLevelCanRideHorse() then UIUtils.SetActive(self.event_Horse,false) end local realSpeed = carEntity.components.attr:GetAttrValue(LuaConst.AttrType.spd) if realSpeed > FP.Const.Zero then UIUtils.SetActive(self.event_Accelerator,true) end end function UIVirtualController:ShowJoystick() self:ControllerSetActive(true) end --下车 function UIVirtualController:OffCar(entity ,carEntity) if entity ~= LogicUpdateManager._curEntity then return end if not carEntity.components.drive.IsFirstPerspective then return end self.antiShake = false self.alwaysShowStick = false self.needToWorldDir = true UIUtils.SetActive(self.event_Accelerator,false) UIUtils.SetImageColor(self.sprite_Back , ColorOne) UIUtils.SetImageColor(self.sprite_Fore , ColorOne) self.sprite_Back_Transform:SetLocalPosition(VECTORFAR.x, VECTORFAR.y, VECTORFAR.z) if LevelManager.CurLevelCanRideHorse() and MDL.Unlock.IsUnlock(MDL.Unlock.FunctionType.WarHorse) then UIUtils.SetActive(self.event_Horse,true) end end function UIVirtualController:EnterCheatModel(isEnter) self.isCheatModel = isEnter end function UIVirtualController:OnAutoFightStateChanged() UIUtils.SetActive(self.gameObject, not BattleDataGetter.AutoFightIsSystemLevel()) end --region 左摇杆控制常驻技能的显示 function UIVirtualController:ShowStaticJoySkillRange(skillJoyStickData) self.skillJoyStickData = skillJoyStickData end --endregion 左摇杆控制常驻技能的显示 --region -- *********** Scene Camera ************* -- function UIVirtualController:SetCameraChangeButton(show,free) if self.isShowCameraChange ~= show then self.isShowCameraChange = show UIUtils.SetActive(self.event_CameraChange.gameObject,show) end if free then self.state_Camera:SetState(1) else self.state_Camera:SetState(0) end end function UIVirtualController:OnCameraTargetPatternChange(targeted) if self.isShowCameraChange then UIUtils.SetActive(self.event_CameraChange.gameObject, not targeted) end end function UIVirtualController:OnCameraTargetEnd() if self.isShowCameraChange then UIUtils.SetActive(self.event_CameraChange.gameObject, true) end end return UIVirtualController