Beginnings of the netcode
This commit is contained in:
		
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -350,4 +350,5 @@ MigrationBackup/
 | 
			
		||||
.ionide/
 | 
			
		||||
 | 
			
		||||
.mono
 | 
			
		||||
.import
 | 
			
		||||
.import
 | 
			
		||||
builds
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										48
									
								
								export_presets.cfg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								export_presets.cfg
									
									
									
									
									
										Normal file
									
								
							@ -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=""
 | 
			
		||||
							
								
								
									
										6
									
								
								nodes/Game.tscn
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								nodes/Game.tscn
									
									
									
									
									
										Normal file
									
								
							@ -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 )
 | 
			
		||||
@ -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={
 | 
			
		||||
 | 
			
		||||
@ -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"]
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										23
									
								
								scripts/Game.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								scripts/Game.cs
									
									
									
									
									
										Normal file
									
								
							@ -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();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										56
									
								
								scripts/Network/Client.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								scripts/Network/Client.cs
									
									
									
									
									
										Normal file
									
								
							@ -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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								scripts/Network/Data/PlayerData.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								scripts/Network/Data/PlayerData.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,8 @@
 | 
			
		||||
namespace Network.Data
 | 
			
		||||
{
 | 
			
		||||
    public struct PlayerData
 | 
			
		||||
    {
 | 
			
		||||
        public int peer_id;
 | 
			
		||||
        public string name;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										11
									
								
								scripts/Network/INetworkObject.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								scripts/Network/INetworkObject.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
			
		||||
using Godot;
 | 
			
		||||
 | 
			
		||||
namespace Network
 | 
			
		||||
{
 | 
			
		||||
    public interface INetworkObject
 | 
			
		||||
    {
 | 
			
		||||
        int port { get; }
 | 
			
		||||
        NetworkedMultiplayerENet peer { get; set; }
 | 
			
		||||
        void Close();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										91
									
								
								scripts/Network/Server.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								scripts/Network/Server.cs
									
									
									
									
									
										Normal file
									
								
							@ -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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user