main.gd 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. extends Node
  2. var tween: Tween
  3. var sub_tween: Tween
  4. @onready var icon: Sprite2D = %Icon
  5. @onready var icon_start_position := icon.position
  6. @onready var countdown_label: Label = %CountdownLabel
  7. @onready var path: Path2D = $Path2D
  8. @onready var progress: TextureProgressBar = %Progress
  9. func _process(_delta: float) -> void:
  10. if not tween or not tween.is_running():
  11. return
  12. progress.value = tween.get_total_elapsed_time()
  13. func start_animation() -> void:
  14. # Reset the icon to original state.
  15. reset()
  16. # Create the Tween. Also sets the initial animation speed.
  17. # All methods that modify Tween will return the Tween, so you can chain them.
  18. tween = create_tween().set_speed_scale(%SpeedSlider.value)
  19. # Sets the amount of loops. 1 loop = 1 animation cycle, so e.g. 2 loops will play animation twice.
  20. if %Infinite.button_pressed:
  21. tween.set_loops() # Called without arguments, the Tween will loop infinitely.
  22. else:
  23. tween.set_loops(%Loops.value)
  24. # Step 1
  25. if is_step_enabled("MoveTo", 1.0):
  26. # tween_*() methods return a Tweener object. Its methods can also be chained, but
  27. # it's stored in a variable here for readability (chained lines tend to be long).
  28. # Note the usage of ^"NodePath". A regular "String" is accepted too, but it's very slightly slower.
  29. var tweener := tween.tween_property(icon, ^"position", Vector2(400, 250), 1.0)
  30. tweener.set_ease(%Ease1.selected)
  31. tweener.set_trans(%Trans1.selected)
  32. # Step 2
  33. if is_step_enabled("ColorRed", 1.0):
  34. tween.tween_property(icon, ^"self_modulate", Color.RED, 1.0)
  35. # Step 3
  36. if is_step_enabled("MoveRight", 1.0):
  37. # as_relative() makes the value relative, so in this case it moves the icon
  38. # 200 pixels from the previous position.
  39. var tweener := tween.tween_property(icon, ^"position:x", 200.0, 1.0).as_relative()
  40. tweener.set_ease(%Ease3.selected)
  41. tweener.set_trans(%Trans3.selected)
  42. if is_step_enabled("Roll", 0.0):
  43. # parallel() makes the Tweener run in parallel to the previous one.
  44. var tweener := tween.parallel().tween_property(icon, ^"rotation", TAU, 1.0)
  45. tweener.set_ease(%Ease3.selected)
  46. tweener.set_trans(%Trans3.selected)
  47. # Step 4
  48. if is_step_enabled("MoveLeft", 1.0):
  49. tween.tween_property(icon, ^"position", Vector2.LEFT * 200, 1.0).as_relative()
  50. if is_step_enabled("Jump", 0.0):
  51. # Jump has 2 substeps, so to make it properly parallel, it can be done in a sub-Tween.
  52. # Here we are calling a lambda method that creates a sub-Tween.
  53. # Any number of Tweens can animate a single object in the same time.
  54. tween.parallel().tween_callback(func():
  55. # Note that transition is set on Tween, but ease is set on Tweener.
  56. # Values set on Tween will affect all Tweeners (as defaults) and values
  57. # on Tweeners can override them.
  58. sub_tween = create_tween().set_speed_scale(%SpeedSlider.value).set_trans(Tween.TRANS_SINE)
  59. sub_tween.tween_property(icon, ^"position:y", -150.0, 0.5).as_relative().set_ease(Tween.EASE_OUT)
  60. sub_tween.tween_property(icon, ^"position:y", 150.0, 0.5).as_relative().set_ease(Tween.EASE_IN)
  61. )
  62. # Step 5
  63. if is_step_enabled("Blink", 2.0):
  64. # Loops are handy when creating some animations.
  65. for i in 10:
  66. tween.tween_callback(icon.hide).set_delay(0.1)
  67. tween.tween_callback(icon.show).set_delay(0.1)
  68. # Step 6
  69. if is_step_enabled("Teleport", 0.5):
  70. # Tweening a value with 0 duration makes it change instantly.
  71. tween.tween_property(icon, ^"position", Vector2(325, 325), 0)
  72. tween.tween_interval(0.5)
  73. # Binds can be used for advanced callbacks.
  74. tween.tween_callback(icon.set_position.bind(Vector2(680, 215)))
  75. # Step 7
  76. if is_step_enabled("Curve", 3.5):
  77. # Method tweening is useful for animating values that can't be directly interpolated.
  78. # It can be used for remapping and some very advanced animations.
  79. # Here it's used for moving sprite along a path, using inline lambda function.
  80. var tweener := tween.tween_method(
  81. func(v: float) -> void:
  82. icon.position = path.position + path.curve.sample_baked(v), 0.0, path.curve.get_baked_length(), 3.0
  83. ).set_delay(0.5)
  84. tweener.set_ease(%Ease7.selected)
  85. tweener.set_trans(%Trans7.selected)
  86. # Step 8
  87. if is_step_enabled("Wait", 2.0):
  88. # ...
  89. tween.tween_interval(2)
  90. # Step 9
  91. if is_step_enabled("Countdown", 3.0):
  92. tween.tween_callback(countdown_label.show)
  93. tween.tween_method(do_countdown, 4, 1, 3)
  94. tween.tween_callback(countdown_label.hide)
  95. # Step 10
  96. if is_step_enabled("Enlarge", 0.0):
  97. tween.tween_property(icon, ^"scale", Vector2.ONE * 5, 0.5).set_trans(Tween.TRANS_ELASTIC).set_ease(Tween.EASE_OUT)
  98. if is_step_enabled("Vanish", 1.0):
  99. tween.parallel().tween_property(icon, ^"self_modulate:a", 0.0, 1.0)
  100. if %Loops.value > 1 or %Infinite.button_pressed:
  101. tween.tween_callback(icon.show)
  102. tween.tween_callback(icon.set_self_modulate.bind(Color.WHITE))
  103. # RESET step
  104. if %Reset.button_pressed:
  105. tween.tween_callback(reset.bind(true))
  106. func do_countdown(number: int) -> void:
  107. countdown_label.text = str(number)
  108. func reset(soft: bool = false) -> void:
  109. icon.position = icon_start_position
  110. icon.self_modulate = Color.WHITE
  111. icon.rotation = 0
  112. icon.scale = Vector2.ONE
  113. icon.show()
  114. countdown_label.hide()
  115. if soft:
  116. # Only reset properties.
  117. return
  118. if tween:
  119. tween.kill()
  120. tween = null
  121. if sub_tween:
  122. sub_tween.kill()
  123. sub_tween = null
  124. progress.max_value = 0
  125. func is_step_enabled(step: String, expected_time: float) -> bool:
  126. var enabled: bool = get_node("%" + step).button_pressed
  127. if enabled:
  128. progress.max_value += expected_time
  129. return enabled
  130. func pause_resume() -> void:
  131. if tween and tween.is_valid():
  132. if tween.is_running():
  133. tween.pause()
  134. else:
  135. tween.play()
  136. if sub_tween and sub_tween.is_valid():
  137. if sub_tween.is_running():
  138. sub_tween.pause()
  139. else:
  140. sub_tween.play()
  141. func kill_tween() -> void:
  142. if tween:
  143. tween.kill()
  144. if sub_tween:
  145. sub_tween.kill()
  146. func speed_changed(value: float) -> void:
  147. if tween:
  148. tween.set_speed_scale(value)
  149. if sub_tween:
  150. sub_tween.set_speed_scale(value)
  151. %SpeedLabel.text = str("x", value)
  152. func infinite_toggled(button_pressed: bool) -> void:
  153. %Loops.editable = not button_pressed