on client side work playerboards

This commit is contained in:
2025-02-18 12:26:31 +08:00
parent 1c8a6aab9f
commit f66c6eeb63
+45 -24
View File
@@ -305,7 +305,7 @@ func _on_peer_disconnected(peer_id):
players.erase(peer_id) players.erase(peer_id)
add_bot(get_next_available_bot_id()) add_bot(get_next_available_bot_id())
@rpc("any_peer", "call_local") @rpc("any_peer", "call_local")
func add_player_character(peer_id): func add_player_character(peer_id):
connected_peer_ids.append(peer_id) connected_peer_ids.append(peer_id)
var player_character = player_scene.instantiate() var player_character = player_scene.instantiate()
@@ -322,25 +322,28 @@ func add_player_character(peer_id):
# Set goals based on player ID if server # Set goals based on player ID if server
if multiplayer.is_server(): if multiplayer.is_server():
var goal_index = peer_id - 1 var goal_index = peer_id - 1
# Verify index is valid
if goal_index >= 0 and goal_index < preset_goals.size(): if goal_index >= 0 and goal_index < preset_goals.size():
# Convert to int array before assigning # Convert to int array before assigning
var goals: Array[int] = [] var goals: Array[int] = []
for g in preset_goals[goal_index]: for g in preset_goals[goal_index]:
goals.append(g) goals.append(g)
player_character.goals = goals player_character.goals = goals
# Force sync goals to everyone, including host
rpc("sync_player_goals", peer_id, goals) 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(): if peer_id == multiplayer.get_unique_id():
local_player_character = player_character local_player_character = player_character
update_button_states() update_button_states()
update_playerboard_ui() update_playerboard_ui()
#update_goals_ui() update_all_players_goals() # Force UI update for local player
# Sync this player's goals to everyone # Request goals from server if we're a client
if multiplayer.is_server(): if not multiplayer.is_server():
rpc("sync_player_goals", peer_id, player_character.goals) rpc_id(1, "request_goals_from_server", peer_id)
if multiplayer.is_server(): if multiplayer.is_server():
if peer_id > 1: # Not the host if peer_id > 1: # Not the host
# Assign preset goals # Assign preset goals
@@ -658,25 +661,43 @@ func update_all_players_goals():
@rpc("any_peer", "call_local") @rpc("any_peer", "call_local")
func sync_player_goals(player_id: int, goals: Array): 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) var player_idx = players.find(player_id)
if player_idx >= 0 and player_idx < 4: if player_idx >= 0 and player_idx < 4:
var goals_grid = $AllPlayerGoals.get_child(player_idx) _update_player_goals_ui(player_idx, goals)
for slot_idx in range(9):
var slot = goals_grid.get_child(slot_idx) # Helper function to update specific player's goals UI
var goal_value = goals[slot_idx] func _update_player_goals_ui(player_idx: int, goals: Array):
var goals_grid = $AllPlayerGoals.get_child(player_idx)
# Hide all tiles for slot_idx in range(9):
slot.get_node("TileHeart").hide() var slot = goals_grid.get_child(slot_idx)
slot.get_node("TileDiamond").hide() var goal_value = goals[slot_idx] if slot_idx < goals.size() else -1
slot.get_node("TileStar").hide()
slot.get_node("TileCoin").hide() # Hide all tiles first
for tile in ["TileHeart", "TileDiamond", "TileStar", "TileCoin"]:
# Show appropriate tile slot.get_node(tile).hide()
match goal_value:
7: slot.get_node("TileHeart").show() # Show appropriate tile
8: slot.get_node("TileDiamond").show() match goal_value:
9: slot.get_node("TileStar").show() 7: slot.get_node("TileHeart").show()
10: slot.get_node("TileCoin").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 # 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: func initialize_random_goals(_size:int, min_value:int, max_value:int, null_count:float) -> Array: