|
|
|
|
@ -39,15 +39,81 @@ func _ready(): |
|
|
|
|
add_child(weapons_container) |
|
|
|
|
print("Created WeaponsContainer") |
|
|
|
|
|
|
|
|
|
# Clients: Remove manually placed weapons (server will sync them via RPC) |
|
|
|
|
if not multiplayer.is_server(): |
|
|
|
|
_cleanup_manual_weapons_on_client() |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
Network.connect("player_connected", Callable(self, "_on_player_connected")) |
|
|
|
|
multiplayer.peer_disconnected.connect(_remove_player) |
|
|
|
|
|
|
|
|
|
# Initialize any manually placed weapons in the scene |
|
|
|
|
_initialize_manual_weapons() |
|
|
|
|
|
|
|
|
|
# Spawn initial weapons when server starts |
|
|
|
|
_spawn_initial_weapons() |
|
|
|
|
|
|
|
|
|
func _cleanup_manual_weapons_on_client(): |
|
|
|
|
"""Remove manually placed weapons on clients (server will sync them via RPC)""" |
|
|
|
|
if not weapons_container: |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
var weapons_to_remove = [] |
|
|
|
|
for child in weapons_container.get_children(): |
|
|
|
|
if child is WorldWeapon and child.weapon_id == -1: |
|
|
|
|
weapons_to_remove.append(child) |
|
|
|
|
|
|
|
|
|
for weapon in weapons_to_remove: |
|
|
|
|
print("[Client] Removing manually placed weapon: ", weapon.name) |
|
|
|
|
weapon.queue_free() |
|
|
|
|
|
|
|
|
|
func _initialize_manual_weapons(): |
|
|
|
|
"""Initialize any WorldWeapon nodes manually placed in the level scene""" |
|
|
|
|
if not multiplayer.is_server(): |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
if not weapons_container: |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
# Find all WorldWeapon nodes in the weapons container |
|
|
|
|
var manual_weapons = [] |
|
|
|
|
for child in weapons_container.get_children(): |
|
|
|
|
if child is WorldWeapon: |
|
|
|
|
manual_weapons.append(child) |
|
|
|
|
|
|
|
|
|
if manual_weapons.is_empty(): |
|
|
|
|
print("[Server] No manually placed weapons found") |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
print("[Server] Found ", manual_weapons.size(), " manually placed weapon(s)") |
|
|
|
|
|
|
|
|
|
# Initialize each manually placed weapon |
|
|
|
|
for weapon in manual_weapons: |
|
|
|
|
# Skip if already initialized (weapon_id != -1) |
|
|
|
|
if weapon.weapon_id != -1: |
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
# Assign unique ID |
|
|
|
|
_weapon_spawn_counter += 1 |
|
|
|
|
weapon.weapon_id = _weapon_spawn_counter |
|
|
|
|
|
|
|
|
|
# Set deterministic name for networking |
|
|
|
|
var old_name = weapon.name |
|
|
|
|
weapon.name = "WorldWeapon_" + str(weapon.weapon_id) |
|
|
|
|
|
|
|
|
|
# Track in active weapons |
|
|
|
|
_active_weapons[weapon.weapon_id] = weapon |
|
|
|
|
|
|
|
|
|
# Connect cleanup signal |
|
|
|
|
weapon.tree_exiting.connect(_on_weapon_removed.bind(weapon.weapon_id)) |
|
|
|
|
|
|
|
|
|
print("[Server] Initialized manual weapon '", old_name, "' with ID: ", weapon.weapon_id, " at position: ", weapon.global_position) |
|
|
|
|
|
|
|
|
|
# Verify weapon_data is set |
|
|
|
|
if not weapon.weapon_data: |
|
|
|
|
push_error("Manual weapon '", old_name, "' has no WeaponData assigned!") |
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
func _spawn_initial_weapons(): |
|
|
|
|
if not multiplayer.is_server(): |
|
|
|
|
return |
|
|
|
|
|