--[[ 目标指向 ]] local pi = math.pi local math_cos = math.cos local math_sin = math.sin local UIGuildPartyTargetView = UIViewClass() function UIGuildPartyTargetView:OnCreated() self.isFirstTick = true self.targetAccId = nil self.entityId = nil self.distanceStr = UIUtils.GetLocalizedTxt(Const_IDS["IDS_TargetDistance"]) self.entityPosVec3 = Vector3.New(0, 0, 0) self.entityScreenPosVec3 = Vector3.New(0, 0, 0) self.myEntityPosVec3 = Vector3.New(0, 0, 0) self.myEntityScreenPosVec3 = Vector3.New(0, 0, 0) self.screenWidth = nil self.screenHeight = nil self.lastHasEntity = false self.lastShowArror = false end function UIGuildPartyTargetView:OnOpened(context) if context.entityId then self.entityId = context.entityId elseif context.targetAccId then self.targetAccId = context.targetAccId end self.myEntity = BattleDataGetter.GetCurEntity() self:StartRepeatFunc(self.OnLateUpdateFun, 0, -1, false) end function UIGuildPartyTargetView:OnLateUpdateFun(deltaTime, timeSinceUpdate) if self.targetAccId then self.entityId = SyncMoveManager.GetEntityIDByAcid(self.targetAccId) end local entity = BattleDataGetter.GetEntityByEntityID(self.entityId) if entity and self.myEntity then if not self.lastHasEntity then UIUtils.SetActive(self.group_Target, true) self.lastHasEntity = true end local rotation = self:GetRotation() local eulerAngleZ = rotation.eulerAngles.z if (eulerAngleZ<20 and eulerAngleZ>-20) or(eulerAngleZ>340) then if self.lastShowArror then UIUtils.SetActive(self.txt_Distance.gameObject, true) UIUtils.SetActive(self.Arrow, false) self.lastShowArror = false end local distance = FP.V2.Distance(entity:GetLogicPos(), self.myEntity:GetLogicPos()) UIUtils.SetLabel(self.txt_Distance, string.format(self.distanceStr, string.format("%.1f", distance[1]/1000))) else if not self.lastShowArror then UIUtils.SetActive(self.txt_Distance.gameObject, false) UIUtils.SetActive(self.Arrow, true) self.lastShowArror = true end self.Arrow.transform.rotation = rotation end self:SetOvalXY(rotation) elseif self.isFirstTick or self.lastHasEntity then UIUtils.SetActive(self.group_Target, false) UIUtils.SetActive(self.txt_Distance.gameObject, true) UIUtils.SetActive(self.Arrow, false) self.lastShowArror = false self.lastHasEntity = false end self.isFirstTick = false end function UIGuildPartyTargetView:GetRotation() if not self.screenWidth then self.screenWidth = UnityEngine.Screen.width self.screenHeight = UnityEngine.Screen.height end local entity = BattleDataGetter.GetEntityByEntityID(self.entityId) local entityPos = entity:GetLogicPos() local myEntityPos = self.myEntity:GetLogicPos() self.entityPosVec3.x = FP.tonumberV1(entityPos.x) self.entityPosVec3.y = FP.tonumberV1(entity:GetLogicPosY()) self.entityPosVec3.z = FP.tonumberV1(entityPos.z) self.myEntityPosVec3.x = FP.tonumberV1(myEntityPos.x) self.myEntityPosVec3.y = FP.tonumberV1(self.myEntity:GetLogicPosY()) self.myEntityPosVec3.z = FP.tonumberV1(myEntityPos.z) self.entityScreenPosVec3.x, self.entityScreenPosVec3.y, self.entityScreenPosVec3.z = UIUtils.WorldToScreenPoint(self.entityPosVec3.x, self.entityPosVec3.y, self.entityPosVec3.z, 0, 0, 0) self.entityScreenPosVec3.z = 0 self.entityScreenPosVec3.x = self.entityScreenPosVec3.x - self.screenWidth*0.5 self.entityScreenPosVec3.y = self.entityScreenPosVec3.y - self.screenHeight*0.5 self.myEntityScreenPosVec3.x, self.myEntityScreenPosVec3.y, self.myEntityScreenPosVec3.z = UIUtils.WorldToScreenPoint(self.myEntityPosVec3.x, self.myEntityPosVec3.y, self.myEntityPosVec3.z, 0, 0, 0) self.myEntityScreenPosVec3.x = self.myEntityScreenPosVec3.x - self.screenWidth*0.5 self.myEntityScreenPosVec3.y = self.myEntityScreenPosVec3.y - self.screenHeight*0.5 self.myEntityScreenPosVec3.z = 0 local rotation if UIUtils.IsInViewPort(self.entityPosVec3.x, self.entityPosVec3.y, self.entityPosVec3.z) then rotation = UnityEngine.Quaternion.FromToRotation(Vector3.New(0, 1, 0), self.entityScreenPosVec3) else self.entityPosVec3.x = self.entityPosVec3.x - self.myEntityPosVec3.x self.entityPosVec3.y = 0 self.entityPosVec3.z = self.entityPosVec3.z - self.myEntityPosVec3.z local cameraPosX, cameraPosY, cameraPosZ = AuxUtil.MainCameraTrans:GetPosition() local cameraFoward = AuxUtil.MainCameraTrans.transform.forward cameraFoward.y = 0 rotation = UnityEngine.Quaternion.FromToRotation(cameraFoward, self.entityPosVec3) rotation.z = -rotation.y rotation.y = 0 end return rotation end function UIGuildPartyTargetView:SetOvalXY(rotation) local ScreenDis = math.sqrt((self.entityScreenPosVec3.x)^2+(self.entityScreenPosVec3.y)^2) local realDis = ScreenDis ScreenDis = ScreenDis>300 and 300 or ScreenDis ScreenDis = ScreenDis<100 and 100 or ScreenDis local x, y = 0, 0 if realDis < 400 then x, y = 100*math_sin((540-rotation.eulerAngles.z)*pi/180), 100*math_cos((540-rotation.eulerAngles.z)*pi/180) x = self.entityScreenPosVec3.x+x y = self.entityScreenPosVec3.y+y else x, y = ScreenDis*math_sin((360-rotation.eulerAngles.z)*pi/180), ScreenDis*math_cos((360-rotation.eulerAngles.z)*pi/180) end self.img_Bg.transform:SetLocalPosition(x, y) end return UIGuildPartyTargetView