KitchenChaos/Assets/Scripts/Counters/CuttingCounter.cs

132 lines
3.9 KiB
C#

using System;
using System.Linq;
using UnityEngine;
public class CuttingCounter : BaseCounter, IHasProgress, IKitchenObjectParent
{
[SerializeField] private CuttingRecipeSO[] cuttingRecipeSOArray;
private int cuttingProgress;
public static event EventHandler OnAnyCut;
public event EventHandler<IHasProgress.ProgressChangedEventArgs> OnProgressChanged;
public event EventHandler OnCut;
public override void Interact(Player player)
{
if (KitchenObject == null)
{
// Debug.Log("There is no KitchenObject here");
if (player.KitchenObject != null)
{
if (HasRecipeWithInput(player.KitchenObject.KitchenObjectSO))
{
// Debug.Log("Player is putting KitchenObject to ClearCounter");
player.KitchenObject.SetKitchenObjectParent(this);
player.KitchenObject = null;
cuttingProgress = 0;
CuttingRecipeSO cuttingRecipeSO = GetCuttingRecipeSOWithInput(KitchenObject.KitchenObjectSO);
OnProgressChanged?.Invoke(this, new()
{
ProgressNormalized = (float)cuttingProgress / cuttingRecipeSO.cuttingProgressMax
});
}
else
{
// Debug.Log("KitchenObject is not cuttable!");
}
}
else
{
// Debug.Log("Player not carrying anything!");
}
}
else
{
// Debug.Log("There is a KitchenObject");
if (!player.KitchenObject)
{
return;
}
// Debug.Log("Player is carrying something!");
if (player.KitchenObject.TryGetPlate(out PlateKitchenObject plateKitchenObject))
{
// Debug.Log("Player is holding a plate");
if (plateKitchenObject.TryAddIngredient(KitchenObject.KitchenObjectSO))
{
KitchenObject.DestroySelf();
}
}
else
{
// Debug.Log("Player is taking KitchenObject from CuttingCounter");
KitchenObject.SetKitchenObjectParent(player);
KitchenObject = null;
}
}
}
public override void InteractAlternate(Player player)
{
if (KitchenObject)
{
KitchenObjectSO currentOnCounterKitchenObjectSO = KitchenObject.KitchenObjectSO;
// Debug.Log("There is a KitchenObject on the CuttingCounter");
if (HasRecipeWithInput(currentOnCounterKitchenObjectSO))
{
// Debug.Log("Cutting the KitchenObject...");
cuttingProgress++;
OnCut?.Invoke(this, System.EventArgs.Empty);
OnAnyCut?.Invoke(this, System.EventArgs.Empty);
CuttingRecipeSO cuttingRecipeSO = GetCuttingRecipeSOWithInput(currentOnCounterKitchenObjectSO);
OnProgressChanged?.Invoke(this,
new() { ProgressNormalized = (float)cuttingProgress / cuttingRecipeSO.cuttingProgressMax });
if (cuttingProgress >= cuttingRecipeSO.cuttingProgressMax)
{
// Debug.Log("The KitchenObject completly sliced.");
KitchenObjectSO outputKitchenObjectSO = GetOutputForInput(currentOnCounterKitchenObjectSO);
if (outputKitchenObjectSO == null)
{
// Debug.LogError($"Kein OutputKitchenObject SO für {currentOnCounterKitchenObjectSO} gefunden!");
}
else
{
KitchenObject.DestroySelf();
KitchenObject newKitchenObject =
KitchenObject.SpawnKitchenObject(outputKitchenObjectSO, this);
newKitchenObject.gameObject.SetActive(true);
}
}
else
{
// Debug.Log($"The KitchenObject is more cuttable {cuttingProgress}/{cuttingRecipeSO.cuttingProgressMax}");
}
}
else
{
// Debug.Log("The KitchenObject is no more sliceable!");
}
}
else
{
// Debug.Log("There is nothing to cut!");
}
}
private bool HasRecipeWithInput(KitchenObjectSO inputKitchenObjectSO)
{
return GetCuttingRecipeSOWithInput(inputKitchenObjectSO) != null;
}
private KitchenObjectSO GetOutputForInput(KitchenObjectSO inputKitchenObjectSO)
{
CuttingRecipeSO cuttingRecipeSO = GetCuttingRecipeSOWithInput(inputKitchenObjectSO);
return cuttingRecipeSO ? cuttingRecipeSO.output : null;
}
private CuttingRecipeSO GetCuttingRecipeSOWithInput(KitchenObjectSO inputKitchenObjectSO)
{
return cuttingRecipeSOArray.FirstOrDefault(cuttingRecipeSO => cuttingRecipeSO.input == inputKitchenObjectSO);
}
}