源战役客户端
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

141 linhas
3.6 KiB

  1. GotoComponent = GotoComponent or BaseClass(BaseItem)
  2. local GotoComponent = GotoComponent
  3. local RoleManager = RoleManager
  4. local SceneManager = SceneManager
  5. local GameMath = GameMath
  6. function GotoComponent:__init()
  7. self.base_file = "common"
  8. self.layout_file = "GotoComponent"
  9. self.update_enable = false
  10. self.pos_change = false
  11. self:Load()
  12. end
  13. function GotoComponent:Load_callback()
  14. self.arrow = self:GetChild("Arrow")
  15. self.circle = self:GetChild("Circle")
  16. self:InitEvent()
  17. if self.need_refreshData then
  18. local pos = self.pos
  19. self.pos = nil
  20. self:SetData(pos)
  21. end
  22. end
  23. function GotoComponent:InitEvent()
  24. local function onBtnClickHandler(target)
  25. if target == self.gameObject then
  26. self:FindWay()
  27. end
  28. end
  29. AddClickEvent(self.gameObject,onBtnClickHandler)
  30. end
  31. function GotoComponent:FindWay()
  32. if self.pos then
  33. local findVo = FindVo.New()
  34. findVo.type = FindVo.POINT
  35. findVo.sceneId = SceneManager.Instance:GetSceneId()
  36. findVo.id = 0
  37. findVo.x = self.pos.x / SceneObj.LogicRealRatio.x
  38. findVo.y = self.pos.y / SceneObj.LogicRealRatio.y
  39. Scene.Instance:FindElement(findVo)
  40. end
  41. end
  42. function GotoComponent:SetData(pos)
  43. if pos and (self.pos == nil or (self.pos.x ~= pos.x or self.pos.y ~= pos.y)) then
  44. self.pos = pos
  45. self.pos_change = true
  46. if self.is_loaded then
  47. self.need_refreshData = false
  48. self.percentage = 0
  49. self.runningTime = 0
  50. self.reverse = false
  51. self.time = 0.4
  52. self:UpdateArrowPos()
  53. self:StartUpdateBeat()
  54. else
  55. self.need_refreshData = true
  56. end
  57. end
  58. end
  59. function GotoComponent:__delete()
  60. if self.update_enable then
  61. self.update_enable = false
  62. self:RemoveUpdateBeat()
  63. end
  64. end
  65. function GotoComponent:StartUpdateBeat( )
  66. if self._use_delete_method then return end
  67. if not self.update_enable then
  68. self.update_enable = true
  69. Runner:getInstance():AddRunObj(self, 2)
  70. local function onMainRoleMove()
  71. self.pos_change = true
  72. end
  73. self.main_role_move_id = GlobalEventSystem:Bind(ObjectEventType.MAINROLE_MOVE_EVENT_IMME,onMainRoleMove)
  74. end
  75. end
  76. function GotoComponent:RemoveUpdateBeat( )
  77. if self.update_enable then
  78. self.update_enable = false
  79. Runner.Instance:RemoveRunObj(self)
  80. self.pos = nil
  81. if self.main_role_move_id then
  82. GlobalEventSystem:UnBind(self.main_role_move_id)
  83. self.main_role_move_id = nil
  84. end
  85. end
  86. end
  87. function GotoComponent:UpdateArrowPos()
  88. local mx, my = RoleManager.Instance.mainRoleInfo.pos_x, RoleManager.Instance.mainRoleInfo.pos_y
  89. local dir = co.TableXY(self.pos.x - mx, self.pos.y - my)
  90. co.NormaliseXYTable(dir)
  91. mx = mx + dir.x * 170
  92. my = my + dir.y * 170 + 75
  93. local pos = MainCamera.Instance:WorldToViewportPoint(mx, my, 0) / MainCamera.Instance:GetCameraRatio()
  94. self:SetPosition(pos.x, pos.y)
  95. end
  96. function GotoComponent:Update(now_time,elapse_time)
  97. if self.update_enable then
  98. if self.pos_change then
  99. self.pos_change = false
  100. local mx, my = RoleManager.Instance.mainRoleInfo.pos_x, RoleManager.Instance.mainRoleInfo.pos_y + 75
  101. local angle = SceneManager.Instance:GetAngle(co.TableXY(mx, my), self.pos, true)
  102. if angle then
  103. self.arrow.localRotation = Quaternion.Euler(0, 0, angle)
  104. end
  105. self:UpdateArrowPos()
  106. end
  107. local scale = GameMath.Lerp(1,0.88,self.percentage)
  108. self.circle.localScale = Vector3(scale,scale,scale)
  109. self:UpdatePercentage(now_time,elapse_time)
  110. end
  111. end
  112. function GotoComponent:UpdatePercentage(now_time,elapse_time)
  113. self.runningTime = self.runningTime + elapse_time
  114. if self.reverse then
  115. self.percentage = 1 - self.runningTime / self.time
  116. else
  117. self.percentage = self.runningTime / self.time
  118. end
  119. if self.percentage > 1 or self.percentage < 0 then
  120. self.runningTime = 0
  121. self.reverse = not self.reverse
  122. end
  123. end