Advertisement
Guest User

Untitled

a guest
Mar 15th, 2025
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. extends Node2D
  2.  
  3. @export var offset: Vector2 = Vector2(0, 0)
  4. @export var travel_time: float = 2.0
  5.  
  6. @export_enum("Linear", "Sine", "Quint", "Quart", "Quad", "Expo", "Elastic", "Cubic", "Circ", "Bounce", "Back")
  7. var transition_index: int = 0
  8.  
  9. @export_enum("Ease In", "Ease Out", "Ease In Out", "Ease Out In")
  10. var ease_index: int = 0
  11.  
  12. var base_position: Vector2
  13.  
  14. func _ready() -> void:
  15. # Store the position you set in the editor as the "start" point.
  16. base_position = position
  17. _start_tween_out()
  18.  
  19. func _start_tween_out() -> void:
  20.  
  21. var tween = create_tween()
  22. # Move from base_position to base_position + offset
  23. var track = tween.tween_property(self, "position", base_position + offset, travel_time)
  24. track.set_trans(_get_transition_constant(transition_index))
  25. track.set_ease(_get_ease_constant(ease_index))
  26. tween.tween_callback(Callable(self, "_start_tween_back"))
  27.  
  28. func _start_tween_back() -> void:
  29. var tween = create_tween()
  30. # Move from base_position + offset back to base_position
  31. var track = tween.tween_property(self, "position", base_position, travel_time)
  32. track.set_trans(_get_transition_constant(transition_index))
  33. track.set_ease(_get_ease_constant(ease_index))
  34. tween.tween_callback(Callable(self, "_start_tween_out"))
  35.  
  36. func _get_transition_constant(index: int) -> int:
  37. match index:
  38. 0: return Tween.TRANS_LINEAR
  39. 1: return Tween.TRANS_SINE
  40. 2: return Tween.TRANS_QUINT
  41. 3: return Tween.TRANS_QUART
  42. 4: return Tween.TRANS_QUAD
  43. 5: return Tween.TRANS_EXPO
  44. 6: return Tween.TRANS_ELASTIC
  45. 7: return Tween.TRANS_CUBIC
  46. 8: return Tween.TRANS_CIRC
  47. 9: return Tween.TRANS_BOUNCE
  48. 10: return Tween.TRANS_BACK
  49. return Tween.TRANS_LINEAR # Fallback
  50.  
  51. func _get_ease_constant(index: int) -> int:
  52. match index:
  53. 0: return Tween.EASE_IN
  54. 1: return Tween.EASE_OUT
  55. 2: return Tween.EASE_IN_OUT
  56. 3: return Tween.EASE_OUT_IN
  57. return Tween.EASE_IN # Fallback
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement