Test Multiplayer

This commit is contained in:
Andrew Tang 2022-07-25 02:29:27 +08:00
parent 59b74b946a
commit 3fbf2640da
11 changed files with 203 additions and 0 deletions

View File

@ -0,0 +1,3 @@
source_md5="47313fa4c47a9963fddd764e1ec6e4a8"
dest_md5="26ea799ea0a3da9e753b3ebe822e0570"

View File

@ -0,0 +1,35 @@
extends Node
const DEFAULT_PORT = 28960
const MAX_CLIENTS = 6
var server = null
var client = null
var ip_address = ""
func _ready() -> void:
ip_address = IP.get_local_addresses()[3]
for ip in IP.get_local_addresses():
if ip.begins_with("192.168."):
ip_address = ip
get_tree().connect("connected_to_server", self, "_connected_to_server")
get_tree().connect("server_disconnected", self, "_server_disconnected")
func _create_server() -> void:
server = NetworkedMultiplayerENet.new()
server.create_server(DEFAULT_PORT, MAX_CLIENTS)
get_tree().set_network_peer(server)
func _join_server() -> void:
client = NetworkedMultiplayerENet.new()
client.create_client(ip_address, DEFAULT_PORT)
get_tree().set_network_peer(client)
func _connected_to_server() -> void:
print("Successfully connected to the server")
func _server_disconnected() -> void:
print("Disconnected from the server")

View File

@ -0,0 +1,32 @@
extends Control
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
# 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")
func _player_disconnected(id) -> void:
print("Player " + str(id) + "disconnected")
func _on_Create_Server_pressed():
multiplayer.config_ui.hide()
Network._create_server()
func _on_Join_Server_pressed():
if server_ip_address.text != "":
multiplayer_config_ui.hide()
Network.ip_address = server_ip_address.text
Network._join_server()

View File

@ -0,0 +1,58 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Network_Setup.gd" type="Script" id=1]
[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
anchor_bottom = 1.0
[node name="Create_Server" type="Button" parent="Multiplayer_Configure"]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -125.0
margin_top = 21.0
margin_right = 125.0
margin_bottom = 121.0
text = "Create Server"
[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 = -125.0
margin_top = -129.0
margin_right = 125.0
margin_bottom = -29.0
text = "Join Server"
[node name="Server_IP_Address" type="LineEdit" parent="Multiplayer_Configure"]
margin_left = 389.0
margin_top = 82.0
margin_right = 639.0
margin_bottom = 132.0
text = "Enter IP Address"
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
[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"]

View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Network.gd" type="Script" id=1]
[node name="Node" type="Node"]
script = ExtResource( 1 )

View File

@ -0,0 +1,7 @@
[gd_resource type="Environment" load_steps=2 format=2]
[sub_resource type="ProceduralSky" id=1]
[resource]
background_mode = 2
background_sky = SubResource( 1 )

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://icon.png"
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

View File

@ -0,0 +1,26 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters
config_version=4
[application]
config/name="Multiplayer Test"
config/icon="res://icon.png"
[autoload]
Network="*res://Network.gd"
[physics]
common/enable_pause_aware_picking=true
[rendering]
environment/default_environment="res://default_env.tres"