marcher: nombre fixe de frames pour marcher

Reproductible et à pas fixe. Toujours le même temps pour accélérer et
décélérer. Si c'est pas beau ça!
This commit is contained in:
Thomas Lavocat
2023-04-21 17:28:21 +02:00
parent 12380cb3ac
commit 3f3666d2a5
2 changed files with 61 additions and 30 deletions

View File

@@ -1,11 +1,5 @@
extends CharacterBody2D
const WALK_SPEED = 200.0
const ACCELERATION_SPEED = WALK_SPEED * 6.0
const JUMP_VELOCITY = -400.0
## Maximum speed at which the player can fall.
const TERMINAL_VELOCITY = 400
## The player listens for input actions appended with this suffix.[br]
## Used to separate controls for multiple players in splitscreen.
@export var action_suffix := ""
@@ -17,20 +11,67 @@ var gravity: int = ProjectSettings.get("physics/2d/default_gravity")
@onready var camera := $Camera2D as Camera2D
var _double_jump_charged := false
@export var WALKING_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.2, 0.6, 0.8, 0.9, 0.9, 1,
1.1, 1.1, 1.1, 1.2, 1.2, 1.2,
1.2, 1.2, 1.3, 1.4, 1.5, 1.6,
1.8, 1.9, 3]
@export var FALL_SPEED_TABLE = [0, 0.4]
@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 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
@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
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 _physics_process(delta: float) -> void:
if is_on_floor():
_double_jump_charged = true
if Input.is_action_just_pressed("jump" + action_suffix):
try_jump()
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.
velocity.y *= 0.6
pass
# Fall.
velocity.y = minf(TERMINAL_VELOCITY, velocity.y + gravity * delta)
velocity.y = minf(400, velocity.y + gravity * delta)
var direction := Input.get_axis("move_left" + action_suffix, "move_right" + action_suffix) * WALK_SPEED
velocity.x = move_toward(velocity.x, direction, ACCELERATION_SPEED * delta)
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:
@@ -47,30 +88,19 @@ func _physics_process(delta: float) -> void:
animation.play()
func get_new_animation(is_shooting := false) -> String:
var animation_new: String
if is_on_floor():
if absf(velocity.x) > 0.1:
if walking_step > 0:
animation_new = "walk"
else:
animation_new = "idle"
else:
if velocity.y > 0.0:
animation_new = "falling_straight"
if walking_step > 0:
animation_new = "falling_diagonals"
else:
animation_new = "falling_straight"
else:
animation_new = "jumping"
return animation_new
func try_jump() -> void:
if is_on_floor():
pass
elif _double_jump_charged:
_double_jump_charged = false
velocity.x *= 2.5
else:
return
velocity.y = JUMP_VELOCITY

View File

@@ -201,10 +201,11 @@ height = 52.0
[node name="Princesse" type="CharacterBody2D"]
script = ExtResource("1_dkp7s")
WALKING_SPEED = 100
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
sprite_frames = SubResource("SpriteFrames_q52wx")
animation = &"idle"
animation = &"wall_stick"
[node name="PlatformDetector" type="RayCast2D" parent="."]
collision_mask = 8