68 lines
2.0 KiB
C#
68 lines
2.0 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public class Net : Node {
|
|
|
|
private PacketPeerUDP PacketPeer;
|
|
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) {
|
|
if (IsServer) { Server.Update(delta); }
|
|
if (IsClient) { Client.Update(delta); }
|
|
}
|
|
|
|
public override void _ExitTree() {
|
|
if (IsServer) { Server.Free(); }
|
|
if (IsClient) { Client.Free(); }
|
|
}
|
|
|
|
public void StartClient() {
|
|
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() {
|
|
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);
|
|
}
|
|
}
|
|
}
|