Make moving updates work

This commit is contained in:
Sofia 2020-08-07 06:24:46 +03:00
parent 088d236fe0
commit 8a4b5f858c
7 changed files with 650 additions and 56 deletions

File diff suppressed because one or more lines are too long

View File

@ -20,6 +20,7 @@ namespace NeonTea.Quakeball.Net {
RegisterPacket(typeof(HelloPckt)); RegisterPacket(typeof(HelloPckt));
RegisterPacket(typeof(SpawnPckt)); RegisterPacket(typeof(SpawnPckt));
RegisterPacket(typeof(SelfIdentPckt)); RegisterPacket(typeof(SelfIdentPckt));
RegisterPacket(typeof(PlayerUpdatePckt));
} }
public override void ConnectionStatusChanged(ConnectionStatus oldStatus, ConnectionStatus newStatus, Connection conn) { public override void ConnectionStatusChanged(ConnectionStatus oldStatus, ConnectionStatus newStatus, Connection conn) {

View File

@ -1,7 +1,9 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using NeonTea.Quakeball.Player;
using NeonTea.Quakeball.Net.Packets; using NeonTea.Quakeball.Net.Packets;
using NeonTea.Quakeball.Interface;
using NeonTea.Quakeball.TeaNet.Peers; using NeonTea.Quakeball.TeaNet.Peers;
using NeonTea.Quakeball.TeaNet.Packets; using NeonTea.Quakeball.TeaNet.Packets;
@ -13,8 +15,8 @@ namespace NeonTea.Quakeball.Net.Instances {
private Connection Server; private Connection Server;
private Dictionary<ulong, NetPlayer> Players = new Dictionary<ulong, NetPlayer>(); private Dictionary<ulong, NetPlayer> Players = new Dictionary<ulong, NetPlayer>();
private NetPlayer LocalPlayer; private NetPlayer LocalPlayer;
private bool SelfIdentified = false;
public override void Start(string address, int port, PeerMessageListener listener) { public override void Start(string address, int port, PeerMessageListener listener) {
if (Peer != null) { if (Peer != null) {
@ -33,6 +35,7 @@ namespace NeonTea.Quakeball.Net.Instances {
} }
public override void Connected(Connection conn) { public override void Connected(Connection conn) {
Terminal.Singleton.Println($"{conn.uid} Connected");
if (Server == null) { if (Server == null) {
Server = conn; Server = conn;
} else { } else {
@ -41,6 +44,7 @@ namespace NeonTea.Quakeball.Net.Instances {
} }
public override void Disconnected(Connection conn) { public override void Disconnected(Connection conn) {
Terminal.Singleton.Println($"{conn.uid} Disconnected: {conn.ClosingReason}");
if (Server == conn) { if (Server == conn) {
Server = null; Server = null;
} }
@ -51,6 +55,7 @@ namespace NeonTea.Quakeball.Net.Instances {
SelfIdentPckt ident = (SelfIdentPckt)packet; SelfIdentPckt ident = (SelfIdentPckt)packet;
LocalPlayer.Id = ident.PlayerId; LocalPlayer.Id = ident.PlayerId;
Players.Add(LocalPlayer.Id, LocalPlayer); Players.Add(LocalPlayer.Id, LocalPlayer);
SelfIdentified = true;
SpawnPckt spawn = new SpawnPckt(); SpawnPckt spawn = new SpawnPckt();
spawn.Location = LocalPlayer.Controlled.transform.position; spawn.Location = LocalPlayer.Controlled.transform.position;
@ -62,6 +67,19 @@ namespace NeonTea.Quakeball.Net.Instances {
} }
GameObject obj = Net.SpawnPlayer(spawn.Location); GameObject obj = Net.SpawnPlayer(spawn.Location);
NetPlayer player = new NetPlayer(spawn.PlayerId, obj); NetPlayer player = new NetPlayer(spawn.PlayerId, obj);
Players.Add(spawn.PlayerId, player);
} else if (packet is PlayerUpdatePckt) {
PlayerUpdatePckt updatePckt = (PlayerUpdatePckt)packet;
if (updatePckt.PlayerId == LocalPlayer.Id) {
return; // Ignore, again.
}
Players[updatePckt.PlayerId].Controlled.GetComponent<RemotePlayer>().QueuePacket(updatePckt);
}
}
public override void UpdateLocalPlayer(PlayerUpdatePckt pckt) {
if (SelfIdentified) {
Peer.SendUnreliable(Server.uid, pckt);
} }
} }
} }

View File

@ -1,6 +1,7 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using NeonTea.Quakeball.Net.Packets;
using NeonTea.Quakeball.TeaNet.Peers; using NeonTea.Quakeball.TeaNet.Peers;
using NeonTea.Quakeball.TeaNet.Packets; using NeonTea.Quakeball.TeaNet.Packets;
@ -16,6 +17,7 @@ namespace NeonTea.Quakeball.Net.Instances {
public abstract void Connected(Connection conn); public abstract void Connected(Connection conn);
public abstract void Disconnected(Connection conn); public abstract void Disconnected(Connection conn);
public abstract void Handle(Connection conn, Packet packet); public abstract void Handle(Connection conn, Packet packet);
public abstract void UpdateLocalPlayer(PlayerUpdatePckt pckt);
public void Stop() { public void Stop() {
if (Peer != null) { if (Peer != null) {

View File

@ -2,6 +2,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using NeonTea.Quakeball.Net.Packets; using NeonTea.Quakeball.Net.Packets;
using NeonTea.Quakeball.Player;
using NeonTea.Quakeball.Interface;
using NeonTea.Quakeball.TeaNet.Peers; using NeonTea.Quakeball.TeaNet.Peers;
using NeonTea.Quakeball.TeaNet.Packets; using NeonTea.Quakeball.TeaNet.Packets;
@ -33,7 +35,9 @@ namespace NeonTea.Quakeball.Net.Instances {
} }
public override void Connected(Connection conn) { public override void Connected(Connection conn) {
Terminal.Singleton.Println($"{conn.uid} Connected");
foreach (NetPlayer p in Players.Values) { foreach (NetPlayer p in Players.Values) {
if (p.Controlled == null) { // Not yet initialized, sending later. if (p.Controlled == null) { // Not yet initialized, sending later.
continue; continue;
} }
@ -50,6 +54,7 @@ namespace NeonTea.Quakeball.Net.Instances {
} }
public override void Disconnected(Connection conn) { public override void Disconnected(Connection conn) {
Terminal.Singleton.Println($"{conn.uid} Disconnected: {conn.ClosingReason}");
} }
public override void Handle(Connection conn, Packet packet) { public override void Handle(Connection conn, Packet packet) {
@ -64,6 +69,12 @@ namespace NeonTea.Quakeball.Net.Instances {
spawn.Location = obj.transform.position; spawn.Location = obj.transform.position;
SendReliableToAll(spawn); SendReliableToAll(spawn);
} }
} else if (packet is PlayerUpdatePckt) {
PlayerUpdatePckt updatePckt = (PlayerUpdatePckt)packet;
if (Players[conn.uid].Controlled != null) {
updatePckt.PlayerId = conn.uid;
Players[conn.uid].Controlled.GetComponent<RemotePlayer>().QueuePacket(updatePckt);
}
} }
} }
@ -72,5 +83,17 @@ namespace NeonTea.Quakeball.Net.Instances {
Peer.SendReliable(p.Id, packet); Peer.SendReliable(p.Id, packet);
} }
} }
public void SendUnreliableToAll(Packet packet) {
foreach (NetPlayer p in Players.Values) {
Peer.SendUnreliable(p.Id, packet);
}
}
public override void UpdateLocalPlayer(PlayerUpdatePckt pckt) {
Debug.Log("Update local to everyone!");
pckt.PlayerId = LocalPlayer.Id;
SendUnreliableToAll(pckt);
}
} }
} }

View File

@ -4,6 +4,8 @@ using NeonTea.Quakeball.TeaNet.Packets;
namespace NeonTea.Quakeball.Net.Packets { namespace NeonTea.Quakeball.Net.Packets {
public class PlayerUpdatePckt : Packet { public class PlayerUpdatePckt : Packet {
public ulong PlayerId;
public Vector3 MoveDirection; public Vector3 MoveDirection;
public byte MoveStyle; public byte MoveStyle;
public bool Jumping; public bool Jumping;
@ -21,9 +23,26 @@ namespace NeonTea.Quakeball.Net.Packets {
} }
public override void Read(ByteBuffer buffer) { public override void Read(ByteBuffer buffer) {
PlayerId = buffer.ReadULong();
float x = buffer.ReadFloat();
float y = buffer.ReadFloat();
float z = buffer.ReadFloat();
MoveDirection = new Vector3(x, y, z);
MoveStyle = buffer.Read();
Jumping = buffer.ReadBool();
Pitch = buffer.ReadFloat();
Yaw = buffer.ReadFloat();
} }
public override void Write(ByteBuffer buffer) { public override void Write(ByteBuffer buffer) {
buffer.Write(PlayerId);
buffer.Write(MoveDirection.x);
buffer.Write(MoveDirection.y);
buffer.Write(MoveDirection.z);
buffer.Write(MoveStyle);
buffer.Write(Jumping);
buffer.Write(Pitch);
buffer.Write(Yaw);
} }
} }
} }

View File

@ -1,5 +1,7 @@
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
using NeonTea.Quakeball.Net;
using NeonTea.Quakeball.Net.Packets;
namespace NeonTea.Quakeball.Player { namespace NeonTea.Quakeball.Player {
/// <summary>A controller class for a local player. Handles input and updates the relevant components.</summary> /// <summary>A controller class for a local player. Handles input and updates the relevant components.</summary>
@ -80,7 +82,10 @@ namespace NeonTea.Quakeball.Player {
PreviousPlayerUpdate = Time.time; PreviousPlayerUpdate = Time.time;
WantsToJump = false; WantsToJump = false;
//new Net.Packets.PlayerUpdatePckt(Player.MoveDirection, Player.CurrentMoveStyle, Player.Jumping, Player.Pitch, Player.Yaw); if (Net.Net.Singleton.Instance != null) {
PlayerUpdatePckt pckt = new PlayerUpdatePckt(Player.MoveDirection, Player.CurrentMoveStyle, Player.Jumping, Player.Pitch, Player.Yaw);
Net.Net.Singleton.Instance.UpdateLocalPlayer(pckt);
}
} }
} }
} }