jump.gd 1.8 KB

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