Browse Source

Made client listen and made UI matter maybe

master
Teascade 4 years ago
parent
commit
d9b6cc2165
  1. 1
      .gitignore
  2. 25
      Properties/AssemblyInfo.cs
  3. 4
      scenes/MainMenu.tscn
  4. 3
      scripts/Client.cs
  5. 68
      scripts/Net.cs
  6. 4
      scripts/Server.cs

1
.gitignore

@ -3,5 +3,4 @@
/.mono/
/mono/
/dist/
/Properties/
*.import

25
Properties/AssemblyInfo.cs

@ -0,0 +1,25 @@
using System.Reflection;
// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
[assembly: AssemblyTitle("TicTacToe")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("1.0.*")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

4
scenes/MainMenu.tscn

@ -169,9 +169,9 @@ anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -84.0
margin_top = -39.0
margin_top = -36.0
margin_right = 83.0
margin_bottom = -15.0
margin_bottom = -12.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 0

3
scripts/Client.cs

@ -14,12 +14,13 @@ public class Client : Peer {
public override void Initialize(string address, int port) {
GD.print("Start client.");
GD.print("Starting to send stuff to " + address + ":" + port);
ServerConn = new Connection(address, port);
StartListening(port, address);
}
public override void ReceivePacket(byte[] buffer, string address, int port) {
GD.print("Client received stuff from " + address + " : " + port + " :");
GD.print(buffer[0]);
}
public override void Update(float delta) {

68
scripts/Net.cs

@ -1,4 +1,5 @@
using Godot;
using System;
public class Net : Node {
@ -6,11 +7,21 @@ 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) {
@ -24,44 +35,33 @@ public class Net : Node {
}
public void StartClient() {
//if (IsClient() || IsServer()) { return; }
if (IsClient || IsServer) { return; }
Client = new Client(PacketPeer);
Client.Initialize("localhost", 7070);
string address;
int port;
SetAddressAndPort(out address, out port, "localhost", 7070);
Client.Initialize(address, port);
}
public void StartServer() {
//if (IsClient() || IsServer()) { return; }
if (IsClient || IsServer) { return; }
Server = new Server(PacketPeer);
Server.Initialize("*", 7070);
string address;
int port;
SetAddressAndPort(out address, out port, "*", 7070);
Server.Initialize(address, port);
}
}
//extends Node2D
//# Member variables
//var thread = Thread.new()
//# This function runs in a thread!
//# Threads always take one userdata argument
//func _bg_load(path):
// print("THREAD FUNC!")
// # Load the resource
// var tex = ResourceLoader.load(path)
// # Call _bg_load_done on main thread
// call_deferred("_bg_load_done")
// return tex # return it
//func _bg_load_done():
// # Wait for the thread to complete, get the returned value
// var tex = thread.wait_to_finish()
// # Set to the sprite
// get_node("sprite").set_texture(tex)
//func _on_load_pressed():
// if (thread.is_active()):
// # Already working
// return
// print("START THREAD!")
// thread.start(self, "_bg_load", "res://mona.png")
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);
}
}
}

4
scripts/Server.cs

@ -5,6 +5,8 @@ public class Server : Peer {
private static PacketPeerUDP PacketPeer;
private byte[] TempBuffer = { 1 };
public Server(PacketPeerUDP packetPeer) : base(packetPeer) {
PacketPeer = packetPeer;
}
@ -12,6 +14,7 @@ public class Server : Peer {
public override void Initialize(string address, int port) {
StartListening(port, address);
GD.print("Server initialization finished.");
GD.print("Started server on " + address + ":" + port);
}
public override void ReceivePacket(byte[] buffer, string address, int port) {
@ -19,6 +22,7 @@ public class Server : Peer {
for (int i = 0; i < buffer.Length; i++) {
GD.print(buffer[i]);
}
SendBuffer(TempBuffer, new Connection(address, port));
}
public override void Update(float delta) {

Loading…
Cancel
Save