SpaceServer/Scripts/RootNode.gd

59 lines
1.5 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 = []
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...')
2020-02-04 16:46:32 +00:00
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass