hotel-madness/scripts/utils/game.gd

96 lines
2.5 KiB
GDScript

## SPDX-License-Identifier: GPL-3.0-or-later
## Copyright (c) 2024 interstellardevelopment.org
extends Node
# This tells the game what the logmode is.
# 0 -> None
# 1 -> Errors
# 2 -> Errors/Warnings
# 3 -> Errors/Warnings/Infos
# 4 -> All
var logmode: int = 3
var ip: String = "127.0.0.1"
var port: int = 25262
var max_clients: int = 1024
var roomname: String = ""
var username: String = ""
var mapname: String = "Testmap"
var launchmode: int = 0
func _ready() -> void:
Log.info("Running on: %s %s (%s)" % [OS.get_distribution_name(), OS.get_version(), OS.get_name()])
randomize()
load_settings()
var args: PackedStringArray = OS.get_cmdline_args()
var skip: bool = false
for i: int in range(args.size()):
if !skip:
Log.info("Argument %s passed." % args[i])
match args[i]:
"--logmode":
logmode = int(args[i+1])
skip = true
"--username":
username = args[i+1]
skip = true
"--server":
launchmode = 2
"--client":
launchmode = 1
"res://start.tscn":
pass # Catch in order to prevent a warning.
_:
Log.warning("Unknown argument: %s!" % args[i])
else:
skip = false
if DisplayServer.get_name() == "headless":
launchmode = 2
if OS.is_debug_build():
logmode = 4
match launchmode:
0:
# Menu
if get_tree().change_scene_to_file("res://scenes/ui/main_menu.tscn") != OK:
Log.error("Couldn't change to the main menu scene! Closing application.", "Couldn't change to the main menu scene!")
1:
# Client with direct join
Networking.join_room()
2:
# Server
if Networking.start_server() != OK:
Log.error("Failed to start server! Closing application.", "Failed to start server!")
func save_settings() -> void:
var to_save: Dictionary = {
"ip": ip,
"roomname": roomname,
"username": username,
}
var to_save_string: String = JSON.stringify(to_save)
var save_file: FileAccess = FileAccess.open("user://save.json", FileAccess.WRITE)
if save_file == null:
Log.warning("Couldn't save the json.")
return
save_file.store_string(to_save_string)
func load_settings() -> void:
var save_file: FileAccess = FileAccess.open("user://save.json", FileAccess.READ)
if save_file == null:
Log.warning("Couldn't open the json save.")
return
var json_string: String = save_file.get_as_text()
var json: JSON = JSON.new()
if json.parse(json_string) != OK:
Log.warning("Couldn't parse the json save.")
return
var data: Dictionary = json.data
ip = data["ip"]
roomname = data["roomname"]
username = data["username"]