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.

132 lines
3.0 KiB

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