Les voitures ont un meilleur gps

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.
This commit is contained in:
Thomas
2025-02-25 11:37:45 +01:00
parent 8a6af2b01e
commit dcb63f7fe0
8 changed files with 80 additions and 47 deletions

File diff suppressed because one or more lines are too long

View File

@@ -2,19 +2,58 @@ class_name NPCCar
extends PathFollow2D
@export var car : Car
@export var speed = 0.1
@export var distanceMax = 100
var astar_grid: AStarGrid2D
var toFollow: Array[Vector2i]
@export var world: TileMapLayer
var CARLAYER = 1
func _ready() -> void:
astar_grid = AStarGrid2D.new()
astar_grid.region = world.get_used_rect()
astar_grid.cell_size = Vector2(48, 48)
astar_grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE
astar_grid.update()
# only take into account the tiles that are marked with a navigation layer for cars
for x in world.get_used_rect().size.x:
for y in world.get_used_rect().size.y:
var tile_position = Vector2(
x + world.get_used_rect().position.x,
y + world.get_used_rect().position.y,
)
var tile_data = world.get_cell_tile_data(tile_position)
if tile_data == null or tile_data.get_navigation_polygon(CARLAYER) == null:
astar_grid.set_point_solid(tile_position)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if car == null:
return
if (delta > 0.01):
return
if position.distance_to(car.position) < distanceMax:
progress += car.speed * delta
if progress_ratio > 1:
progress_ratio = progress_ratio - 1
if progress > 0:
car.moveTo(position);
# make the wanted position on the track move ahead by a certain amount
if position.distance_to(car.position) < distanceMax and (toFollow == null or toFollow.is_empty()):
progress += 200
# compute the new navigation points the car should follow
var points = astar_grid.get_id_path(
world.local_to_map(world.to_local(car.global_position)),
world.local_to_map(world.to_local(global_position))
).slice(1)
if !points.is_empty():
toFollow = points
if OS.is_debug_build():
$Label.text = (
"position "+str(global_position)+
"\n, map position "+ str(world.local_to_map(world.to_local(global_position)))+
"\npoint to follow "+str(toFollow)+
"\n world position" + str(world.global_position)
)
if !toFollow.is_empty():
if world.local_to_map(world.to_local(car.global_position)) == toFollow.front():
toFollow.pop_front()
if !toFollow.is_empty():
car.moveTo(world.to_global(world.map_to_local(toFollow.front())));

View File

