Untitled
extends CharacterBody2D const SPEED = 125.0 const ACCELERATION = 800.0 const FRICTION = 1200.0 const JUMP_VELOCITY = -300.0 const DASH_SPEED = 300.0 const WALL_JUMP_PUSHBACK = 200 const WALL_SLIDE_GRAVITY = 100 @onready var jump_buffer_timer = $JumpBufferTimer @onready var anim = $AnimatedSprite2D @onready var coyote_timer = $CoyoteTimer @onready var dash_timer = $DashTimer var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") var can_coyote_jump = false var jump_buffered = false var dashing = false var is_wall_sliding = false var input_axis = Input.get_axis("ui_left", "ui_right") func _physics_process(delta): apply_gravity(delta) handle_jump() handle_wall_slide(delta) handle_dash() var input_axis = Input.get_axis("ui_left", "ui_right") if input_axis == -1: get_node("AnimatedSprite2D").flip_h = true elif input_axis == 1: get_node("AnimatedSprite2D").flip_h = false handle_acceleration(input_axis, delta) apply_friction(input_axis, delta) handle_animation() var was_on_wall = is_on_wall() var was_on_floor = is_on_floor() move_and_slide() if was_on_floor and !is_on_floor() and velocity.y >= 0: can_coyote_jump = true coyote_timer.start() if is_on_floor() and jump_buffered: jump_buffered = false _jump() func apply_gravity(delta): if not is_on_floor() and not can_coyote_jump: velocity.y += gravity * delta func handle_jump(): if Input.is_action_just_pressed("ui_accept"): if is_on_floor() or can_coyote_jump: velocity.y = JUMP_VELOCITY if Input.is_action_just_pressed("ui_accept"): if is_on_wall() and !is_on_floor(): velocity.y = JUMP_VELOCITY velocity.x = -WALL_JUMP_PUSHBACK else: jump_buffered = true jump_buffer_timer.start() anim.play("jump") elif Input.is_action_just_released("ui_accept") and velocity.y < JUMP_VELOCITY / 2: velocity.y = JUMP_VELOCITY / 2 func handle_dash(): if Input.is_action_just_pressed("dash"): dashing = true dash_timer.start() func handle_acceleration(input_axis, delta): var target_speed = SPEED if not dashing else DASH_SPEED if input_axis != 0: velocity.x = move_toward(velocity.x, target_speed * input_axis, ACCELERATION * delta) func apply_friction(input_axis, delta): if input_axis == 0: velocity.x = move_toward(velocity.x, 0, FRICTION * delta) func handle_animation(): if velocity.y == 0: anim.play("RUN" if velocity.x != 0 else "idle") elif velocity.y > 0: anim.play("fall") func _jump(): velocity.y = JUMP_VELOCITY can_coyote_jump = false func _on_coyote_timer_timeout(): can_coyote_jump = false func _on_jump_buffer_timer_timeout(): jump_buffered = false func _on_dash_timer_timeout(): dashing = false func handle_wall_slide(delta): if is_on_wall() and !is_on_floor(): if Input.is_action_pressed("ui_left") or Input.is_action_pressed("ui_right"): is_wall_sliding = true else: is_wall_sliding = false else: is_wall_sliding = false if is_wall_sliding: velocity.y += (WALL_SLIDE_GRAVITY * delta) velocity.y = min(velocity.y, WALL_SLIDE_GRAVITY)
Leave a Comment