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.

135 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 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. Debug.Log("Veggie" + VeggieName + " " + change);
  77. for (int i = 0; i < Veggies.Length; i++)
  78. {
  79. if(Veggies[i].Name == VeggieName)
  80. {
  81. heldQtys[i] += change;
  82. }
  83. }
  84. yield return new WaitForEndOfFrame();
  85. }
  86. public bool CheckVictory()
  87. {
  88. bool winning = true;
  89. for (int i = 0; i < Veggies.Length; i++)
  90. {
  91. if (Qtys[i] != heldQtys[i])
  92. {
  93. winning = false;
  94. }
  95. }
  96. return winning;
  97. }
  98. }
  99. [Serializable]
  100. public class Vegetable
  101. {
  102. public string Name;
  103. public Sprite Image;
  104. }
  105. [Serializable]
  106. public class Frame
  107. {
  108. public Image VegetableSpot;
  109. public TextMeshProUGUI Qty;
  110. }