update bot experimental

This commit is contained in:
2025-01-28 16:34:46 +08:00
parent 5a9092adfc
commit f3d720d91d
16 changed files with 272 additions and 375 deletions
+29 -12
View File
@@ -232,11 +232,19 @@ func _on_join_pressed():
func _on_peer_connected(new_peer_id):
if multiplayer.is_server():
await get_tree().create_timer(1).timeout
rpc("add_newly_connected_player_character", new_peer_id)
rpc_id(new_peer_id, "add_previously_connected_player_characters", connected_peer_ids)
# Increase delay to ensure scene is ready
await get_tree().create_timer(1.5).timeout
# Sync full state first
rpc_id(new_peer_id, "sync_game_state", players, bots, game_started, turn_based_mode)
# Then add players in correct order
for peer_id in connected_peer_ids:
rpc_id(new_peer_id, "add_player_character", peer_id)
# Finally add the new player
add_player_character(new_peer_id)
rpc("add_newly_connected_player_character", new_peer_id)
replace_bot_with_player(new_peer_id)
func _on_peer_disconnected(peer_id):
@@ -270,23 +278,32 @@ func add_bot(bot_id):
@rpc("call_local")
func create_bot(bot_id):
# Ensure we're not duplicating bots
if has_node(str(bot_id)):
push_error("Bot already exists: " + str(bot_id))
return
var bot_character = player_scene.instantiate()
bot_character.set_multiplayer_authority(1)
if not bot_character:
push_error("Failed to instantiate bot scene")
return
bot_character.set_multiplayer_authority(1) # Server controls bots
bot_character.name = str(bot_id)
bot_character.is_bot = true # Set bot flag
add_child(bot_character)
# Add to scene tree
call_deferred("add_child", bot_character)
# Add to groups after adding to scene tree
bot_character.add_to_group("Players", true)
bot_character.add_to_group("Bots", true)
# Get behavior tree reference
var behavior_tree = bot_character.get_node_or_null("BehaviorTree")
if behavior_tree:
behavior_tree.enabled = true
behavior_tree.actor = bot_character
if multiplayer.is_server():
bots.append(bot_id)
players.append(bot_id)
# Sync bot status after a short delay to ensure node is ready
await get_tree().create_timer(0.1).timeout
bot_character.rpc("sync_bot_status", true)
func replace_bot_with_player(player_id):
if multiplayer.is_server() and bots.size() > 0: