2024-08-02 22:38:08 +02:00
## Copyright (C) 2024 Patrick_Pluto
##
## This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
##
## This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
2024-08-02 20:13:42 +02:00
extends Node
const DEFAULT_SERVER_IP = " 127.0.0.1 "
const PORT = 36969
const MAX_CONNECTIONS = 16
var players = { }
var players_numbered = [ ]
var label
var map
var character = preload ( " res://objects/player.tscn " )
func join_game ( ip ) :
var peer = ENetMultiplayerPeer . new ( )
var error = peer . create_client ( ip , PORT )
if error :
print ( " error " )
return error
multiplayer . multiplayer_peer = peer
func create_game ( ) :
var peer = ENetMultiplayerPeer . new ( )
var error = peer . create_server ( PORT , MAX_CONNECTIONS )
if error :
print ( " error " )
return error
multiplayer . multiplayer_peer = peer
print ( " done " )
@ rpc ( " any_peer " , " call_remote " , " reliable " )
func send_playerinfo ( name , id ) :
if multiplayer . is_server ( ) :
2024-08-02 22:30:30 +02:00
var done = 0
2024-08-02 20:13:42 +02:00
players [ id ] = name
label = get_tree ( ) . root . get_node ( " create/player_list/list " )
2024-08-02 22:30:30 +02:00
label . text = " "
2024-08-02 20:13:42 +02:00
for w in players :
2024-08-02 22:30:30 +02:00
if done == 1 :
label . text = str ( label . text + " \n " + players [ w ] )
else :
label . text = players [ w ]
done = 1
2024-08-02 20:13:42 +02:00
@ rpc ( " authority " , " call_local " , " reliable " )
func start_game ( server_players ) :
players = server_players
players_numbered = players . keys ( )
get_tree ( ) . change_scene_to_file ( " res://maps/base_map.tscn " )
map = get_tree ( ) . root . get_node ( " . " )
var i = 0
for w in players :
var player = character . instantiate ( )
player . name = " player " + str ( i )
i += 1
player . position . z = - i * 1.5
map . add_child ( player )
@ rpc ( " any_peer " , " call_remote " , " unreliable " )
func sync_player ( node_name , position , rotation ) :
var current_character = get_tree ( ) . root . get_node ( " ./ " + node_name )
current_character . position = position
current_character . rotation = rotation
2024-08-02 22:30:30 +02:00
@ rpc ( " authority " , " call_local " , " reliable " )
func sync_computers ( node_name , current ) :
var current_character = get_tree ( ) . root . get_node ( " ./map/ " + node_name )
print ( current_character )
current_character . current = current