From e38d18b5cd5261d195ee36b61ae2da842080a4d0 Mon Sep 17 00:00:00 2001 From: Thomas Lavocat Date: Sun, 23 Apr 2023 09:13:37 +0200 Subject: [PATCH] saut: la princesse peut sauter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Si le joueur maintient la barre espace la princesse saute plus haut. Il faudra tuner les variables. Prochaine étape: modifier la décélération dans l'air. --- princesse.gd | 64 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/princesse.gd b/princesse.gd index c0ecef5..3c6b6b2 100644 --- a/princesse.gd +++ b/princesse.gd @@ -13,16 +13,15 @@ var _double_jump_charged := false @export var WALKING_SPEED = 400 @export var FALLING_SPEED = 400 +@export var JUMPING_SPEED = 400 @export var X_SPEED_TABLE = [0, 0.1, 0.15, 0.2, 0.3, 0.6, 0.9, 1] @export var X_SPEED_DECEL = [0, 0.1, 0.6, 1] @export var X_SPEED_AIR_DECEL =[0, 0.1, 0.6, 1] @export var FALL_SPEED_TABLE = [0, 0.1, 0.15, 0.2, 0.3, 0.6, 0.9, 1] -@export var JUMP_SPEED_TABLE = [0, 0.4, 0.8, 1.2, 1.6, 2, 2.4, 2.8, 3, 3, 4, 5] +@export var JUMP_SPEED_TABLE = [0, 0.1, 0.15, 0.2, 0.25, + 0.6, 0.65, 0.7, 0.75, 0.8, 0.9, 1] @export var KICK_SPEED_TABLE = [0, 0.2, 0.4, 0.6, 1, 1.6, 2.4, 3] - -# Nombre d'inréments composant un saut -@export var JUMPING_COUNTER = 20 # Nombre d'incrément à rajouter lorsque la touche de saut est maintenue @export var JUMPING_COUNTER_REFILL = 2 # Tous les combien d'incréments rajouter un refill sur le compteur @@ -37,6 +36,9 @@ var walking_step: int = -1 var init_decel: bool = true var jumping : bool = false +var jumping_step : int = -1 +var jump_key_counter : int = 0 + var falling_step : int = -1 func walk(direction:int) -> int: @@ -56,7 +58,8 @@ func walk(direction:int) -> int: walking_step-=1 return speed * direction return 0 - + + func fall() -> int: if jumping or is_on_floor(): falling_step = -1 @@ -64,18 +67,55 @@ func fall() -> int: else: falling_step = min(falling_step+1, FALL_SPEED_TABLE.size()-1) return FALL_SPEED_TABLE[falling_step] * FALLING_SPEED - + + +func jump() -> int: + if is_on_ceiling(): + end_jump() + + if not jumping: + return velocity.y + + if jump_key_counter > 0 and jump_key_counter % JUMPING_KEY_COUNTER_THRESHOLD == 0: + jumping_step = min( + jumping_step + JUMPING_COUNTER_REFILL, + JUMP_SPEED_TABLE.size() -1 + ) + + if jumping_step > 0: + jumping_step -= 1 + return JUMP_SPEED_TABLE[jumping_step] * JUMPING_SPEED * -1 + else: + end_jump() + return velocity.y + +func end_jump(): + jumping = false + jumping_step = -1 + jump_key_counter = 0 func _physics_process(delta: float) -> void: if Input.is_action_just_pressed("jump" + action_suffix): - pass - elif Input.is_action_just_released("jump" + action_suffix) and velocity.y < 0.0: - # The player let go of jump early, reduce vertical momentum. - pass + if is_on_floor(): + if not jumping: + jumping = true + jumping_step = JUMP_SPEED_TABLE.size()-1 + jump_key_counter = 0 - velocity.y = fall() + if Input.is_action_pressed("jump" + action_suffix): + if jumping: + jump_key_counter += 1 + else: + jump_key_counter = 0 + + velocity.y = jump() + velocity.y = fall() + + walking = ( + Input.is_action_pressed("move_left" + action_suffix) or + Input.is_action_pressed("move_right" + action_suffix) + ) - walking = Input.is_action_pressed("move_left" + action_suffix) or Input.is_action_pressed("move_right" + action_suffix) velocity.x = walk( Input.get_axis( "move_left" + action_suffix,