KitchenChaos/Assets/Scripts/DeliveryManager.cs

107 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class DeliveryManager : MonoBehaviour
{
public static DeliveryManager Instance { get; private set; }
public List<RecipeSO> WaitingRecipeSOList { get; private set; }
public int SuccessfulRecipes { get; private set; }
public int Points { get; private set; }
public event EventHandler OnRecipeSpawned;
public event EventHandler OnRecipeCompleted;
public event EventHandler<RecipeEventArgs> OnRecipeSuccess;
public event EventHandler OnRecipeFailed;
[SerializeField] private RecipeListSO recipeListSO;
private float spawnRecipeTimer;
private const float spawnRecipeTimerMax = 3f;
private const int waitingRecipesMax = 5;
private void Awake()
{
Instance = this;
WaitingRecipeSOList = new();
}
private void Update()
{
spawnRecipeTimer -= Time.deltaTime;
if (!(spawnRecipeTimer <= 0f))
{
return;
}
spawnRecipeTimer = spawnRecipeTimerMax;
if (WaitingRecipeSOList.Count >= waitingRecipesMax)
{
return;
}
if (KitchenGameManager.Instance.IsGamePlaying())
{
RecipeSO waitingRecipeSO = recipeListSO.recipeSOList[Random.Range(0, recipeListSO.recipeSOList.Count)];
// Debug.Log(waitingRecipeSO.recipeName);
WaitingRecipeSOList.Add(waitingRecipeSO);
OnRecipeSpawned?.Invoke(this, EventArgs.Empty);
}
}
public void DeliverRecipe(PlateKitchenObject plateKitchenObject)
{
foreach (RecipeSO waitingRecipeSO in WaitingRecipeSOList)
{
if (waitingRecipeSO.KitchenObjectSOList.Count != plateKitchenObject.GetKitchenObjectSOList().Count)
{
// Debug.Log("Plate has WRONG number of ingridients!");
continue;
}
// Debug.Log($"Plate has the right number of ingredients for {waitingRecipeSO.recipeName}");
bool plateContentsMatchesRecipe = true;
foreach (KitchenObjectSO recipeKitchenObjectSO in waitingRecipeSO.KitchenObjectSOList)
{
// Debug.Log("Cycling through all ingredients in the recipe");
bool ingredientFound = false;
foreach (KitchenObjectSO plateKitchenObjectSO in plateKitchenObject.GetKitchenObjectSOList())
{
// Debug.Log("Cycling through all ingredients on the plate");
if (plateKitchenObjectSO != recipeKitchenObjectSO)
{
// Debug.Log($"Ingredient {plateKitchenObjectSO.objectName} DOES NOT match!");
continue;
}
// Debug.Log($"Ingredient {plateKitchenObjectSO.objectName} matches");
ingredientFound = true;
break;
}
if (ingredientFound)
{
continue;
}
// Debug.Log($"This Recipe ingredient {recipeKitchenObjectSO.objectName} was not found on the plate!");
plateContentsMatchesRecipe = false;
}
if (!plateContentsMatchesRecipe)
{
Points -= 1;
Debug.Log("Player did not deliver a correct recipe from the waitingRecipes!");
OnRecipeFailed?.Invoke(this, EventArgs.Empty);
continue;
}
Debug.Log($"Player delievered a correct {waitingRecipeSO.RecipeName}.");
_ = WaitingRecipeSOList.Remove(waitingRecipeSO);
SuccessfulRecipes++;
Points += waitingRecipeSO.Points;
OnRecipeCompleted?.Invoke(this, EventArgs.Empty);
OnRecipeSuccess?.Invoke(this, new RecipeEventArgs() { RecipeSO = waitingRecipeSO });
return;
}
}
}