From 4c0513e84aa2ac2dcd8f7df4ba69ca299a4bd9be Mon Sep 17 00:00:00 2001 From: Haze Weathers Date: Tue, 23 May 2023 12:06:55 -0400 Subject: [PATCH] walking acceleration when player is grounded --- objects/player/player_scholar.gd | 13 +++++++++++++ objects/player/player_scholar.tscn | 5 +++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/objects/player/player_scholar.gd b/objects/player/player_scholar.gd index 3ad1301..ffbc17f 100644 --- a/objects/player/player_scholar.gd +++ b/objects/player/player_scholar.gd @@ -10,6 +10,8 @@ const DeathSplatter = preload("res://objects/player/player_death_particles.tscn" # EXPORTS # ## horizontal movement speed export var walk_speed: float = 50.0 +## frames until walk speed peak (at 60fps reference) +export var walk_acceleration_frames: float = 1.0 ## speed to push pushable objects at export var push_speed: float = 25.0 ## climbing speed @@ -232,6 +234,17 @@ func _process_horizontal_movement(delta: float) -> void: if input_dir != 0.0: graphics.scale.x = input_dir +## player movement with acceleration +func _process_horizontal_movement_grounded(delta: float) -> void: + var input_dir = sign(Input.get_axis("ui_left", "ui_right")) # sign() to normalize + if input_dir == 0.0 or input_dir != sign(velocity.x): + velocity.x = 0.0 + var acceleration = lerp(0.0, walk_speed, 1.0 / walk_acceleration_frames) * 60.0 + velocity.x += input_dir * acceleration * delta + velocity.x = clamp(velocity.x, -walk_speed, walk_speed) + if input_dir != 0.0: + graphics.scale.x = input_dir + ## walk/idle state func _process_can_walk(delta: float) -> void: if sign(Input.get_axis("ui_left", "ui_right")) != 0.0: diff --git a/objects/player/player_scholar.tscn b/objects/player/player_scholar.tscn index fea69b5..714f1c4 100644 --- a/objects/player/player_scholar.tscn +++ b/objects/player/player_scholar.tscn @@ -762,6 +762,7 @@ collision_layer = 128 collision_mask = 7 moving_platform_apply_velocity_on_leave = 1 script = ExtResource( 1 ) +walk_acceleration_frames = 8.0 gravity = 700.0 jump_force = 140.0 double_jump_force = 124.0 @@ -1158,7 +1159,7 @@ script = ExtResource( 8 ) [connection signal="state_entered" from="StateChart/Root/Movement/Grounded" to="." method="_on_Grounded_state_entered"] [connection signal="state_physics_processing" from="StateChart/Root/Movement/Grounded" to="." method="_process_grounded"] [connection signal="state_physics_processing" from="StateChart/Root/Movement/Grounded/CanWalk" to="." method="_process_can_walk"] -[connection signal="state_physics_processing" from="StateChart/Root/Movement/Grounded/CanWalk" to="." method="_process_horizontal_movement"] +[connection signal="state_physics_processing" from="StateChart/Root/Movement/Grounded/CanWalk" to="." method="_process_horizontal_movement_grounded"] [connection signal="state_entered" from="StateChart/Root/Movement/Grounded/CanWalk/Still" to="." method="_on_Still_state_entered"] [connection signal="state_entered" from="StateChart/Root/Movement/Grounded/CanWalk/Walking" to="." method="_on_Walking_state_entered"] [connection signal="state_entered" from="StateChart/Root/Movement/Grounded/CanWalk/Blinking" to="." method="_on_Blinking_state_entered"] @@ -1168,8 +1169,8 @@ script = ExtResource( 8 ) [connection signal="state_physics_processing" from="StateChart/Root/Movement/Grounded/Pushing" to="." method="_process_pushing"] [connection signal="state_entered" from="StateChart/Root/Movement/Airborne" to="." method="_on_Airborne_state_entered"] [connection signal="state_physics_processing" from="StateChart/Root/Movement/Airborne" to="." method="_process_gravity"] -[connection signal="state_physics_processing" from="StateChart/Root/Movement/Airborne/Jump" to="." method="_process_jump"] [connection signal="state_physics_processing" from="StateChart/Root/Movement/Airborne/Jump" to="." method="_process_horizontal_movement"] +[connection signal="state_physics_processing" from="StateChart/Root/Movement/Airborne/Jump" to="." method="_process_jump"] [connection signal="state_entered" from="StateChart/Root/Movement/Airborne/Jump/NormalJump" to="." method="_on_NormalJump_state_entered"] [connection signal="state_exited" from="StateChart/Root/Movement/Airborne/Jump/NormalJump" to="." method="_on_NormalJump_state_exited"] [connection signal="state_entered" from="StateChart/Root/Movement/Airborne/Jump/LadderJump" to="." method="_on_LadderJump_state_entered"]