From 33a9848524dc4177b57e5d29413ac8bdb7321d7b Mon Sep 17 00:00:00 2001 From: Dustin Date: Sun, 20 Apr 2025 16:36:36 -0700 Subject: [PATCH] Pews now pew in multiplayer. Game ends but also crashes over network. --- lib/singleton_autoloads/NetworkManager.gd | 18 ++++++++++++------ src/classes/projectile.gd | 15 ++++++++++++--- src/components/PewMachine.gd | 11 ++++++----- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/lib/singleton_autoloads/NetworkManager.gd b/lib/singleton_autoloads/NetworkManager.gd index 8b979d9..6f9b3f2 100644 --- a/lib/singleton_autoloads/NetworkManager.gd +++ b/lib/singleton_autoloads/NetworkManager.gd @@ -64,6 +64,7 @@ func _server_disconnected(): end_game() + # Callback from SceneTree, only for clients (not server). func _connected_fail(): get_tree().set_network_peer(null) # Remove peer @@ -83,14 +84,17 @@ func unregister_player(id): emit_signal("player_list_changed") func instance_on_peer_players(_scene :Resource, _node_path :NodePath, _global_position :Vector2 , _as_toplevel = true): - print ("Scene Path: ", _scene.resource_path, - " Instance At: ", _node_path, - " Position : ", _global_position) +# print ("Scene Path: ", _scene.resource_path, +# " Instance At: ", _node_path, +# " Position : ", _global_position) + ## was going to create local instance here but, lets see if I don't have to. + ## makes me thing that node name and path must be very important for multiplayer rpc("load_scene_on_all_clients", _scene.resource_path, _node_path, _global_position, _as_toplevel) - -remotesync func load_scene_on_all_clients(_scene :String, _node_path :NodePath, _global_position :Vector2 , _as_toplevel = true): +## remote means only runs on peers, remotesync goes both here and in peers +## because I want the node I load in two steps +remote func load_scene_on_all_clients(_scene :String, _node_path :NodePath, _global_position :Vector2 , _as_toplevel = true): print ( "-- Remote Scene Call -- \n", " Scene Path: ", _scene, " Instance At: ", _node_path, @@ -216,8 +220,10 @@ func begin_game(): pre_start_game(spawn_points) - func end_game(): + rpc("end_clients_game") + +remotesync func end_clients_game(): get_tree().call_group("world","queue_free") # if has_node("/root/Main/ViewportContainer/Viewport/World"): # Game is in progress. # # End it diff --git a/src/classes/projectile.gd b/src/classes/projectile.gd index 99dd27b..48bb902 100644 --- a/src/classes/projectile.gd +++ b/src/classes/projectile.gd @@ -10,23 +10,32 @@ extends Area2D export var velocity_pps :float = 1 export var lifespan :float = -1 var direction_normal := Vector2(0,0) +puppet var puppet_direction_normal :Vector2 +puppet var puppet_position var time_alive :float = 0.0 # Called when the node enters the scene tree for the first time. func _ready(): - pass # Replace with function body. - + puppet_direction_normal = direction_normal + puppet_position = global_position # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): + if is_network_master(): + position += direction_normal * velocity_pps * delta + rset("puppet_position", global_position) + else: + global_position = puppet_position + if lifespan != -1: time_alive += delta if time_alive > lifespan: queue_free() rotate(delta * 10) - position += direction_normal * velocity_pps * delta +##TODO: Haven't determined if I want to create a receiver for projectiles or just inherit from an +## interactable for projectiles. func _on_body_entered(body): var colliding_node = body if colliding_node.has_node("Projectile_Receiver"): diff --git a/src/components/PewMachine.gd b/src/components/PewMachine.gd index ca8a66b..9618d08 100644 --- a/src/components/PewMachine.gd +++ b/src/components/PewMachine.gd @@ -48,23 +48,24 @@ func shoot(direction = 1): # position.x = position.x * direction #shifting position before fire was unreliable if not timer.is_stopped(): return false - #var bullet = loaded_scene.instance() - var bullet = NetworkManager.instance_on_peer_players(loaded_scene, self.get_path(), global_position , true) + var bullet = loaded_scene.instance() # if (direction > 0): # bullet.global_position = global_position # else: # bullet.global_position = global_position - Vector2(position.x * 2, 0) if bullet is Projectile: print ("i'm a projectile") -# bullet.global_position = global_position + bullet.global_position = global_position bullet.direction_normal = normal_vector #bullet.linear_velocity = Vector2(direction * BULLET_VELOCITY, 0) - #bullet.set_as_toplevel(true) -# add_child(bullet) + bullet.set_as_toplevel(true) + add_child(bullet) # sound_shoot.play() timer.start() + + NetworkManager.instance_on_peer_players(loaded_scene, self.get_path(), global_position , true) return true #var bomb_name = String(get_name()) + str(bomb_index)