Browse Source

Reciepes are synced between

master
JoshuaReason 5 years ago
parent
commit
5c1114f852
3 changed files with 83 additions and 2 deletions
  1. +30
    -0
      Assets/Scipts/Multiplayer/Core/PlayerProtocol.cs
  2. +27
    -0
      Assets/Scipts/Multiplayer/Server/PlayerServerManager.cs
  3. +26
    -2
      Assets/Scipts/Recipe.cs

+ 30
- 0
Assets/Scipts/Multiplayer/Core/PlayerProtocol.cs View File

@ -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();
}
}
}

+ 27
- 0
Assets/Scipts/Multiplayer/Server/PlayerServerManager.cs View File

@ -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<VegMsg>(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);
}
}
}
}

+ 26
- 2
Assets/Scipts/Recipe.cs View File

@ -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<Player>();
PlayerCount = Multiplayer.PlayersManager.Instance.RemotePlayers.Count + 1;
localPlayer = PlayersManager.Instance.LocalPlayer.GetComponent<Player>();
PlayerCount = PlayersManager.Instance.RemotePlayers.Count + 1;
localPlayer.GetComponent<LocalPlayer>().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<VegMsg>(msg, out vegMsg))
return;
StartCoroutine(ItemLookup(vegMsg.String, vegMsg.Int));
}
IEnumerator ItemLookup(string VeggieName, int change)

Loading…
Cancel
Save