Des feux rouges!
Pour arrêter les voiture vroom vroom
This commit is contained in:
15
vehicules/brakeZone.tscn
Normal file
15
vehicules/brakeZone.tscn
Normal file
@@ -0,0 +1,15 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://dpyadhlqawviq"]
|
||||
|
||||
[ext_resource type="Script" path="res://vehicules/brake_zone.gd" id="1_bbbt2"]
|
||||
|
||||
[node name="BrakeZone" type="Area2D"]
|
||||
collision_layer = 2
|
||||
collision_mask = 3
|
||||
script = ExtResource("1_bbbt2")
|
||||
|
||||
[node name="Label" type="Label" parent="."]
|
||||
offset_right = 40.0
|
||||
offset_bottom = 23.0
|
||||
|
||||
[connection signal="body_entered" from="." to="." method="_on_body_entered"]
|
||||
[connection signal="body_exited" from="." to="." method="_on_body_exited"]
|
||||
14
vehicules/brake_zone.gd
Normal file
14
vehicules/brake_zone.gd
Normal file
@@ -0,0 +1,14 @@
|
||||
extends Node2D
|
||||
|
||||
@export var fullStop: bool = true
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
$Label.text = str(body)+ " " + "body entered "
|
||||
if body.has_method("brake"):
|
||||
(body as Car).brake(fullStop)
|
||||
|
||||
|
||||
func _on_body_exited(body: Node2D) -> void:
|
||||
$Label.text = str(body)+ " " + "body exited "
|
||||
if body.has_method("accelerate"):
|
||||
(body as Car).accelerate()
|
||||
@@ -3,9 +3,15 @@ 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 targetPosition = null
|
||||
@@ -19,13 +25,26 @@ func updateFacingDirectionInAnimationTree():
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
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)
|
||||
$ZIndexControler/ShapeCast2D.enabled = !$CollisionHorizontal.disabled
|
||||
|
||||
if (targetPosition != null):
|
||||
velocity = (targetPosition - position) * speed * delta;
|
||||
velocity = (targetPosition - position) * current_speed * delta;
|
||||
|
||||
move_and_slide()
|
||||
if velocity:
|
||||
last_facing_direction = velocity.normalized()
|
||||
|
||||
updateFacingDirectionInAnimationTree()
|
||||
|
||||
func brake(needsToStop : bool):
|
||||
brakePedal = true;
|
||||
fullStop = needsToStop;
|
||||
|
||||
func accelerate():
|
||||
brakePedal = false;
|
||||
|
||||
@@ -550,9 +550,10 @@ radius = 31.0
|
||||
height = 186.0
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_yiphh"]
|
||||
size = Vector2(240, 15)
|
||||
size = Vector2(200, 15)
|
||||
|
||||
[node name="Car" type="CharacterBody2D" node_paths=PackedStringArray("debugLabel")]
|
||||
collision_mask = 3
|
||||
motion_mode = 1
|
||||
script = ExtResource("1_vkq5y")
|
||||
debugLabel = NodePath("Label")
|
||||
@@ -600,4 +601,4 @@ offset_bottom = -19.0
|
||||
[node name="ShapeCast2D" type="ShapeCast2D" parent="ZIndexControler"]
|
||||
position = Vector2(1, -13)
|
||||
shape = SubResource("RectangleShape2D_yiphh")
|
||||
target_position = Vector2(0, -46)
|
||||
target_position = Vector2(1, -32)
|
||||
|
||||
Reference in New Issue
Block a user