2020-08-07 03:46:09 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using NeonTea.Quakeball.Net.Packets;
|
|
|
|
|
using NeonTea.Quakeball.TeaNet.Peers;
|
|
|
|
|
using NeonTea.Quakeball.TeaNet.Packets;
|
|
|
|
|
|
|
|
|
|
namespace NeonTea.Quakeball.Net.Instances {
|
|
|
|
|
public class Client : NetInstance {
|
|
|
|
|
|
|
|
|
|
private NetChaperone Net;
|
|
|
|
|
|
|
|
|
|
private Connection Server;
|
|
|
|
|
|
|
|
|
|
private Dictionary<ulong, NetPlayer> Players = new Dictionary<ulong, NetPlayer>();
|
|
|
|
|
|
|
|
|
|
private NetPlayer LocalPlayer;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
LocalPlayer.Controlled = GameObject.FindGameObjectWithTag("Player");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Connected(Connection conn) {
|
|
|
|
|
if (Server == null) {
|
|
|
|
|
Server = conn;
|
|
|
|
|
} else {
|
|
|
|
|
Peer.Disconnect(conn.uid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Disconnected(Connection conn) {
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
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 04:11:53 +02:00
|
|
|
|
GameObject obj = Net.SpawnPlayer(spawn.Location);
|
|
|
|
|
NetPlayer player = new NetPlayer(spawn.PlayerId, obj);
|
2020-08-07 03:46:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|