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
|
@ -22,3 +22,5 @@ shape = SubResource("CapsuleShape3D_am313")
|
||||||
[node name="NavigationAgent3D" type="NavigationAgent3D" parent="."]
|
[node name="NavigationAgent3D" type="NavigationAgent3D" parent="."]
|
||||||
avoidance_enabled = true
|
avoidance_enabled = true
|
||||||
height = 2.0
|
height = 2.0
|
||||||
|
|
||||||
|
[node name="Timer" type="Timer" parent="."]
|
||||||
|
|
|
@ -39,39 +39,6 @@ environment = SubResource("Environment_pq0iv")
|
||||||
[node name="Closet" parent="." instance=ExtResource("2_yvpvm")]
|
[node name="Closet" parent="." instance=ExtResource("2_yvpvm")]
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2, 10, 2)
|
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2, 10, 2)
|
||||||
|
|
||||||
[node name="Npc7" parent="." instance=ExtResource("3_x3gyc")]
|
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 40, 30, 0)
|
|
||||||
|
|
||||||
[node name="Npc8" parent="." instance=ExtResource("3_x3gyc")]
|
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 30, -40)
|
|
||||||
|
|
||||||
[node name="Npc9" parent="." instance=ExtResource("3_x3gyc")]
|
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 40, 30, -40)
|
|
||||||
|
|
||||||
[node name="Npc10" parent="." instance=ExtResource("3_x3gyc")]
|
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -40, 30, -40)
|
|
||||||
|
|
||||||
[node name="Npc11" parent="." instance=ExtResource("3_x3gyc")]
|
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -40, 30, 40)
|
|
||||||
|
|
||||||
[node name="Npc12" parent="." instance=ExtResource("3_x3gyc")]
|
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 40, 30, 40)
|
|
||||||
|
|
||||||
[node name="Npc6" parent="." instance=ExtResource("3_x3gyc")]
|
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 40, 20, 0)
|
|
||||||
|
|
||||||
[node name="Npc5" parent="." instance=ExtResource("3_x3gyc")]
|
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 20, -40)
|
|
||||||
|
|
||||||
[node name="Npc4" parent="." instance=ExtResource("3_x3gyc")]
|
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 40, 20, -40)
|
|
||||||
|
|
||||||
[node name="Npc3" parent="." instance=ExtResource("3_x3gyc")]
|
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -40, 20, -40)
|
|
||||||
|
|
||||||
[node name="Npc2" parent="." instance=ExtResource("3_x3gyc")]
|
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -40, 20, 40)
|
|
||||||
|
|
||||||
[node name="Npc1" parent="." instance=ExtResource("3_x3gyc")]
|
[node name="Npc1" parent="." instance=ExtResource("3_x3gyc")]
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 40, 20, 40)
|
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 40, 20, 40)
|
||||||
|
|
||||||
|
@ -86,3 +53,5 @@ skeleton = NodePath("../..")
|
||||||
|
|
||||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="NavigationRegion3D/StaticBody3D"]
|
[node name="CollisionShape3D" type="CollisionShape3D" parent="NavigationRegion3D/StaticBody3D"]
|
||||||
shape = SubResource("BoxShape3D_rnmx0")
|
shape = SubResource("BoxShape3D_rnmx0")
|
||||||
|
|
||||||
|
[node name="Timer" type="Timer" parent="."]
|
||||||
|
|
|
@ -1,44 +1,57 @@
|
||||||
extends CharacterBody3D
|
extends CharacterBody3D
|
||||||
|
|
||||||
var movement_speed: float = 3.0
|
var movement_speed: float = 3.0
|
||||||
|
var angry_meter: int = 5
|
||||||
var target: CharacterBody3D
|
var target: CharacterBody3D
|
||||||
var movement_target_position: Vector3
|
@onready var timer: Timer = $Timer
|
||||||
|
|
||||||
var skip: bool = false
|
var skip: bool = false
|
||||||
|
|
||||||
@onready var navigation_agent: NavigationAgent3D = $NavigationAgent3D
|
@onready var navigation_agent: NavigationAgent3D = $NavigationAgent3D
|
||||||
|
|
||||||
func _ready() -> void:
|
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()
|
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:
|
func first_frame() -> void:
|
||||||
|
await get_tree().physics_frame
|
||||||
|
timer.start()
|
||||||
actor_setup(999999999)
|
actor_setup(999999999)
|
||||||
|
|
||||||
# determines the closest player and navigate.
|
# determines the closest player and navigate.
|
||||||
func actor_setup(lowest_distance: float) -> void:
|
func actor_setup(lowest_distance: float) -> void:
|
||||||
if !skip:
|
for child: Node in get_tree().root.get_node("/root/"+Game.mapname+"/").get_children():
|
||||||
for child: Node in get_tree().root.get_node("/root/Testmap").get_children():
|
if child is Node3D:
|
||||||
if child is Node3D:
|
if child.has_method("playerstub") and position.distance_to((child as Node3D).position) < lowest_distance:
|
||||||
if child.has_method("playerstub") and position.distance_to((child as Node3D).position) < lowest_distance:
|
target = child
|
||||||
target = child
|
|
||||||
skip = true
|
|
||||||
|
|
||||||
var dist: float = target.position.distance_to(movement_target_position)
|
navigation_agent.set_target_position(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:
|
func _physics_process(_delta: float) -> void:
|
||||||
actor_setup(position.distance_to(target.position))
|
if Networking.isManager and angry_meter == 0:
|
||||||
|
actor_setup(position.distance_to(target.position))
|
||||||
|
|
||||||
if navigation_agent.is_navigation_finished():
|
if navigation_agent.is_navigation_finished():
|
||||||
return
|
return
|
||||||
|
|
||||||
var current_agent_position: Vector3 = global_position
|
var current_agent_position: Vector3 = global_position
|
||||||
var next_path_position: Vector3 = navigation_agent.get_next_path_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)
|
||||||
|
|
||||||
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"):
|
if Input.is_action_just_pressed("freecam"):
|
||||||
freecam = !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 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()
|
var direction: Vector3 = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
||||||
|
|
|
@ -280,3 +280,17 @@ func player_sync(position: Vector3, rotation: Vector3) -> void:
|
||||||
var player: CharacterBody3D = get_node("/root/"+Game.mapname+"/"+peer)
|
var player: CharacterBody3D = get_node("/root/"+Game.mapname+"/"+peer)
|
||||||
player.position = position
|
player.position = position
|
||||||
player.rotation = rotation
|
player.rotation = rotation
|
||||||
|
|
||||||
|
func npc_sync_call(position: Vector3, rotation: Vector3, node_name: String) -> void:
|
||||||
|
for peer: int in Networking.get_ids():
|
||||||
|
if peer != multiplayer.get_unique_id():
|
||||||
|
if rpc_id(peer, "npc_sync", position, rotation, node_name) != OK:
|
||||||
|
Log.warning("Couldn't send RPC to %d!" % peer)
|
||||||
|
|
||||||
|
# Synchronizes the npc state.
|
||||||
|
@rpc("any_peer", "call_local", "reliable")
|
||||||
|
func npc_sync(position: Vector3, rotation: Vector3, node_name: String) -> void:
|
||||||
|
if managerID == multiplayer.get_remote_sender_id():
|
||||||
|
var npc: CharacterBody3D = get_node("/root/"+Game.mapname+"/"+node_name)
|
||||||
|
npc.position = position
|
||||||
|
npc.rotation = rotation
|
||||||
|
|
Loading…
Reference in a new issue