quakeball/Assets/Scripts/Networking/Net.cs

54 lines
1.6 KiB
C#
Raw Normal View History

using UnityEngine;
using NeonTea.Quakeball.Networking.Instances;
using NeonTea.Quakeball.TeaNet.Peers;
2020-08-07 23:47:03 +02:00
using NeonTea.Quakeball.TeaNet.Packets;
using System.Collections.Generic;
namespace NeonTea.Quakeball.Networking {
2020-08-02 20:32:30 +02:00
public class Net {
public static Net Singleton = new Net();
public NetInstance Instance;
2020-08-08 19:14:34 +02:00
public void StartClient(string address, int port, string nick, PeerMessageListener listener) {
Instance = new Client();
2020-08-08 19:14:34 +02:00
Instance.Start(address, port, nick, listener);
}
2020-08-08 19:14:34 +02:00
public void StartServer(string address, int port, string nick, PeerMessageListener listener) {
Instance = new Server();
2020-08-08 19:14:34 +02:00
Instance.Start(address, port, nick, listener);
}
2020-08-05 03:21:04 +02:00
public static void Quit() {
2020-08-07 04:36:20 +02:00
if (Singleton.Instance != null) {
Singleton.Instance.Stop();
}
2020-08-05 03:21:04 +02:00
}
[RuntimeInitializeOnLoadMethod]
static void RunOnStart() {
Application.quitting += Quit;
}
}
2020-08-07 23:47:03 +02:00
public static class ByteBufferExtensions {
/// <summary>Extension: Reads Vector3</summary>
public static Vector3 ReadVector3(this ByteBuffer buffer) {
float x = buffer.ReadFloat();
float y = buffer.ReadFloat();
float z = buffer.ReadFloat();
return new Vector3(x, y, z);
}
/// <summary>Extension: Writes Vector3</summary>
public static void Write(this ByteBuffer buffer, Vector3 vector) {
buffer.Write(vector.x);
buffer.Write(vector.y);
buffer.Write(vector.z);
}
}
}