jump.gd 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. extends "../motion.gd"
  2. @export var base_max_horizontal_speed: float = 400.0
  3. @export var air_acceleration: float = 1000.0
  4. @export var air_deceleration: float = 2000.0
  5. @export var air_steering_power: float = 50.0
  6. @export var gravity: float = 1600.0
  7. var enter_velocity = Vector2()
  8. var max_horizontal_speed = 0.0
  9. var horizontal_speed = 0.0
  10. var horizontal_velocity = Vector2()
  11. var vertical_speed = 0.0
  12. var height = 0.0
  13. func initialize(speed, velocity):
  14. horizontal_speed = speed
  15. if speed > 0.0:
  16. max_horizontal_speed = speed
  17. else:
  18. max_horizontal_speed = base_max_horizontal_speed
  19. enter_velocity = velocity
  20. func enter():
  21. var input_direction = get_input_direction()
  22. update_look_direction(input_direction)
  23. if input_direction:
  24. horizontal_velocity = enter_velocity
  25. else:
  26. horizontal_velocity = Vector2()
  27. vertical_speed = 600.0
  28. owner.get_node(^"AnimationPlayer").play("idle")
  29. func update(delta):
  30. var input_direction = get_input_direction()
  31. update_look_direction(input_direction)
  32. move_horizontally(delta, input_direction)
  33. animate_jump_height(delta)
  34. if height <= 0.0:
  35. emit_signal("finished", "previous")
  36. func move_horizontally(delta, direction):
  37. if direction:
  38. horizontal_speed += air_acceleration * delta
  39. else:
  40. horizontal_speed -= air_deceleration * delta
  41. horizontal_speed = clamp(horizontal_speed, 0, max_horizontal_speed)
  42. var target_velocity = horizontal_speed * direction.normalized()
  43. var steering_velocity = (target_velocity - horizontal_velocity).normalized() * air_steering_power
  44. horizontal_velocity += steering_velocity
  45. owner.velocity = horizontal_velocity
  46. owner.move_and_slide()
  47. func animate_jump_height(delta):
  48. vertical_speed -= gravity * delta
  49. height += vertical_speed * delta
  50. height = max(0.0, height)
  51. owner.get_node(^"BodyPivot").position.y = -height