Separate Jump from regular update

This commit is contained in:
Sofia 2020-08-07 23:09:31 +03:00
parent 92165cebc2
commit c3ff626980
8 changed files with 99 additions and 50 deletions

View File

@ -22,6 +22,7 @@ namespace NeonTea.Quakeball.Networking {
RegisterPacket(typeof(SelfIdentPckt));
RegisterPacket(typeof(MultiplePlayerUpdatesPckt));
RegisterPacket(typeof(PlayerUpdatePckt));
RegisterPacket(typeof(PlayerJumpPckt));
}
public override void ConnectionStatusChanged(ConnectionStatus oldStatus, ConnectionStatus newStatus, Connection conn) {

View File

@ -76,13 +76,9 @@ namespace NeonTea.Quakeball.Networking.Instances {
foreach (PlayerUpdatePckt pckt in multiple.Updates) {
HandleUpdatePlayer(pckt);
}
}
}
public override void UpdateLocalPlayer() {
if (SelfIdentified && Server != null) {
PlayerUpdatePckt pckt = LocalPlayer.Controlled.CreatePacket();
Peer.SendUnreliable(Server.uid, pckt);
} else if (packet is PlayerJumpPckt) {
PlayerJumpPckt jump = (PlayerJumpPckt)packet;
// Jump!
}
}
@ -92,5 +88,19 @@ namespace NeonTea.Quakeball.Networking.Instances {
}
Players[pckt.PlayerId].Controlled.GetComponent<RemotePlayer>().QueuePacket(pckt);
}
public override void UpdateLocalPlayer() {
if (SelfIdentified && Server != null) {
PlayerUpdatePckt pckt = LocalPlayer.Controlled.CreatePacket();
Peer.SendUnreliable(Server.uid, pckt);
}
}
public override void LocalPlayerJump() {
if (SelfIdentified && Server != null) {
PlayerJumpPckt jump = new PlayerJumpPckt();
Peer.SendReliable(Server.uid, jump);
}
}
}
}

View File

