Refactor to fit game-shell structure; externalized lobby client code
This commit is contained in:
167
game_core.gd
Normal file
167
game_core.gd
Normal file
@@ -0,0 +1,167 @@
|
||||
class_name GameCore extends Node
|
||||
|
||||
|
||||
@onready var game_server: GameServer = $game_server
|
||||
@onready var game_client: GameClient = $game_client
|
||||
|
||||
|
||||
#
|
||||
#
|
||||
# Editor -> Debug > Customize run instance
|
||||
#
|
||||
# -- client host_snooker bobs_game
|
||||
#
|
||||
# -- client join bobs_game
|
||||
#
|
||||
#
|
||||
# CLI
|
||||
#
|
||||
# If going straight to game server:
|
||||
# ./billiards.sh -- server_5001 game snooker
|
||||
# ./billiards.sh -- join_server 127.0.0.1 5001
|
||||
#
|
||||
# If going through lobby server:
|
||||
# ./billiards.sh -- client host_snooker bobs_game
|
||||
# ./billiards.sh -- client join bobs_game
|
||||
#
|
||||
#
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_load_scene()
|
||||
_parse_args()
|
||||
|
||||
|
||||
func _load_scene() -> void:
|
||||
add_child(Globals.multiplayer_data.multiplayer_synchronizer)
|
||||
move_child(Globals.multiplayer_data.multiplayer_synchronizer, 0)
|
||||
|
||||
Globals.game_state.set_game_scene(
|
||||
"res://scenes/screens/start_screen/start.tscn"
|
||||
)
|
||||
|
||||
func _parse_args() -> void:
|
||||
for arg in OS.get_cmdline_user_args():
|
||||
if "server_" in arg:
|
||||
server_(arg)
|
||||
elif "host_" in arg:
|
||||
host_(arg)
|
||||
|
||||
match arg:
|
||||
"join_server":
|
||||
join_server(arg)
|
||||
"config":
|
||||
config(arg)
|
||||
"client":
|
||||
client(arg)
|
||||
"game":
|
||||
game(arg)
|
||||
"join":
|
||||
join(arg)
|
||||
_:
|
||||
pass
|
||||
|
||||
func _set_game_mode(mode: String) -> void:
|
||||
match mode:
|
||||
"8ball":
|
||||
Globals.game_data.game_mode = "8ball"
|
||||
Globals.game_data.SelectedGameMode = \
|
||||
Globals.game_data.GameModeType.BALL_8
|
||||
"9ball":
|
||||
Globals.game_data.game_mode = "9ball"
|
||||
Globals.game_data.SelectedGameMode = \
|
||||
Globals.game_data.GameModeType.BALL_9
|
||||
"snooker":
|
||||
Globals.game_data.game_mode = "snooker"
|
||||
Globals.game_data.SelectedGameMode = \
|
||||
Globals.game_data.GameModeType.SNOOKER
|
||||
"freeball":
|
||||
Globals.game_data.game_mode = "freeball"
|
||||
Globals.game_data.SelectedGameMode = \
|
||||
Globals.game_data.GameModeType.FREE_BALL
|
||||
_:
|
||||
Globals.game_data.game_mode = "freeball"
|
||||
Globals.game_data.SelectedGameMode = \
|
||||
Globals.game_data.GameModeType.FREE_BALL
|
||||
|
||||
|
||||
func server_(arg: String) -> void:
|
||||
var server_port = arg.split("_")[1]
|
||||
|
||||
game_server.start_server("0.0.0.0", int(server_port))
|
||||
|
||||
func host_(arg: String) -> void:
|
||||
var game_mode = arg.split("_")[1]
|
||||
var match_name = OS.get_cmdline_user_args()[-1]
|
||||
|
||||
while not $scene/lobby_screen.client.lobby_fully_connected:
|
||||
await get_tree().process_frame
|
||||
|
||||
$scene/lobby_screen.find_child(game_mode, true, false).emit_signal("pressed")
|
||||
$scene/lobby_screen.find_child("new_match_name_input", true, false).text = match_name
|
||||
$scene/lobby_screen.find_child("match_create_bttn", true, false).emit_signal("pressed")
|
||||
|
||||
func join_server(_arg: String) -> void:
|
||||
var server_address = OS.get_cmdline_user_args()[1]
|
||||
var server_port = OS.get_cmdline_user_args()[2]
|
||||
|
||||
Globals.multiplayer_data.set_multiplayer_active()
|
||||
|
||||
# TODO: Need to check if true still...
|
||||
# NOTE: Need to load lobby in order for signals to get bound
|
||||
# which then game server calls on its load to do handoff
|
||||
Globals.game_state.set_game_scene(
|
||||
"res://scenes/screens/lobby_screen/lobby.tscn"
|
||||
)
|
||||
|
||||
game_client.start_client(server_address, int(server_port))
|
||||
|
||||
func config(_arg: String) -> void:
|
||||
var config_entry = OS.get_cmdline_user_args()[-1]
|
||||
var config_file = ConfigFile.new()
|
||||
var err = config_file.load("res://configs/games.cfg")
|
||||
|
||||
if err != OK:
|
||||
push_error("Config file not loaded...")
|
||||
return
|
||||
|
||||
if not config_file.has_section(config_entry):
|
||||
push_error("Config entry doesn't exist: ", config_entry)
|
||||
return
|
||||
|
||||
var keys = config_file.get_section_keys(config_entry)
|
||||
for key in keys:
|
||||
match key:
|
||||
"server":
|
||||
var _server = config_file.get_value(config_entry, key)
|
||||
"client1":
|
||||
var _client1 = config_file.get_value(config_entry, key)
|
||||
"client2":
|
||||
var _client2 = config_file.get_value(config_entry, key)
|
||||
_:
|
||||
pass
|
||||
|
||||
func client(_arg: String) -> void:
|
||||
Globals.game_state.set_game_scene(
|
||||
"res://scenes/screens/lobby_screen/lobby.tscn"
|
||||
)
|
||||
|
||||
$scene/lobby_screen.find_child("host_connect_bttn", true, false).emit_signal("pressed")
|
||||
|
||||
func game(_arg: String) -> void:
|
||||
var game_mode = OS.get_cmdline_user_args()[-1]
|
||||
|
||||
_set_game_mode(game_mode)
|
||||
|
||||
Globals.multiplayer_data.set_multiplayer_active()
|
||||
Globals.game_state.set_game_scene( "res://scenes/game.tscn" )
|
||||
|
||||
func join(_arg: String) -> void:
|
||||
var _match_id = OS.get_cmdline_user_args()[-1]
|
||||
|
||||
var button = $scene/lobby_screen.find_child("join_bttn", true, false)
|
||||
while not button:
|
||||
await get_tree().process_frame
|
||||
button = $scene/lobby_screen.find_child("join_bttn", true, false)
|
||||
|
||||
button.emit_signal("pressed")
|
||||
Reference in New Issue
Block a user