diff --git a/scenes/entities/npc.tscn b/scenes/entities/npc.tscn index 7a09ef6..5c4960b 100644 --- a/scenes/entities/npc.tscn +++ b/scenes/entities/npc.tscn @@ -22,3 +22,5 @@ shape = SubResource("CapsuleShape3D_am313") [node name="NavigationAgent3D" type="NavigationAgent3D" parent="."] avoidance_enabled = true height = 2.0 + +[node name="Timer" type="Timer" parent="."] diff --git a/scenes/maps/testmap.tscn b/scenes/maps/testmap.tscn index 899341a..e7e9544 100644 --- a/scenes/maps/testmap.tscn +++ b/scenes/maps/testmap.tscn @@ -39,39 +39,6 @@ environment = SubResource("Environment_pq0iv") [node name="Closet" parent="." instance=ExtResource("2_yvpvm")] 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")] 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"] shape = SubResource("BoxShape3D_rnmx0") + +[node name="Timer" type="Timer" parent="."] diff --git a/scripts/entities/npc.gd b/scripts/entities/npc.gd index c1f9b15..6c07a25 100644 --- a/scripts/entities/npc.gd +++ b/scripts/entities/npc.gd @@ -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 diff --git a/scripts/entities/player.gd b/scripts/entities/player.gd index 9b7c941..0453b4a 100644 --- a/scripts/entities/player.gd +++ b/scripts/entities/player.gd @@ -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() diff --git a/scripts/utils/networking.gd b/scripts/utils/networking.gd index ce97f67..4c487cb 100644 --- a/scripts/utils/networking.gd +++ b/scripts/utils/networking.gd @@ -280,3 +280,17 @@ func player_sync(position: Vector3, rotation: Vector3) -> void: var player: CharacterBody3D = get_node("/root/"+Game.mapname+"/"+peer) player.position = position 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