diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/But_ChangeScene.gd b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/But_ChangeScene.gd new file mode 100644 index 0000000..f8a43ac --- /dev/null +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/But_ChangeScene.gd @@ -0,0 +1,18 @@ +#BUT_CHANGESCENE: +# Generic template script allowing GUI linking of scenes by button press. + +tool +extends Button + +#Creates param usable in the UI; and the params next to export make it string and file browser +export(String, FILE) var next_scene_path: = "" + +var tempToggle = 0 + +func _on_But_NewGame_button_up(): + get_tree().change_scene(next_scene_path) + + +func _get_configuration_warning() -> String: + return "next_scene_path must be set for this button to work" if next_scene_path == "" else "" + diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Global.gd b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Global.gd index 6c826ed..e0efd6b 100644 --- a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Global.gd +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Global.gd @@ -1,7 +1,7 @@ extends Node -func instance_node(node: Object, parent: Object) -> Object: +func _instance_node(node: Object, parent: Object) -> Object: var node_instance = node.instance() parent.add_child(node_instance) return node_instance diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network.gd b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network.gd index 052b427..e3a25a8 100644 --- a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network.gd +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network.gd @@ -5,8 +5,12 @@ const MAX_CLIENTS = 6 var server = null var client = null +var player_counter = 1 +var player = load("res://Player.tscn") var ip_address = "" +var room_name = "" +var room_password = "" func _ready() -> void: ip_address = IP.get_local_addresses()[3] @@ -33,3 +37,17 @@ func _connected_to_server() -> void: func _server_disconnected() -> void: print("Disconnected from the server") + +func _set_server_info(name, password) -> void: + room_name = name + room_password = password + +func _instance_player(id, name) -> void: + var player_instance = Global._instance_node(player, Players) + player_instance.name = str(id) + player_instance._set_name(name) + #Using player_counter to determine the positioning of the player's name on the screen + #So that they aren't all overlapping + player_instance._set_name_position(player_counter) + player_instance.set_network_master(id) + player_counter += 1 diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network_Setup.tscn b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network_Setup.tscn index 49caf72..b4b93c3 100644 --- a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network_Setup.tscn +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Network_Setup.tscn @@ -1,11 +1,10 @@ [gd_scene load_steps=2 format=2] -[ext_resource path="res://Network_Setup.gd" type="Script" id=1] +[ext_resource path="res://But_ChangeScene.gd" type="Script" id=2] [node name="Network_Setup" type="Control"] anchor_right = 1.0 anchor_bottom = 1.0 -script = ExtResource( 1 ) [node name="Multiplayer_Configure" type="Control" parent="."] anchor_right = 1.0 @@ -21,6 +20,8 @@ margin_top = 21.0 margin_right = 125.0 margin_bottom = 121.0 text = "Create Server" +script = ExtResource( 2 ) +next_scene_path = "res://Room_Creation.tscn" [node name="Join_Server" type="Button" parent="Multiplayer_Configure"] anchor_left = 0.5 @@ -32,8 +33,11 @@ margin_top = -129.0 margin_right = 125.0 margin_bottom = -29.0 text = "Join Server" +script = ExtResource( 2 ) +next_scene_path = "res://Room_Join.tscn" [node name="Server_IP_Address" type="LineEdit" parent="Multiplayer_Configure"] +visible = false margin_left = 389.0 margin_top = 82.0 margin_right = 639.0 @@ -54,5 +58,5 @@ margin_bottom = -26.0 align = 1 valign = 1 -[connection signal="pressed" from="Multiplayer_Configure/Create_Server" to="." method="_on_Create_Server_pressed"] -[connection signal="pressed" from="Multiplayer_Configure/Join_Server" to="." method="_on_Join_Server_pressed"] +[connection signal="button_up" from="Multiplayer_Configure/Create_Server" to="Multiplayer_Configure/Create_Server" method="_on_But_NewGame_button_up"] +[connection signal="button_up" from="Multiplayer_Configure/Join_Server" to="Multiplayer_Configure/Join_Server" method="_on_But_NewGame_button_up"] diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Room_Creation.gd b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Room_Creation.gd new file mode 100644 index 0000000..dc7ba6f --- /dev/null +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Room_Creation.gd @@ -0,0 +1,53 @@ +extends Control + + +var playerCounter = 1 +var player = load("res://Player.tscn") + +onready var multiplayer_config_ui = $Multiplayer_Configure +#onready var server_ip_address = $Multiplayer_Configure/Server_IP_Address +onready var device_ip_address = $CanvasLayer/Device_IP_Address +onready var player_name = $Multiplayer_Configure/Player_Name +onready var room_name = $Multiplayer_Configure/Room_Name +onready var room_password = $Multiplayer_Configure/Room_Password +onready var room_ui = $Room_UI + + + +# Called when the node enters the scene tree for the first time. +func _ready(): + get_tree().connect("network_peer_connected", self, "_player_connected") + get_tree().connect("network_peer_disconnected", self, "_player_disconnected") + get_tree().connect("connected_to_server", self, "_connected_to_server") + + device_ip_address.text = Network.ip_address + +func _player_connected(id) -> void: + print("Player " + str(id) + "connected") + Network._instance_player(id, "New Player") + +func _player_disconnected(id) -> void: + print("Player " + str(id) + "disconnected") + + if Players.has_node(str(id)): + Players.get_node(str(id)).queue_free() + +func _on_Create_Server_pressed(): + multiplayer_config_ui.hide() + Network._create_server() + Network._instance_player(get_tree().get_network_unique_id(), player_name.text) + Network._set_server_info(room_name.text, room_password.text) + $Room_UI/Room_Name.text = room_name.text + room_ui.show() + +#func _on_Join_Server_pressed(): +# if server_ip_address.text != "": +# multiplayer_config_ui.hide() +# Network.ip_address = server_ip_address.text +# Network._join_server() + +func _connected_to_server() -> void: + yield(get_tree().create_timer(0.1), "timeout") + Network._instance_player(get_tree().get_network_unique_id(), player_name.text) + + diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Room_Creation.tscn b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Room_Creation.tscn new file mode 100644 index 0000000..28c5aaf --- /dev/null +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Room_Creation.tscn @@ -0,0 +1,91 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://Room_Creation.gd" type="Script" id=1] +[ext_resource path="res://But_ChangeScene.gd" type="Script" id=2] + +[node name="Room_Creation" type="Control"] +anchor_right = 1.0 +anchor_bottom = 1.0 +script = ExtResource( 1 ) + +[node name="Back" type="Button" parent="."] +margin_left = 312.0 +margin_top = 419.0 +margin_right = 474.0 +margin_bottom = 493.0 +text = "Back" +script = ExtResource( 2 ) +next_scene_path = "res://Network_Setup.tscn" + +[node name="Multiplayer_Configure" type="Control" parent="."] +margin_right = 40.0 +margin_bottom = 40.0 + +[node name="Create_Server" type="Button" parent="Multiplayer_Configure"] +margin_left = 592.0 +margin_top = 419.0 +margin_right = 754.0 +margin_bottom = 493.0 +text = "Create" + +[node name="Room_Name" type="LineEdit" parent="Multiplayer_Configure"] +margin_left = 452.0 +margin_top = 250.0 +margin_right = 619.0 +margin_bottom = 274.0 +text = "Room Name" +align = 1 + +[node name="Room_Password" type="LineEdit" parent="Multiplayer_Configure"] +margin_left = 452.0 +margin_top = 315.0 +margin_right = 619.0 +margin_bottom = 339.0 +text = "Room Password" +align = 1 + +[node name="Player_Name" type="LineEdit" parent="Multiplayer_Configure"] +margin_left = 451.0 +margin_top = 188.0 +margin_right = 618.0 +margin_bottom = 212.0 +text = "Player Name" +align = 1 + +[node name="CanvasLayer" type="CanvasLayer" parent="."] + +[node name="Device_IP_Address" type="Label" parent="CanvasLayer"] +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_left = -1.0 +margin_top = -76.0 +margin_right = -1.0 +margin_bottom = -26.0 +align = 1 +valign = 1 + +[node name="Room_UI" type="Control" parent="."] +visible = false +anchor_right = 1.0 +anchor_bottom = 1.0 + +[node name="Room_Name" type="Label" parent="Room_UI"] +anchor_left = 0.5 +anchor_right = 0.5 +margin_left = -80.5 +margin_right = 80.5 +margin_bottom = 14.0 +align = 1 + +[node name="Players" type="Label" parent="Room_UI"] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_top = -225.0 +margin_bottom = -225.0 +text = "Players:" +align = 1 +valign = 1 + +[connection signal="button_up" from="Back" to="Back" method="_on_But_NewGame_button_up"] +[connection signal="pressed" from="Multiplayer_Configure/Create_Server" to="." method="_on_Create_Server_pressed"] diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Room_Join.gd b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Room_Join.gd new file mode 100644 index 0000000..5202eea --- /dev/null +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Room_Join.gd @@ -0,0 +1,13 @@ +extends Control + +onready var multiplayer_config_ui = $Multiplayer_Configure +onready var server_ip_address = $Multiplayer_Configure/Server_IP_Address +onready var room_password = $Multiplayer_Configure/Room_Password +onready var player_name = $Multiplayer_Configure/Player_Name + +func _on_Join_Server_pressed(): + if server_ip_address.text != "": + multiplayer_config_ui.hide() + Network.ip_address = server_ip_address.text + Network._join_server() + Network._instance_player(get_tree().get_network_unique_id(), player_name.text) diff --git a/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Room_Join.tscn b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Room_Join.tscn new file mode 100644 index 0000000..a2ca72a --- /dev/null +++ b/Phase2/Godot ===(Game Code)===/Experiments/Andrew_Multiplayer_Test/Room_Join.tscn @@ -0,0 +1,60 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://Room_Join.gd" type="Script" id=1] +[ext_resource path="res://But_ChangeScene.gd" type="Script" id=2] + +[node name="Room_Join" type="Control"] +anchor_right = 1.0 +anchor_bottom = 1.0 +script = ExtResource( 1 ) + +[node name="Multiplayer_Configure" type="Control" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 + +[node name="Server_IP_Address" type="LineEdit" parent="Multiplayer_Configure"] +margin_left = 386.0 +margin_top = 236.0 +margin_right = 636.0 +margin_bottom = 286.0 +text = "Enter Room IP" +align = 1 + +[node name="Join_Server" type="Button" parent="Multiplayer_Configure"] +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +margin_left = 21.0 +margin_top = 93.0 +margin_right = 271.0 +margin_bottom = 193.0 +text = "Join Server" + +[node name="Room_Password" type="LineEdit" parent="Multiplayer_Configure"] +margin_left = 388.0 +margin_top = 311.0 +margin_right = 638.0 +margin_bottom = 361.0 +text = "Room Password" +align = 1 + +[node name="Player_Name" type="LineEdit" parent="Multiplayer_Configure"] +margin_left = 387.0 +margin_top = 164.0 +margin_right = 637.0 +margin_bottom = 214.0 +text = "Player Name" +align = 1 + +[node name="Back" type="Button" parent="."] +margin_left = 235.0 +margin_top = 394.0 +margin_right = 485.0 +margin_bottom = 494.0 +text = "Back" +script = ExtResource( 2 ) +next_scene_path = "res://Network_Setup.tscn" + +[connection signal="pressed" from="Multiplayer_Configure/Join_Server" to="." method="_on_Join_Server_pressed"] +[connection signal="button_up" from="Back" to="Back" method="_on_But_NewGame_button_up"]