player.gd 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. class_name Player extends CharacterBody2D
  2. signal coin_collected()
  3. const WALK_SPEED = 300.0
  4. const ACCELERATION_SPEED = WALK_SPEED * 6.0
  5. const JUMP_VELOCITY = -725.0
  6. ## Maximum speed at which the player can fall.
  7. const TERMINAL_VELOCITY = 700
  8. ## The player listens for input actions appended with this suffix.[br]
  9. ## Used to separate controls for multiple players in splitscreen.
  10. @export var action_suffix := ""
  11. var gravity: int = ProjectSettings.get("physics/2d/default_gravity")
  12. @onready var platform_detector := $PlatformDetector as RayCast2D
  13. @onready var animation_player := $AnimationPlayer as AnimationPlayer
  14. @onready var shoot_timer := $ShootAnimation as Timer
  15. @onready var sprite := $Sprite2D as Sprite2D
  16. @onready var jump_sound := $Jump as AudioStreamPlayer2D
  17. @onready var gun = sprite.get_node(^"Gun") as Gun
  18. @onready var camera := $Camera as Camera2D
  19. var _double_jump_charged := false
  20. func _physics_process(delta: float) -> void:
  21. if is_on_floor():
  22. _double_jump_charged = true
  23. if Input.is_action_just_pressed("jump" + action_suffix):
  24. try_jump()
  25. elif Input.is_action_just_released("jump" + action_suffix) and velocity.y < 0.0:
  26. # The player let go of jump early, reduce vertical momentum.
  27. velocity.y *= 0.6
  28. # Fall.
  29. velocity.y = minf(TERMINAL_VELOCITY, velocity.y + gravity * delta)
  30. var direction := Input.get_axis("move_left" + action_suffix, "move_right" + action_suffix) * WALK_SPEED
  31. velocity.x = move_toward(velocity.x, direction, ACCELERATION_SPEED * delta)
  32. if not is_zero_approx(velocity.x):
  33. if velocity.x > 0.0:
  34. sprite.scale.x = 1.0
  35. else:
  36. sprite.scale.x = -1.0
  37. floor_stop_on_slope = not platform_detector.is_colliding()
  38. move_and_slide()
  39. var is_shooting := false
  40. if Input.is_action_just_pressed("shoot" + action_suffix):
  41. is_shooting = gun.shoot(sprite.scale.x)
  42. var animation := get_new_animation(is_shooting)
  43. if animation != animation_player.current_animation and shoot_timer.is_stopped():
  44. if is_shooting:
  45. shoot_timer.start()
  46. animation_player.play(animation)
  47. func get_new_animation(is_shooting := false) -> String:
  48. var animation_new: String
  49. if is_on_floor():
  50. if absf(velocity.x) > 0.1:
  51. animation_new = "run"
  52. else:
  53. animation_new = "idle"
  54. else:
  55. if velocity.y > 0.0:
  56. animation_new = "falling"
  57. else:
  58. animation_new = "jumping"
  59. if is_shooting:
  60. animation_new += "_weapon"
  61. return animation_new
  62. func try_jump() -> void:
  63. if is_on_floor():
  64. jump_sound.pitch_scale = 1.0
  65. elif _double_jump_charged:
  66. _double_jump_charged = false
  67. velocity.x *= 2.5
  68. jump_sound.pitch_scale = 1.5
  69. else:
  70. return
  71. velocity.y = JUMP_VELOCITY
  72. jump_sound.play()