@ -17,7 +17,9 @@ namespace NeonTea.Quakeball.Networking.Instances {
public abstract void Connected(Connection conn);
public abstract void Disconnected(Connection conn);
public abstract void Handle(Connection conn, Packet packet);
public abstract void UpdateLocalPlayer();
public abstract void LocalPlayerJump();
public void Stop() {
if (Peer != null) {

View File

@ -80,6 +80,13 @@ namespace NeonTea.Quakeball.Networking.Instances {
updatePckt.PlayerId = conn.uid;
Players[conn.uid].Controlled.GetComponent<RemotePlayer>().QueuePacket(updatePckt);
}
} else if (packet is PlayerJumpPckt) {
PlayerJumpPckt jump = (PlayerJumpPckt)packet;
if (Players[conn.uid].Controlled != null) {
// Check if Players[conn.uid].Controlled can jump, and do the following if true
jump.PlayerId = conn.uid;
SendReliableToAll(jump);
}
}
}
@ -106,6 +113,11 @@ namespace NeonTea.Quakeball.Networking.Instances {
SendUnreliableToAll(pckt);
}
public override void LocalPlayerJump() {
PlayerJumpPckt jump = new PlayerJumpPckt(LocalPlayer.Id);
SendReliableToAll(jump);
}
private void AddPlayer(NetPlayer player) {
Players.Add(player.Id, player);
PlayerList.Add(player);

View File

@ -0,0 +1,39 @@
using UnityEngine;
using NeonTea.Quakeball.TeaNet.Packets;
using System.Collections.Generic;
namespace NeonTea.Quakeball.Networking.Packets {
public class MultiplePlayerUpdatesPckt : Packet {
public List<PlayerUpdatePckt> Updates = new List<PlayerUpdatePckt>();
public MultiplePlayerUpdatesPckt() { }
public MultiplePlayerUpdatesPckt(List<NetPlayer> players) {
foreach (NetPlayer p in players) {
if (p.Controlled == null) {
continue;
}
Updates.Add(p.Controlled.CreatePacket(p.Id));
}
}
public override void Read(ByteBuffer buffer) {
Updates.Clear();
int count = buffer.ReadInt();
for (int i = 0; i < count; i++) {
PlayerUpdatePckt pckt = new PlayerUpdatePckt();
pckt.Read(buffer);
Updates.Add(pckt);
}
}
public override void Write(ByteBuffer buffer) {
buffer.Write(Updates.Count);
foreach (PlayerUpdatePckt p in Updates) {
p.Write(buffer);
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 505fbc702b619804a8ab46b6211db974
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,59 +1,21 @@
using UnityEngine;
using NeonTea.Quakeball.TeaNet.Packets;
using System.Collections.Generic;
using NeonTea.Quakeball.Networking;
namespace NeonTea.Quakeball.Networking.Packets {
public class MultiplePlayerUpdatesPckt : Packet {
public List<PlayerUpdatePckt> Updates = new List<PlayerUpdatePckt>();
public MultiplePlayerUpdatesPckt() { }
public MultiplePlayerUpdatesPckt(List<NetPlayer> players) {
foreach (NetPlayer p in players) {
if (p.Controlled == null) {
continue;
}
Updates.Add(p.Controlled.CreatePacket(p.Id));
}
}
public override void Read(ByteBuffer buffer) {
Updates.Clear();
int count = buffer.ReadInt();
for (int i = 0; i < count; i++) {
PlayerUpdatePckt pckt = new PlayerUpdatePckt();
pckt.Read(buffer);
Updates.Add(pckt);
}
}
public override void Write(ByteBuffer buffer) {
buffer.Write(Updates.Count);
foreach (PlayerUpdatePckt p in Updates) {
p.Write(buffer);
}
}
}
public class PlayerUpdatePckt : Packet {
public ulong PlayerId;
public Vector3 MoveDirection;
public byte MoveStyle;
public bool Jumping;
public float Pitch;
public float Yaw;
public PlayerUpdatePckt() { }
public PlayerUpdatePckt(Vector3 moveDirection, byte moveStyle, bool jumping, float pitch, float yaw, ulong id = 0) {
public PlayerUpdatePckt(Vector3 moveDirection, byte moveStyle, float pitch, float yaw, ulong id = 0) {
MoveDirection = moveDirection;
MoveStyle = moveStyle;
Jumping = jumping;
Pitch = pitch;
Yaw = yaw;
PlayerId = id;
@ -66,7 +28,6 @@ namespace NeonTea.Quakeball.Networking.Packets {
float z = buffer.ReadFloat();
MoveDirection = new Vector3(x, y, z);
MoveStyle = buffer.Read();
Jumping = buffer.ReadBool();
Pitch = buffer.ReadFloat();
Yaw = buffer.ReadFloat();
}
@ -77,9 +38,23 @@ namespace NeonTea.Quakeball.Networking.Packets {
buffer.Write(MoveDirection.y);
buffer.Write(MoveDirection.z);
buffer.Write(MoveStyle);
buffer.Write(Jumping);
buffer.Write(Pitch);
buffer.Write(Yaw);
}
}
public class PlayerJumpPckt : Packet {
public ulong PlayerId;
public PlayerJumpPckt() { }
public PlayerJumpPckt(ulong id) { PlayerId = id; }
public override void Read(ByteBuffer buffer) {
PlayerId = buffer.ReadULong();
}
public override void Write(ByteBuffer buffer) {
buffer.Write(PlayerId);
}
}
}

View File

@ -65,7 +65,7 @@ namespace NeonTea.Quakeball.Players {
/// <summary>Creates a PlayerUpdatePckt representing this Player's current status, for sending to other peers.</summary>
public PlayerUpdatePckt CreatePacket(ulong id = 0) {
return new PlayerUpdatePckt(MoveDirection, CurrentMoveStyle, Jumping, Pitch, Yaw, id);
return new PlayerUpdatePckt(MoveDirection, CurrentMoveStyle, Pitch, Yaw, id);
}
/// <summary>Updates this Player with the given packet, and sets it to null. No reusing packets.</summary>
@ -74,7 +74,6 @@ namespace NeonTea.Quakeball.Players {
Yaw = packet.Yaw;
MoveDirection = packet.MoveDirection;
CurrentMoveStyle = packet.MoveStyle;
Jumping = packet.Jumping;
packet = null;
}