You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.0 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. using System;
  7. using Multiplayer;
  8. public class Recipe : MonoBehaviour {
  9. Player localPlayer;
  10. int PlayerCount;
  11. public Vegetable[] Veggies;
  12. public Frame[] Frames;
  13. public int[] Qtys;
  14. public int[] heldQtys;
  15. public GameObject RecipeCanvas;
  16. public Image Inventory;
  17. // Use this for initialization
  18. void Start () {
  19. for (int i = 0; i < PlayerCount; i++)
  20. {
  21. int rand = UnityEngine.Random.Range(0, Qtys.Length - 1);
  22. Qtys[rand] += 1;
  23. }
  24. for (int i = 0; i < 4; i++) //Set the recipe UI
  25. {
  26. Frames[i].VegetableSpot.sprite = Veggies[i].Image;
  27. Frames[i].Qty.text = Qtys[i] + "";
  28. }
  29. localPlayer = PlayersManager.Instance.LocalPlayer.GetComponent<Player>();
  30. PlayerCount = PlayersManager.Instance.RemotePlayers.Count + 1;
  31. localPlayer.GetComponent<LocalPlayer>().recipe = this;
  32. }
  33. private void OnEnable()
  34. {
  35. ClientManager.Instance.Client.RegisterHandler(PlayerMsgID.Vegetable, recieveVeg);
  36. }
  37. private void OnDisable()
  38. {
  39. ClientManager.Instance.Client.UnregisterHandler(PlayerMsgID.Vegetable);
  40. }
  41. //Button calls
  42. public void OpenRecipe()
  43. {
  44. RecipeCanvas.SetActive(true);
  45. }
  46. public void CloseRecipe()
  47. {
  48. RecipeCanvas.SetActive(false);
  49. }
  50. //Veggie interactions
  51. public void pickupItem(Vegetable newVeggie)
  52. {
  53. localPlayer.heldVeggie = newVeggie;
  54. Inventory.gameObject.SetActive(true);
  55. Inventory.sprite = newVeggie.Image;
  56. }
  57. public void dropItem()
  58. {
  59. localPlayer.heldVeggie = null;
  60. Inventory.gameObject.SetActive(false);
  61. }
  62. public void friendlyPickup(string VeggieName, int change)
  63. {
  64. StartCoroutine(ItemLookup(VeggieName, change));
  65. VegMsg msg = new VegMsg(ClientManager.Instance.ID, VeggieName, change);
  66. ClientManager.Instance.SendMessage(PlayerMsgID.Vegetable, msg);
  67. }
  68. public void recieveVeg(UnityEngine.Networking.NetworkMessage msg)
  69. {
  70. VegMsg vegMsg;
  71. if (!Utility.ReadMessage<VegMsg>(msg, out vegMsg))
  72. return;
  73. StartCoroutine(ItemLookup(vegMsg.String, vegMsg.Int));
  74. }
  75. IEnumerator ItemLookup(string VeggieName, int change)
  76. {
  77. for (int i = 0; i < Veggies.Length; i++)
  78. {
  79. if(Veggies[i].Name == VeggieName)
  80. {
  81. heldQtys[i] += change;
  82. }
  83. }
  84. yield return new WaitForEndOfFrame();
  85. }
  86. public bool CheckVictory()
  87. {
  88. bool winning = true;
  89. for (int i = 0; i < Veggies.Length; i++)
  90. {
  91. if (Qtys[i] != heldQtys[i])
  92. {
  93. winning = false;
  94. }
  95. }
  96. return winning;
  97. }
  98. }
  99. [Serializable]
  100. public class Vegetable
  101. {
  102. public string Name;
  103. public Sprite Image;
  104. }
  105. [Serializable]
  106. public class Frame
  107. {
  108. public Image VegetableSpot;
  109. public TextMeshProUGUI Qty;
  110. }