2020-08-07 03:46:09 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
2020-08-07 20:24:43 +02:00
|
|
|
|
using NeonTea.Quakeball.Players;
|
2020-08-07 20:20:13 +02:00
|
|
|
|
using NeonTea.Quakeball.Networking.Packets;
|
2020-08-07 05:24:46 +02:00
|
|
|
|
using NeonTea.Quakeball.Interface;
|
2020-08-07 03:46:09 +02:00
|
|
|
|
using NeonTea.Quakeball.TeaNet.Peers;
|
|
|
|
|
using NeonTea.Quakeball.TeaNet.Packets;
|
|
|
|
|
|
2020-08-07 20:20:13 +02:00
|
|
|
|
namespace NeonTea.Quakeball.Networking.Instances {
|
2020-08-07 03:46:09 +02:00
|
|
|
|
public class Client : NetInstance {
|
|
|
|
|
|
|
|
|
|
private NetChaperone Net;
|
|
|
|
|
|
|
|
|
|
private Connection Server;
|
|
|
|
|
|
|
|
|
|
private Dictionary<ulong, NetPlayer> Players = new Dictionary<ulong, NetPlayer>();
|
|
|
|
|
private NetPlayer LocalPlayer;
|
2020-08-07 05:24:46 +02:00
|
|
|
|
private bool SelfIdentified = false;
|
2020-08-07 03:46:09 +02:00
|
|
|
|
|
|
|
|
|
public override void Start(string address, int port, PeerMessageListener listener) {
|
|
|
|
|
if (Peer != null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Peer = new Peer(Fingerprint);
|
|
|
|
|
Peer.MessageListener = listener;
|
|
|
|
|
Peer.Start(0);
|
|
|
|
|
byte ident = Peer.RegisterProtocol(new GameProtocol(this));
|
|
|
|
|
Peer.Connect(address, port, ident);
|
|
|
|
|
|
|
|
|
|
Net = GameObject.FindGameObjectWithTag("Net").GetComponent<NetChaperone>();
|
|
|
|
|
|
|
|
|
|
LocalPlayer = new NetPlayer(ulong.MaxValue - 1);
|
2020-08-07 21:07:58 +02:00
|
|
|
|
LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
|
2020-08-07 03:46:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Connected(Connection conn) {
|
2020-08-07 05:24:46 +02:00
|
|
|
|
Terminal.Singleton.Println($"{conn.uid} Connected");
|
2020-08-07 03:46:09 +02:00
|
|
|
|
if (Server == null) {
|
|
|
|
|
Server = conn;
|
|
|
|
|
} else {
|
|
|
|
|
Peer.Disconnect(conn.uid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Disconnected(Connection conn) {
|
2020-08-07 05:24:46 +02:00
|
|
|
|
Terminal.Singleton.Println($"{conn.uid} Disconnected: {conn.ClosingReason}");
|
2020-08-07 03:46:09 +02:00
|
|
|
|
if (Server == conn) {
|
|
|
|
|
Server = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Handle(Connection conn, Packet packet) {
|
|
|
|
|
if (packet is SelfIdentPckt) {
|
|
|
|
|
SelfIdentPckt ident = (SelfIdentPckt)packet;
|
|
|
|
|
LocalPlayer.Id = ident.PlayerId;
|
|
|
|
|
Players.Add(LocalPlayer.Id, LocalPlayer);
|
2020-08-07 05:24:46 +02:00
|
|
|
|
SelfIdentified = true;
|
2020-08-07 03:46:09 +02:00
|
|
|
|
|
|
|
|
|
SpawnPckt spawn = new SpawnPckt();
|
|
|
|
|
spawn.Location = LocalPlayer.Controlled.transform.position;
|
|
|
|
|
Peer.SendReliable(Server.uid, spawn);
|
|
|
|
|
} else if (packet is SpawnPckt) {
|
|
|
|
|
SpawnPckt spawn = (SpawnPckt)packet;
|
|
|
|
|
if (spawn.PlayerId == LocalPlayer.Id) {
|
|
|
|
|
return; // Ignore, it's their own.
|
|
|
|
|
}
|
2020-08-07 21:07:58 +02:00
|
|
|
|
Player obj = Net.SpawnPlayer(spawn.Location).GetComponent<Player>();
|
2020-08-07 04:11:53 +02:00
|
|
|
|
NetPlayer player = new NetPlayer(spawn.PlayerId, obj);
|
2020-08-07 05:24:46 +02:00
|
|
|
|
Players.Add(spawn.PlayerId, player);
|
2020-08-07 21:07:58 +02:00
|
|
|
|
} else if (packet is MultiplePlayerUpdatesPckt) {
|
|
|
|
|
MultiplePlayerUpdatesPckt multiple = (MultiplePlayerUpdatesPckt)packet;
|
|
|
|
|
foreach (PlayerUpdatePckt pckt in multiple.Updates) {
|
|
|
|
|
HandleUpdatePlayer(pckt);
|
2020-08-07 05:24:46 +02:00
|
|
|
|
}
|
2020-08-08 00:37:03 +02:00
|
|
|
|
} else if (packet is MultipleSyncsPckt) {
|
|
|
|
|
MultipleSyncsPckt multiple = (MultipleSyncsPckt)packet;
|
|
|
|
|
foreach (PlayerSyncPacket pckt in multiple.Syncs) {
|
|
|
|
|
HandleSyncPckt(pckt);
|
|
|
|
|
}
|
2020-08-07 22:09:31 +02:00
|
|
|
|
} else if (packet is PlayerJumpPckt) {
|
|
|
|
|
PlayerJumpPckt jump = (PlayerJumpPckt)packet;
|
2020-08-07 22:25:56 +02:00
|
|
|
|
Players[jump.PlayerId].Controlled.Jump();
|
2020-08-07 05:24:46 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 22:09:31 +02:00
|
|
|
|
private void HandleUpdatePlayer(PlayerUpdatePckt pckt) {
|
|
|
|
|
if (pckt.PlayerId == LocalPlayer.Id) {
|
|
|
|
|
return; // Ignore, again.
|
|
|
|
|
}
|
2020-08-08 00:15:33 +02:00
|
|
|
|
Players[pckt.PlayerId].Controlled.ProcessUpdatePacket(pckt);
|
2020-08-07 22:09:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 00:37:03 +02:00
|
|
|
|
private void HandleSyncPckt(PlayerSyncPacket syncPckt) {
|
|
|
|
|
if (syncPckt.Unsynced || syncPckt.PlayerId != LocalPlayer.Id) {
|
|
|
|
|
Players[syncPckt.PlayerId].Controlled.ProcessSyncPacket(syncPckt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 21:07:58 +02:00
|
|
|
|
public override void UpdateLocalPlayer() {
|
|
|
|
|
if (SelfIdentified && Server != null) {
|
2020-08-07 23:59:51 +02:00
|
|
|
|
PlayerUpdatePckt pckt = LocalPlayer.Controlled.CreateUpdatePacket();
|
2020-08-07 05:24:46 +02:00
|
|
|
|
Peer.SendUnreliable(Server.uid, pckt);
|
2020-08-07 03:46:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-07 21:07:58 +02:00
|
|
|
|
|
2020-08-07 22:09:31 +02:00
|
|
|
|
public override void LocalPlayerJump() {
|
|
|
|
|
if (SelfIdentified && Server != null) {
|
|
|
|
|
PlayerJumpPckt jump = new PlayerJumpPckt();
|
|
|
|
|
Peer.SendReliable(Server.uid, jump);
|
2020-08-07 21:07:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-07 23:57:11 +02:00
|
|
|
|
|
|
|
|
|
public override void SendPlayerSync() {
|
|
|
|
|
if (SelfIdentified && Server != null) {
|
2020-08-08 00:15:33 +02:00
|
|
|
|
PlayerSyncPacket packet = LocalPlayer.Controlled.CreateSyncPacket();
|
|
|
|
|
Peer.SendUnreliable(Server.uid, packet);
|
2020-08-07 23:57:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-07 03:46:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|