Files
let-him-cook/Assets/Scripts/Cooking.cs
2024-03-12 15:34:00 +01:00

114 lines
3.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cooking : MonoBehaviour
{
// void CheckDishes()
// {
// foreach (GameObject ingredient in currentCollisions) {
// Debug.Log(ingredient.name);
// Debug.Log(ingredient.GetComponent<Ingredient>().NutritionalValue);
// }
// }
public Vector3 FinishedDishPosition;
void CreateDish() {
List <GameObject> Ingredients = GetIngredients();
List <GameObject> AllDishes = new List <GameObject>();
// Find all dishes ingredients can make
foreach (GameObject Ingredient in Ingredients) {
List <GameObject> Dishes = Ingredient.GetComponent<Ingredient>().IngredientIn;
foreach (GameObject Dish in Dishes) {
AllDishes.Add(Dish);
}
}
List <GameObject> PossibleDishes = new List <GameObject>();
// Find the dishes, where all ingredients exist
foreach (GameObject Dish in AllDishes) {
List <GameObject> RequiredIngredients = Dish.GetComponent<Dish>().RequiredIngredients;
int NumberOfIngredients = RequiredIngredients.Count;
int IngredientsFulfilled = 0;
foreach (GameObject Ingredient in Ingredients) {
if (RequiredIngredients.Contains(Ingredient)) {
IngredientsFulfilled++;
}
}
if (IngredientsFulfilled == NumberOfIngredients) {
PossibleDishes.Add(Dish);
}
}
// No dishes possible
if (PossibleDishes.Count < 1) {
return;
}
GameObject BestDish = null;
foreach (GameObject Dish in PossibleDishes) {
if (BestDish == null) {
BestDish = Dish;
continue;
}
int CurrentNutritionalValue = Dish.GetComponent<Dish>().NutritionalValue;
int BestNutrionnalValue = BestDish.GetComponent<Dish>().NutritionalValue;
if (CurrentNutritionalValue > BestNutrionnalValue) {
BestDish = Dish;
}
}
// Remove ingredients and add the dish
foreach (GameObject Ingredient in Ingredients) {
Transform transform = Ingredient.GetComponent<Ingredient>().transform;
transform.position = Vector3.up * 50;
}
BestDish.transform.position = FinishedDishPosition;
}
// Keep track of ingredients going on and off the cooker.
List <GameObject> currentCollisions = new List <GameObject>();
private void OnTriggerEnter2D(Collider2D other) {
currentCollisions.Add (other.gameObject);
CreateDish();
}
private void OnTriggerExit2D(Collider2D other) {
currentCollisions.Remove (other.gameObject);
}
List <GameObject> GetIngredients()
{
return currentCollisions;
}
// void Update()
// {
// GetIngredients();
// }
}