| | | 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 TMPro; |
| | | 11 | | using Unity.VisualScripting; |
| | | 12 | | using UnityEngine; |
| | | 13 | | |
| | | 14 | | public class NutritionScaleBehaviour : MonoBehaviour |
| | | 15 | | { |
| | | 16 | | [SerializeField] private GameObject obj; |
| | | 17 | | [SerializeField] private FoodItem item; |
| | | 18 | | |
| | | 19 | | [Header("Text Displays")] |
| | | 20 | | [SerializeField] private GameObject foodName; |
| | | 21 | | [SerializeField] private GameObject weight; |
| | | 22 | | [SerializeField] private GameObject calories; |
| | | 23 | | [SerializeField] private GameObject protein; |
| | | 24 | | [SerializeField] private GameObject carbohydrates; |
| | | 25 | | [SerializeField] private GameObject sugar; |
| | | 26 | | [SerializeField] private GameObject fiber; |
| | | 27 | | [SerializeField] private GameObject fat; |
| | | 28 | | |
| | | 29 | | private TextMeshProUGUI textMeshPro_name_text; |
| | | 30 | | private TextMeshProUGUI textMeshPro_weight_text; |
| | | 31 | | private TextMeshProUGUI textMeshPro_calories_text; |
| | | 32 | | private TextMeshProUGUI textMeshPro_protein_text; |
| | | 33 | | private TextMeshProUGUI textMeshPro_carbohydrates_text; |
| | | 34 | | private TextMeshProUGUI textMeshPro_sugar_text; |
| | | 35 | | private TextMeshProUGUI textMeshPro_fiber_text; |
| | | 36 | | private TextMeshProUGUI textMeshPro_fat_text; |
| | | 37 | | |
| | | 38 | | // Start is called before the first frame update |
| | | 39 | | void Start() |
| | 1 | 40 | | { |
| | 1 | 41 | | obj = null; |
| | 1 | 42 | | item = null; |
| | | 43 | | |
| | 1 | 44 | | textMeshPro_name_text = foodName.GetComponent<TextMeshProUGUI>(); |
| | 1 | 45 | | textMeshPro_weight_text = weight.GetComponent<TextMeshProUGUI>(); |
| | 1 | 46 | | textMeshPro_calories_text = calories.GetComponent<TextMeshProUGUI>(); |
| | 1 | 47 | | textMeshPro_protein_text = protein.GetComponent<TextMeshProUGUI>(); |
| | 1 | 48 | | textMeshPro_carbohydrates_text = carbohydrates.GetComponent<TextMeshProUGUI>(); |
| | 1 | 49 | | textMeshPro_sugar_text = sugar.GetComponent<TextMeshProUGUI>(); |
| | 1 | 50 | | textMeshPro_fiber_text = fiber.GetComponent<TextMeshProUGUI>(); |
| | 1 | 51 | | textMeshPro_fat_text = fat.GetComponent<TextMeshProUGUI>(); |
| | | 52 | | |
| | | 53 | | |
| | | 54 | | |
| | 1 | 55 | | } |
| | | 56 | | |
| | | 57 | | // Update is called once per frame |
| | | 58 | | void Update() |
| | 1682 | 59 | | { |
| | | 60 | | // Guard statement |
| | 1682 | 61 | | if (obj == null) |
| | 1682 | 62 | | { |
| | 1682 | 63 | | item = null; |
| | 1682 | 64 | | textMeshPro_name_text.text = ""; |
| | | 65 | | |
| | 1682 | 66 | | textMeshPro_weight_text.text = ""; |
| | 1682 | 67 | | textMeshPro_calories_text.text = ""; |
| | 1682 | 68 | | textMeshPro_protein_text.text = ""; |
| | 1682 | 69 | | textMeshPro_carbohydrates_text.text = ""; |
| | 1682 | 70 | | textMeshPro_sugar_text.text = ""; |
| | 1682 | 71 | | textMeshPro_fiber_text.text = ""; |
| | 1682 | 72 | | textMeshPro_fat_text.text = ""; |
| | 1682 | 73 | | return; |
| | | 74 | | } |
| | 0 | 75 | | if (item == null) |
| | 0 | 76 | | { |
| | 0 | 77 | | item = obj.GetComponentInParent<FoodItem>(); |
| | 0 | 78 | | } |
| | 0 | 79 | | if (item == null) return; |
| | | 80 | | |
| | | 81 | | // Functional code |
| | | 82 | | |
| | | 83 | | // Set the food name |
| | 0 | 84 | | textMeshPro_name_text.text = item.type.ToString(); |
| | | 85 | | |
| | | 86 | | // Get the nutritional values and round them to one decimal place |
| | 0 | 87 | | float weight = Mathf.Round(item.weight * 10f) / 10f; |
| | 0 | 88 | | float calories = Mathf.Round(item.calories * 10f) / 10f; |
| | 0 | 89 | | float protein = Mathf.Round(item.protein * 10f) / 10f; |
| | 0 | 90 | | float carbohydrates = Mathf.Round(item.carbohydrates * 10f) / 10f; |
| | 0 | 91 | | float sugar = Mathf.Round(item.sugar * 10f) / 10f; |
| | 0 | 92 | | float fibre = Mathf.Round(item.fibre * 10f) / 10f; |
| | 0 | 93 | | float fat = Mathf.Round(item.fat * 10f) / 10f; |
| | | 94 | | |
| | | 95 | | // Set the text to the newly rounded values |
| | 0 | 96 | | textMeshPro_weight_text.text = weight.ToString() + "g"; |
| | 0 | 97 | | textMeshPro_calories_text.text = calories.ToString() + "kcal"; |
| | 0 | 98 | | textMeshPro_protein_text.text = protein.ToString() + "g"; |
| | 0 | 99 | | textMeshPro_carbohydrates_text.text = carbohydrates.ToString() + "g"; |
| | 0 | 100 | | textMeshPro_sugar_text.text = sugar.ToString() + "g"; |
| | 0 | 101 | | textMeshPro_fiber_text.text = fibre.ToString() + "g"; |
| | 0 | 102 | | textMeshPro_fat_text.text = fat.ToString() + "g"; |
| | 1682 | 103 | | } |
| | | 104 | | |
| | | 105 | | private void OnTriggerEnter(Collider other) |
| | 0 | 106 | | { |
| | | 107 | | //Debug.Log("Object entered: " + other.name); |
| | 0 | 108 | | obj = other.gameObject; |
| | | 109 | | |
| | 0 | 110 | | } |
| | | 111 | | |
| | | 112 | | private void OnTriggerExit(Collider other) |
| | 0 | 113 | | { |
| | | 114 | | //Debug.Log("Object exited"); |
| | 0 | 115 | | obj = null; |
| | 0 | 116 | | } |
| | | 117 | | } |