-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHostLobby.gd
75 lines (55 loc) · 2.07 KB
/
HostLobby.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
extends Node2D
onready var lobby = get_tree().get_root().get_node("Lobby")
var list_of_players = []
func _ready():
get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
var profile = {"name": "Host", "id":1}
list_of_players.append(profile)
update_playerOutput()
func _player_disconnected(id):
var playerDisconnected = null
for player in list_of_players:
if player.id == id:
playerDisconnected = player
if playerDisconnected != null:
print("Player " + playerDisconnected.name + " - " + str(playerDisconnected.id) + " disconnected!")
list_of_players.erase(playerDisconnected)
update_playerOutput()
remote func player_connected(id, playerName):
print("Player " + playerName + " - " + str(id) + " connected!")
var profile = {"name": playerName, "id":id}
list_of_players.append(profile)
update_playerOutput()
func update_playerOutput():
var output_text = ""
for player in list_of_players:
output_text = output_text + player.name + " - " + str(player.id) + "\n"
$playerOutput.set_text(output_text)
func _on_startGameButton_pressed():
var redPlayerId = int($redPlayerID.get_text())
var bluePlayerId = int($bluePlayerID.get_text())
if found(redPlayerId) and found(bluePlayerId):
set_as_player(redPlayerId, bluePlayerId, "RED")
set_as_player(bluePlayerId, redPlayerId, "BLUE")
print("Starting Game!")
lobby.rpc("startgame") #Start game for everyone else
startgame() #Start game for server
get_tree().set_refuse_new_network_connections(true)
else:
print("Start Game Failed - player IDs not recognised")
func set_as_player(targetPlayerID, targetOpponentID, targetColour):
if targetPlayerID != 1:
lobby.rpc_id(targetPlayerID, "set_as_player", targetOpponentID, targetColour)
else:
Helper.opponent_id = targetOpponentID
Helper.myTeamColour = targetColour
Helper.isSpectator = false
func found(playerID):
for player in list_of_players:
if player.id == playerID:
return true
return false
func startgame():
var world = preload("res://Worlds/Ace.tscn").instance()
get_tree().get_root().add_child(world)
hide()