extends KinematicBody2D class_name Player export var run_speed := 100 export var jump_power := 180 export var gravity: = 500 export var max_gravity:= 500 export var respawn_position:=Vector2.ZERO export var wall_slide_friction:=.2 export var wall_jump_speed_factor := Vector2(2.5 ,.85) export var dash_thrust = 500 signal landed signal jumping signal died var _velocity: Vector2 = Vector2.ZERO var _landing_position:Vector2 = Vector2.ZERO var _in_air = false; var _is_wall_jumping := false var _alive := false; var _is_wall_sliding := false; var _is_dashing := false; var _can_dash := true; func _ready() -> void: $AnimationPlayer.play("fade_in") respawn_position = position func _physics_process(delta: float) -> void: if _alive: var is_jump_canceled: = Input.is_action_just_released("jump") and !_is_wall_jumping and _velocity.y < 0.0 var direction: = get_direction() _is_wall_sliding = _velocity.y >=0 and is_on_wall() and !is_on_floor() _velocity = calculate_move_velocity(direction,is_jump_canceled, delta) _velocity = move_and_slide(_velocity, Vector2.UP) update_sprite(direction) if is_on_floor() and !_is_dashing: _can_dash = true; if is_on_floor() and _in_air and !_is_dashing: _in_air = false _is_wall_jumping = false emit_signal("landed", position) if _is_wall_sliding: _is_wall_jumping = false func get_direction() -> Vector2: var right_strength = min(Input.get_action_strength("direction_right") * 2, 1) var left_strength = min(Input.get_action_strength("direction_left") * 2, 1) return Vector2( right_strength - left_strength, -1.0 if Input.is_action_just_pressed("jump") and (is_on_floor() or is_on_wall()) else 1.0 ) func calculate_move_velocity(direction:Vector2, is_jump_canceled:bool, delta:float)->Vector2: var output: = _velocity var current_gravity = gravity * wall_slide_friction if _is_wall_sliding else gravity output.x = lerp(_velocity.x, run_speed * direction.x, .09 if _in_air else .5) output.y += current_gravity * delta if direction.y == -1.0: # we are jumping _in_air = true if _is_wall_sliding: # wall jump _is_wall_jumping = true var desired = -(run_speed * wall_jump_speed_factor.x * direction.x) output.x = desired output.y = jump_power * wall_jump_speed_factor.y * direction.y pass else: #jump output.y = jump_power * direction.y emit_signal("jumping", position) if not _is_dashing: if is_jump_canceled: output.y = 0 if output.y < max_gravity*-1: output.y = max_gravity if output.y > 0 and !is_on_floor(): _in_air = true; if _is_dashing: return _velocity var dash_velocity := Vector2( (-1 if $Sprite.flip_h else 1) * dash_thrust, 0 ) if Input.is_action_just_pressed("dash") and _can_dash and !_is_wall_sliding: output = dash_velocity _can_dash = false _is_dashing = true # turn off gravity while dashing $DashParticles.emitting = true $DashParticles.position = Vector2( position.x, position.y - 8 ) $DashParticles.set_as_toplevel(true) $DashTimeout.start() return output; func _respawn(): position = respawn_position _velocity = Vector2.ZERO $AnimationPlayer.play("fade_in") func _revive(): _alive = true func update_sprite(direction:Vector2)->void: var air_animation = "jump" if _velocity.y <= 0 else "fall" if _velocity.x > .5 and not _is_wall_sliding: $AnimationPlayer.play("run" if is_on_floor() else air_animation) $Sprite.flip_h = false elif _velocity.x < -.5 and not _is_wall_sliding: $AnimationPlayer.play("run" if is_on_floor() else air_animation) $Sprite.flip_h = true else: if not _in_air: $AnimationPlayer.play("idle") if _is_wall_sliding: $AnimationPlayer.play("wall_slide") return if _in_air: $AnimationPlayer.play(air_animation) return if _is_dashing: return $AnimationPlayer.play("jump") func _on_die_animation_done(): emit_signal("died") _respawn() func die(): _alive = false $AnimationPlayer.play("die") func _on_DashTimeout_timeout() -> void: $DashParticles.emitting = false _velocity.x = run_speed * _velocity.normalized().x; _is_dashing = false pass # Replace with function body.