diff --git a/Assets/GameObjects/Scenes/TestScene.unity b/Assets/GameObjects/Scenes/TestScene.unity index f9bf47e..7294cdb 100644 --- a/Assets/GameObjects/Scenes/TestScene.unity +++ b/Assets/GameObjects/Scenes/TestScene.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 0} - m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1} + m_IndirectSpecularColor: {r: 0.17393494, g: 0.2174847, b: 0.299652, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -11623,6 +11623,36 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: ef9117cc11410da479d1af23fbe77b23, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!1 &1280423820 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1280423821} + m_Layer: 0 + m_Name: SpawnPoint + m_TagString: Respawn + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1280423821 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1280423820} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0.02, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 11 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1349294154 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Networking/Instances/Client.cs b/Assets/Scripts/Networking/Instances/Client.cs index b23994e..0759822 100644 --- a/Assets/Scripts/Networking/Instances/Client.cs +++ b/Assets/Scripts/Networking/Instances/Client.cs @@ -83,17 +83,23 @@ namespace NeonTea.Quakeball.Networking.Instances { Players.Add(LocalPlayer.Id, LocalPlayer); SelfIdentified = true; - SpawnPckt spawn = new SpawnPckt(); + SpawnPckt spawn = new SpawnPckt(LocalPlayer.Controlled.transform.position); 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) { + if (spawn.PlayerId == LocalPlayer.Id && spawn.IsInitial) { return; // Ignore, it's their own. } - Player obj = Net.SpawnPlayer(spawn.Location).GetComponent(); - NetPlayer player = new NetPlayer(spawn.PlayerId, obj); - Players.Add(spawn.PlayerId, player); + if (spawn.IsInitial) { + Player obj = Net.SpawnPlayer(spawn.Location).GetComponent(); + NetPlayer player = new NetPlayer(spawn.PlayerId, obj); + Players.Add(spawn.PlayerId, player); + } else { + if (Players.ContainsKey(spawn.PlayerId)) { + HandlePlayerDeath(spawn.PlayerId, spawn.Location); + } + } } else if (packet is MultiplePlayerUpdatesPckt) { MultiplePlayerUpdatesPckt multiple = (MultiplePlayerUpdatesPckt)packet; foreach (PlayerUpdatePckt pckt in multiple.Updates) { @@ -185,5 +191,11 @@ namespace NeonTea.Quakeball.Networking.Instances { Peer.SendUnreliable(Server.uid, packet); } } + + public void HandlePlayerDeath(ulong uid, Vector3 respawn) { + if (Players[uid].Controlled != null) { + Players[uid].Controlled.transform.position = respawn; + } + } } } \ No newline at end of file diff --git a/Assets/Scripts/Networking/Instances/Server.cs b/Assets/Scripts/Networking/Instances/Server.cs index 0970cfe..e761fb9 100644 --- a/Assets/Scripts/Networking/Instances/Server.cs +++ b/Assets/Scripts/Networking/Instances/Server.cs @@ -57,9 +57,7 @@ namespace NeonTea.Quakeball.Networking.Instances { if (p.Controlled == null) { // Not yet initialized, sending later. continue; } - SpawnPckt spawn = new SpawnPckt(); - spawn.PlayerId = p.Id; - spawn.Location = p.Controlled.transform.position; + SpawnPckt spawn = new SpawnPckt(p.Controlled.transform.position, p.Id); Peer.SendReliableLater(conn.uid, spawn); } NetPlayer RemotePlayer = new NetPlayer(PlayerIdCounter++); @@ -80,13 +78,11 @@ namespace NeonTea.Quakeball.Networking.Instances { public override void Handle(Connection conn, Packet packet) { if (packet is SpawnPckt) { SpawnPckt spawn = (SpawnPckt)packet; - if (Players[conn.uid].Controlled == null) { + if (Players[conn.uid].Controlled == null && spawn.IsInitial) { Player obj = Net.SpawnPlayer(spawn.Location).GetComponent(); Players[conn.uid].Controlled = obj; - spawn = new SpawnPckt(); - spawn.PlayerId = conn.uid; - spawn.Location = obj.transform.position; + spawn = new SpawnPckt(obj.transform.position, conn.uid); SendReliableToAll(spawn, except: spawn.PlayerId); } } else if (packet is PlayerUpdatePckt) { @@ -208,5 +204,13 @@ namespace NeonTea.Quakeball.Networking.Instances { HitPckt hit = new HitPckt(id); SendReliableToAll(hit); } + + public void HandlePlayerDeath(ulong uid) { + Vector3 point = GameObject.FindGameObjectWithTag("Respawn").transform.position; + Player player = Players[uid].Controlled; + player.transform.position = point; + SpawnPckt spawn = new SpawnPckt(point, uid, false); + SendReliableToAll(spawn); + } } } \ No newline at end of file diff --git a/Assets/Scripts/Networking/Packets/SpawnPckt.cs b/Assets/Scripts/Networking/Packets/SpawnPckt.cs index 493d9da..54606a0 100644 --- a/Assets/Scripts/Networking/Packets/SpawnPckt.cs +++ b/Assets/Scripts/Networking/Packets/SpawnPckt.cs @@ -6,6 +6,13 @@ namespace NeonTea.Quakeball.Networking.Packets { public ulong PlayerId; public Vector3 Location; + public bool IsInitial; + + public SpawnPckt(Vector3 location, ulong id = 0, bool isinitial = true) { + PlayerId = id; + Location = location; + IsInitial = isinitial; + } public override void Read(ByteBuffer buffer) { float x = buffer.ReadFloat(); @@ -13,6 +20,7 @@ namespace NeonTea.Quakeball.Networking.Packets { float z = buffer.ReadFloat(); Location = new Vector3(x, y, z); PlayerId = buffer.ReadULong(); + IsInitial = buffer.ReadBool(); } public override void Write(ByteBuffer buffer) { @@ -20,6 +28,7 @@ namespace NeonTea.Quakeball.Networking.Packets { buffer.Write(Location.y); buffer.Write(Location.z); buffer.Write(PlayerId); + buffer.Write(IsInitial); } } diff --git a/Assets/Scripts/Players/Player.cs b/Assets/Scripts/Players/Player.cs index febb38c..12c4bc3 100644 --- a/Assets/Scripts/Players/Player.cs +++ b/Assets/Scripts/Players/Player.cs @@ -195,6 +195,9 @@ namespace NeonTea.Quakeball.Players { } public void Hit() { + if (Net.Singleton.Instance is Server) { + ((Server)Net.Singleton.Instance).HandlePlayerDeath(NetId); + } Debug.Log("I was hit! Aaagh!"); Splatter.Play(); }