KitchenChaos/Assets/Scripts/Counters/CuttingCounter.cs

100 lines
5.0 KiB
C#

using System;
using System.Linq;
using ScriptableObjects;
using UnityEngine;
namespace Counters {
public class CuttingCounter : BaseCounter, IHasProgress {
[SerializeField] private CuttingRecipeSO[] cuttingRecipeSOArray;
private int cuttingProgress;
public event EventHandler<IHasProgress.ProgressChangedEventArgs> OnProgressChanged;
public event EventHandler OnCut;
public override void Interact(Player player) {
if (!HasKitchenObject()) {
// Debug.Log("There is no KitchenObject here");
if (player.HasKitchenObject()) {
if (HasRecipeWithInput(player.GetKitchenObject().GetKitchenObjectSO())) {
// Debug.Log("Player is putting KitchenObject to ClearCounter");
player.GetKitchenObject().SetKitchenObjectParent(this);
player.ClearKitchenObject();
cuttingProgress = 0;
CuttingRecipeSO cuttingRecipeSO =
GetCuttingRecipeSOWithInput(GetKitchenObject().GetKitchenObjectSO());
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.HasKitchenObject()) return;
// Debug.Log("Player is carrying something!");
if (player.GetKitchenObject().TryGetPlate(out PlateKitchenObject plateKitchenObject)) {
// Debug.Log("Player is holding a plate");
if (plateKitchenObject.TryAddIngredient(GetKitchenObject().GetKitchenObjectSO()))
GetKitchenObject().DestroySelf();
}
else {
// Debug.Log("Player is taking KitchenObject from CuttingCounter");
GetKitchenObject().SetKitchenObjectParent(player);
ClearKitchenObject();
}
}
}
public override void InteractAlternate(Player player) {
if (HasKitchenObject()) {
KitchenObjectSO currentOnCounterKitchenObjectSO = GetKitchenObject().GetKitchenObjectSO();
// Debug.Log("There is a KitchenObject on the CuttingCounter");
if (HasRecipeWithInput(currentOnCounterKitchenObjectSO)) {
// Debug.Log("Cutting the KitchenObject...");
cuttingProgress++;
OnCut?.Invoke(this, 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 {
GetKitchenObject().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) => GetCuttingRecipeSOWithInput(inputKitchenObjectSO);
private KitchenObjectSO GetOutputForInput(KitchenObjectSO inputKitchenObjectSO) {
CuttingRecipeSO cuttingRecipeSO = GetCuttingRecipeSOWithInput(inputKitchenObjectSO);
return cuttingRecipeSO ? cuttingRecipeSO.output : null;
}
private CuttingRecipeSO GetCuttingRecipeSOWithInput(KitchenObjectSO inputKitchenObjectSO) =>
cuttingRecipeSOArray.FirstOrDefault(cuttingRecipeSO => cuttingRecipeSO.input == inputKitchenObjectSO);
}
}