open-fpsz/main.gd
2024-05-08 10:25:06 -04:00

64 lines
2.6 KiB
GDScript

# This file is part of open-fpsz.
#
# 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 Node3D
@export_category("Modes")
@export var SINGLEPLAYER : PackedScene
@export var MULTIPLAYER : PackedScene
func _ready() -> void:
# only run server in headless
if DisplayServer.get_name() == "headless":
Global.type = MULTIPLAYER.instantiate()
Global.type.start_server(9000, MapsManager.maps[MapsManager._rng.randi_range(0, len(MapsManager.maps) - 1)])
return
# connect boot menu signals
$BootMenu.start_demo.connect(_start_demo)
$BootMenu/MultiplayerPanelContainer.start_server.connect(_start_server)
$BootMenu/MultiplayerPanelContainer.join_server.connect(_join_server)
# do not set initial window mode for debug build
if not OS.is_debug_build():
DisplayServer.window_set_mode(Settings.get_value("video", "window_mode"))
func _unhandled_input(event : InputEvent) -> void:
if event.is_action_pressed("exit"):
if Global.type is Multiplayer:
$BootMenu._on_multiplayer_pressed()
else:
$BootMenu._on_main_menu_pressed()
# reset game mode and get back to main menu
Global.type = null
if DisplayServer.get_name() != "headless":
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
return
func _start_demo() -> void:
Global.type = SINGLEPLAYER.instantiate()
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
$BootMenu.hide()
func _start_server(port : int, nickname : String) -> void:
Global.type = MULTIPLAYER.instantiate()
Global.type.start_server(port, MapsManager.maps[$BootMenu/MultiplayerPanelContainer.map_selector.selected], nickname)
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
$BootMenu.hide()
func _join_server(host : String, port : int, nickname : String) -> void:
Global.type = MULTIPLAYER.instantiate()
Global.type.connected_to_server.connect($BootMenu/MultiplayerPanelContainer._on_connected_to_server)
Global.type.connection_failed.connect($BootMenu/MultiplayerPanelContainer._on_connection_failed)
Global.type.join_server(host, port, nickname)
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED