Les voiture maintenant suivent des coordonnées fournies par un AStarGrid2D au lieu de bétement faire une diagonale vers le prochain point du lapin ce qui fait que, maintenant elle ne peuvent plus vraiment monter sur les troitoirs car ils ne font pas partis du calque de navigation.
54 lines
1.6 KiB
GDScript
54 lines
1.6 KiB
GDScript
class_name Car
|
|
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
|
|
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
|
|
|
|
var targetGlobalPosition = null
|
|
var direction = Vector2(0,0)
|
|
|
|
func moveTo(position: Vector2) -> void:
|
|
targetGlobalPosition = position;
|
|
|
|
func updateFacingDirectionInAnimationTree():
|
|
animation_tree.set("parameters/CarStates/driving/blend_position", last_facing_direction)
|
|
animation_tree.set("parameters/CarStates/idling/blend_position", last_facing_direction)
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
$ZIndexControler/ShapeCast2D.enabled = !$CollisionHorizontal.disabled
|
|
|
|
if brakePedal:
|
|
if fullStop:
|
|
current_speed = max(0, current_speed - acceleration)
|
|
else:
|
|
current_speed = max(slowZoneSpeed, current_speed - acceleration)
|
|
else:
|
|
current_speed = min(current_speed + acceleration, speed)
|
|
|
|
if (targetGlobalPosition != null):
|
|
direction = (targetGlobalPosition - global_position).normalized()
|
|
velocity = direction * current_speed
|
|
|
|
move_and_slide()
|
|
if velocity:
|
|
last_facing_direction = velocity.normalized()
|
|
updateFacingDirectionInAnimationTree()
|
|
|
|
func brake(needsToStop : bool):
|
|
brakePedal = true;
|
|
fullStop = needsToStop;
|
|
|
|
func accelerate():
|
|
brakePedal = false;
|