1
Fork 0
mirror of https://github.com/Steffo99/nanogolf.git synced 2024-11-22 08:04:21 +00:00
algodist-steffo-nanogolf/scenes/main.gd

190 lines
8.6 KiB
GDScript3
Raw Normal View History

2024-02-18 17:15:59 +00:00
extends Node
class_name Main
2024-02-27 02:54:47 +00:00
@onready var scene_tree: SceneTree = get_tree()
2024-03-16 04:25:23 +00:00
@export var interface_instance: Interface
const main_menu_scene: PackedScene = preload("res://scenes/main_menu.tscn")
var main_menu_instance: MainMenu = null
2024-02-27 02:54:47 +00:00
const game_scene: PackedScene = preload("res://scenes/game.tscn")
var server_game_instance: Game = null
var client_game_instance: Game = null
2024-02-28 05:30:45 +00:00
const lobby_menu_scene: PackedScene = preload("res://scenes/lobby_menu.tscn")
var lobby_menu_instance: LobbyMenu = null
2024-02-27 02:54:47 +00:00
2024-03-16 04:25:23 +00:00
const game_hud_scene: PackedScene = preload("res://scenes/game_hud.tscn")
var game_hud_instance: GameHUD = null
2024-03-18 06:23:17 +00:00
const results_menu_scene: PackedScene = preload("res://scenes/results_menu.tscn")
var results_menu_instance: ResultsMenu = null
2024-03-12 14:45:08 +00:00
func init_main_menu() -> void:
main_menu_instance = main_menu_scene.instantiate()
2024-02-27 02:54:47 +00:00
main_menu_instance.hosting_confirmed.connect(_on_hosting_confirmed)
main_menu_instance.connecting_confirmed.connect(_on_connecting_confirmed)
interface_instance.add_child(main_menu_instance)
2024-03-12 14:45:08 +00:00
func deinit_main_menu() -> void:
2024-02-27 02:54:47 +00:00
main_menu_instance.queue_free()
2024-03-16 04:25:23 +00:00
main_menu_instance = null
2024-02-27 02:54:47 +00:00
2024-03-12 14:45:08 +00:00
func init_server_game(server_port: int) -> void:
2024-02-27 02:54:47 +00:00
server_game_instance = game_scene.instantiate()
server_game_instance.name = "Server"
add_child(server_game_instance, true)
print("[Main] Creating server at: *:", server_port)
var smp: SceneMultiplayer = SceneMultiplayer.new()
var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
var error: Error = peer.create_server(server_port)
if error != OK or peer.get_connection_status() == MultiplayerPeer.CONNECTION_DISCONNECTED:
push_error(error)
return
2024-02-27 03:51:01 +00:00
scene_tree.set_multiplayer(smp, ^"/root/Main/Server")
2024-02-28 05:30:45 +00:00
server_game_instance.init_signals()
2024-03-18 05:08:31 +00:00
server_game_instance.lost_connection.connect(_on_lost_connection)
2024-02-27 02:54:47 +00:00
smp.set_multiplayer_peer(peer)
2024-03-12 14:45:08 +00:00
func deinit_server_game() -> void:
2024-03-07 04:06:37 +00:00
server_game_instance.multiplayer.multiplayer_peer = null
2024-03-16 05:38:35 +00:00
scene_tree.set_multiplayer(null, ^"/root/Main/Server")
2024-02-27 02:54:47 +00:00
server_game_instance.queue_free()
2024-03-16 04:25:23 +00:00
server_game_instance = null
2024-03-12 14:45:08 +00:00
func init_client_game(player_name: String, player_color: Color, server_address: String, server_port: int) -> void:
2024-02-27 02:54:47 +00:00
client_game_instance = game_scene.instantiate()
client_game_instance.name = "Client"
add_child(client_game_instance, true)
print("[Main] Creating client connecting to: ", server_address, ":", server_port)
var smp = SceneMultiplayer.new()
var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
var error: Error = peer.create_client(server_address, server_port)
if error != OK or peer.get_connection_status() == MultiplayerPeer.CONNECTION_DISCONNECTED:
push_error(error)
return
scene_tree.set_multiplayer(smp, ^"/root/Main/Client")
2024-02-28 05:30:45 +00:00
client_game_instance.init_signals()
2024-03-10 04:51:36 +00:00
client_game_instance.local_player_name = player_name
client_game_instance.local_player_color = player_color
2024-03-16 04:55:02 +00:00
client_game_instance.phase_tracker.phase_changed.connect(_on_phase_changed)
2024-03-18 05:08:31 +00:00
client_game_instance.lost_connection.connect(_on_lost_connection)
2024-02-27 02:54:47 +00:00
smp.set_multiplayer_peer(peer)
2024-03-12 14:45:08 +00:00
func deinit_client_game() -> void:
2024-03-07 04:06:37 +00:00
client_game_instance.multiplayer.multiplayer_peer = null
2024-03-16 05:38:35 +00:00
scene_tree.set_multiplayer(null, ^"/root/Main/Client")
2024-02-27 02:54:47 +00:00
client_game_instance.queue_free()
2024-03-16 04:25:23 +00:00
client_game_instance = null
2024-03-12 14:45:08 +00:00
func init_lobby_menu() -> void:
2024-02-28 05:30:45 +00:00
lobby_menu_instance = lobby_menu_scene.instantiate()
lobby_menu_instance.init_refs()
2024-03-18 05:08:31 +00:00
lobby_menu_instance.leave_confirmed.connect(_on_lost_connection)
2024-03-12 14:45:08 +00:00
if server_game_instance:
lobby_menu_instance.start_button.disabled = false
lobby_menu_instance.start_confirmed.connect(server_game_instance._on_main_start_confirmed)
2024-03-18 05:08:31 +00:00
client_game_instance.multiplayer.server_disconnected.connect(_on_lost_connection)
2024-03-10 04:51:36 +00:00
client_game_instance.player_dir.child_entered_tree.connect(lobby_menu_instance.players_list._on_playernode_created)
client_game_instance.player_dir.child_exiting_tree.connect(lobby_menu_instance.players_list._on_playernode_destroyed)
client_game_instance.player_dir.playernode_name_changed.connect(lobby_menu_instance.players_list._on_playernode_name_changed)
client_game_instance.player_dir.playernode_color_changed.connect(lobby_menu_instance.players_list._on_playernode_color_changed)
client_game_instance.player_dir.playernode_possessed.connect(lobby_menu_instance.players_list._on_playernode_possessed)
interface_instance.add_child(lobby_menu_instance)
2024-02-28 05:30:45 +00:00
2024-03-12 14:45:08 +00:00
func deinit_lobby_menu() -> void:
2024-03-18 05:08:31 +00:00
lobby_menu_instance.leave_confirmed.disconnect(_on_lost_connection)
2024-03-15 18:57:15 +00:00
if server_game_instance:
lobby_menu_instance.start_confirmed.disconnect(server_game_instance._on_main_start_confirmed)
2024-03-18 05:08:31 +00:00
client_game_instance.multiplayer.server_disconnected.disconnect(_on_lost_connection)
2024-03-15 18:57:15 +00:00
client_game_instance.player_dir.child_entered_tree.disconnect(lobby_menu_instance.players_list._on_playernode_created)
client_game_instance.player_dir.child_exiting_tree.disconnect(lobby_menu_instance.players_list._on_playernode_destroyed)
client_game_instance.player_dir.playernode_name_changed.disconnect(lobby_menu_instance.players_list._on_playernode_name_changed)
client_game_instance.player_dir.playernode_color_changed.disconnect(lobby_menu_instance.players_list._on_playernode_color_changed)
client_game_instance.player_dir.playernode_possessed.disconnect(lobby_menu_instance.players_list._on_playernode_possessed)
2024-02-28 05:30:45 +00:00
lobby_menu_instance.queue_free()
2024-03-16 04:25:23 +00:00
lobby_menu_instance = null
func init_game_hud() -> void:
game_hud_instance = game_hud_scene.instantiate()
client_game_instance.level_manager.level_completed.connect(game_hud_instance._on_level_completed)
client_game_instance.player_dir.playernode_name_changed.connect(game_hud_instance._on_playernode_name_changed)
client_game_instance.player_dir.playernode_color_changed.connect(game_hud_instance._on_playernode_color_changed)
client_game_instance.player_dir.playernode_possessed.connect(game_hud_instance._on_playernode_possessed)
client_game_instance.player_dir.playernode_score_reported.connect(game_hud_instance._on_playernode_score_reported)
client_game_instance.player_dir.playernode_scores_changed.connect(game_hud_instance._on_playernode_scores_changed)
2024-03-18 05:08:31 +00:00
client_game_instance.player_dir.playernode_putt_performed.connect(game_hud_instance._on_playernode_putt_performed)
2024-03-16 04:25:23 +00:00
interface_instance.add_child(game_hud_instance)
func deinit_game_hud() -> void:
client_game_instance.level_manager.level_completed.disconnect(game_hud_instance._on_level_completed)
client_game_instance.player_dir.playernode_name_changed.disconnect(game_hud_instance._on_playernode_name_changed)
client_game_instance.player_dir.playernode_color_changed.disconnect(game_hud_instance._on_playernode_color_changed)
client_game_instance.player_dir.playernode_possessed.disconnect(game_hud_instance._on_playernode_possessed)
client_game_instance.player_dir.playernode_score_reported.disconnect(game_hud_instance._on_playernode_score_reported)
client_game_instance.player_dir.playernode_scores_changed.disconnect(game_hud_instance._on_playernode_scores_changed)
2024-03-18 05:08:31 +00:00
client_game_instance.player_dir.playernode_putt_performed.disconnect(game_hud_instance._on_playernode_putt_performed)
2024-03-16 04:25:23 +00:00
game_hud_instance.queue_free()
game_hud_instance = null
2024-03-18 06:23:17 +00:00
func init_results_menu() -> void:
results_menu_instance = results_menu_scene.instantiate()
results_menu_instance.player_dir = client_game_instance.player_dir
results_menu_instance.leave_confirmed.connect(_on_lost_connection)
interface_instance.add_child(results_menu_instance)
func deinit_results_menu() -> void:
results_menu_instance.queue_free()
results_menu_instance = null
2024-03-12 14:45:08 +00:00
func _ready() -> void:
init_main_menu()
2024-03-12 14:45:08 +00:00
func _on_hosting_confirmed(player_name: String, player_color: Color, server_port: int) -> void:
2024-02-27 02:54:47 +00:00
deinit_main_menu()
init_server_game(server_port)
2024-02-28 05:30:45 +00:00
init_client_game(player_name, player_color, "127.0.0.1", server_port)
init_lobby_menu()
2024-03-12 14:45:08 +00:00
func _on_connecting_confirmed(player_name: String, player_color: Color, server_address: String, server_port: int) -> void:
2024-02-27 02:54:47 +00:00
deinit_main_menu()
init_client_game(player_name, player_color, server_address, server_port)
2024-02-28 05:30:45 +00:00
init_lobby_menu()
2024-03-07 04:06:37 +00:00
2024-03-18 05:08:31 +00:00
func _on_lost_connection() -> void:
2024-03-16 05:38:35 +00:00
if lobby_menu_instance != null:
2024-03-15 18:57:15 +00:00
deinit_lobby_menu()
2024-03-16 05:38:35 +00:00
if game_hud_instance != null:
deinit_game_hud()
if client_game_instance != null:
2024-03-15 18:57:15 +00:00
deinit_client_game()
2024-03-07 04:06:37 +00:00
if server_game_instance != null:
deinit_server_game()
init_main_menu()
2024-03-12 14:45:08 +00:00
func _on_phase_changed(old: PhaseTracker.Phase, new: PhaseTracker.Phase) -> void:
# First, deinitialize the current interface
match old:
PhaseTracker.Phase.LOBBY:
deinit_lobby_menu()
PhaseTracker.Phase.PLAYING:
2024-03-16 04:25:23 +00:00
deinit_game_hud()
2024-03-12 14:45:08 +00:00
PhaseTracker.Phase.ENDED:
2024-03-18 06:23:17 +00:00
deinit_results_menu()
2024-03-12 14:45:08 +00:00
# Then, initialize the new one
match new:
PhaseTracker.Phase.LOBBY:
init_lobby_menu()
PhaseTracker.Phase.PLAYING:
2024-03-16 04:25:23 +00:00
init_game_hud()
2024-03-12 14:45:08 +00:00
PhaseTracker.Phase.ENDED:
2024-03-18 06:23:17 +00:00
init_results_menu()