PipeGenerator.gd 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. extends Node2D
  2. @export var item: PackedScene = null
  3. var item_amount := 0
  4. @export_enum("Up", "Down", "Left", "Right") var direction := 0
  5. func _ready() -> void:
  6. $Timer.start()
  7. func _physics_process(_delta: float) -> void:
  8. $Check.target_position = [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT][direction] * 16
  9. $Check.position = $Check.target_position.normalized()
  10. func on_timeout() -> void:
  11. if item == null or item_amount >= 3 or $Check.is_colliding(): return
  12. $AnimationPlayer.stop()
  13. var node = item.instantiate()
  14. node.global_position = $Joint.global_position
  15. add_sibling(node)
  16. $Joint.remote_path = node.get_path()
  17. item_amount += 1
  18. node.set_process(false)
  19. node.set_physics_process(false)
  20. node.reset_physics_interpolation()
  21. var z_old = node.z_index
  22. node.z_index = -10
  23. $AnimationPlayer.play(get_direction_string([Vector2.DOWN, Vector2.UP, Vector2.RIGHT, Vector2.LEFT][direction]))
  24. await get_tree().process_frame
  25. node.reset_physics_interpolation()
  26. await $AnimationPlayer.animation_finished
  27. $Joint.remote_path = ""
  28. if is_instance_valid(node):
  29. node.set_process(true)
  30. node.z_index = z_old
  31. node.set_physics_process(true)
  32. node.tree_exited.connect(func(): item_amount -= 1)
  33. func get_direction_string(direction_vector := Vector2.UP) -> String:
  34. match direction_vector:
  35. Vector2.UP:
  36. return "Up"
  37. Vector2.DOWN:
  38. return "Down"
  39. Vector2.LEFT:
  40. return "Left"
  41. Vector2.RIGHT:
  42. return "Right"
  43. _:
  44. return ""