2024-08-08 21:49:24 +02:00
## freeftf
## 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, either version 3 of the License, or (at your option) any later version.
##
## 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/>.
extends Node
var offline
func _ready ( ) :
offline = multiplayer . multiplayer_peer
multiplayer . server_disconnected . connect ( _on_server_disconnected )
func join_game ( ) :
var peer = ENetMultiplayerPeer . new ( )
2024-08-13 20:43:54 +02:00
peer . create_client ( Game . settings [ " ip " ] , int ( Game . settings [ " port " ] ) )
2024-08-08 21:49:24 +02:00
multiplayer . multiplayer_peer = peer
func is_valid ( sender_id , must_be_from_owner ) :
if must_be_from_owner :
if sender_id == Game . player_list . keys ( ) [ 0 ] or Game . is_server :
return true
for id in Game . player_list :
if sender_id == id :
return true
if sender_id == 0 or sender_id == 1 :
return true
return false
func _on_server_disconnected ( ) :
Game . reset ( )
get_tree ( ) . change_scene_to_file ( " res://menus/main_menu.tscn " )
@ rpc ( " authority " , " call_remote " , " reliable " )
func _on_player_disconnected ( id ) :
var x = 0
for i in Game . player_list :
if i == id :
var current_character = get_tree ( ) . root . get_node ( " ./ " + Game . map_name + " /player " + str ( x ) )
if current_character . hp == 0 :
Game . dead -= 1
elif current_character . is_frozen :
Game . frozen -= 1
elif current_character . beast :
Game . player_escaped = true
Game . players = 0
else :
Game . players -= 1
await get_tree ( ) . create_timer ( 0.5 ) . timeout
current_character . free ( )
2024-08-11 19:43:03 +02:00
if current_character . captured_by != null :
current_character . captured_by . lost_one ( )
2024-08-08 21:49:24 +02:00
x += 1
Game . player_list . erase ( id )
@ rpc ( " authority " , " call_remote " , " reliable " )
func room_closed ( ) :
_on_server_disconnected ( )
@ rpc ( " authority " , " call_remote " , " reliable " )
func send_playerinfo ( ) :
Matchmaking . rpc_id ( 1 , " recieve_playerinfo " , Game . player_name , Game . room_name )
@ rpc ( " authority " , " call_remote " , " reliable " )
func get_player_list ( server_player_list ) :
Game . player_list = server_player_list
@ rpc ( " any_peer " , " call_local " , " reliable " )
func start_game ( ) :
if is_valid ( multiplayer . get_remote_sender_id ( ) , true ) :
Game . start_game ( )
@ rpc ( " any_peer " , " call_local " , " reliable " )
func player_hit ( target , beast ) :
if is_valid ( multiplayer . get_remote_sender_id ( ) , false ) :
Game . player_hit ( target , beast )
@ rpc ( " any_peer " , " call_local " , " unreliable " )
func sync_player ( node_name , position , rotation ) :
if is_valid ( multiplayer . get_remote_sender_id ( ) , false ) :
Game . sync_player ( node_name , position , rotation )
@ rpc ( " any_peer " , " call_local " , " unreliable " )
func sync_hammer ( node_name , rotation ) :
if is_valid ( multiplayer . get_remote_sender_id ( ) , false ) :
Game . sync_hammer ( node_name , rotation )
@ rpc ( " any_peer " , " call_local " , " reliable " )
func sync_computers ( node_name , current ) :
if is_valid ( multiplayer . get_remote_sender_id ( ) , true ) :
Game . sync_computers ( node_name , current )
@ rpc ( " any_peer " , " call_local " , " reliable " )
func sync_beast ( player ) :
if is_valid ( multiplayer . get_remote_sender_id ( ) , true ) :
Game . sync_beast ( player )