local UIMiniMapPointItem, private = class("UIMiniMapPointItem") --隐藏时挪出视野范围 local HidePosX = 999 local HidePosY = 999 local MapPointEffectPath = ConstAssetPathConfig.MapPointEffectPath local Image = Image local UIAnimationPlayer = UIAnimationPlayer local UIEffect = TK2Core.UI.UIEffect local UICDTimer = TK2Core.UI.UICDTimer --PointType为0,是自己,外部传Image;其他类型,传路径和Parent,自己生成资源自己管理 function UIMiniMapPointItem:initialize(PointType,Resource,Parent) self.ActiveState = true if PointType == 0 then local obj = Resource.gameObject self.PoolPath = nil self.PointImage = Resource self.PointEffect = obj:GetComponent(typeof(UIEffect)) else local obj = TK2API.GetInstance(Resource,Parent).gameObject self.PoolPath = Resource self.PointImage = obj:GetComponent(typeof(Image)) self.PointEffect = obj:GetComponent(typeof(UIEffect)) self.PointAnim = obj:GetComponent(typeof(UIAnimationPlayer)) self:SetPointImage(PointType) end self.PointTrans = self.PointImage and self.PointImage.transform or nil self.lastPosX = nil self.lastPosY = nil self.lastAngle = nil end function UIMiniMapPointItem:InitUITimer() self.PointUITimer = self.PointImage.gameObject:GetComponent(typeof(UICDTimer)) end function UIMiniMapPointItem:StartCountDown(time) if self.PointUITimer then self.PointUITimer:ResetAndBeginCustomTimer(false, time, 0, "({0}s)", 1) end end function UIMiniMapPointItem:SetPointPos(posX,posY,angle) if angle then if ((self.lastAngle == nil) or (self.lastAngle and angle ~= self.lastAngle)) then self.PointTrans:SetLocalEulerAngles(0,0,angle) self.lastAngle = angle end end if self.lastPosX~= posX or self.lastPosX ~= posY then self.PointTrans:SetLocalPosition(posX,posY,0) self.lastPosX = posX self.lastPosY = posY end end function UIMiniMapPointItem:SetPointImage(PointType) if self.PointType and self.PointType == PointType then --类型一样的不处理 return end self.PointType = PointType local path = CTL.MiniMap.GetMapPointPath(PointType) if path then self.PointImage.enabled = true self:SetPointImageByPath(path) else self.PointImage.enabled = false end end function UIMiniMapPointItem:SetPointImageByPath(path) UIUtils.SetImageByAssetBundle(self.PointImage, path) self.PointImage:SetNativeSize() if CTL.MiniMap.IsMapPointLastSibling(self.PointType) then self.PointImage.rectTransform:SetAsLastSibling() elseif CTL.MiniMap.IsMapPointFirstSibling(self.PointType) then self.PointImage.rectTransform:SetAsFirstSibling() end end function UIMiniMapPointItem:SetAsLastSibling() self.PointImage.rectTransform:SetAsLastSibling() end function UIMiniMapPointItem:PlayStressEffect(type, duration) local effect = CTL.MiniMap.GetStressEffect(type) if effect then self:PlayPointEffect(effect, duration) else self:PlayPointAnimation() end end function UIMiniMapPointItem:StopAllEffect() self:StopPointEffect() self:StopPointAnimation() end function UIMiniMapPointItem:PlayPointEffect(effect, duration) if not self.PointEffect or string.isNilOrEmpty(effect) then return end if duration then self.PointEffect:PlayUIEffect(effect, duration) else self.PointEffect:PlayUIEffect(effect, -1) end end function UIMiniMapPointItem:StopPointEffect() if self.PointEffect then self.PointEffect:StopUIEffect() end end function UIMiniMapPointItem:PlayPointAnimation() if self.PointAnim then self.PointAnim:PlayAnim() end end function UIMiniMapPointItem:PlayPointAnimationByPath(animPath) if self.PointAnim then self.PointAnim:PlayAnimByPath(animPath) end end function UIMiniMapPointItem:StopPointAnimation() if self.PointAnim then self.PointAnim:StopAnim(true) end end function UIMiniMapPointItem:SetActive(state) if state == self.ActiveState then return end self.ActiveState = state if state == false then self.PointTrans:SetLocalPosition(HidePosX,HidePosY,0) if self.PointEffect then self.PointEffect:StopUIEffect() end if self.PointUITimer then self.PointUITimer:EndTimer() end end end function UIMiniMapPointItem:SetMapPoint02Label(str) local label = UIUtils.GetLocalizationTextFromChild(self.PointTrans.gameObject ,0) UIUtils.SetLabel(label, str) end function UIMiniMapPointItem:Clear() self:StopPointAnimation() self:StopPointEffect() self.PointImage.enabled = true private.RemoveContainer(self) if self.PoolPath then TK2API.ReturnInstance(self.PoolPath,self.PointImage.gameObject) end self.PointType = nil self.PointTrans = nil self.PointImage = nil self.PointEffect = nil self.PointUITimer = nil self.PointAnim = nil self.minimapRoot = nil self.levelItemInfo = nil end --编辑器是用程序原图种的点,与美术输出的图尺寸不一样大,需要乘一个地图缩放比例:原图尺寸/美术尺寸 --美术将程序图缩小,不算上缩放比例才是正确值 function private:GetPosRate() local uiMapRate = 1 if self.minimapRoot and self.minimapRoot.miniMapData and self.minimapRoot.miniMapTex then local oriMapSize = self.minimapRoot.miniMapData.OriMapSize local realMapSize = self.minimapRoot.miniMapData.UIMapSize if realMapSize > 0 and realMapSize > oriMapSize then uiMapRate = oriMapSize / realMapSize end end return uiMapRate end function UIMiniMapPointItem:InitLevelPara(minimapRoot,levelItemInfo) self.minimapRoot = minimapRoot self.levelItemInfo = levelItemInfo self:SetActive(true) self:SetPointPos(levelItemInfo.PosX * private.GetPosRate(self), levelItemInfo.PosY * private.GetPosRate(self)) --元素组,生成其他元素 if levelItemInfo.OperType == LuaConst.MiniMapOperType.MiniMapPoint then if levelItemInfo.LayoutType == LuaConst.EntityLayoutType.Random_Circle then --圆随机 local extraNum = levelItemInfo.Count - 1 if extraNum > 0 then local nodeList = private.GetMapPointNodeList(self, extraNum) for i, obj in ipairs(nodeList) do local radius = UnityEngine.Random.Range(0, levelItemInfo.Radius) local angle = UnityEngine.Random.Range(0, 360) local x = math.cos(angle) * radius local y = math.sin(angle) * radius obj.transform:SetLocalPosition(x, y, 0) end end elseif levelItemInfo.LayoutType == LuaConst.EntityLayoutType.Random_Square then --矩形随机 local extraNum = levelItemInfo.Count - 1 if extraNum > 0 then local nodeList = private.GetMapPointNodeList(self, extraNum) for i, obj in ipairs(nodeList) do local x = UnityEngine.Random.Range(-levelItemInfo.Length / 2, levelItemInfo.Length / 2) local y = UnityEngine.Random.Range(-levelItemInfo.Width / 2, levelItemInfo.Width / 2) obj.transform:SetLocalPosition(x, y, 0) end end elseif levelItemInfo.LayoutType == LuaConst.EntityLayoutType.Matrix then --固定行列 local extraNum = levelItemInfo.Row * levelItemInfo.Col - 1 if extraNum > 0 then local nodeList = private.GetMapPointNodeList(self, extraNum) for i, obj in ipairs(nodeList) do local x = ((i) % levelItemInfo.Col) * levelItemInfo.Gap local y = math.floor((i) / levelItemInfo.Col) * levelItemInfo.Gap obj.transform:SetLocalPosition(x, y, 0) end end end elseif levelItemInfo.OperType == LuaConst.MiniMapOperType.MiniMapArea then --占领资源大小要跟UI显示大小一致 UIUtils.SetImageByAssetBundle(self.PointImage, levelItemInfo.AreaPath) UIUtils.SetGraphicSize(self.PointImage,minimapRoot.MapSizeX,minimapRoot.MapSizeY) end end function private:GetMapPointNodeList(num) if self.minimapRoot == nil then return nil end if self.NodeContainer == nil then self.NodeContainer = {} end for i = 1, num do local mapItem = TK2API.GetInstance(MapPointEffectPath,self:GetPointImageTrans()).gameObject:GetComponent(typeof(Image)) local path = CTL.MiniMap.GetMapPointPath(self.PointType) if path then UIUtils.SetImageByAssetBundle(mapItem, path) mapItem:SetNativeSize() end self.NodeContainer[i] = mapItem end return self.NodeContainer end function UIMiniMapPointItem:HighLightState(state) if self.highListState and self.highListState == state then return end self.highListState = state UIUtils.PlayObjEffect(self.PointImage.gameObject,state and 1 or 0) end function UIMiniMapPointItem:SetTargetInfo(targetItem,speed) self.TargetPosX = targetItem.lastPosX self.TargetPosY = targetItem.lastPosY self.Speed = speed end function UIMiniMapPointItem:MoveToTarget() local offsetPosX = self.TargetPosX - self.lastPosX local offsetPosY = self.TargetPosY - self.lastPosY local disSqr = (offsetPosX) ^ 2 + (offsetPosY) ^ 2 if disSqr < 0.01 then return true end local speedFrame = self.Speed / LuaConst.LogicFrameRate local posX = self.lastPosX + speedFrame * (offsetPosX > 0 and 1 or -1) local posY = self.lastPosY + speedFrame * (offsetPosY > 0 and 1 or -1) self:SetPointPos(posX * private.GetPosRate(self), posY * private.GetPosRate(self)) return false end --隐藏/切换关卡,将所有子item挪到池子里 function private:RemoveContainer() if self.NodeContainer and self.minimapRoot then for i = 1, #self.NodeContainer do TK2API.ReturnInstance(MapPointEffectPath,self.NodeContainer[i]) end end end return UIMiniMapPointItem