TrackRider.gd 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. class_name TrackRider
  2. extends Node2D
  3. @export var attached_entity: Node2D = null
  4. @export_range(1, 8, 1) var speed := 2
  5. @export_enum("Forward", "Backward") var direction := 0
  6. var velocity := Vector2.ZERO
  7. var last_position := Vector2.ZERO
  8. var direction_vector := Vector2i.ZERO
  9. var current_track: Track = null
  10. var track_idx := -1
  11. var can_attach := true
  12. var travelling_on_rail := false
  13. func _ready() -> void:
  14. start()
  15. await get_tree().physics_frame
  16. if attached_entity != null:
  17. attach_to_joint(attached_entity)
  18. func start() -> void:
  19. current_track = null
  20. track_idx = -1
  21. func check_for_entities() -> void:
  22. for i in $Hitbox.get_overlapping_bodies():
  23. print(i)
  24. if i.has_node("TrackJoint"):
  25. attach_to_joint(i)
  26. return
  27. for i in $Hitbox.get_overlapping_areas():
  28. print(i)
  29. if i.owner.has_node("TrackJoint"):
  30. attach_to_joint(i.owner)
  31. return
  32. func _physics_process(delta: float) -> void:
  33. if attached_entity == null:
  34. check_for_entities()
  35. return
  36. if travelling_on_rail == false:
  37. velocity.y += 10
  38. global_position += velocity * delta
  39. check_for_rail()
  40. last_position = global_position
  41. func attach_to_joint(node: Node2D) -> void:
  42. var joint = node.get_node("TrackJoint")
  43. joint.is_attached = true
  44. if joint.movement_node != null:
  45. joint.movement_node.active = false
  46. joint.attached.emit()
  47. elif joint.disable_physics:
  48. node.set_physics_process(false)
  49. joint.rider = self
  50. node.physics_interpolation_mode = Node.PHYSICS_INTERPOLATION_MODE_OFF
  51. node.reparent($Joint, false)
  52. node.position = joint.offset
  53. attached_entity = node
  54. func check_for_rail() -> void:
  55. if travelling_on_rail == false and can_attach:
  56. for i in $Hitbox.get_overlapping_areas():
  57. if i.get_parent() is TrackPiece and i.get_parent().owner != current_track:
  58. var piece: TrackPiece = i.get_parent()
  59. if piece.owner.length <= 0:
  60. continue
  61. global_position = piece.global_position
  62. travelling_on_rail = true
  63. current_track = piece.owner
  64. track_idx = piece.idx
  65. if track_idx >= current_track.path.size():
  66. direction = 1
  67. if direction == 1:
  68. track_idx -= 1
  69. track_idx = clamp(track_idx, 0, current_track.path.size() - 1)
  70. if travelling_on_rail:
  71. direction_vector = current_track.path[track_idx] * [1, -1][direction]
  72. if current_track != null:
  73. move_tween(Vector2(direction_vector))
  74. func move_tween(new_direction := Vector2.ZERO) -> void:
  75. var tween = create_tween()
  76. tween.tween_property(self, "global_position", global_position + (new_direction * 16), float(1.0 if new_direction.is_normalized() else 1.414) / (speed * 2))
  77. await tween.finished
  78. track_idx += [1, -1][direction]
  79. if track_idx >= current_track.length and direction == 0:
  80. track_idx = current_track.length - 1
  81. if current_track.end_point == 0:
  82. direction = 1
  83. else:
  84. detach_from_rail()
  85. return
  86. if track_idx < 0 and direction == 1:
  87. track_idx = 0
  88. if current_track.start_point == 0:
  89. direction = 0
  90. else:
  91. detach_from_rail()
  92. return
  93. check_for_rail()
  94. func detach_from_rail() -> void:
  95. can_attach = false
  96. travelling_on_rail = false
  97. track_idx = -1
  98. velocity = direction_vector * speed * 48
  99. await get_tree().create_timer(0.1, false).timeout
  100. can_attach = true