extends CharacterBody2D const WALK_SPEED = 200.0 const ACCELERATION_SPEED = WALK_SPEED * 6.0 const JUMP_VELOCITY = -400.0 ## Maximum speed at which the player can fall. const TERMINAL_VELOCITY = 400 ## The player listens for input actions appended with this suffix.[br] ## Used to separate controls for multiple players in splitscreen. @export var action_suffix := "" var gravity: int = ProjectSettings.get("physics/2d/default_gravity") @onready var platform_detector := $PlatformDetector as RayCast2D @onready var animation := $AnimatedSprite2D as AnimatedSprite2D @onready var animation_player := $AnimationPlayer as AnimationPlayer @onready var camera := $Camera2D as Camera2D var _double_jump_charged := false func _physics_process(delta: float) -> void: if is_on_floor(): _double_jump_charged = true if Input.is_action_just_pressed("jump" + action_suffix): try_jump() elif Input.is_action_just_released("jump" + action_suffix) and velocity.y < 0.0: # The player let go of jump early, reduce vertical momentum. velocity.y *= 0.6 # Fall. velocity.y = minf(TERMINAL_VELOCITY, velocity.y + gravity * delta) var direction := Input.get_axis("move_left" + action_suffix, "move_right" + action_suffix) * WALK_SPEED velocity.x = move_toward(velocity.x, direction, ACCELERATION_SPEED * delta) if not is_zero_approx(velocity.x): if velocity.x > 0.0: animation.scale.x = 1.0 else: animation.scale.x = -1.0 floor_stop_on_slope = not platform_detector.is_colliding() move_and_slide() # Character is colliding var anim := get_new_animation(false) if anim != animation.animation: animation.animation = anim animation.play() func get_new_animation(is_shooting := false) -> String: var animation_new: String if is_on_floor(): if absf(velocity.x) > 0.1: animation_new = "walk" else: animation_new = "idle" else: if velocity.y > 0.0: animation_new = "falling_straight" else: animation_new = "jumping" return animation_new func try_jump() -> void: if is_on_floor(): pass elif _double_jump_charged: _double_jump_charged = false velocity.x *= 2.5 else: return velocity.y = JUMP_VELOCITY