From 5480209285c50b9c07da722b8ffa62b3a97f3802 Mon Sep 17 00:00:00 2001 From: Thomas Lavocat Date: Sat, 29 Apr 2023 15:56:31 +0200 Subject: [PATCH] =?UTF-8?q?saut:=20permettre=20le=20saut=20trop=20anticip?= =?UTF-8?q?=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- princesse.gd | 16 +++++++++++----- princesse.tscn | 8 ++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/princesse.gd b/princesse.gd index 19a96f9..826dfa9 100644 --- a/princesse.gd +++ b/princesse.gd @@ -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: diff --git a/princesse.tscn b/princesse.tscn index 7cce985..746270d 100644 --- a/princesse.tscn +++ b/princesse.tscn @@ -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