From 5559c3b31e39c765e3ea3384644b828ed0d2e303 Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Wed, 28 Oct 2020 10:25:11 -0400 Subject: [PATCH] Beginnings of the netcode --- .gitignore | 3 +- export_presets.cfg | 48 ++++++++++++++++ nodes/Game.tscn | 6 ++ project.godot | 4 ++ scenes/GameWorld.tscn | 1 - scripts/Game.cs | 23 ++++++++ scripts/Network/Client.cs | 56 ++++++++++++++++++ scripts/Network/Data/PlayerData.cs | 8 +++ scripts/Network/INetworkObject.cs | 11 ++++ scripts/Network/Server.cs | 91 ++++++++++++++++++++++++++++++ 10 files changed, 249 insertions(+), 2 deletions(-) create mode 100644 export_presets.cfg create mode 100644 nodes/Game.tscn create mode 100644 scripts/Game.cs create mode 100644 scripts/Network/Client.cs create mode 100644 scripts/Network/Data/PlayerData.cs create mode 100644 scripts/Network/INetworkObject.cs create mode 100644 scripts/Network/Server.cs diff --git a/.gitignore b/.gitignore index 6c9f330..de93623 100644 --- a/.gitignore +++ b/.gitignore @@ -350,4 +350,5 @@ MigrationBackup/ .ionide/ .mono -.import \ No newline at end of file +.import +builds diff --git a/export_presets.cfg b/export_presets.cfg new file mode 100644 index 0000000..883e80d --- /dev/null +++ b/export_presets.cfg @@ -0,0 +1,48 @@ +[preset.0] + +name="HTML5" +platform="HTML5" +runnable=true +custom_features="" +export_filter="all_resources" +include_filter="" +exclude_filter="" +export_path="" +patch_list=PoolStringArray( ) +script_export_mode=1 +script_encryption_key="" + +[preset.0.options] + +vram_texture_compression/for_desktop=true +vram_texture_compression/for_mobile=false +html/custom_html_shell="" +html/head_include="" +custom_template/release="" +custom_template/debug="" + +[preset.1] + +name="Server" +platform="Linux/X11" +runnable=true +custom_features="" +export_filter="all_resources" +include_filter="" +exclude_filter="" +export_path="builds/client/godot-csharp-example.x86_64" +patch_list=PoolStringArray( ) +script_export_mode=1 +script_encryption_key="" + +[preset.1.options] + +texture_format/bptc=false +texture_format/s3tc=true +texture_format/etc=false +texture_format/etc2=false +texture_format/no_bptc_fallbacks=true +binary_format/64_bits=true +binary_format/embed_pck=true +custom_template/release="" +custom_template/debug="" diff --git a/nodes/Game.tscn b/nodes/Game.tscn new file mode 100644 index 0000000..880a090 --- /dev/null +++ b/nodes/Game.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://scripts/Game.cs" type="Script" id=1] + +[node name="Game" type="Node"] +script = ExtResource( 1 ) diff --git a/project.godot b/project.godot index 9eeeb71..852fe95 100644 --- a/project.godot +++ b/project.godot @@ -19,6 +19,10 @@ config/name="godot-csharp-example" run/main_scene="res://scenes/GameWorld.tscn" config/icon="res://icon.png" +[autoload] + +Game="*res://nodes/Game.tscn" + [input] move_up={ diff --git a/scenes/GameWorld.tscn b/scenes/GameWorld.tscn index 713d42e..0f0b76c 100644 --- a/scenes/GameWorld.tscn +++ b/scenes/GameWorld.tscn @@ -47,4 +47,3 @@ tile_data = PoolIntArray( -131077, 0, 0, -196605, 0, 0, 131067, 0, 0, 65539, 0, [node name="Orc" parent="." instance=ExtResource( 5 )] position = Vector2( 40, 0 ) -[connection signal="UpdatePlayerGuiHandler" from="Player" to="PlayerGui" method="OnUpdatePlayerGui"] diff --git a/scripts/Game.cs b/scripts/Game.cs new file mode 100644 index 0000000..08fc8e2 --- /dev/null +++ b/scripts/Game.cs @@ -0,0 +1,23 @@ +using Godot; +using Network; + +public class Game : Node +{ + public static Network.Network network; + public override void _Ready() + { + network = new Network.Network(GetTree()); + AddChild(network); + + if(OS.GetEnvironment("DEDICATED_SERVER") == "true" || OS.GetName() == "Server") + { + GD.Print("Attempting to start server..."); + network.StartServer(); + } + else + { + GD.Print("Attempting to start client..."); + network.StartClient(); + } + } +} \ No newline at end of file diff --git a/scripts/Network/Client.cs b/scripts/Network/Client.cs new file mode 100644 index 0000000..59564bd --- /dev/null +++ b/scripts/Network/Client.cs @@ -0,0 +1,56 @@ +using Godot; + +namespace Network +{ + public class Client : Node, INetworkObject + { + public static Client main; + public static SceneTree tree; + public int port { get; } + public NetworkedMultiplayerENet peer { get; set; } + + public Client(SceneTree _tree, string host, int _port = 7777) + { + Name = "Network"; + if (tree == null) + tree = _tree; + port = _port; + + peer = new NetworkedMultiplayerENet(); + peer.CreateClient(host, port); + tree.NetworkPeer = peer; + + main = this; + + //Intialize signal connects + tree.Connect("connected_to_server", this, "ConnectedToServer"); + tree.Connect("connection_failed", this, "ConnectionFailed"); + tree.Connect("server_disconnected", this, "ServerDisconnected"); + } + public void RegisterPlayer(string name) + { + Rpc("RegisterPlayer", "Fred"); + } + + public void ConnectedToServer() + { + GD.Print("Connected to server!"); + RegisterPlayer("Bro"); + } + + public void ConnectionFailed() + { + GD.Print("Failed to connect to server!"); + } + + public void ServerDisconnected() + { + GD.Print("Server disconnected!"); + } + + public void Close() + { + tree.NetworkPeer = null; + } + } +} \ No newline at end of file diff --git a/scripts/Network/Data/PlayerData.cs b/scripts/Network/Data/PlayerData.cs new file mode 100644 index 0000000..c5f17b0 --- /dev/null +++ b/scripts/Network/Data/PlayerData.cs @@ -0,0 +1,8 @@ +namespace Network.Data +{ + public struct PlayerData + { + public int peer_id; + public string name; + } +} \ No newline at end of file diff --git a/scripts/Network/INetworkObject.cs b/scripts/Network/INetworkObject.cs new file mode 100644 index 0000000..1ab9cbb --- /dev/null +++ b/scripts/Network/INetworkObject.cs @@ -0,0 +1,11 @@ +using Godot; + +namespace Network +{ + public interface INetworkObject + { + int port { get; } + NetworkedMultiplayerENet peer { get; set; } + void Close(); + } +} \ No newline at end of file diff --git a/scripts/Network/Server.cs b/scripts/Network/Server.cs new file mode 100644 index 0000000..f33a30c --- /dev/null +++ b/scripts/Network/Server.cs @@ -0,0 +1,91 @@ +using Godot; +using Network.Data; +using System; + +namespace Network +{ + public class Network : Node, INetworkObject + { + public static Network main; + public static SceneTree tree; + public int port { get; } + public int max_players { get; set; } + public NetworkedMultiplayerENet peer { get; set; } + //public PlayerData[] playerData; + + public Network(SceneTree _tree, int _port = 7777) + { + port = _port; + tree = _tree; + } + + public void StartServer(int _max_players = 32) + { + max_players = _max_players; + + peer = new NetworkedMultiplayerENet(); + peer.CreateServer(port, max_players); + tree.NetworkPeer = peer; + if(tree.NetworkPeer != null) + { + GD.Print("Successfully started server!"); + } else { + throw new Exception("Failed to start server!"); + } + main = this; + + //Intialize signal connects + tree.Connect("network_peer_connected", this, "NetworkPeerConnected"); + tree.Connect("network_peer_disconnected", this, "NetworkPeerDisconnected"); + } + + public void StartClient(string host = "127.0.0.1") + { + peer = new NetworkedMultiplayerENet(); + peer.CreateClient(host, port); + tree.NetworkPeer = peer; + + main = this; + + //Intialize signal connects + tree.Connect("connected_to_server", this, "ConnectedToServer"); + tree.Connect("connection_failed", this, "ConnectionFailed"); + tree.Connect("server_disconnected", this, "ServerDisconnected"); + } + + public void NetworkPeerConnected(int id) + { + GD.Print("Player connected to the server!"); + } + public void NetworkPeerDisconnected(int id) + { + GD.Print("Player left the server!"); + } + public void ConnectedToServer() + { + GD.Print("Connected to server!"); + Rpc("RegisterPlayer", "Fred"); + } + + public void ConnectionFailed() + { + GD.Print("Failed to connect to server!"); + } + + public void ServerDisconnected() + { + GD.Print("Server disconnected!"); + } + + [Remote] + public void RegisterPlayer(string name) + { + GD.Print($"Loaded {name}"); + } + + public void Close() + { + tree.NetworkPeer = null; + } + } +} \ No newline at end of file