saut: permettre le saut trop anticipé

Si le joueur appuyer sur la touche de saut un peu avant de toucher le
sol, alors le personnage sautera au moment d'avoir touché le sol. ça
permet au joueur de ne pas avoir totalement besoin d'être super précis
sur la commande de saut pour pourtant faire bouger le personnage.
Le mécanisme utilise un RayCast sous les pieds de la princesse pour
savoir où se trouve le sol.
Il restera à tuner cette variable dans le futur.
This commit is contained in:
Thomas Lavocat
2023-04-29 15:56:31 +02:00
parent c38297ec28
commit 5480209285
2 changed files with 17 additions and 7 deletions

View File

@@ -7,6 +7,7 @@ extends CharacterBody2D
var gravity: int = ProjectSettings.get("physics/2d/default_gravity")
@onready var wall_detect_left := $wall_detect_left as RayCast2D
@onready var wall_detect_right := $wall_detect_right as RayCast2D
@onready var ground_far_detect := $ground_far_detect as RayCast2D
@onready var animation := $AnimatedSprite2D as AnimatedSprite2D
@onready var camera := $Camera2D as Camera2D
@@ -43,6 +44,7 @@ var init_direction_change = true
# variables d'état relatives au saut
var jumping : bool = false
var need_jump : bool = false
var jumping_step : int = -1
var jump_key_counter : int = 0
@@ -170,11 +172,15 @@ func _physics_process(delta: float) -> void:
grab_wall = pressing_wall and Input.is_action_pressed("grab" + action_suffix)
if Input.is_action_just_pressed("jump" + action_suffix):
if is_on_floor():
if not jumping:
jumping = true
jumping_step = JUMP_SPEED_TABLE.size()-1
jump_key_counter = 0
if ground_far_detect.is_colliding() or is_on_floor():
need_jump=true
if need_jump and is_on_floor():
need_jump = false
if not jumping:
jumping = true
jumping_step = JUMP_SPEED_TABLE.size()-1
jump_key_counter = 0
if Input.is_action_pressed("jump" + action_suffix):
if jumping:

View File

@@ -215,6 +215,10 @@ animation = &"idle"
target_position = Vector2(-10, 0)
collision_mask = 16
[node name="wall_detect_right" type="RayCast2D" parent="."]
target_position = Vector2(14, 0)
collision_mask = 16
[node name="Camera2D" type="Camera2D" parent="."]
zoom = Vector2(2, 2)
@@ -222,6 +226,6 @@ zoom = Vector2(2, 2)
position = Vector2(2, 0)
shape = SubResource("CapsuleShape2D_6r7th")
[node name="wall_detect_right" type="RayCast2D" parent="."]
target_position = Vector2(14, 0)
[node name="ground_far_detect" type="RayCast2D" parent="."]
target_position = Vector2(0, 38)
collision_mask = 16