| | | 1 | | /* |
| | | 2 | | * |
| | | 3 | | * Code by: |
| | | 4 | | * Dimitrios Vlachos |
| | | 5 | | * djv1@student.london.ac.uk |
| | | 6 | | * dimitri.j.vlachos@gmail.com |
| | | 7 | | * |
| | | 8 | | */ |
| | | 9 | | |
| | | 10 | | using System.Collections; |
| | | 11 | | using System.Collections.Generic; |
| | | 12 | | using UnityEngine; |
| | | 13 | | |
| | | 14 | | public class PotBehaviour : MonoBehaviour |
| | | 15 | | { |
| | | 16 | | [Header("Pot Stuff")] |
| | | 17 | | float boilingTime; |
| | | 18 | | private bool isBoiling; |
| | | 19 | | public GameObject foodParent; |
| | | 20 | | |
| | | 21 | | [SerializeField, Tooltip("Time, in seconds, until locking food in the pot")] |
| | | 22 | | float parentTime; |
| | | 23 | | |
| | | 24 | | [Header("Sounds")] |
| | | 25 | | [SerializeField, Tooltip("List of dropping sounds to use")] |
| | | 26 | | List<AudioClip> dropSounds; |
| | | 27 | | [SerializeField, Tooltip("List of boiling sounds to use")] |
| | | 28 | | List<AudioClip> boilSounds; |
| | | 29 | | [SerializeField] AudioSource audioSource; |
| | | 30 | | |
| | 2 | 31 | | public List<GameObject> foodInPot = new(); |
| | | 32 | | |
| | | 33 | | [Header("Nutritional information")] |
| | | 34 | | FoodItem potFoodItem; |
| | | 35 | | |
| | | 36 | | [Header("Objective information")] |
| | 2 | 37 | | public int numPotatos = 0; |
| | 2 | 38 | | [SerializeField] float minPotatoSize = 40; |
| | 2 | 39 | | public int numCarrots = 0; |
| | 2 | 40 | | [SerializeField] float minCarrotSize = 1; |
| | | 41 | | |
| | | 42 | | // Start is called before the first frame update |
| | | 43 | | void Start() |
| | 1 | 44 | | { |
| | 1 | 45 | | isBoiling = false; |
| | 1 | 46 | | potFoodItem = GetComponent<FoodItem>(); |
| | 1 | 47 | | } |
| | | 48 | | |
| | | 49 | | // Update is called once per frame |
| | | 50 | | void Update() |
| | 1682 | 51 | | { |
| | 1682 | 52 | | UpdateNutrition(); |
| | 1682 | 53 | | } |
| | | 54 | | |
| | | 55 | | private void UpdateNutrition() |
| | 1682 | 56 | | { |
| | 1682 | 57 | | float weight = 0f; |
| | 1682 | 58 | | float calories = 0f; |
| | 1682 | 59 | | float protein = 0f; |
| | 1682 | 60 | | float carbohydrates = 0f; |
| | 1682 | 61 | | float sugar = 0f; |
| | 1682 | 62 | | float fibre = 0f; |
| | 1682 | 63 | | float fat = 0f; |
| | | 64 | | |
| | 1682 | 65 | | numPotatos = 0; |
| | 1682 | 66 | | numCarrots = 0; |
| | | 67 | | |
| | 1682 | 68 | | FoodItem[] foodInPot = foodParent.GetComponentsInChildren<FoodItem>(); |
| | | 69 | | |
| | 7862 | 70 | | foreach (var f in foodInPot) |
| | 1408 | 71 | | { |
| | 1408 | 72 | | weight += f.weight; |
| | 1408 | 73 | | calories += f.calories; |
| | 1408 | 74 | | protein += f.protein; |
| | 1408 | 75 | | carbohydrates += f.carbohydrates; |
| | 1408 | 76 | | sugar += f.sugar; |
| | 1408 | 77 | | fibre += f.fibre; |
| | 1408 | 78 | | fat += f.fat; |
| | | 79 | | |
| | 1408 | 80 | | switch (f.type) |
| | | 81 | | { |
| | | 82 | | case FoodItem.FoodType.Potato: |
| | 1408 | 83 | | if (f.weight >= minPotatoSize && f.hasBeenCut) |
| | 0 | 84 | | { |
| | 0 | 85 | | numPotatos ++; |
| | 0 | 86 | | } |
| | 1408 | 87 | | break; |
| | | 88 | | case FoodItem.FoodType.Carrot: |
| | 0 | 89 | | if (f.weight >= minCarrotSize && f.hasBeenCut) |
| | 0 | 90 | | { |
| | 0 | 91 | | numCarrots++; |
| | 0 | 92 | | } |
| | 0 | 93 | | break; |
| | | 94 | | } |
| | 1408 | 95 | | } |
| | | 96 | | |
| | 1682 | 97 | | potFoodItem.weight = weight; |
| | 1682 | 98 | | potFoodItem.calories = calories; |
| | 1682 | 99 | | potFoodItem.protein = protein; |
| | 1682 | 100 | | potFoodItem.carbohydrates = carbohydrates; |
| | 1682 | 101 | | potFoodItem.sugar = sugar; |
| | 1682 | 102 | | potFoodItem.fibre = fibre; |
| | 1682 | 103 | | potFoodItem.fat = fat; |
| | 1682 | 104 | | } |
| | | 105 | | |
| | | 106 | | void Boil() |
| | 0 | 107 | | { |
| | 0 | 108 | | isBoiling = true; |
| | 0 | 109 | | int randomSound = Random.Range(0, boilSounds.Count); |
| | 0 | 110 | | AudioClip clip = boilSounds[randomSound]; |
| | 0 | 111 | | audioSource.clip = clip; |
| | 0 | 112 | | audioSource.loop = true; |
| | 0 | 113 | | StartCoroutine(BoilCountdown()); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | IEnumerator BoilCountdown() |
| | 0 | 117 | | { |
| | 0 | 118 | | yield return new WaitForSeconds(boilingTime); |
| | 0 | 119 | | isBoiling = false; |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | private void OnCollisionEnter(Collision collision) |
| | 109 | 123 | | { |
| | 162 | 124 | | if (collision.gameObject.layer != 6) return; |
| | 56 | 125 | | PlayDropSound(); |
| | 109 | 126 | | } |
| | | 127 | | |
| | | 128 | | private void PlayDropSound() |
| | 56 | 129 | | { |
| | 56 | 130 | | if (isBoiling) return; // Guard statement to prevent boiling sound stopping |
| | 108 | 131 | | if (audioSource.isPlaying) return; |
| | | 132 | | |
| | 4 | 133 | | int randomSound = Random.Range(0, dropSounds.Count); |
| | 4 | 134 | | AudioClip clip = dropSounds[randomSound]; |
| | 4 | 135 | | audioSource.clip = clip; |
| | 4 | 136 | | audioSource.loop = false; |
| | 4 | 137 | | audioSource.Play(); |
| | 56 | 138 | | } |
| | | 139 | | |
| | | 140 | | private void OnTriggerEnter(Collider other) |
| | 36 | 141 | | { |
| | 36 | 142 | | GameObject obj = other.gameObject; |
| | | 143 | | |
| | | 144 | | // Guard statements |
| | | 145 | | //if (obj.layer != 6) return; // If object not on food layer |
| | 36 | 146 | | if (!obj.CompareTag("Potato") && !obj.CompareTag("Carrot") && !obj.CompareTag("Carrot")) |
| | 32 | 147 | | { |
| | 32 | 148 | | return; |
| | | 149 | | } |
| | | 150 | | |
| | 4 | 151 | | FoodItem f = obj.GetComponentInParent<FoodItem>(); |
| | | 152 | | |
| | 4 | 153 | | Debug.Log("Object entered: " + obj); |
| | 4 | 154 | | f.StartCountdowm(foodParent); |
| | 36 | 155 | | } |
| | | 156 | | |
| | | 157 | | private void OnTriggerExit(Collider other) |
| | 18 | 158 | | { |
| | 18 | 159 | | GameObject obj = other.gameObject; |
| | | 160 | | |
| | | 161 | | // Guard statements |
| | 18 | 162 | | if (!obj.CompareTag("Potato") && !obj.CompareTag("Carrot") && !obj.CompareTag("Carrot")) |
| | 14 | 163 | | { |
| | 14 | 164 | | return; |
| | | 165 | | } |
| | | 166 | | |
| | 4 | 167 | | Debug.Log("Object exited: " + other); |
| | 4 | 168 | | FoodItem f = obj.GetComponentInParent<FoodItem>(); |
| | | 169 | | |
| | 4 | 170 | | Debug.Log("Object entered: " + obj); |
| | 4 | 171 | | f.EndCountDown(); |
| | 18 | 172 | | } |
| | | 173 | | |
| | | 174 | | /*IEnumerator ParentFoodCountdown(GameObject obj) |
| | | 175 | | { |
| | | 176 | | // Wait parenting cooldown time |
| | | 177 | | yield return new WaitForSeconds(parentTime); |
| | | 178 | | |
| | | 179 | | // If the object is still in the trigger area |
| | | 180 | | // then parent it to the pot |
| | | 181 | | if (foodInPot.Contains(obj)) |
| | | 182 | | { |
| | | 183 | | obj.transform.SetParent(transform); |
| | | 184 | | obj.GetComponent<Rigidbody>().isKinematic = true; |
| | | 185 | | } |
| | | 186 | | }*/ |
| | | 187 | | } |