SpaceServer/Scripts/RootNode.gd

92 lines
2.4 KiB
GDScript
Raw Normal View History

2020-02-04 16:46:32 +00:00
extends Node
2020-08-16 12:11:59 +00:00
const MAX_PLAYERS : int = 2
2020-02-04 16:46:32 +00:00
const PORT : int = 9374
const Arena = preload("res://Scenes/Arena.tscn")
var peer : NetworkedMultiplayerENet = null
2020-02-04 16:46:32 +00:00
var ids = []
2020-02-11 10:01:09 +00:00
var console_output : TextEdit = null
var ready_ids = []
var name_dict = {}
var color_dict = {}
2020-02-04 16:46:32 +00:00
func _ready() -> void:
peer = NetworkedMultiplayerENet.new()
2020-02-04 16:46:32 +00:00
peer.create_server( PORT, MAX_PLAYERS )
get_tree().set_network_peer( peer )
get_tree().connect("network_peer_connected", self, "_peer_connected")
get_tree().connect("network_peer_disconnected", self, "_peer_disconnected")
2020-02-11 10:01:09 +00:00
console_output = $ConsoleOutput
func console_print(text : String) -> void:
console_output.text += "\n" + text
console_output.cursor_set_line(console_output.get_line_count())
print(text)
2020-02-04 16:46:32 +00:00
func _peer_connected(id : int) -> void:
if not id:
return
ids.append(id)
2020-02-11 10:01:09 +00:00
self.console_print("Player has connected with id " + String(id))
if len(ids) == MAX_PLAYERS:
console_print('Got required players... Ready to start game...')
peer.refuse_new_connections = true
rpc('start_game')
2020-02-04 16:46:32 +00:00
# TODO: When we reach max players -> start the game
func _peer_disconnected(id : int) -> void:
ids.erase(id)
2020-02-11 10:01:09 +00:00
self.console_print("Player has disconnected with id " + String(id))
2020-02-04 16:46:32 +00:00
remote func client_ready(id : int) -> void:
if id in ready_ids:
return
ready_ids.append(id)
if len(ready_ids) == MAX_PLAYERS:
start_game()
func start_game() -> void:
var arena = Arena.instance()
add_child(arena)
arena.init()
console_print('Starting Game...')
send_name_dict()
send_color_dict()
func send_name_dict() -> void:
for element in name_dict.keys():
rpc("add_name_to_dict", element, name_dict[element])
remote func set_display_name(id : int, display_name : String) -> void:
name_dict[id] = display_name
func send_color_dict() -> void:
for element in color_dict.keys():
rpc("add_color_to_dict", element, color_dict[element])
remote func set_player_color(id : int, color : Color) -> void:
console_print("Setting color of " + String(id) + " to " + String(color))
color_dict[id] = color
2021-05-14 10:27:05 +00:00
func end_game() -> void:
$Arena.queue_free()
console_print("Ending Game...")
# Reset root node data
ids = []
ready_ids = []
name_dict = {}
color_dict = {}
peer.close_connection()
peer.create_server( PORT, MAX_PLAYERS )
peer.refuse_new_connections = false
remote func request_exit() -> void:
console_print("Exit requested...")
rpc("exit_requested")
$ExitTimer.start()