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.

136 lines
3.0 KiB

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