player.gd 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. class_name Player
  2. extends CharacterBody2D
  3. # Keep this in sync with the AnimationTree's state names.
  4. const States = {
  5. IDLE = "idle",
  6. WALK = "walk",
  7. RUN = "run",
  8. FLY = "fly",
  9. FALL = "fall",
  10. }
  11. const WALK_SPEED = 200.0
  12. const ACCELERATION_SPEED = WALK_SPEED * 6.0
  13. const JUMP_VELOCITY = -400.0
  14. ## Maximum speed at which the player can fall.
  15. const TERMINAL_VELOCITY = 400
  16. var falling_slow = false
  17. var falling_fast = false
  18. var no_move_horizontal_time = 0.0
  19. @onready var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
  20. @onready var sprite = $Sprite2D
  21. @onready var sprite_scale = sprite.scale.x
  22. func _ready():
  23. $AnimationTree.active = true
  24. func _physics_process(delta: float) -> void:
  25. var is_jumping = false
  26. if Input.is_action_just_pressed("jump"):
  27. is_jumping = try_jump()
  28. elif Input.is_action_just_released("jump") and velocity.y < 0.0:
  29. # The player let go of jump early, reduce vertical momentum.
  30. velocity.y *= 0.6
  31. # Fall.
  32. velocity.y = minf(TERMINAL_VELOCITY, velocity.y + gravity * delta)
  33. var direction := Input.get_axis("move_left", "move_right") * WALK_SPEED
  34. velocity.x = move_toward(velocity.x, direction, ACCELERATION_SPEED * delta)
  35. if no_move_horizontal_time > 0.0:
  36. # After doing a hard fall, don't move for a short time.
  37. velocity.x = 0.0
  38. no_move_horizontal_time -= delta
  39. if not is_zero_approx(velocity.x):
  40. if velocity.x > 0.0:
  41. sprite.scale.x = 1.0 * sprite_scale
  42. else:
  43. sprite.scale.x = -1.0 * sprite_scale
  44. move_and_slide()
  45. # After applying our motion, update our animation to match.
  46. # Calculate falling speed for animation purposes.
  47. if velocity.y >= TERMINAL_VELOCITY:
  48. falling_fast = true
  49. falling_slow = false
  50. elif velocity.y > 300:
  51. falling_slow = true
  52. if is_jumping:
  53. $AnimationTree["parameters/jump/request"] = AnimationNodeOneShot.ONE_SHOT_REQUEST_FIRE
  54. if is_on_floor():
  55. # Most animations change when we run, land, or take off.
  56. if falling_fast:
  57. $AnimationTree["parameters/land_hard/request"] = AnimationNodeOneShot.ONE_SHOT_REQUEST_FIRE
  58. no_move_horizontal_time = 0.4
  59. elif falling_slow:
  60. $AnimationTree["parameters/land/request"] = AnimationNodeOneShot.ONE_SHOT_REQUEST_FIRE
  61. if abs(velocity.x) > 50:
  62. $AnimationTree["parameters/state/transition_request"] = States.RUN
  63. $AnimationTree["parameters/run_timescale/scale"] = abs(velocity.x) / 60
  64. elif velocity.x:
  65. $AnimationTree["parameters/state/transition_request"] = States.WALK
  66. $AnimationTree["parameters/walk_timescale/scale"] = abs(velocity.x) / 12
  67. else:
  68. $AnimationTree["parameters/state/transition_request"] = States.IDLE
  69. falling_fast = false
  70. falling_slow = false
  71. else:
  72. if velocity.y > 0:
  73. $AnimationTree["parameters/state/transition_request"] = States.FALL
  74. else:
  75. $AnimationTree["parameters/state/transition_request"] = States.FLY
  76. func try_jump() -> bool:
  77. if is_on_floor():
  78. velocity.y = JUMP_VELOCITY
  79. return true
  80. return false