Further improved NPC logic, and made it multiplayer friendly.
This commit is contained in:
parent
1ca0a2bdc0
commit
1ee6f6cc6d
5 changed files with 56 additions and 57 deletions
|
@ -1,44 +1,57 @@
|
|||
extends CharacterBody3D
|
||||
|
||||
var movement_speed: float = 3.0
|
||||
|
||||
var angry_meter: int = 5
|
||||
var target: CharacterBody3D
|
||||
var movement_target_position: Vector3
|
||||
@onready var timer: Timer = $Timer
|
||||
|
||||
var skip: bool = false
|
||||
|
||||
@onready var navigation_agent: NavigationAgent3D = $NavigationAgent3D
|
||||
|
||||
func _ready() -> void:
|
||||
if Networking.isManager:
|
||||
if timer.timeout.connect(_timer_done) != OK:
|
||||
Log.error("FAILED to connect the timeout signal to _timer_done!", "Internal Error: Failed to connect signal.")
|
||||
first_frame.call_deferred()
|
||||
|
||||
func _timer_done() -> void:
|
||||
if angry_meter > 0:
|
||||
Log.debug("angrier %d" % angry_meter)
|
||||
angry_meter -= 1
|
||||
else:
|
||||
timer.stop()
|
||||
first_frame()
|
||||
|
||||
func first_frame() -> void:
|
||||
await get_tree().physics_frame
|
||||
timer.start()
|
||||
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)
|
||||
for child: Node in get_tree().root.get_node("/root/"+Game.mapname+"/").get_children():
|
||||
if child is Node3D:
|
||||
if child.has_method("playerstub") and position.distance_to((child as Node3D).position) < lowest_distance:
|
||||
target = child
|
||||
|
||||
navigation_agent.set_target_position(target.position)
|
||||
|
||||
func _physics_process(_delta: float) -> void:
|
||||
actor_setup(position.distance_to(target.position))
|
||||
|
||||
if navigation_agent.is_navigation_finished():
|
||||
return
|
||||
if Networking.isManager and angry_meter == 0:
|
||||
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
|
||||
|
||||
Networking.npc_sync_call(position, rotation, name)
|
||||
|
||||
|
||||
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
|
||||
|
|
|
@ -40,6 +40,7 @@ func _physics_process(delta: float) -> void:
|
|||
|
||||
if Input.is_action_just_pressed("freecam"):
|
||||
freecam = !freecam
|
||||
($CollisionShape3D as CollisionShape3D).disabled = !($CollisionShape3D as CollisionShape3D).disabled
|
||||
|
||||
var input_dir: Vector2 = Input.get_vector("move_left", "move_right", "move_forwards", "move_backwards")
|
||||
var direction: Vector3 = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue