44 lines
1014 B
GDScript
44 lines
1014 B
GDScript
extends CharacterBody3D
|
|
|
|
@export var move_speed := 3.0
|
|
@export var rotation_speed := 8.0
|
|
|
|
var sim_id: int = -1
|
|
var npc_name: String = ""
|
|
var profession: String = ""
|
|
var target_position: Vector3
|
|
var has_target := false
|
|
|
|
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:
|
|
return
|
|
|
|
var to_target := target_position - global_position
|
|
to_target.y = 0
|
|
|
|
if to_target.length() < 0.25:
|
|
velocity = Vector3.ZERO
|
|
move_and_slide()
|
|
return
|
|
|
|
var direction := to_target.normalized()
|
|
velocity.x = direction.x * move_speed
|
|
velocity.z = direction.z * move_speed
|
|
velocity.y = 0
|
|
|
|
move_and_slide()
|
|
|
|
var target_angle := atan2(direction.x, direction.z)
|
|
rotation.y = lerp_angle(rotation.y, target_angle, rotation_speed * delta)
|