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
2.9 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.Player;
  20. PlayerCount = PlayersManager.Instance.Players.Count;
  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 friendlyPickup(string VeggieName, int change)
  62. {
  63. StartCoroutine(ItemLookup(VeggieName, change));
  64. VegMsg msg = new VegMsg(ClientManager.Instance.ID, VeggieName, change);
  65. ClientManager.Instance.SendMessage(PlayerMsgID.Vegetable, msg);
  66. }
  67. public void recieveVeg(UnityEngine.Networking.NetworkMessage msg)
  68. {
  69. VegMsg vegMsg;
  70. if (!Utility.ReadMessage<VegMsg>(msg, out vegMsg))
  71. return;
  72. StartCoroutine(ItemLookup(vegMsg.String, vegMsg.Int));
  73. }
  74. IEnumerator ItemLookup(string VeggieName, int change)
  75. {
  76. for (int i = 0; i < Veggies.Length; i++)
  77. {
  78. if(Veggies[i].Name == VeggieName)
  79. {
  80. heldQtys[i] += change;
  81. }
  82. }
  83. yield return new WaitForEndOfFrame();
  84. }
  85. public bool CheckVictory()
  86. {
  87. bool winning = true;
  88. for (int i = 0; i < Veggies.Length; i++)
  89. {
  90. if (Qtys[i] != heldQtys[i])
  91. {
  92. winning = false;
  93. }
  94. }
  95. return winning;
  96. }
  97. }
  98. [Serializable]
  99. public class Vegetable
  100. {
  101. public string Name;
  102. public Sprite Image;
  103. }
  104. [Serializable]
  105. public class Frame
  106. {
  107. public Image VegetableSpot;
  108. public TextMeshProUGUI Qty;
  109. }