Plutôt que d'annuler le saut immédiatement, juste empécher le refill. De cette manière lorsque la princesse se tape la tête au plafond, elle garde un peu d'apesanteur sans pour autant rester coincé là haut.
155 lines
4.5 KiB
GDScript
155 lines
4.5 KiB
GDScript
extends CharacterBody2D
|
|
|
|
## The player listens for input actions appended with this suffix.[br]
|
|
## Used to separate controls for multiple players in splitscreen.
|
|
@export var action_suffix := ""
|
|
|
|
var gravity: int = ProjectSettings.get("physics/2d/default_gravity")
|
|
@onready var platform_detector := $PlatformDetector as RayCast2D
|
|
@onready var animation := $AnimatedSprite2D as AnimatedSprite2D
|
|
@onready var animation_player := $AnimationPlayer as AnimationPlayer
|
|
@onready var camera := $Camera2D as Camera2D
|
|
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.1, 0.2, 0.5,
|
|
0.6, 0.7, 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'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
|
|
@export var JUMPING_KEY_COUNTER_THRESHOLD = 3
|
|
|
|
# Nombre de frames coyote durant lesquelle le joueur peut encore sauter
|
|
# sans encore être au sol
|
|
@export var COYOTE_LENGTH := 20
|
|
|
|
var walking : bool = false
|
|
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:
|
|
var table = X_SPEED_TABLE
|
|
if walking:
|
|
walking_step = min(walking_step+1, table.size() -1)
|
|
init_decel = true
|
|
else:
|
|
table = X_SPEED_DECEL
|
|
if init_decel:
|
|
walking_step = min(walking_step, table.size() - 1)
|
|
init_decel = false
|
|
|
|
if walking_step > 0:
|
|
var speed = table[walking_step] * WALKING_SPEED
|
|
if not walking:
|
|
walking_step-=1
|
|
return speed * direction
|
|
return 0
|
|
|
|
|
|
func fall() -> int:
|
|
if jumping or is_on_floor():
|
|
falling_step = -1
|
|
return velocity.y
|
|
else:
|
|
falling_step = min(falling_step+1, FALL_SPEED_TABLE.size()-1)
|
|
return FALL_SPEED_TABLE[falling_step] * FALLING_SPEED
|
|
|
|
|
|
func jump() -> int:
|
|
if not jumping:
|
|
return velocity.y
|
|
|
|
if not is_on_ceiling() and 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):
|
|
if is_on_floor():
|
|
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:
|
|
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)
|
|
)
|
|
|
|
velocity.x = walk(
|
|
Input.get_axis(
|
|
"move_left" + action_suffix,
|
|
"move_right" + action_suffix
|
|
)
|
|
)
|
|
|
|
if not is_zero_approx(velocity.x):
|
|
if velocity.x > 0.0:
|
|
animation.scale.x = 1.0
|
|
else:
|
|
animation.scale.x = -1.0
|
|
|
|
floor_stop_on_slope = not platform_detector.is_colliding()
|
|
move_and_slide() # Character is colliding
|
|
|
|
var anim := get_new_animation(false)
|
|
if anim != animation.animation:
|
|
animation.animation = anim
|
|
animation.play()
|
|
|
|
|
|
func get_new_animation(is_shooting := false) -> String:
|
|
var animation_new: String
|
|
|
|
if is_on_floor():
|
|
if walking_step > 0:
|
|
animation_new = "walk"
|
|
else:
|
|
animation_new = "idle"
|
|
else:
|
|
if velocity.y > 0.0:
|
|
if walking_step > 0:
|
|
animation_new = "falling_diagonals"
|
|
else:
|
|
animation_new = "falling_straight"
|
|
else:
|
|
animation_new = "jumping"
|
|
return animation_new
|