This commit is contained in:
2025-02-04 18:08:56 +08:00
parent c578c3e97e
commit 7d022775d3
3 changed files with 40 additions and 30 deletions
+33 -23
View File
@@ -239,24 +239,24 @@ func _on_host_pressed():
multiplayer.multiplayer_peer = multiplayer_peer multiplayer.multiplayer_peer = multiplayer_peer
$NetworkInfo/UniquePeerID.text = str(multiplayer.get_unique_id()) $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) add_player_character(1)
players.append(1) players.append(1)
# Generate goals for host first # Add bots with their own goals
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)
for i in range(2, max_players + 1): for i in range(2, max_players + 1):
add_bot(i) add_bot(i)
@@ -315,6 +315,18 @@ func add_player_character(peer_id):
add_child(player_character) add_child(player_character)
player_character.add_to_group("Players", true) 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(): if peer_id == multiplayer.get_unique_id():
local_player_character = player_character local_player_character = player_character
update_button_states() 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 var bot_to_replace = get_node_or_null(str(bots[0])) if bots.size() > 0 else null
if bot_to_replace: if bot_to_replace:
player_character.goals = bot_to_replace.goals.duplicate() 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): func add_bot(bot_id):
rpc("create_bot", bot_id) rpc("create_bot", bot_id)
@@ -372,10 +381,11 @@ func create_bot(bot_id):
bots.append(bot_id) bots.append(bot_id)
players.append(bot_id) players.append(bot_id)
# Assign preset goals # Assign goals from preset array
var goal_index = bot_id - 2 var goal_index = bot_id - 1
if goal_index < preset_goals.size(): 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 # Sync bot status after a short delay to ensure node is ready
await get_tree().create_timer(0.1).timeout 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) rpc("sync_player_goals", bot_id, bot_character.goals)
# Only generate goals for new bots, not replacement bots # Only generate goals for new bots, not replacement bots
if not (players.size() > max_players): #if not (players.size() > max_players):
bot_character.append_random_goals() #bot_character.append_random_goals()
# Always sync the bot's goals # Always sync the bot's goals
rpc("sync_player_goals", bot_id, bot_character.goals) rpc("sync_player_goals", bot_id, bot_character.goals)
+1 -1
View File
@@ -37,7 +37,7 @@ floors = 2
metadata/_editor_floor_ = Vector3(0, 1, 0) metadata/_editor_floor_ = Vector3(0, 1, 0)
[node name="Camera3D" type="Camera3D" parent="."] [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") environment = ExtResource("4_ky38j")
fov = 35.5 fov = 35.5
+6 -6
View File
@@ -395,11 +395,11 @@ func initialize_random_goals(_size:int, min_value:int, max_value:int, null_count
return result return result
func append_random_goals(): # Remove this since goals are now set by main.gd
goals.append_array(initialize_random_goals(9, 7, 10, 1.0)) #func append_random_goals():
# goals.append_array(initialize_random_goals(9, 7, 10, 1.0))
if is_multiplayer_authority(): # if is_multiplayer_authority():
rpc("sync_goals", goals) # rpc("sync_goals", goals)
func bot_try_grab_item() -> bool: func bot_try_grab_item() -> bool:
if not enhanced_gridmap or action_points <= 0: 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") @rpc("any_peer", "call_local")
func sync_goals(new_goals: Array): func sync_goals(new_goals: Array):
goals = new_goals goals = new_goals.duplicate() # Make sure to duplicate the array
@rpc("any_peer", "call_local") @rpc("any_peer", "call_local")
func sync_playerboard(new_playerboard: Array): func sync_playerboard(new_playerboard: Array):