local UICompass = UIViewClass() local pi = math.pi local math_cos = math.cos local math_sin = math.sin function UICompass:OnOpened(context) self.distanceStr = UIUtils.GetLocalizedTxt(Const_IDS["IDS_TargetDistance"]) self.myEntity = BattleDataGetter.GetCurEntity() self.destPosX, self.destPosZ = FP.tonumberV2(context.entity:GetLogicPos()) self.dirVector = Vector3.New(0, 0, 0) self:StartRepeatFunc(self.OnLateUpdateFun, 0, -1, false) self:RegistSystemEvent(EventID.UI_Interact_Show, self.ShowInteractUI) end function UICompass:ShowInteractUI(isShow, interactData, triggerEntity, interactEntity, interactIndex) if self.context.entity == interactEntity then UIUtils.SetActive(self.txt_Distance, not isShow) UIUtils.SetActive(self.img_TextBg, not isShow) UIUtils.SetActive(self.img_CompassPointer, not isShow) if isShow then self:StopRepeatFunc(self.OnLateUpdateFun) self.effect_ShowUp:PlayUIEffect() CTL.Tips.AddTipsMsg(Const_IDS.IDS_DigTreasure_FindTreasure) CTL.Sound.PlayGameSound("UI_find_precious") else self:StartRepeatFunc(self.OnLateUpdateFun, 0, -1, false) self.effect_ShowUp:StopUIEffect() end end end function UICompass:OnLateUpdateFun(deltaTime, timeSinceUpdate) if self.myEntity then local rotation = self:GetRotation() local distance = FP.V2.Distance(self.context.entity:GetLogicPos(), self.myEntity:GetLogicPos()) UIUtils.SetLabel(self.txt_Distance, string.format(self.distanceStr, string.format("%.1f", FP.tonumberV1(distance)))) end end function UICompass:GetRotation() local myPosX, myPosZ = FP.tonumberV2(self.myEntity:GetLogicPos()) local postion = self.img_TextBg.transform.position local dirX = self.destPosX - myPosX local dirZ = self.destPosZ - myPosZ local cameraForward = AuxUtil.MainCameraTrans.transform.forward self.dirVector.x = dirX self.dirVector.z = dirZ --local temp = Vector3.New(dirX, 0, dirZ) local rotation rotation = UnityEngine.Quaternion.FromToRotation(cameraForward, self.dirVector) rotation.z = -rotation.y rotation.y = 0 rotation.x = 0 self.img_CompassPointer.transform.rotation = rotation end return UICompass