feat: add npc path finding to the zones based on needs

This commit is contained in:
Rijad Zuzo
2026-06-19 10:02:42 +02:00
parent e2549ac50c
commit af3495f4e3
4 changed files with 88 additions and 23 deletions
+42 -13
View File
@@ -1,41 +1,70 @@
extends CharacterBody3D
const DEBUG_LOGS := false
@export var move_speed := 3.0
@export var rotation_speed := 8.0
@export var waypoint_reached_distance := 0.35
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
var sim_id: int = -1
var npc_name: String = ""
var profession: String = ""
var target_position: Vector3
var has_target := false
var current_path: PackedVector3Array = []
var path_index := 0
var current_target := Vector3.INF
func debug_log(message: String) -> void:
if DEBUG_LOGS:
print("[NPCVisual] ", name, " | ", message)
func setup_from_sim(npc: SimNPC) -> void:
sim_id = npc.id
npc_name = npc.npc_name
profession = npc.profession
name = "NPC_%s_%s" % [sim_id, npc_name]
target_position = global_position
func set_target_position(pos: Vector3) -> void:
target_position = pos
has_target = true
func _physics_process(delta: float) -> void:
if not has_target:
if current_target.distance_to(pos) < 0.1:
return
var to_target := target_position - global_position
to_target.y = 0
current_target = pos
nav_agent.target_position = pos
if to_target.length() < 0.25:
await get_tree().physics_frame
current_path = NavigationServer3D.map_get_path(
get_world_3d().navigation_map,
global_position,
pos,
true
)
path_index = 0
debug_log("New target: %s Path points: %s" % [pos, current_path.size()])
func _physics_process(delta: float) -> void:
if current_path.is_empty() or path_index >= current_path.size():
velocity = Vector3.ZERO
move_and_slide()
return
var direction := to_target.normalized()
var next_pos := current_path[path_index]
var flat_next_pos := Vector3(next_pos.x, global_position.y, next_pos.z)
var to_next := flat_next_pos - global_position
if to_next.length() <= waypoint_reached_distance:
path_index += 1
return
var direction := to_next.normalized()
velocity.x = direction.x * move_speed
velocity.z = direction.z * move_speed
velocity.y = 0
velocity.y = 0.0
move_and_slide()
+6
View File
@@ -26,3 +26,9 @@ mesh = SubResource("CapsuleMesh_d844k")
[node name="FaceMarker" type="MeshInstance3D" parent="."]
transform = Transform3D(0.15, 0, 0, 0, 0.15, 0, 0, 0, 0.35, 0, 1.2, 0.59052056)
mesh = SubResource("BoxMesh_d844k")
[node name="NavigationAgent3D" type="NavigationAgent3D" parent="."]
path_desired_distance = 0.3
target_desired_distance = 0.5
height = 1.7
radius = 0.4