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.

129 lines
2.9 KiB

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. for (int i = 0; i < PlayerCount; i++)
  23. {
  24. int rand = UnityEngine.Random.Range(0, Qtys.Length - 1);
  25. Qtys[rand] += 1;
  26. }
  27. for (int i = 0; i < 4; i++) //Set the recipe UI
  28. {
  29. Frames[i].VegetableSpot.sprite = Veggies[i].Image;
  30. Frames[i].Qty.text = Qtys[i] + "";
  31. }
  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. Inventory.gameObject.SetActive(true);
  54. Inventory.sprite = newVeggie.Image;
  55. }
  56. public void friendlyPickup(string VeggieName, int change)
  57. {
  58. StartCoroutine(ItemLookup(VeggieName, change));
  59. VegMsg msg = new VegMsg(ClientManager.Instance.ID, VeggieName, change);
  60. ClientManager.Instance.SendMessage(PlayerMsgID.Vegetable, msg);
  61. }
  62. public void recieveVeg(UnityEngine.Networking.NetworkMessage msg)
  63. {
  64. VegMsg vegMsg;
  65. if (!Utility.ReadMessage<VegMsg>(msg, out vegMsg))
  66. return;
  67. StartCoroutine(ItemLookup(vegMsg.String, vegMsg.Int));
  68. }
  69. IEnumerator ItemLookup(string VeggieName, int change)
  70. {
  71. for (int i = 0; i < Veggies.Length; i++)
  72. {
  73. if(Veggies[i].Name == VeggieName)
  74. {
  75. heldQtys[i] += change;
  76. }
  77. }
  78. yield return new WaitForEndOfFrame();
  79. }
  80. public bool CheckVictory()
  81. {
  82. bool winning = true;
  83. for (int i = 0; i < Veggies.Length; i++)
  84. {
  85. if (Qtys[i] != heldQtys[i])
  86. {
  87. winning = false;
  88. }
  89. }
  90. return winning;
  91. }
  92. }
  93. [Serializable]
  94. public class Vegetable
  95. {
  96. public string Name;
  97. public Sprite Image;
  98. }
  99. [Serializable]
  100. public class Frame
  101. {
  102. public Image VegetableSpot;
  103. public TextMeshProUGUI Qty;
  104. }