elevator.gd 785 B

12345678910111213141516171819202122232425
  1. extends AnimatableBody2D
  2. @export var color : String
  3. @export var speed : float = 1
  4. @export var height : int = 1
  5. @onready var start_position = position
  6. @onready var end_position = Vector2(position.x, position.y - 32 * height)
  7. @onready var path_length = start_position.y - end_position.y
  8. var curr_tween
  9. func on_button_toggled(button_color, button_enabled):
  10. if button_color == color:
  11. if curr_tween:
  12. curr_tween.kill()
  13. curr_tween = get_tree().create_tween()
  14. var target_position
  15. if button_enabled:
  16. target_position = Vector2(start_position.x, end_position.y)
  17. else:
  18. target_position = Vector2(start_position.x, start_position.y)
  19. var time = position.distance_to(target_position) / path_length * speed
  20. curr_tween.tween_property(self, "position", target_position, time)