From f1fe29c2f8d9b0481dc6096a3fd52ec1603d8e7e Mon Sep 17 00:00:00 2001 From: teascade Date: Wed, 22 Nov 2017 00:51:27 +0200 Subject: [PATCH] Made the UI work and did some fixing --- scripts/Client.cs | 6 +++++- scripts/MainMenu.cs | 38 ++++++++++++++++++++++++++++++++++++-- scripts/Net.cs | 33 ++------------------------------- 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/scripts/Client.cs b/scripts/Client.cs index cdedfb7..6f9589b 100644 --- a/scripts/Client.cs +++ b/scripts/Client.cs @@ -7,6 +7,7 @@ public class Client : Peer { private Connection ServerConn; private byte[] TempBuffer = {1}; + private float Timer = 0; public Client(PacketPeerUDP packetPeer) : base(packetPeer) { PacketPeer = packetPeer; @@ -16,7 +17,7 @@ public class Client : Peer { GD.print("Start client."); GD.print("Starting to send stuff to " + address + ":" + port); ServerConn = new Connection(address, port); - StartListening(port, address); + StartListening(port, "*"); } public override void ReceivePacket(byte[] buffer, string address, int port) { @@ -24,6 +25,9 @@ public class Client : Peer { } public override void Update(float delta) { + Timer += delta; + if (Timer < 1) { return; } + Timer = 0; GD.print("Update client."); SendBuffer(TempBuffer, ServerConn); } diff --git a/scripts/MainMenu.cs b/scripts/MainMenu.cs index f11c9a6..f99d62e 100644 --- a/scripts/MainMenu.cs +++ b/scripts/MainMenu.cs @@ -5,18 +5,52 @@ public class MainMenu : Panel { private Net Net; + private LineEdit IPAddressEdit; + private LineEdit PortEdit; + public override void _Ready() { Net = (Net) GetNode("/root/Net"); + var IPAddressNode = GetNode("IPAddress"); + var PortNode = GetNode("Port"); + if (IPAddressNode is LineEdit) { + IPAddressEdit = (LineEdit) IPAddressNode; + } + if (PortNode is LineEdit) { + PortEdit = (LineEdit) PortNode; + } } private void OnLaunchServer() { GD.print("Launch Server!"); - Net.StartServer(); + string address; + int port; + SetAddressAndPort(out address, out port, "*", 7070); + Net.StartServer(address, port); } private void OnLaunchClient() { GD.print("Launch Client!"); - Net.StartClient(); + string address; + int port; + SetAddressAndPort(out address, out port, "localhost", 7070); + Net.StartClient(address, port); + } + + private void SetAddressAndPort(out string address, out int port, string defaultAddress, int defaultPort) { + if (IPAddressEdit == null || IPAddressEdit.GetText() == "") { + address = defaultAddress; + } else { + address = IPAddressEdit.GetText(); + } + port = defaultPort; + if (PortEdit != null && PortEdit.GetText() != "") { + //port = PortEdit.GetText(); + int temp_port; + Int32.TryParse(PortEdit.GetText(), out temp_port); + if (temp_port > 80) { + port = temp_port; + } + } } } diff --git a/scripts/Net.cs b/scripts/Net.cs index 0b37e19..33e9704 100644 --- a/scripts/Net.cs +++ b/scripts/Net.cs @@ -7,21 +7,11 @@ public class Net : Node { private Client Client; private Server Server; - private LineEdit IPAddressEdit; - private LineEdit PortEdit; - public bool IsClient { get { return Client != null; } } public bool IsServer { get { return Server != null; } } public override void _Ready() { PacketPeer = new PacketPeerUDP(); - var IPAddressNode = GetNode("IPAddress"); - var PortNode = GetNode("Port"); - if (IPAddressNode is LineEdit) { - IPAddressEdit = (LineEdit) IPAddressNode; - } if (PortNode is LineEdit) { - PortEdit = (LineEdit) PortNode; - } } public override void _Process(float delta) { @@ -34,34 +24,15 @@ public class Net : Node { if (IsClient) { Client.Free(); } } - public void StartClient() { + public void StartClient(string address, int port) { if (IsClient || IsServer) { return; } Client = new Client(PacketPeer); - string address; - int port; - SetAddressAndPort(out address, out port, "localhost", 7070); Client.Initialize(address, port); } - public void StartServer() { + public void StartServer(string address, int port) { if (IsClient || IsServer) { return; } Server = new Server(PacketPeer); - string address; - int port; - SetAddressAndPort(out address, out port, "*", 7070); Server.Initialize(address, port); } - - private void SetAddressAndPort(out string address, out int port, string defaultAddress, int defaultPort) { - if (IPAddressEdit == null || IPAddressEdit.GetText() == "") { - address = defaultAddress; - } else { - address = IPAddressEdit.GetText(); - } - port = defaultPort; - if (PortEdit != null && PortEdit.GetText() != "") { - //port = PortEdit.GetText(); - Int32.TryParse(PortEdit.GetText(), out port); - } - } }