princesse: réorganisation codale

Pour y voir plus clair maintenant que la physique se porte bien dans ce
jeu vidéo, j'ai fait du ménage et organisé les fonctions de la
princesse. Ca devrait être plus facile de rajouter du fromage et des
sauts muraux qui demandent un peu plus d'intrication dans les états.
This commit is contained in:
Thomas Lavocat
2023-04-29 18:51:49 +02:00
parent 80fd4f4758
commit 4411cf825e
2 changed files with 111 additions and 74 deletions

View File

@@ -11,7 +11,11 @@ var gravity: int = ProjectSettings.get("physics/2d/default_gravity")
@onready var animation := $AnimatedSprite2D as AnimatedSprite2D @onready var animation := $AnimatedSprite2D as AnimatedSprite2D
@onready var camera := $Camera2D as Camera2D @onready var camera := $Camera2D as Camera2D
################################################################################
#
# Constantes de déplacement à pimper dans l'inspecteur
#
################################################################################
@export var WALKING_SPEED = 400 @export var WALKING_SPEED = 400
@export var FALLING_SPEED = 400 @export var FALLING_SPEED = 400
@export var JUMPING_SPEED = 400 @export var JUMPING_SPEED = 400
@@ -30,33 +34,49 @@ var gravity: int = ProjectSettings.get("physics/2d/default_gravity")
# sans encore être au sol # sans encore être au sol
@export var COYOTE_LENGTH := 15 @export var COYOTE_LENGTH := 15
# Différence de vitesse de ralentissement entre le sol et l'air
@export var WALK_INCR_GROUND : int = 1
@export var WALK_INCR_AIR : int = 3
################################################################################
#
# Etat de la princesse
#
################################################################################
# Direction dans laquelle est positioné le personnage. # Direction dans laquelle est positioné le personnage.
var direction : int = 1 var direction : int = 1
# Variables détats relatives à la marche. var walking : bool = false # si le joueur veut marche ou non
var walking : bool = false var walking_step: int = -1 # où la princesse en est dans son tableau d'accel
var walking_step: int = -1 var walk_incr_reserve : int = 0 # utilisé pour la différence ralentissement air/sol
@export var WALK_INCR_GROUND : int = 1 var init_decel: bool = true # utilisé pour initialiser la décélération
@export var WALK_INCR_AIR : int = 3 var init_direction_change = true # utilisé pour initialiser le changement de direction
var walk_incr_reserve : int = 0
var init_decel: bool = true
var init_direction_change = true
# variables d'état relatives au saut # variables d'état relatives au saut
var jumping : bool = false var jumping : bool = false # Princesse est-elle en train de sauter ?
var need_jump : bool = false var need_jump : bool = false # Le joueur veut-il sauter et peut il ?
var jumping_step : int = -1 var jumping_step : int = -1 # Où en est la princesse dans son tableau d'accel
var jump_key_counter : int = 0 var jump_key_counter : int = 0 # Où en est-on du refil ?
# Coyote time
var coyote_ground = [] var coyote_ground = []
var falling_step : int = -1 # variable d'état relative à la gravité
var falling_step : int = -1 # Où en est la princess dans son acceleration de chute ?
var pressing_wall = false # Variables d'état relative à l'accroche au mur
var grab_wall = false var pressing_wall = false # Princesse est elle en contact avec un mur ?
var grab_wall = false # Je joueur veut-il et peut-il s'accrocher au mur ?
################################################################################
const PLATFORM_LAYER = 1 << (5 - 1) # collision layer 5 #
const PICS_BLOCK_LAYER = 1 << (6 - 1) # collision layer 6 # Gestion d'avec quoi Princesse collisionne
#
################################################################################
const PLATFORM_LAYER = 1 << (5 - 1) # collision layer 5 -> plateformes
const PICS_BLOCK_LAYER = 1 << (6 - 1) # collision layer 6 -> pics
const CHEESE_LAYER = 1 << (6 - 1) # collision layer 7 -> fromages
var layer_of_collision = null var layer_of_collision = null
func walk(direction:int) -> float: func walk(direction:int) -> float:
@@ -137,8 +157,8 @@ func walk(direction:int) -> float:
return speed * direction return speed * direction
return velocity.x return velocity.x
func fall() -> float: func fall() -> float:
# fait tomber princesse
if jumping: if jumping:
falling_step = -1 falling_step = -1
return velocity.y return velocity.y
@@ -162,8 +182,8 @@ func fall() -> float:
falling_step = min(falling_step+1, FALL_SPEED_TABLE.size()-1) falling_step = min(falling_step+1, FALL_SPEED_TABLE.size()-1)
return FALL_SPEED_TABLE[falling_step] * FALLING_SPEED return FALL_SPEED_TABLE[falling_step] * FALLING_SPEED
func jump() -> float: func jump() -> float:
# fait sauter princesse
if not jumping: if not jumping:
return velocity.y return velocity.y
@@ -181,11 +201,14 @@ func jump() -> float:
return velocity.y return velocity.y
func end_jump(): func end_jump():
# termine le saut de Princesse
jumping = false jumping = false
jumping_step = -1 jumping_step = -1
jump_key_counter = 0 jump_key_counter = 0
func choose_animation_orientation() -> void: func choose_animation_orientation() -> void:
# Oriente l'animation correctement en fonction de laquelle on joue et de
# la direction du personnage
if not is_zero_approx(velocity.x): if not is_zero_approx(velocity.x):
if velocity.x > 0.0: if velocity.x > 0.0:
animation.scale.x = 1.0 animation.scale.x = 1.0
@@ -199,76 +222,89 @@ func choose_animation_orientation() -> void:
animation.scale.x = -1 animation.scale.x = -1
func play_animation() -> void: func play_animation() -> void:
# Joue l'animation de Princesse et dans le bon sens
choose_animation_orientation()
var anim := get_new_animation() var anim := get_new_animation()
if anim != animation.animation: if anim != animation.animation:
animation.animation = anim animation.animation = anim
animation.play() animation.play()
func _physics_process(delta: float) -> void: func move_and_handle_collisions() -> void:
# Bouge Princesse et réagis aux éléments avec lesquels elle rentre en collision
coyote_ground.append(is_on_floor())
if coyote_ground.size() > COYOTE_LENGTH:
coyote_ground.remove_at(0)
pressing_wall = wall_detect_left.is_colliding() or wall_detect_right.is_colliding()
grab_wall = pressing_wall and Input.is_action_pressed("grab" + action_suffix)
if Input.is_action_just_pressed("jump" + action_suffix):
if ground_far_detect.is_colliding() or (is_on_floor() or coyote_ground[0]):
need_jump=true
if need_jump and (is_on_floor() or coyote_ground[0]):
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:
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)
)
var axis = Input.get_axis(
"move_left" + action_suffix,
"move_right" + action_suffix
)
if not is_zero_approx(axis):
if axis < 0:
direction = -1
else:
direction = 1
velocity.x = walk(direction)
#floor_stop_on_slope = not platform_detector.is_colliding()
var collision = move_and_slide() var collision = move_and_slide()
if collision: if collision:
var collider = get_last_slide_collision().get_collider() var collider = get_last_slide_collision().get_collider()
if collider is TileMap: if collider is TileMap:
var tile_rid = get_last_slide_collision().get_collider_rid() var tile_rid = get_last_slide_collision().get_collider_rid()
layer_of_collision = PhysicsServer2D.body_get_collision_layer(tile_rid) layer_of_collision = PhysicsServer2D.body_get_collision_layer(tile_rid)
if layer_of_collision == PICS_BLOCK_LAYER: if layer_of_collision == PICS_BLOCK_LAYER:
get_tree(). reload_current_scene() get_tree(). reload_current_scene()
choose_animation_orientation() func read_input() -> void:
# Lis les commandes du joueur pour piloter Princesse
# Le joueur veut-il et peut-il s'accrocher au mur ?
grab_wall = pressing_wall and Input.is_action_pressed("grab" + action_suffix)
# Le joueur veut-il sauter ?
if Input.is_action_just_pressed("jump" + action_suffix):
# Peut-il sauter ?
if ground_far_detect.is_colliding() or (is_on_floor() or coyote_ground[0]):
need_jump=true
# Dès qu'il peut et veut sauter, déclencher le saut
if need_jump and (is_on_floor() or coyote_ground[0]):
need_jump = false
if not jumping:
jumping = true
jumping_step = JUMP_SPEED_TABLE.size()-1
jump_key_counter = 0
# Si il continue d'appuyer, lui rallonger son saut
if Input.is_action_pressed("jump" + action_suffix):
if jumping:
jump_key_counter += 1
else:
jump_key_counter = 0
# Le joueur veut-il marcher ?
walking = (
Input.is_action_pressed("move_left" + action_suffix) or
Input.is_action_pressed("move_right" + action_suffix)
)
# Calculer dans quelle direction il veut marcher
var axis = Input.get_axis(
"move_left" + action_suffix,
"move_right" + action_suffix
)
if not is_zero_approx(axis):
if axis < 0:
direction = -1
else:
direction = 1
func compute_state() -> void:
# Met à jour une partie de l'état de la princesse
# gestion du coyote time sur le contact au sol
coyote_ground.append(is_on_floor())
if coyote_ground.size() > COYOTE_LENGTH:
coyote_ground.remove_at(0)
# gestion de l'état de la pression au mur
pressing_wall = wall_detect_left.is_colliding() or wall_detect_right.is_colliding()
func _physics_process(delta: float) -> void:
compute_state()
read_input()
velocity.y = jump()
velocity.y = fall()
velocity.x = walk(direction)
move_and_handle_collisions()
play_animation() play_animation()
func get_new_animation() -> String: func get_new_animation() -> String:
# Renvoie la bonne annimation en fonction de l'état de la princesse
var animation_new: String var animation_new: String
if is_on_floor(): if is_on_floor():

View File

@@ -209,7 +209,8 @@ WALK_INCR_AIR = 6
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] [node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
sprite_frames = SubResource("SpriteFrames_q52wx") sprite_frames = SubResource("SpriteFrames_q52wx")
animation = &"idle" animation = &"jump_impulsion"
frame_progress = 0.443433
[node name="wall_detect_left" type="RayCast2D" parent="."] [node name="wall_detect_left" type="RayCast2D" parent="."]
target_position = Vector2(-10, 0) target_position = Vector2(-10, 0)