Reskin player

This commit is contained in:
2024-10-23 14:56:11 +08:00
parent 09df1be654
commit 52636a2a7e
40 changed files with 647 additions and 66 deletions
+33 -9
View File
@@ -46,6 +46,7 @@ func _on_peer_connected(new_peer_id):
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)
rpc_id(new_peer_id, "sync_game_state", players, bots, game_started)
add_player_character(new_peer_id)
replace_bot_with_player(new_peer_id)
@@ -65,25 +66,35 @@ func add_player_character(peer_id):
local_player_character = player_character
func add_bot(bot_id):
if multiplayer.is_server():
rpc("create_bot", bot_id)
@rpc("call_local")
func create_bot(bot_id):
var bot_character = player_scene.instantiate()
bot_character.set_multiplayer_authority(1) # Set server as authority for bots
bot_character.name = str(bot_id)
add_child(bot_character)
bot_character.add_to_group("Players", true)
bot_character.add_to_group("Bots", true)
bots.append(bot_id)
players.append(bot_id)
if multiplayer.is_server():
bots.append(bot_id)
players.append(bot_id)
func replace_bot_with_player(player_id):
if bots.size() > 0:
if multiplayer.is_server() and bots.size() > 0:
var bot_id = bots.pop_front()
players.erase(bot_id)
players.append(player_id)
var bot_node = get_node(str(bot_id))
if bot_node:
bot_node.queue_free()
rpc("remove_bot", bot_id)
rpc("sync_players", players)
@rpc("call_local")
func remove_bot(bot_id):
var bot_node = get_node(str(bot_id))
if bot_node:
bot_node.queue_free()
func get_next_available_bot_id():
for i in range(2, max_players + 1):
if not i in players:
@@ -99,6 +110,17 @@ func add_previously_connected_player_characters(peer_ids):
for peer_id in peer_ids:
add_player_character(peer_id)
@rpc("call_local")
func sync_game_state(current_players, current_bots, is_game_started):
players = current_players
bots = current_bots
game_started = is_game_started
# Create bot characters for existing bots
for bot_id in bots:
if not has_node(str(bot_id)):
create_bot(bot_id)
func _process(_delta):
if multiplayer.is_server() and game_started:
rpc("sync_turn_index", current_turn_index)
@@ -107,13 +129,15 @@ func start_game():
if multiplayer.is_server():
game_started = true
connected_peer_ids.sort()
rpc("sync_game_start", connected_peer_ids)
rpc("sync_game_start", connected_peer_ids, players, bots)
current_turn_index = -1
next_turn()
@rpc
func sync_game_start(peer_ids):
@rpc("call_local")
func sync_game_start(peer_ids, current_players, current_bots):
connected_peer_ids = peer_ids
players = current_players
bots = current_bots
game_started = true
@rpc("reliable")