2024-02-22 06:22:22 +11:00
|
|
|
class_name MultiplayerLobby extends Control
|
2023-11-08 14:28:55 +11:00
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
signal player_connected(peer_id: int, player_profile: PlayerProfile)
|
|
|
|
signal player_disconnected(peer_id: int)
|
2023-11-08 14:28:55 +11:00
|
|
|
signal disconnected_from_server
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
const SERVER_PORT: int = 58008
|
|
|
|
const MAX_PLAYERS: int = 4
|
2023-11-08 14:28:55 +11:00
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
@export var server_form: ServerForm
|
|
|
|
@export var scoreboard: Scoreboard
|
|
|
|
@export var loadout_editor: HeroSelector
|
|
|
|
@export var chatbox: Chatbox
|
2023-11-08 14:28:55 +11:00
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
var enet_peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
|
|
|
|
var alert_popup_scene: PackedScene = preload("res://Scenes/Menus/alert_popup.tscn")
|
|
|
|
var connected_players_profiles: Dictionary = {}
|
2023-11-08 14:28:55 +11:00
|
|
|
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func _ready() -> void:
|
2023-11-08 14:28:55 +11:00
|
|
|
multiplayer.peer_connected.connect(_on_player_connected)
|
|
|
|
multiplayer.peer_disconnected.connect(_on_player_disconnected)
|
|
|
|
multiplayer.connected_to_server.connect(_on_connection_succeeded)
|
|
|
|
multiplayer.connection_failed.connect(_on_connection_failed)
|
|
|
|
multiplayer.server_disconnected.connect(_on_server_disconnected)
|
|
|
|
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func _on_player_connected(peer_id: int) -> void:
|
2023-11-08 14:28:55 +11:00
|
|
|
add_player.rpc_id(peer_id, Data.player_profile.to_dict())
|
|
|
|
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func _on_player_disconnected(peer_id: int) -> void:
|
2023-11-16 00:07:41 +11:00
|
|
|
if chatbox:
|
2023-11-17 20:49:38 +11:00
|
|
|
chatbox.append_message("SERVER", Color.TOMATO, connected_players_profiles[peer_id].display_name + " has disconnected!")
|
2023-11-08 14:28:55 +11:00
|
|
|
connected_players_profiles.erase(peer_id)
|
|
|
|
player_disconnected.emit(peer_id)
|
|
|
|
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func _on_connection_succeeded() -> void:
|
2023-11-08 14:28:55 +11:00
|
|
|
setup_game(multiplayer.get_unique_id())
|
|
|
|
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func _on_connection_failed() -> void:
|
2023-11-08 14:28:55 +11:00
|
|
|
multiplayer.multiplayer_peer = null
|
2024-02-22 06:22:22 +11:00
|
|
|
var popup: AlertPopup = alert_popup_scene.instantiate() as AlertPopup
|
2023-11-08 14:28:55 +11:00
|
|
|
popup.set_popup("Unable to connect to server", "OK")
|
|
|
|
add_child(popup)
|
|
|
|
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func _on_server_disconnected() -> void:
|
2023-11-08 14:28:55 +11:00
|
|
|
multiplayer.multiplayer_peer = null
|
|
|
|
disconnected_from_server.emit()
|
|
|
|
|
|
|
|
|
|
|
|
func create_server() -> void:
|
|
|
|
enet_peer.create_server(SERVER_PORT, MAX_PLAYERS)
|
|
|
|
multiplayer.multiplayer_peer = enet_peer
|
|
|
|
setup_game(1)
|
|
|
|
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func setup_game(peer_id: int) -> void:
|
2023-11-08 14:28:55 +11:00
|
|
|
player_disconnected.connect(Game.remove_player)
|
|
|
|
Game.spawn_level()
|
|
|
|
scoreboard.all_players_ready.connect(start_game)
|
|
|
|
Game.game_restarted.connect(setup_the_ui)
|
2023-11-17 20:49:38 +11:00
|
|
|
Game.chatbox = chatbox
|
2023-11-08 14:28:55 +11:00
|
|
|
setup_the_ui()
|
|
|
|
chatbox.username = Data.player_profile.display_name
|
|
|
|
Data.player_profile.display_name_changed.connect(chatbox.change_username)
|
2023-12-09 01:09:12 +11:00
|
|
|
loadout_editor.hero_selected.connect(Data.player_profile.set_preferred_class)
|
|
|
|
loadout_editor.hero_selected.connect(edit_player_profile)
|
2023-11-08 14:28:55 +11:00
|
|
|
connected_players_profiles[peer_id] = Data.player_profile
|
|
|
|
player_connected.emit(peer_id, Data.player_profile)
|
|
|
|
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func setup_the_ui() -> void:
|
2023-11-08 14:28:55 +11:00
|
|
|
scoreboard.unready_all_players()
|
|
|
|
scoreboard.set_visible(true)
|
|
|
|
loadout_editor.set_visible(true)
|
|
|
|
$ReadyButton.set_visible(true)
|
|
|
|
chatbox.set_visible(true)
|
|
|
|
|
|
|
|
|
|
|
|
func connect_to_server() -> void:
|
2024-02-22 06:22:22 +11:00
|
|
|
var ip: String = server_form.get_server_ip() if server_form.get_server_ip() else "localhost"
|
|
|
|
var port: String = server_form.get_server_port() if server_form.get_server_port() else str(SERVER_PORT)
|
2023-11-08 14:28:55 +11:00
|
|
|
enet_peer.create_client(ip, int(port))
|
|
|
|
multiplayer.multiplayer_peer = enet_peer
|
|
|
|
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func ready_player() -> void:
|
|
|
|
var peer_id: int = multiplayer.get_unique_id()
|
2023-11-08 14:28:55 +11:00
|
|
|
networked_ready_player.rpc(peer_id)
|
|
|
|
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func start_game() -> void:
|
2023-11-08 14:28:55 +11:00
|
|
|
enet_peer.refuse_new_connections = true
|
|
|
|
Game.spawn_players(connected_players_profiles.keys(), connected_players_profiles, chatbox.opened, chatbox.closed)
|
|
|
|
scoreboard.set_visible(false)
|
|
|
|
loadout_editor.set_visible(false)
|
|
|
|
|
|
|
|
|
2024-03-29 21:58:40 +11:00
|
|
|
#TODO: what the fuck is this doing lol
|
|
|
|
func edit_player_profile(_argument: int) -> void:
|
2024-02-22 06:22:22 +11:00
|
|
|
var profile_dict: Dictionary = Data.player_profile.to_dict()
|
2023-11-08 14:28:55 +11:00
|
|
|
networked_edit_player_profile.rpc(multiplayer.get_unique_id(), profile_dict)
|
|
|
|
|
|
|
|
|
|
|
|
@rpc("any_peer", "reliable", "call_local")
|
2024-02-22 06:22:22 +11:00
|
|
|
func networked_edit_player_profile(peer_id: int, new_profile_dict: Dictionary) -> void:
|
2023-11-08 14:28:55 +11:00
|
|
|
connected_players_profiles[peer_id].set_display_name(new_profile_dict["display_name"])
|
|
|
|
connected_players_profiles[peer_id].set_preferred_class(new_profile_dict["preferred_class"])
|
|
|
|
|
|
|
|
|
|
|
|
@rpc("any_peer","reliable")
|
2024-02-22 06:22:22 +11:00
|
|
|
func add_player(new_player_profile_dict: Dictionary) -> void:
|
|
|
|
var new_player_peer_id: int = multiplayer.get_remote_sender_id()
|
|
|
|
var new_player_profile: PlayerProfile = PlayerProfile.from_dict(new_player_profile_dict)
|
2023-11-16 00:07:41 +11:00
|
|
|
if chatbox:
|
2023-11-17 20:49:38 +11:00
|
|
|
chatbox.append_message("SERVER", Color.TOMATO, new_player_profile.display_name + " has connected!")
|
2023-11-08 14:28:55 +11:00
|
|
|
connected_players_profiles[new_player_peer_id] = new_player_profile
|
|
|
|
player_connected.emit(new_player_peer_id, new_player_profile)
|
|
|
|
|
|
|
|
|
|
|
|
@rpc("any_peer", "reliable", "call_local")
|
2024-02-22 06:22:22 +11:00
|
|
|
func networked_ready_player(peer_id: int) -> void:
|
2023-11-08 14:28:55 +11:00
|
|
|
scoreboard.set_player_ready_state(peer_id, true)
|
2023-12-17 02:02:16 +11:00
|
|
|
|
|
|
|
|
|
|
|
func _on_button_mouse_entered() -> void:
|
|
|
|
$AudioStreamPlayer.play()
|