From f66c6eeb63ed6eae7a9776032bd0e298698af61d Mon Sep 17 00:00:00 2001 From: adtpdn Date: Tue, 18 Feb 2025 12:26:31 +0800 Subject: [PATCH] on client side work playerboards --- scenes/main.gd | 69 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/scenes/main.gd b/scenes/main.gd index 982ff47..d1e9935 100644 --- a/scenes/main.gd +++ b/scenes/main.gd @@ -305,7 +305,7 @@ func _on_peer_disconnected(peer_id): players.erase(peer_id) add_bot(get_next_available_bot_id()) -@rpc("any_peer", "call_local") +@rpc("any_peer", "call_local") func add_player_character(peer_id): connected_peer_ids.append(peer_id) var player_character = player_scene.instantiate() @@ -322,25 +322,28 @@ func add_player_character(peer_id): # 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 + # Force sync goals to everyone, including host rpc("sync_player_goals", peer_id, goals) + # Update UI immediately for server + update_all_players_goals() + # Local player setup if peer_id == multiplayer.get_unique_id(): local_player_character = player_character update_button_states() update_playerboard_ui() - #update_goals_ui() + update_all_players_goals() # Force UI update for local player - # Sync this player's goals to everyone - if multiplayer.is_server(): - rpc("sync_player_goals", peer_id, player_character.goals) - + # Request goals from server if we're a client + if not multiplayer.is_server(): + rpc_id(1, "request_goals_from_server", peer_id) + if multiplayer.is_server(): if peer_id > 1: # Not the host # Assign preset goals @@ -658,25 +661,43 @@ func update_all_players_goals(): @rpc("any_peer", "call_local") func sync_player_goals(player_id: int, goals: Array): + # Update the player's goals first + var player = get_node_or_null(str(player_id)) + if player: + player.goals = goals.duplicate() + + # Then update UI + update_all_players_goals() + + # Force update for specific player in AllPlayerGoals var player_idx = players.find(player_id) if player_idx >= 0 and player_idx < 4: - var goals_grid = $AllPlayerGoals.get_child(player_idx) - for slot_idx in range(9): - var slot = goals_grid.get_child(slot_idx) - var goal_value = goals[slot_idx] - - # Hide all tiles - slot.get_node("TileHeart").hide() - slot.get_node("TileDiamond").hide() - slot.get_node("TileStar").hide() - slot.get_node("TileCoin").hide() - - # Show appropriate tile - match goal_value: - 7: slot.get_node("TileHeart").show() - 8: slot.get_node("TileDiamond").show() - 9: slot.get_node("TileStar").show() - 10: slot.get_node("TileCoin").show() + _update_player_goals_ui(player_idx, goals) + +# Helper function to update specific player's goals UI +func _update_player_goals_ui(player_idx: int, goals: Array): + var goals_grid = $AllPlayerGoals.get_child(player_idx) + for slot_idx in range(9): + var slot = goals_grid.get_child(slot_idx) + var goal_value = goals[slot_idx] if slot_idx < goals.size() else -1 + + # Hide all tiles first + for tile in ["TileHeart", "TileDiamond", "TileStar", "TileCoin"]: + slot.get_node(tile).hide() + + # Show appropriate tile + match goal_value: + 7: slot.get_node("TileHeart").show() + 8: slot.get_node("TileDiamond").show() + 9: slot.get_node("TileStar").show() + 10: slot.get_node("TileCoin").show() + +@rpc("any_peer") +func request_goals_from_server(requesting_peer_id: int): + if multiplayer.is_server(): + var goal_index = requesting_peer_id - 1 + if goal_index >= 0 and goal_index < preset_goals.size(): + rpc("sync_player_goals", requesting_peer_id, preset_goals[goal_index]) # Add this function near the top with other helper functions func initialize_random_goals(_size:int, min_value:int, max_value:int, null_count:float) -> Array: