< Summary

Class:PotBehaviour
Assembly:Test
File(s):E:/Unity/Unity Project/VR-Cooking-Demo/Assets/_Scripts/PotBehaviour.cs
Covered lines:76
Uncovered lines:20
Coverable lines:96
Total lines:187
Line coverage:79.1% (76 of 96)
Covered branches:0
Total branches:0
Covered methods:8
Total methods:10
Method coverage:80% (8 of 10)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PotBehaviour()0%000100%
Start()0%000100%
Update()0%000100%
UpdateNutrition()0%00081.4%
Boil()0%0000%
BoilCountdown()0%0000%
OnCollisionEnter(...)0%000100%
PlayDropSound()0%00090.91%
OnTriggerEnter(...)0%000100%
OnTriggerExit(...)0%000100%

File(s)

E:/Unity/Unity Project/VR-Cooking-Demo/Assets/_Scripts/PotBehaviour.cs

#LineLine coverage
 1/*
 2 *
 3 * Code by:
 4 *      Dimitrios Vlachos
 5 *      djv1@student.london.ac.uk
 6 *      dimitri.j.vlachos@gmail.com
 7 *
 8 */
 9
 10using System.Collections;
 11using System.Collections.Generic;
 12using UnityEngine;
 13
 14public 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
 231    public List<GameObject> foodInPot = new();
 32
 33    [Header("Nutritional information")]
 34    FoodItem potFoodItem;
 35
 36    [Header("Objective information")]
 237    public int numPotatos = 0;
 238    [SerializeField] float minPotatoSize = 40;
 239    public int numCarrots = 0;
 240    [SerializeField] float minCarrotSize = 1;
 41
 42    // Start is called before the first frame update
 43    void Start()
 144    {
 145        isBoiling = false;
 146        potFoodItem = GetComponent<FoodItem>();
 147    }
 48
 49    // Update is called once per frame
 50    void Update()
 168251    {
 168252        UpdateNutrition();
 168253    }
 54
 55    private void UpdateNutrition()
 168256    {
 168257        float weight = 0f;
 168258        float calories = 0f;
 168259        float protein = 0f;
 168260        float carbohydrates = 0f;
 168261        float sugar = 0f;
 168262        float fibre = 0f;
 168263        float fat = 0f;
 64
 168265        numPotatos = 0;
 168266        numCarrots = 0;
 67
 168268        FoodItem[] foodInPot = foodParent.GetComponentsInChildren<FoodItem>();
 69
 786270        foreach (var f in foodInPot)
 140871        {
 140872            weight += f.weight;
 140873            calories += f.calories;
 140874            protein += f.protein;
 140875            carbohydrates += f.carbohydrates;
 140876            sugar += f.sugar;
 140877            fibre += f.fibre;
 140878            fat += f.fat;
 79
 140880            switch (f.type)
 81            {
 82                case FoodItem.FoodType.Potato:
 140883                    if (f.weight >= minPotatoSize && f.hasBeenCut)
 084                    {
 085                        numPotatos ++;
 086                    }
 140887                    break;
 88                case FoodItem.FoodType.Carrot:
 089                    if (f.weight >= minCarrotSize && f.hasBeenCut)
 090                    {
 091                        numCarrots++;
 092                    }
 093                    break;
 94            }
 140895        }
 96
 168297        potFoodItem.weight = weight;
 168298        potFoodItem.calories = calories;
 168299        potFoodItem.protein = protein;
 1682100        potFoodItem.carbohydrates = carbohydrates;
 1682101        potFoodItem.sugar = sugar;
 1682102        potFoodItem.fibre = fibre;
 1682103        potFoodItem.fat = fat;
 1682104    }
 105
 106    void Boil()
 0107    {
 0108        isBoiling = true;
 0109        int randomSound = Random.Range(0, boilSounds.Count);
 0110        AudioClip clip = boilSounds[randomSound];
 0111        audioSource.clip = clip;
 0112        audioSource.loop = true;
 0113        StartCoroutine(BoilCountdown());
 0114    }
 115
 116    IEnumerator BoilCountdown()
 0117    {
 0118        yield return new WaitForSeconds(boilingTime);
 0119        isBoiling = false;
 0120    }
 121
 122    private void OnCollisionEnter(Collision collision)
 109123    {
 162124        if (collision.gameObject.layer != 6) return;
 56125        PlayDropSound();
 109126    }
 127
 128    private void PlayDropSound()
 56129    {
 56130        if (isBoiling) return; // Guard statement to prevent boiling sound stopping
 108131        if (audioSource.isPlaying) return;
 132
 4133        int randomSound = Random.Range(0, dropSounds.Count);
 4134        AudioClip clip = dropSounds[randomSound];
 4135        audioSource.clip = clip;
 4136        audioSource.loop = false;
 4137        audioSource.Play();
 56138    }
 139
 140    private void OnTriggerEnter(Collider other)
 36141    {
 36142        GameObject obj = other.gameObject;
 143
 144        // Guard statements
 145        //if (obj.layer != 6) return; // If object not on food layer
 36146        if (!obj.CompareTag("Potato") && !obj.CompareTag("Carrot") && !obj.CompareTag("Carrot"))
 32147        {
 32148            return;
 149        }
 150
 4151        FoodItem f = obj.GetComponentInParent<FoodItem>();
 152
 4153        Debug.Log("Object entered: " + obj);
 4154        f.StartCountdowm(foodParent);
 36155    }
 156
 157    private void OnTriggerExit(Collider other)
 18158    {
 18159        GameObject obj = other.gameObject;
 160
 161        // Guard statements
 18162        if (!obj.CompareTag("Potato") && !obj.CompareTag("Carrot") && !obj.CompareTag("Carrot"))
 14163        {
 14164            return;
 165        }
 166
 4167        Debug.Log("Object exited: " + other);
 4168        FoodItem f = obj.GetComponentInParent<FoodItem>();
 169
 4170        Debug.Log("Object entered: " + obj);
 4171        f.EndCountDown();
 18172    }
 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}