From 7d022775d3649b78f7a7b779a56c9a7dc776d355 Mon Sep 17 00:00:00 2001 From: adtpdn Date: Tue, 4 Feb 2025 18:08:56 +0800 Subject: [PATCH] fixed --- scenes/main.gd | 56 ++++++++++++++++++++++++++++-------------------- scenes/main.tscn | 2 +- scenes/player.gd | 12 +++++------ 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/scenes/main.gd b/scenes/main.gd index baf0773..1e1c702 100644 --- a/scenes/main.gd +++ b/scenes/main.gd @@ -239,24 +239,24 @@ func _on_host_pressed(): multiplayer.multiplayer_peer = multiplayer_peer $NetworkInfo/UniquePeerID.text = str(multiplayer.get_unique_id()) + # Generate all goals first + preset_goals.clear() + for i in range(max_players): + var goals = initialize_random_goals(9, 7, 10, 1.0) + # Convert to int array explicitly + var int_goals: Array[int] = [] + for g in goals: + int_goals.append(g) + preset_goals.append(int_goals) + + # Sync goals to all clients + rpc("sync_preset_goals", preset_goals) + + # Now add host with first set of goals add_player_character(1) players.append(1) - # Generate goals for host first - var host_player = get_node_or_null("1") - if host_player: - host_player.append_random_goals() - rpc("sync_player_goals", 1, host_player.goals) - - # Generate and store goals for future bots/players - var preset_goals = [] - for i in range(2, max_players + 1): - var goals = initialize_random_goals(9, 7, 10, 1.0) - preset_goals.append(goals) - - # Store preset goals in network state - rpc("sync_preset_goals", preset_goals) - + # Add bots with their own goals for i in range(2, max_players + 1): add_bot(i) @@ -315,6 +315,18 @@ func add_player_character(peer_id): add_child(player_character) player_character.add_to_group("Players", true) + # Set goals based on player ID if server + if multiplayer.is_server(): + var goal_index = peer_id - 1 + # Verify index is valid + if goal_index >= 0 and goal_index < preset_goals.size(): + # Convert to int array before assigning + var goals: Array[int] = [] + for g in preset_goals[goal_index]: + goals.append(g) + player_character.goals = goals + rpc("sync_player_goals", peer_id, goals) + if peer_id == multiplayer.get_unique_id(): local_player_character = player_character update_button_states() @@ -338,9 +350,6 @@ func add_player_character(peer_id): var bot_to_replace = get_node_or_null(str(bots[0])) if bots.size() > 0 else null if bot_to_replace: player_character.goals = bot_to_replace.goals.duplicate() - else: - # Only generate new goals if not inheriting from a bot - player_character.append_random_goals() func add_bot(bot_id): rpc("create_bot", bot_id) @@ -372,10 +381,11 @@ func create_bot(bot_id): bots.append(bot_id) players.append(bot_id) - # Assign preset goals - var goal_index = bot_id - 2 + # Assign goals from preset array + var goal_index = bot_id - 1 if goal_index < preset_goals.size(): - bot_character.goals = preset_goals[goal_index] + bot_character.goals = preset_goals[goal_index].duplicate() + rpc("sync_player_goals", bot_id, bot_character.goals) # Sync bot status after a short delay to ensure node is ready await get_tree().create_timer(0.1).timeout @@ -384,8 +394,8 @@ func create_bot(bot_id): rpc("sync_player_goals", bot_id, bot_character.goals) # Only generate goals for new bots, not replacement bots - if not (players.size() > max_players): - bot_character.append_random_goals() + #if not (players.size() > max_players): + #bot_character.append_random_goals() # Always sync the bot's goals rpc("sync_player_goals", bot_id, bot_character.goals) diff --git a/scenes/main.tscn b/scenes/main.tscn index 17af480..983f95d 100644 --- a/scenes/main.tscn +++ b/scenes/main.tscn @@ -37,7 +37,7 @@ floors = 2 metadata/_editor_floor_ = Vector3(0, 1, 0) [node name="Camera3D" type="Camera3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 0.422618, 0.906308, 0, -0.906308, 0.422618, 6.75, 21, 16.5) +transform = Transform3D(1, 0, 0, 0, 0.422618, 0.906308, 0, -0.906308, 0.422618, 7, 24, 17) environment = ExtResource("4_ky38j") fov = 35.5 diff --git a/scenes/player.gd b/scenes/player.gd index 3147a30..0d5627f 100644 --- a/scenes/player.gd +++ b/scenes/player.gd @@ -395,11 +395,11 @@ func initialize_random_goals(_size:int, min_value:int, max_value:int, null_count return result -func append_random_goals(): - goals.append_array(initialize_random_goals(9, 7, 10, 1.0)) - - if is_multiplayer_authority(): - rpc("sync_goals", goals) +# Remove this since goals are now set by main.gd +#func append_random_goals(): +# goals.append_array(initialize_random_goals(9, 7, 10, 1.0)) +# if is_multiplayer_authority(): +# rpc("sync_goals", goals) func bot_try_grab_item() -> bool: if not enhanced_gridmap or action_points <= 0: @@ -939,7 +939,7 @@ func sync_grid_item(x: int, y: int, z: int, item: int): @rpc("any_peer", "call_local") func sync_goals(new_goals: Array): - goals = new_goals + goals = new_goals.duplicate() # Make sure to duplicate the array @rpc("any_peer", "call_local") func sync_playerboard(new_playerboard: Array):