From b862fa80840c33c80a66d609082ee01a83ec3320 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Wed, 25 Dec 2024 17:38:19 +0100 Subject: [PATCH] add coyote timer --- objects/brick_player.tscn | 2 ++ scripts/player.gd | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/objects/brick_player.tscn b/objects/brick_player.tscn index 7abc589..1464af6 100644 --- a/objects/brick_player.tscn +++ b/objects/brick_player.tscn @@ -36,3 +36,5 @@ script = ExtResource("5_geu10") eye_left = NodePath("../Root/Left Eye") eye_right = NodePath("../Root/Right Eye") player_controller = NodePath("..") + +[node name="CoyoteTimer" type="Timer" parent="."] diff --git a/scripts/player.gd b/scripts/player.gd index 404df24..e153998 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -6,15 +6,24 @@ extends CharacterBody2D var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") @onready var root = $Root +@onready var coyote_timer: Timer = $CoyoteTimer @export var jump_height: float = 100 @export var jump_time_to_peak: float = 0.5 @export var jump_time_to_descent: float = 0.4 +@export var coyote_frames: int = 6 +@export var coyote_mode: bool = false +@export var was_last_floor: bool = false + @onready var jump_velocity: float = ((2.0 * jump_height) / jump_time_to_peak) * -1.0 @onready var jump_gravity: float = ((-2.0 * jump_height) / (jump_time_to_peak * jump_time_to_peak)) * -1.0 @onready var fall_gravity: float = ((-2.0 * jump_height) / (jump_time_to_descent * jump_time_to_descent)) * -1.0 +func _ready() -> void: + coyote_timer.timeout.connect(on_coyote_timer_timeout) + coyote_timer.wait_time = coyote_frames / 60.0 + func _process(_delta): if velocity.x > 0.0: root.rotation = deg_to_rad(-10) @@ -24,10 +33,20 @@ func _process(_delta): root.rotation = 0 func _physics_process(delta): + if is_on_floor(): + was_last_floor = true + coyote_mode = false # Reset coyote mode when back on the floor + coyote_timer.stop() # Stop timer when grounded + else: + if was_last_floor: # Start coyote timer only once + coyote_mode = true + coyote_timer.start() + was_last_floor = false + if not is_on_floor(): velocity.y += calculate_gravity() * delta - if Input.is_action_pressed("jump") and is_on_floor(): + if Input.is_action_pressed("jump") and (is_on_floor() or coyote_mode): jump() var direction = Input.get_axis("left", "right") @@ -40,6 +59,10 @@ func _physics_process(delta): func jump(): velocity.y = jump_velocity + coyote_mode = false func calculate_gravity() -> float: return jump_gravity if velocity.y < 0.0 else fall_gravity + +func on_coyote_timer_timeout(): + coyote_mode = false