Browse Source

Made the UI work and did some fixing

master
Teascade 5 years ago
parent
commit
f1fe29c2f8
  1. 6
      scripts/Client.cs
  2. 38
      scripts/MainMenu.cs
  3. 33
      scripts/Net.cs

6
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);
}

38
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;
}
}
}
}

33
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);
}
}
}

Loading…
Cancel
Save