From 5c1114f8526899affd2faf7c1216c16a9f804f53 Mon Sep 17 00:00:00 2001 From: JoshuaReason Date: Sun, 27 Jan 2019 12:02:07 +1100 Subject: [PATCH] Reciepes are synced between --- .../Scipts/Multiplayer/Core/PlayerProtocol.cs | 30 +++++++++++++++++++ .../Multiplayer/Server/PlayerServerManager.cs | 27 +++++++++++++++++ Assets/Scipts/Recipe.cs | 28 +++++++++++++++-- 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/Assets/Scipts/Multiplayer/Core/PlayerProtocol.cs b/Assets/Scipts/Multiplayer/Core/PlayerProtocol.cs index d5bac60..4705552 100644 --- a/Assets/Scipts/Multiplayer/Core/PlayerProtocol.cs +++ b/Assets/Scipts/Multiplayer/Core/PlayerProtocol.cs @@ -16,6 +16,7 @@ namespace Multiplayer public const short GameOver = 204; public const short GameStart = 205; public const short QueryPosition = 206; + public const short Vegetable = 207; } public class PlayerMsg : MessageBase @@ -143,4 +144,33 @@ namespace Multiplayer String = reader.ReadString(); } } + + public class VegMsg : PlayerMsg + { + public string String; + public int Int; + + public VegMsg() { } + + public VegMsg(byte PlayerID, string String, int Int) : base(PlayerID) + { + this.String = String; + this.Int = Int; + } + + public override void Serialize(NetworkWriter writer) + { + base.Serialize(writer); + writer.Write(String); + writer.Write(Int); + } + + public override void Deserialize(NetworkReader reader) + { + base.Deserialize(reader); + String = reader.ReadString(); + Int = reader.ReadInt32(); + } + + } } diff --git a/Assets/Scipts/Multiplayer/Server/PlayerServerManager.cs b/Assets/Scipts/Multiplayer/Server/PlayerServerManager.cs index c487996..af8591e 100644 --- a/Assets/Scipts/Multiplayer/Server/PlayerServerManager.cs +++ b/Assets/Scipts/Multiplayer/Server/PlayerServerManager.cs @@ -21,6 +21,7 @@ namespace Multiplayer { ServerManager.Server.RegisterHandler(PlayerMsgID.Position, RecievePosition); ServerManager.Server.RegisterHandler(PlayerMsgID.QueryPosition, QueryPosition); + ServerManager.Server.RegisterHandler(PlayerMsgID.Vegetable, PickUpVeg); } private void RecievePosition(NetworkMessage msg) @@ -71,5 +72,31 @@ namespace Multiplayer } } + private void PickUpVeg(NetworkMessage msg) + { + VegMsg vegMsg; + if (!Utility.ReadMessage(msg, out vegMsg)) + return; + + + if (!ServerManager.AllPlayers.ContainsKey(vegMsg.ID)) + { + Debug.Log("Recieved Veg from unknown player"); + return; + } + + Player player = ServerManager.AllPlayers[vegMsg.ID]; + + + foreach (Player otherPlayer in ServerManager.AllPlayers.Values) + { + if (otherPlayer.Equals(player)) + continue; + + otherPlayer.Connection.Send(PlayerMsgID.Vegetable, vegMsg); + } + + } + } } diff --git a/Assets/Scipts/Recipe.cs b/Assets/Scipts/Recipe.cs index 5dc642c..883e986 100644 --- a/Assets/Scipts/Recipe.cs +++ b/Assets/Scipts/Recipe.cs @@ -4,6 +4,7 @@ using UnityEngine; using UnityEngine.UI; using TMPro; using System; +using Multiplayer; public class Recipe : MonoBehaviour { @@ -31,13 +32,23 @@ public class Recipe : MonoBehaviour { Frames[i].VegetableSpot.sprite = Veggies[i].Image; Frames[i].Qty.text = Qtys[i] + ""; } - localPlayer = Multiplayer.PlayersManager.Instance.LocalPlayer.GetComponent(); - PlayerCount = Multiplayer.PlayersManager.Instance.RemotePlayers.Count + 1; + localPlayer = PlayersManager.Instance.LocalPlayer.GetComponent(); + PlayerCount = PlayersManager.Instance.RemotePlayers.Count + 1; localPlayer.GetComponent().recipe = this; } + private void OnEnable() + { + ClientManager.Instance.Client.RegisterHandler(PlayerMsgID.Vegetable, recieveVeg); + } + + private void OnDisable() + { + ClientManager.Instance.Client.UnregisterHandler(PlayerMsgID.Vegetable); + } + //Button calls public void OpenRecipe() { @@ -65,6 +76,19 @@ public class Recipe : MonoBehaviour { public void friendlyPickup(string VeggieName, int change) { StartCoroutine(ItemLookup(VeggieName, change)); + + VegMsg msg = new VegMsg(ClientManager.Instance.ID, VeggieName, change); + ClientManager.Instance.SendMessage(PlayerMsgID.Vegetable, msg); + } + + public void recieveVeg(UnityEngine.Networking.NetworkMessage msg) + { + + VegMsg vegMsg; + if (!Utility.ReadMessage(msg, out vegMsg)) + return; + + StartCoroutine(ItemLookup(vegMsg.String, vegMsg.Int)); } IEnumerator ItemLookup(string VeggieName, int change)