GotoComponent = GotoComponent or BaseClass(BaseItem)
|
|
local GotoComponent = GotoComponent
|
|
local RoleManager = RoleManager
|
|
local SceneManager = SceneManager
|
|
local GameMath = GameMath
|
|
function GotoComponent:__init()
|
|
self.base_file = "common"
|
|
self.layout_file = "GotoComponent"
|
|
self.update_enable = false
|
|
self.pos_change = false
|
|
self:Load()
|
|
end
|
|
|
|
function GotoComponent:Load_callback()
|
|
self.arrow = self:GetChild("Arrow")
|
|
self.circle = self:GetChild("Circle")
|
|
self:InitEvent()
|
|
|
|
if self.need_refreshData then
|
|
local pos = self.pos
|
|
self.pos = nil
|
|
self:SetData(pos)
|
|
end
|
|
end
|
|
|
|
function GotoComponent:InitEvent()
|
|
local function onBtnClickHandler(target)
|
|
if target == self.gameObject then
|
|
self:FindWay()
|
|
end
|
|
end
|
|
AddClickEvent(self.gameObject,onBtnClickHandler)
|
|
end
|
|
|
|
function GotoComponent:FindWay()
|
|
if self.pos then
|
|
local findVo = FindVo.New()
|
|
findVo.type = FindVo.POINT
|
|
findVo.sceneId = SceneManager.Instance:GetSceneId()
|
|
findVo.id = 0
|
|
findVo.x = self.pos.x / SceneObj.LogicRealRatio.x
|
|
findVo.y = self.pos.y / SceneObj.LogicRealRatio.y
|
|
|
|
Scene.Instance:FindElement(findVo)
|
|
end
|
|
end
|
|
|
|
function GotoComponent:SetData(pos)
|
|
if pos and (self.pos == nil or (self.pos.x ~= pos.x or self.pos.y ~= pos.y)) then
|
|
self.pos = pos
|
|
self.pos_change = true
|
|
if self.is_loaded then
|
|
self.need_refreshData = false
|
|
self.percentage = 0
|
|
self.runningTime = 0
|
|
self.reverse = false
|
|
self.time = 0.4
|
|
self:UpdateArrowPos()
|
|
self:StartUpdateBeat()
|
|
else
|
|
self.need_refreshData = true
|
|
end
|
|
end
|
|
end
|
|
|
|
function GotoComponent:__delete()
|
|
if self.update_enable then
|
|
self.update_enable = false
|
|
self:RemoveUpdateBeat()
|
|
end
|
|
end
|
|
|
|
function GotoComponent:StartUpdateBeat( )
|
|
if self._use_delete_method then return end
|
|
if not self.update_enable then
|
|
self.update_enable = true
|
|
Runner:getInstance():AddRunObj(self, 2)
|
|
local function onMainRoleMove()
|
|
self.pos_change = true
|
|
end
|
|
self.main_role_move_id = GlobalEventSystem:Bind(ObjectEventType.MAINROLE_MOVE_EVENT_IMME,onMainRoleMove)
|
|
end
|
|
end
|
|
|
|
function GotoComponent:RemoveUpdateBeat( )
|
|
if self.update_enable then
|
|
self.update_enable = false
|
|
Runner.Instance:RemoveRunObj(self)
|
|
self.pos = nil
|
|
if self.main_role_move_id then
|
|
GlobalEventSystem:UnBind(self.main_role_move_id)
|
|
self.main_role_move_id = nil
|
|
end
|
|
end
|
|
end
|
|
|
|
function GotoComponent:UpdateArrowPos()
|
|
local mx, my = RoleManager.Instance.mainRoleInfo.pos_x, RoleManager.Instance.mainRoleInfo.pos_y
|
|
local dir = co.TableXY(self.pos.x - mx, self.pos.y - my)
|
|
co.NormaliseXYTable(dir)
|
|
mx = mx + dir.x * 170
|
|
my = my + dir.y * 170 + 75
|
|
local pos = MainCamera.Instance:WorldToViewportPoint(mx, my, 0) / MainCamera.Instance:GetCameraRatio()
|
|
self:SetPosition(pos.x, pos.y)
|
|
end
|
|
|
|
|
|
function GotoComponent:Update(now_time,elapse_time)
|
|
if self.update_enable then
|
|
if self.pos_change then
|
|
self.pos_change = false
|
|
local mx, my = RoleManager.Instance.mainRoleInfo.pos_x, RoleManager.Instance.mainRoleInfo.pos_y + 75
|
|
local angle = SceneManager.Instance:GetAngle(co.TableXY(mx, my), self.pos, true)
|
|
if angle then
|
|
self.arrow.localRotation = Quaternion.Euler(0, 0, angle)
|
|
end
|
|
self:UpdateArrowPos()
|
|
end
|
|
|
|
local scale = GameMath.Lerp(1,0.88,self.percentage)
|
|
self.circle.localScale = Vector3(scale,scale,scale)
|
|
self:UpdatePercentage(now_time,elapse_time)
|
|
end
|
|
end
|
|
|
|
function GotoComponent:UpdatePercentage(now_time,elapse_time)
|
|
|
|
self.runningTime = self.runningTime + elapse_time
|
|
if self.reverse then
|
|
self.percentage = 1 - self.runningTime / self.time
|
|
else
|
|
self.percentage = self.runningTime / self.time
|
|
end
|
|
|
|
if self.percentage > 1 or self.percentage < 0 then
|
|
self.runningTime = 0
|
|
self.reverse = not self.reverse
|
|
end
|
|
end
|
|
|
|
|