@@ -7,19 +7,19 @@
[sub_resource type="Curve2D" id="Curve2D_shblg"]
_data = {
"points": PackedVector2Array(0, 0, 0, 0, -285, 193, -72.1029, 112.165, 72.1029, -112.165, 62, 108, -92.8736, 1.22052, 92.8736, -1.22052, 220, -16, -72.4586, -10.5935, 72.4586, 10.5935, 2206, -11, -122.624, -4.07119, 122.624, 4.07119, 2387, 165, -98.6077, 103.694, 98.6077, -103.694, 2759, 110, 117.462, 37.6927, -117.462, -37.6927, 2715, -276, 114.239, -35.1712, -114.239, 35.1712, 2384, -293, 50.2246, -13.5362, -50.2246, 13.5362, 2209, -126, 61.4659, 8.33138, -61.4659, -8.33138, 133, -135, 40.4345, 62.8385, -40.4345, -62.8385, 40, -272, 50.2081, -75.7177, -50.2081, 75.7177, -399, -242, -67.7454, -78.8949, 67.7454, 78.8949, -399, 107, 0, 0, 0, 0, -286, 193, 0, 0, 0, 0, -285, 193)
"points": PackedVector2Array(0, 0, 0, 0, -285, 193, -72.1029, 112.165, 72.1029, -112.165, 20, 84, -92.8736, 1.22052, 92.8736, -1.22052, 220, -16, -72.4586, -10.5935, 72.4586, 10.5935, 2206, -11, -122.624, -4.07119, 122.624, 4.07119, 2387, 165, -98.6077, 103.694, 98.6077, -103.694, 2759, 110, 117.462, 37.6927, -117.462, -37.6927, 2715, -276, 114.239, -35.1712, -114.239, 35.1712, 2384, -293, 50.2246, -13.5362, -50.2246, 13.5362, 2209, -126, 61.4659, 8.33138, -61.4659, -8.33138, 133, -135, 40.4345, 62.8385, -40.4345, -62.8385, 40, -272, 50.2081, -75.7177, -50.2081, 75.7177, -399, -242, -67.7454, -78.8949, 67.7454, 78.8949, -399, 107, 0, 0, 0, 0, -286, 193, 0, 0, 0, 0, -285, 193)
}
point_count = 15
[sub_resource type="Curve2D" id="Curve2D_nircx"]
_data = {
"points": PackedVector2Array(0, 0, 0, 0, 803, 1569, 0, 0, 0, 0, 910, 1573, 0, 0, 0, 0, 913, 1368, 0, 0, 0, 0, 1926, 1371, 0, 0, 0, 0, 1931, 342, 0, 0, 0, 0, 2082, 333, 0, 0, 0, 0, 2060, -140, 0, 0, 0, 0, 1718, -143, 0, 0, 0, 0, 1649, 73, 0, 0, 0, 0, 912.469, 60.5339, 0, 0, 0, 0, 848, 58, 0, 0, 0, 0, 873, -21, 0, 0, 0, 0, 904, -1165, 0, 0, 0, 0, -890, -1162, 0, 0, 0, 0, -886, -119, 0, 0, 0, 0, -1062, -106, 0, 0, 0, 0, -1076, 268, 0, 0, 0, 0, -630, 271, 0, 0, 0, 0, -616, 161, 0, 0, 0, 0, 858, 167, 0, 0, 0, 0, 806, 238, 0, 0, 0, 0, 806, 1560)
"points": PackedVector2Array(0, 0, 0, 0, 803, 1569, 0, 0, 0, 0, 910, 1573, 0, 0, 0, 0, 913, 1368, 0, 0, 0, 0, 1938, 1371, 0, 0, 0, 0, 1931, 342, 0, 0, 0, 0, 2082, 333, 0, 0, 0, 0, 2086, -88, 0, 0, 0, 0, 1656, -97, 0, 0, 0, 0, 1649, 73, 0, 0, 0, 0, 912.469, 60.5339, 0, 0, 0, 0, 904, -1165, 0, 0, 0, 0, -890, -1162, 0, 0, 0, 0, -885, -108, 0, 0, 0, 0, -1062, -106, 0, 0, 0, 0, -1073, 283, 0, 0, 0, 0, -624, 293, 0, 0, 0, 0, -616, 161, 0, 0, 0, 0, -577, 160, 0, 0, 0, 0, 798, 164, 0, 0, 0, 0, 806, 1560)
}
point_count = 22
point_count = 20
[node name="scene1" type="Node2D"]
[node name="monde" parent="." instance=ExtResource("1_6vs81")]
[node name="world" parent="." instance=ExtResource("1_6vs81")]
[node name="movibles" type="Node2D" parent="."]
@@ -32,11 +32,10 @@ position = Vector2(87, 74)
position = Vector2(-664, 181)
curve = SubResource("Curve2D_shblg")
[node name="voiture_pnj" parent="movibles/cars/Path2D" node_paths=PackedStringArray("car") instance=ExtResource("3_yuakw")]
[node name="voiture_pnj" parent="movibles/cars/Path2D" node_paths=PackedStringArray("car", "world") instance=ExtResource("3_yuakw")]
position = Vector2(-285, 193)
car = NodePath("../Car")
speed = 0.2
distanceMax = 200
world = NodePath("../../../../world")
[node name="Car" parent="movibles/cars/Path2D" instance=ExtResource("4_bqm78")]
position = Vector2(-287, 196)
@@ -45,11 +44,11 @@ speed = 500
[node name="Path2D2" type="Path2D" parent="movibles/cars"]
curve = SubResource("Curve2D_nircx")
[node name="voiture_pnj" parent="movibles/cars/Path2D2" node_paths=PackedStringArray("car") instance=ExtResource("3_yuakw")]
[node name="voiture_pnj" parent="movibles/cars/Path2D2" node_paths=PackedStringArray("car", "world") instance=ExtResource("3_yuakw")]
position = Vector2(803, 1569)
car = NodePath("../Car")
speed = 0.1
distanceMax = 150
world = NodePath("../../../../world")
[node name="Car" parent="movibles/cars/Path2D2" instance=ExtResource("4_bqm78")]
position = Vector2(803, 1558)

View File

@@ -8,6 +8,8 @@ var light = 0;
func _ready() -> void:
$brakeZone.get_child(1).disabled = !light;
for i in range (lights.size()):
lights[i].visible = i == light
func change_light():
light += 1;

View File

@@ -8,6 +8,7 @@ collision_mask = 3
script = ExtResource("1_bbbt2")
[node name="Label" type="Label" parent="."]
visible = false
offset_right = 40.0
offset_bottom = 23.0

View File

@@ -3,12 +3,14 @@ extends Node2D
@export var fullStop: bool = true
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)
func _on_body_exited(body: Node2D) -> void:
if OS.is_debug_build():
$Label.text = str(body)+ " " + "body exited "
if body.has_method("accelerate"):
(body as Car).accelerate()

View File

@@ -14,10 +14,11 @@ var fullStop = false;
var last_facing_direction = Vector2(0,-1) # facing north
var targetPosition = null
var targetGlobalPosition = null
var direction = Vector2(0,0)
func moveTo(position: Vector2) -> void:
targetPosition = position;
targetGlobalPosition = position;
func updateFacingDirectionInAnimationTree():
animation_tree.set("parameters/CarStates/driving/blend_position", last_facing_direction)
@@ -25,6 +26,8 @@ func updateFacingDirectionInAnimationTree():
# 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)
@@ -32,11 +35,10 @@ func _process(delta: float) -> void:
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) * current_speed * delta
if (targetGlobalPosition != null):
direction = (targetGlobalPosition - global_position).normalized()
velocity = direction * current_speed
move_and_slide()
if velocity:

View File

@@ -6,4 +6,8 @@
position = Vector2(-411, 81)
rotates = false
script = ExtResource("1_fbnn2")
speed = 1.0
[node name="Label" type="Label" parent="."]
visible = false
offset_right = 40.0
offset_bottom = 23.0