quakeball/Assets/Scripts/Networking/Instances/Client.cs

245 lines
9.6 KiB
C#
Raw Permalink Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2020-08-07 20:24:43 +02:00
using NeonTea.Quakeball.Players;
using NeonTea.Quakeball.Networking.Packets;
2020-08-07 05:24:46 +02:00
using NeonTea.Quakeball.Interface;
using NeonTea.Quakeball.TeaNet.Peers;
using NeonTea.Quakeball.TeaNet.Packets;
namespace NeonTea.Quakeball.Networking.Instances {
public class Client : NetInstance {
private NetChaperone Net;
private Connection Server;
2020-08-07 05:24:46 +02:00
private bool SelfIdentified = false;
2020-08-08 02:35:19 +02:00
public float Ping { get; private set; }
private float LastPingReceived;
private byte LastPingIdent;
2020-08-08 09:08:10 +02:00
public Client() {
Net = GameObject.FindGameObjectWithTag("Net").GetComponent<NetChaperone>();
LocalPlayer = new NetPlayer(ulong.MaxValue - 1, false);
2020-08-08 09:08:10 +02:00
LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
}
2020-08-08 19:14:34 +02:00
public override void Start(string address, int port, string nick, PeerMessageListener listener) {
if (Peer.Running) {
return;
}
2020-08-08 21:18:17 +02:00
Players.Clear();
Peer = new Peer(Fingerprint);
Peer.MessageListener = listener;
Peer.Start(0);
byte ident = Peer.RegisterProtocol(new GameProtocol(this));
Peer.Connect(address, port, ident);
2020-08-08 19:14:34 +02:00
LocalPlayer.Nick = nick;
}
public override void OnStop() {
Peer.Disconnect(Server.uid);
Server = null;
foreach (NetPlayer p in Players.Values) {
if (p.Id != LocalPlayer.Id) {
GameObject.Destroy(p.Controlled.gameObject);
}
}
Players.Clear();
Ping = 0;
}
public override void Connected(Connection conn) {
2020-08-07 05:24:46 +02:00
Terminal.Singleton.Println($"{conn.uid} Connected");
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}");
if (Server == conn) {
Server = null;
foreach (NetPlayer p in Players.Values) {
if (p.Id != LocalPlayer.Id) {
GameObject.Destroy(p.Controlled.gameObject);
}
}
Players.Clear();
2020-08-08 21:18:17 +02:00
PlayerList.Clear();
Peer.Stop();
Ping = 0;
}
}
public override void Handle(Connection conn, Packet packet) {
if (packet is SelfIdentPckt) {
SelfIdentPckt ident = (SelfIdentPckt)packet;
LocalPlayer.Id = ident.PlayerId;
LocalPlayer.Team = ident.Team;
LocalPlayer.Controlled.NetId = LocalPlayer.Id;
2020-08-08 21:18:17 +02:00
AddPlayer(LocalPlayer);
2020-08-07 05:24:46 +02:00
SelfIdentified = true;
LocalPlayer.Ready = true;
2020-08-08 19:14:34 +02:00
NicknamePckt nickpckt = new NicknamePckt(LocalPlayer.Nick);
Peer.SendReliableLater(Server.uid, nickpckt);
2020-08-08 19:14:34 +02:00
ReadyPckt ready = new ReadyPckt();
Peer.SendReliable(Server.uid, ready);
2020-08-08 19:14:34 +02:00
} else if (packet is NicknamePckt) {
NicknamePckt nick = (NicknamePckt)packet;
if (nick.PlayerId != LocalPlayer.Id) {
Players[nick.PlayerId].Nick = nick.Nick;
}
} else if (packet is SpawnPckt) {
SpawnPckt spawn = (SpawnPckt)packet;
2020-08-08 08:50:37 +02:00
if (spawn.IsInitial) {
if (spawn.PlayerId == LocalPlayer.Id) {
HandlePlayerRespawn(spawn.PlayerId, spawn.Location);
} else {
Player obj = Net.SpawnPlayer(spawn.Location).GetComponent<Player>();
Players[spawn.PlayerId].Controlled = obj;
}
2020-08-08 14:37:55 +02:00
} else if (Players[spawn.PlayerId].Controlled.IsDead) {
2020-08-08 08:50:37 +02:00
if (Players.ContainsKey(spawn.PlayerId)) {
2020-08-08 14:37:55 +02:00
HandlePlayerRespawn(spawn.PlayerId, spawn.Location);
2020-08-08 08:50:37 +02:00
}
}
} else if (packet is PlayerInitPckt) {
PlayerInitPckt init = (PlayerInitPckt)packet;
if (Players.ContainsKey(init.PlayerId)) {
return; // Got the same init twice
}
NetPlayer player = new NetPlayer(init.PlayerId, true);
player.Nick = init.NicknamePckt.Nick;
player.Team = init.Team;
Player obj = Net.SpawnPlayer(init.SpawnPckt.Location).GetComponent<Player>();
player.Controlled = obj;
AddPlayer(player);
Debug.Log("Added player" + player.Nick);
2020-08-08 14:37:55 +02:00
} else if (packet is DeadPckt) {
DeadPckt dead = (DeadPckt)packet;
if (Players.ContainsKey(dead.DeadPlayerId)) {
HandlePlayerDeath(dead.DeadPlayerId, dead.KillerPlayerId);
}
} 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
}
} else if (packet is MultipleSyncsPckt) {
MultipleSyncsPckt multiple = (MultipleSyncsPckt)packet;
foreach (PlayerSyncPacket pckt in multiple.Syncs) {
HandleSyncPckt(pckt);
}
2020-08-08 03:21:02 +02:00
} else if (packet is PlayerActionPckt) {
PlayerActionPckt action = (PlayerActionPckt)packet;
HandleAction(action);
2020-08-08 02:35:19 +02:00
} else if (packet is PingPckt) {
PingPckt ping = (PingPckt)packet;
ping.ClientReceived = true;
if (!ping.ServerReceived) {
Peer.SendReliable(Server.uid, ping);
LastPingIdent = ping.Identifier;
LastPingReceived = Time.time;
} else {
if (LastPingIdent == ping.Identifier) {
Ping = Time.time - LastPingReceived;
} else {
Ping = Instances.Server.PingInterval + 0.001f;
}
2020-08-08 19:45:59 +02:00
LocalPlayer.Ping = Ping;
2020-08-08 03:44:45 +02:00
UpdatePingBias();
2020-08-08 02:35:19 +02:00
}
2020-08-08 19:37:53 +02:00
} else if (packet is PingListPckt) {
PingListPckt pinglist = (PingListPckt)packet;
foreach (PingInfo info in pinglist.Pings) {
if (info.PlayerId != LocalPlayer.Id) {
Players[info.PlayerId].Ping = info.Ping;
}
}
2020-08-08 06:00:17 +02:00
} else if (packet is HitPckt) {
HitPckt hit = (HitPckt)packet;
if (Players[hit.Target].Controlled != null) {
2020-08-08 09:08:10 +02:00
Players[hit.Target].Controlled.Hit(hit.Source);
2020-08-08 06:00:17 +02:00
}
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 || !Players.ContainsKey(pckt.PlayerId)) {
2020-08-07 22:09:31 +02:00
return; // Ignore, again.
}
Players[pckt.PlayerId].Controlled.ProcessUpdatePacket(pckt);
2020-08-07 22:09:31 +02:00
}
private void HandleSyncPckt(PlayerSyncPacket syncPckt) {
if (Players.ContainsKey(syncPckt.PlayerId) && (syncPckt.Unsynced || syncPckt.PlayerId != LocalPlayer.Id)) {
Players[syncPckt.PlayerId].Controlled.ProcessSyncPacket(syncPckt);
}
}
2020-08-08 03:21:02 +02:00
private void HandleAction(PlayerActionPckt action) {
switch (action.Action) {
case PlayerAction.Jump:
Players[action.PlayerId].Controlled.Jump();
break;
2020-08-08 04:26:17 +02:00
case PlayerAction.Shoot:
ShootData shootData = (ShootData)action.Serializable;
HandleSyncPckt(shootData.SyncPckt);
Players[action.PlayerId].Controlled.Shoot();
break;
2020-08-08 03:21:02 +02:00
}
}
2020-08-08 03:44:45 +02:00
private void UpdatePingBias() {
foreach (NetPlayer p in Players.Values) {
if (p.Id != LocalPlayer.Id) {
p.Controlled.PingBias = Ping;
}
}
}
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-08 04:26:17 +02:00
public override void LocalPlayerAction(PlayerAction action, Serializable serializable = null) {
2020-08-07 22:09:31 +02:00
if (SelfIdentified && Server != null) {
2020-08-08 04:26:17 +02:00
PlayerActionPckt actionPckt = new PlayerActionPckt(action, serializable);
Peer.SendReliable(Server.uid, actionPckt);
}
}
2020-08-07 23:57:11 +02:00
public override void SendPlayerSync() {
if (SelfIdentified && Server != null) {
PlayerSyncPacket packet = LocalPlayer.Controlled.CreateSyncPacket();
Peer.SendUnreliable(Server.uid, packet);
2020-08-07 23:57:11 +02:00
}
}
2020-08-08 08:50:37 +02:00
2020-08-08 14:37:55 +02:00
public void HandlePlayerDeath(ulong dead, ulong killer) {
if (Players[dead].Controlled != null) {
Players[dead].Controlled.Dead(killer);
}
}
public void HandlePlayerRespawn(ulong uid, Vector3 respawn) {
2020-08-08 08:50:37 +02:00
if (Players[uid].Controlled != null) {
2020-08-08 14:37:55 +02:00
Players[uid].Controlled.Respawn(respawn);
2020-08-08 08:50:37 +02:00
}
}
}
}