Initial setup for NPCs and their pathfinding.

This commit is contained in:
Patrick 2024-12-11 22:23:58 +01:00
parent 0fa362aed4
commit 1ca0a2bdc0
6 changed files with 204 additions and 7 deletions

44
scripts/entities/npc.gd Normal file
View file

@ -0,0 +1,44 @@
extends CharacterBody3D
var movement_speed: float = 3.0
var target: CharacterBody3D
var movement_target_position: Vector3
var skip: bool = false
@onready var navigation_agent: NavigationAgent3D = $NavigationAgent3D
func _ready() -> void:
first_frame.call_deferred()
func first_frame() -> void:
actor_setup(999999999)
# determines the closest player and navigate.
func actor_setup(lowest_distance: float) -> void:
if !skip:
for child: Node in get_tree().root.get_node("/root/Testmap").get_children():
if child is Node3D:
if child.has_method("playerstub") and position.distance_to((child as Node3D).position) < lowest_distance:
target = child
skip = true
var dist: float = target.position.distance_to(movement_target_position)
if movement_target_position != target.position and (dist > 5 or position.distance_to(target.position) <= 5 or position.distance_to(movement_target_position) <= 5):
movement_target_position = target.position
navigation_agent.set_target_position(movement_target_position)
func _physics_process(_delta: float) -> void:
actor_setup(position.distance_to(target.position))
if navigation_agent.is_navigation_finished():
return
var current_agent_position: Vector3 = global_position
var next_path_position: Vector3 = navigation_agent.get_next_path_position()
velocity = current_agent_position.direction_to(next_path_position) * movement_speed
if move_and_slide():
pass

View file

@ -10,8 +10,7 @@ const JUMP_VELOCITY: float = 4.5
var activated: bool = false
var freecam: bool = false
@onready
var camera: Camera3D = $Camera3D
@onready var camera: Camera3D = $Camera3D
var camera_rotation: Vector2 = Vector2(0,0)
func _ready() -> void:
@ -55,6 +54,9 @@ func _physics_process(delta: float) -> void:
if move_and_slide():
pass
var fps_counter: Label = $Label
fps_counter.text = str(Engine.get_frames_per_second())
Networking.player_sync_call(position, rotation)
func _input(event: InputEvent) -> void:
@ -70,3 +72,7 @@ func _input(event: InputEvent) -> void:
elif camera.rotation.x <= -1.6:
camera.rotation.x = -1.6
rotate(Vector3.DOWN, camera_rotation.x)
# this way we can check for the player.
func playerstub() -> void:
pass