voitures: freinage en fonction du time delta
le freinage dure un certain temps et ce temps dépends du delta des frames. Sinon les voitures freinent plus ou moins vite en fonction de ce que les pc cibles sont capable d'arriver à faire tourner.
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
extends Node2D
|
||||
|
||||
@export var fullStop: bool = true
|
||||
@export var wantedVelocity: int = 0
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
if OS.is_debug_build():
|
||||
$Label.text = str(body)+ " " + "body entered "
|
||||
if body.has_method("brake"):
|
||||
(body as Car).brake(fullStop)
|
||||
(body as Car).brake(wantedVelocity)
|
||||
|
||||
|
||||
func _on_body_exited(body: Node2D) -> void:
|
||||
|
||||
@@ -3,14 +3,14 @@ extends CharacterBody2D
|
||||
|
||||
@export var debugLabel: Label
|
||||
@export var speed = 750 # How fast the car will move (pixels/sec).
|
||||
@export var acceleration = speed / 100
|
||||
@export var slowZoneSpeed = 300
|
||||
@export var timeToChangeVelocity = 0.5 # How long to break to a full stop
|
||||
|
||||
var targetedVelocity = 0
|
||||
var current_speed = speed;
|
||||
@onready var animation_tree := $AnimationTree
|
||||
@onready var state_machine := animation_tree.get("parameters/driving/playback") as AnimationNodeStateMachinePlayback
|
||||
|
||||
var brakePedal = false;
|
||||
var fullStop = false;
|
||||
|
||||
var last_facing_direction = Vector2(0,-1) # facing north
|
||||
|
||||
@@ -27,14 +27,12 @@ func updateFacingDirectionInAnimationTree():
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
$ZIndexControler/ShapeCast2D.enabled = !$CollisionHorizontal.disabled
|
||||
|
||||
var accelerationStep = (speed * delta / timeToChangeVelocity)
|
||||
|
||||
if brakePedal:
|
||||
if fullStop:
|
||||
current_speed = max(0, current_speed - acceleration)
|
||||
else:
|
||||
current_speed = max(slowZoneSpeed, current_speed - acceleration)
|
||||
current_speed = max(targetedVelocity, current_speed - accelerationStep)
|
||||
else:
|
||||
current_speed = min(current_speed + acceleration, speed)
|
||||
current_speed = min(current_speed + accelerationStep, speed)
|
||||
|
||||
if (targetGlobalPosition != null):
|
||||
direction = (targetGlobalPosition - global_position).normalized()
|
||||
@@ -45,9 +43,9 @@ func _process(delta: float) -> void:
|
||||
last_facing_direction = velocity.normalized()
|
||||
updateFacingDirectionInAnimationTree()
|
||||
|
||||
func brake(needsToStop : bool):
|
||||
func brake(wantedVelocity : int):
|
||||
brakePedal = true;
|
||||
fullStop = needsToStop;
|
||||
targetedVelocity = wantedVelocity;
|
||||
|
||||
func accelerate():
|
||||
brakePedal = false;
|
||||
|
||||
Reference in New Issue
Block a user