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.

137 lines
3.0 KiB

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