KitchenChaos/Assets/Scripts/Counters/StoveCounter.cs

158 lines
6.2 KiB
C#

using System;
using System.Linq;
using UnityEngine;
public class StoveCounter : BaseCounter, IHasProgress, IKitchenObjectParent {
public enum State {
Idle,
Frying,
Fried,
Burned
}
[SerializeField] private FryingRecipeSO[] fryingRecipeSOArray;
[SerializeField] private BurningRecipeSO[] burningRecipeSOArray;
private BurningRecipeSO burningRecipeSO;
private float burningTimer;
private FryingRecipeSO fryingRecipeSO;
private float fryingTimer;
private State state;
private void Start() {
SetStateAndFireEvent(State.Idle);
}
private void Update() {
if (KitchenObject != null)
switch (state) {
case State.Idle:
break;
case State.Frying:
fryingTimer += Time.deltaTime;
FireProgressEvent(fryingTimer / fryingRecipeSO.fryingTimerMax);
if (fryingTimer > fryingRecipeSO.fryingTimerMax) {
KitchenObject.DestroySelf();
KitchenObject.SpawnKitchenObject(fryingRecipeSO.output, this);
// Debug.Log("Object fried");
SetStateAndFireEvent(State.Fried);
burningTimer = 0f;
burningRecipeSO = GetBurningRecipeSOWithInput(KitchenObject.KitchenObjectSO);
}
break;
case State.Fried:
burningTimer += Time.deltaTime;
FireProgressEvent(burningTimer / burningRecipeSO.burningTimerMax);
if (burningTimer > burningRecipeSO.burningTimerMax) {
KitchenObject.DestroySelf();
KitchenObject.SpawnKitchenObject(burningRecipeSO.output, this);
// Debug.Log("Object burned");
SetStateAndFireEvent(State.Burned);
FireProgressEvent(0f);
}
break;
case State.Burned:
break;
}
}
public event EventHandler<IHasProgress.ProgressChangedEventArgs> OnProgressChanged;
public event EventHandler<StateChangedEventArgs> OnStateChanged;
private void SetStateAndFireEvent(State newState) {
state = newState;
OnStateChanged?.Invoke(this, new StateChangedEventArgs{ State = newState });
}
private void FireProgressEvent(float progress) {
OnProgressChanged?.Invoke(this, new IHasProgress.ProgressChangedEventArgs{
ProgressNormalized = progress,
State = state
});
}
public override void Interact(Player player) {
if (KitchenObject == null) {
Debug.Log("There is no KitchenObject here");
if (player.KitchenObject != null && HasRecipeWithInput(player.KitchenObject.KitchenObjectSO)) {
Debug.Log($"Player is putting {player.KitchenObject.KitchenObjectSO.objectName} to StoveCounter");
player.KitchenObject.SetKitchenObjectParent(this);
player.KitchenObject = null;
fryingRecipeSO = GetFryingRecipeSOWithInput(KitchenObject.KitchenObjectSO);
SetStateAndFireEvent(State.Frying);
fryingTimer = 0f;
}
else {
Debug.Log("Player not carrying anything for the StoveCounter!");
}
}
else {
Debug.Log($"There is a {KitchenObject.KitchenObjectSO.objectName} on the StoveCounter");
if (player.KitchenObject != null) {
if (player.KitchenObject.TryGetPlate(out PlateKitchenObject plateKitchenObject)) {
Debug.Log(
$"Player is carrying a {player.KitchenObject.KitchenObjectSO.objectName}, try to put Ingredient onto it...");
if (plateKitchenObject.TryAddIngredient(KitchenObject.KitchenObjectSO)) {
Debug.Log(
$"Player is taking {KitchenObject.KitchenObjectSO.objectName} from StoveCounter to plate");
KitchenObject.DestroySelf();
SetStateAndFireEvent(State.Idle);
FireProgressEvent(0f);
}
else {
Debug.Log(
$"Ingredient {player.KitchenObject.KitchenObjectSO.objectName} does not fit anymore!");
}
}
else {
Debug.Log($"Player is already carrying a {player.KitchenObject.KitchenObjectSO.objectName}!");
}
}
else {
Debug.Log($"Player is taking {KitchenObject.KitchenObjectSO.objectName} from StoveCounter to hand");
KitchenObject.SetKitchenObjectParent(player);
KitchenObject = null;
SetStateAndFireEvent(State.Idle);
FireProgressEvent(0f);
}
}
}
public override void InteractAlternate(Player player) {
Debug.Log("Nothing alternate to do here!");
}
private bool HasRecipeWithInput(KitchenObjectSO inputKitchenObjectSO) {
return GetFryingRecipeSOWithInput(inputKitchenObjectSO);
}
private KitchenObjectSO GetOutputForInput(KitchenObjectSO inputKitchenObjectSO) {
FryingRecipeSO fRSO = GetFryingRecipeSOWithInput(inputKitchenObjectSO);
return fRSO ? fRSO.output : null;
}
private FryingRecipeSO GetFryingRecipeSOWithInput(KitchenObjectSO inputKitchenObjectSO) {
return fryingRecipeSOArray.FirstOrDefault(fRSO => fRSO.input == inputKitchenObjectSO);
}
private BurningRecipeSO GetBurningRecipeSOWithInput(KitchenObjectSO inputKitchenObjectSO) {
BurningRecipeSO burningRecipeSO =
burningRecipeSOArray.FirstOrDefault(fRSO => fRSO.input == inputKitchenObjectSO);
if (burningRecipeSO == null) {
Debug.LogError($"No BurningRecipe found for {inputKitchenObjectSO.objectName}");
return null;
}
return burningRecipeSO;
}
public bool IsFrying() {
return state == State.Frying;
}
public bool IsFried() {
return state == State.Fried;
}
}