/FredBuffet/Assets/Scripts/Cutscripts/Slicing/CutstageTrackingScript.cs

https://bitbucket.org/fredbuffet/fredbuffet · C# · 161 lines · 127 code · 29 blank · 5 comment · 15 complexity · 0736ea2c2dc00367d0d7379f608cd9b2 MD5 · raw file

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. public class CutstageTrackingScript : MonoBehaviour {
  6. [SerializeField] private DictionaryOfStringAndInt _ingredientsNeeded = new DictionaryOfStringAndInt();
  7. [SerializeField] private DictionaryOfStringAndInt _ingredientsCut = new DictionaryOfStringAndInt();
  8. [SerializeField] private int _neededLemon = 0;
  9. [SerializeField] private int _neededOrange = 0;
  10. [SerializeField] private int _neededGrapefruit = 0;
  11. [SerializeField] private int _neededMango = 0;
  12. [SerializeField] private int _neededPineapple = 0;
  13. [SerializeField] private int _neededLime = 0;
  14. public bool CuttingDone = false;
  15. public GameObject NeededHolder;
  16. private GameManagerScript _gameManager;
  17. public GameObject NextButton;
  18. public GameObject Blade;
  19. private float _timePassed = 0.0f;
  20. // Use this for initialization
  21. void Start () {
  22. _ingredientsNeeded.Add("Lemon", _neededLemon);
  23. _ingredientsNeeded.Add("Orange", _neededOrange);
  24. _ingredientsNeeded.Add("Grapefruit", _neededGrapefruit);
  25. _ingredientsNeeded.Add("Mango", _neededMango);
  26. _ingredientsNeeded.Add("Pineapple", _neededPineapple);
  27. _ingredientsNeeded.Add("Lime", _neededLime);
  28. InitializeIngredientsCut();
  29. _gameManager = FindObjectOfType<GameManagerScript>();
  30. InitializeNeeded();
  31. }
  32. private void Update()
  33. {
  34. if (CuttingDone == true)
  35. {
  36. _timePassed += Time.deltaTime;
  37. }
  38. if (_timePassed >= 1.0f)
  39. {
  40. if(GameObject.FindGameObjectWithTag("SliceAble") != null)
  41. {
  42. Destroy(GameObject.FindGameObjectWithTag("SliceAble").gameObject);
  43. }
  44. NextButton.SetActive(true);
  45. }
  46. }
  47. private bool CheckEqual()
  48. {
  49. bool equal = false;
  50. if (_ingredientsNeeded.Count == _ingredientsCut.Count) // Require equal count.
  51. {
  52. equal = true;
  53. foreach (var pair in _ingredientsNeeded)
  54. {
  55. int value;
  56. if (_ingredientsCut.TryGetValue(pair.Key, out value))
  57. {
  58. // Require value be equal.
  59. if (value != pair.Value)
  60. {
  61. equal = false;
  62. break;
  63. }
  64. }
  65. else
  66. {
  67. // Require key be present.
  68. equal = false;
  69. break;
  70. }
  71. }
  72. }
  73. return equal;
  74. }
  75. public void CutIngredient(string IngredientName)
  76. {
  77. Debug.Log(IngredientName);
  78. IngredientName = IngredientName.Substring(0, IngredientName.IndexOf("("));
  79. _ingredientsCut[IngredientName]++;
  80. if (_ingredientsCut[IngredientName] > _ingredientsNeeded[IngredientName])
  81. {
  82. Debug.Log("You Cut Too Much " + IngredientName + "!");
  83. FailStage();
  84. }
  85. else
  86. {
  87. FindObjectOfType<GameManagerScript>().SuccesStage();
  88. }
  89. if (CheckEqual())
  90. {
  91. Debug.Log("Done");
  92. Blade.SetActive(false);
  93. CuttingDone = true;
  94. }
  95. }
  96. private void InitializeIngredientsCut()
  97. {
  98. for (int i = 0; i < _ingredientsNeeded.Count; i++)
  99. {
  100. string key = _ingredientsNeeded.ElementAt(i).Key;
  101. _ingredientsCut.Add(key, 0);
  102. }
  103. }
  104. private void FailStage()
  105. {
  106. _ingredientsCut.Clear(); //too brutal for childern
  107. _gameManager.FailStage(1);
  108. InitializeNeeded();
  109. InitializeIngredientsCut();
  110. }
  111. private void InitializeNeeded()
  112. {
  113. foreach (Transform child in NeededHolder.transform)
  114. {
  115. GameObject.Destroy(child.gameObject);
  116. }
  117. for (int i = 0; i < _ingredientsNeeded.Count; i++)
  118. {
  119. string key = "Needed" + _ingredientsNeeded.ElementAt(i).Key;
  120. int value = _ingredientsNeeded.ElementAt(i).Value;
  121. //Debug.Log(key);
  122. //Debug.Log(value);
  123. for(int j = 0; j < value; j++)
  124. {
  125. GameObject needed = Resources.Load(key) as GameObject;
  126. GameObject spawnNeeded = Instantiate(needed);
  127. spawnNeeded.transform.SetParent(NeededHolder.transform);
  128. spawnNeeded.transform.localScale = Vector3.one;
  129. }
  130. }
  131. }
  132. }