first commit

This commit is contained in:
2024-10-22 16:15:56 +08:00
commit 6b9360d4dc
44 changed files with 2235 additions and 0 deletions
+133
View File
@@ -0,0 +1,133 @@
extends Node3D # This script is attached to a Node3D (main scene)
var multiplayer_peer = ENetMultiplayerPeer.new() # Create a new ENet multiplayer peer
const PORT = 9999 # Define the port for multiplayer
const ADDRESS = "127.0.0.1" # Define the IP address for multiplayer
var connected_peer_ids = [] # List of connected peer IDs
var local_player_character : CharacterBody3D # Reference to the local player character
var player_scene = preload("res://scenes/player.tscn") # Preload the player scene
var current_turn_index = 0 # Index of the current turn
@export var players = [] # List of player IDs
var game_started = false # Flag to track if the game has started
var max_message_input_char = 51 # Maximum characters allowed in a message
func _on_host_pressed(): # Called when the host button is pressed
$NetworkInfo/NetworkSideDisplay.text = "Server"
$Menu.visible = false
multiplayer_peer.create_server(PORT) # Create a multiplayer server
multiplayer.multiplayer_peer = multiplayer_peer
$NetworkInfo/UniquePeerID.text = str(multiplayer.get_unique_id())
add_player_character(1) # Add host player character
players.append(1) # Add host to players list
if players.size() == 2:
start_game() # Start game if two players are connected
multiplayer_peer.peer_connected.connect(_on_peer_connected) # Connect peer connected signal
func _on_join_pressed(): # Called when the join button is pressed
$NetworkInfo/NetworkSideDisplay.text = "Client"
$Menu.visible = false
multiplayer_peer.create_client(ADDRESS, PORT) # Create a multiplayer client
multiplayer.multiplayer_peer = multiplayer_peer
$NetworkInfo/UniquePeerID.text = str(multiplayer.get_unique_id())
func _on_peer_connected(new_peer_id): # Called when a new peer connects
if multiplayer.is_server():
await get_tree().create_timer(1).timeout
rpc("add_newly_connected_player_character", new_peer_id) # RPC call to add new player
rpc_id(new_peer_id, "add_previously_connected_player_characters", connected_peer_ids) # RPC call to sync existing players
add_player_character(new_peer_id)
players.append(new_peer_id)
if players.size() == 2 and not game_started:
start_game() # Start game if two players are connected
func _on_peer_disconnected(peer_id): # Called when a peer disconnects
if multiplayer.is_server():
connected_peer_ids.erase(peer_id)
players.erase(peer_id)
if players.size() < 2:
game_started = false
func add_player_character(peer_id): # Add a player character to the game
connected_peer_ids.append(peer_id)
var player_character = player_scene.instantiate()
player_character.set_multiplayer_authority(peer_id)
add_child(player_character)
player_character.add_to_group("Players",true)
if peer_id == multiplayer.get_unique_id():
local_player_character = player_character
@rpc
func add_newly_connected_player_character(new_peer_id): # RPC function to add a new player character
add_player_character(new_peer_id)
@rpc
func add_previously_connected_player_characters(peer_ids): # RPC function to add existing player characters
for peer_id in peer_ids:
add_player_character(peer_id)
func _process(_delta): # Called every frame
if multiplayer.is_server() and game_started:
rpc("sync_turn_index", current_turn_index) # RPC call to sync turn index
func start_game(): # Start the game
if multiplayer.is_server():
game_started = true
connected_peer_ids.sort()
rpc("sync_game_start", connected_peer_ids) # RPC call to sync game start
current_turn_index = -1
next_turn()
@rpc
func sync_game_start(peer_ids): # RPC function to sync game start
connected_peer_ids = peer_ids
game_started = true
@rpc("reliable")
func sync_turn_index(index): # RPC function to sync turn index
current_turn_index = index
func next_turn(): # Move to the next turn
if multiplayer.is_server():
current_turn_index += 1
if current_turn_index >= connected_peer_ids.size():
current_turn_index = -1
next_turn()
else:
rpc("set_current_turn", connected_peer_ids[current_turn_index]) # RPC call to set current turn
func request_next_turn(): # Request to move to the next turn
if multiplayer.is_server():
end_current_turn()
else:
rpc_id(1, "server_end_current_turn") # RPC call to end turn on server
@rpc("any_peer")
func server_end_current_turn(): # RPC function to end current turn on server
if multiplayer.is_server():
end_current_turn()
@rpc("any_peer", "call_local")
func set_current_turn(player_id): # RPC function to set the current turn
var players = get_tree().get_nodes_in_group("Players")
for player in players:
player.is_my_turn = (player.name == str(player_id))
if player.is_my_turn:
player.start_turn()
func end_current_turn(): # End the current turns
if multiplayer.is_server():
next_turn()
rpc("sync_turn_index", current_turn_index) # RPC call to sync turn index
func _on_message_input_text_submitted(new_text): # Handle message input
if new_text.length() > max_message_input_char:
new_text = new_text.substr(0, max_message_input_char) + " ... "
local_player_character.rpc("display_message", new_text) # RPC call to display message
$MessageInput.text = ""
$MessageInput.release_focus()