Les voitures! Des carrosses à escence.

Et bien les voitures vois-tu, maintenant elles utilisent aussi
l'animation tree, le state machine of the future et les super autres
magiqueries.

Elles bougent grace à un Path2D qui est suivit par un PathFollow2D qui
leur sert de lapin. Elles suivent le lapin, sans cesse et sans cesse.
Elles sont sage.
This commit is contained in:
Thomas
2025-02-21 22:44:29 +01:00
parent 134759b1b5
commit ec29c50631
11 changed files with 700 additions and 261 deletions

View File

@@ -1,33 +1,27 @@
class_name Voiture
extends Area2D
class_name Car
extends CharacterBody2D
var prevPositionX = 0;
var prevPositionY = 0;
@export var speed = 750 # How fast the car will move (pixels/sec).
@onready var animation_tree := $AnimationTree
@onready var state_machine := animation_tree.get("parameters/driving/playback") as AnimationNodeStateMachinePlayback
var last_facing_direction = Vector2(0,-1) # facing north
var targetPosition = null
func moveTo(position: Vector2) -> void:
targetPosition = 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)
func updatePosition(x: float, y: float) -> void:
var velocityX = prevPositionX - x;
var velocityY = prevPositionY - y;
$shapeDown.set_deferred("disabled", true);
$shapeUp.set_deferred("disabled", true);
$shapeLeft.set_deferred("disabled", true);
$shapeRight.set_deferred("disabled", true);
if(abs(velocityX) > abs(velocityY)):
if velocityX > 0 :
$AnimatedSprite2D.animation = "driving_left";
$shapeLeft.set_deferred("disabled", false);
if velocityX < 0 :
$AnimatedSprite2D.animation = "driving_right";
$shapeRight.set_deferred("disabled", false);
if(abs(velocityX) < abs(velocityY)):
if velocityY > 0 :
$AnimatedSprite2D.animation = "driving_up";
$shapeUp.set_deferred("disabled", false);
if velocityY < 0 :
$AnimatedSprite2D.animation = "driving_down";
$shapeDown.set_deferred("disabled", false);
prevPositionX = x;
prevPositionY = y;
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
$AnimatedSprite2D.play()
if (targetPosition != null):
velocity = (targetPosition - position) * speed * delta;
move_and_slide()
if velocity:
last_facing_direction = velocity.normalized()
updateFacingDirectionInAnimationTree()