This repository has been archived on 2025-03-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
mojo-kitchen-chaos/Assets/Scripts/Counters/StoveCounter.cs

236 lines
7.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using static CuttingCounter;
public class StoveCounter : BaseCounter, IHasProgress
{
public event EventHandler<IHasProgress.OnProgressChangeEventsArgs> OnProgressChange;
public event EventHandler<OnStateChangedEventArgs> OnStateChanged;
public class OnStateChangedEventArgs : EventArgs
{
public State state;
}
public enum State
{
Idle,
Frying,
Fried,
Burnt
}
[SerializeField] private FryingRecipeSO[] fryingRecipeSOArray;
[SerializeField] private BurningRecipeSO[] burningRecipeSOArray;
private State state;
private float fryingTimer;
private float burningTimer;
private FryingRecipeSO fryingRecipeSO;
private BurningRecipeSO burningRecipeSO;
private void Start()
{
state = State.Idle;
}
private void Update()
{
if (HasKitchenObject())
{
switch (state)
{
case State.Idle:
break;
case State.Frying:
fryingTimer += Time.deltaTime;
OnProgressChange?.Invoke(this, new IHasProgress.OnProgressChangeEventsArgs
{
progressNormalized = (float)fryingTimer / fryingRecipeSO.fryingTimerMax,
});
if (fryingTimer > fryingRecipeSO.fryingTimerMax)
{
// Fried
GetKitchenObject().DestroySelf();
KitchenObject.SpawnKitchenObject(fryingRecipeSO.output, this);
burningTimer = 0f;
burningRecipeSO = GetBurningRecipeSOWithInput(GetKitchenObject().GetKitchenObjectSO());
state = State.Fried;
OnStateChanged?.Invoke(this, new OnStateChangedEventArgs {
state = state,
});
}
break;
case State.Fried:
burningTimer += Time.deltaTime;
OnProgressChange?.Invoke(this, new IHasProgress.OnProgressChangeEventsArgs
{
progressNormalized = (float)burningTimer / burningRecipeSO.burningTimerMax,
});
if (burningTimer > burningRecipeSO.burningTimerMax)
{
// Fried
GetKitchenObject().DestroySelf();
KitchenObject.SpawnKitchenObject(burningRecipeSO.output, this);
state = State.Burnt;
OnStateChanged?.Invoke(this, new OnStateChangedEventArgs
{
state = state,
});
OnProgressChange?.Invoke(this, new IHasProgress.OnProgressChangeEventsArgs
{
progressNormalized = 0f,
});
}
break;
case State.Burnt:
break;
}
}
}
public override void Interact(Player player)
{
if (!HasKitchenObject())
{
// no KitchenObject here
if (player.HasKitchenObject())
{
// player has object
if (HasRecipeWithInput(player.GetKitchenObject().GetKitchenObjectSO()))
{
// player is carrying an object that can be Fried
player.GetKitchenObject().SetKitchenObjectParent(this);
fryingRecipeSO = GetFryingRecipeSOWithInput(GetKitchenObject().GetKitchenObjectSO());
fryingTimer = 0f;
state = State.Frying;
OnStateChanged?.Invoke(this, new OnStateChangedEventArgs
{
state = state,
});
OnProgressChange?.Invoke(this, new IHasProgress.OnProgressChangeEventsArgs
{
progressNormalized = (float)fryingTimer / fryingRecipeSO.fryingTimerMax,
});
}
}
else
{
// player has nothing, do nothing
}
}
else
{
// KitchenObject is here
if (player.HasKitchenObject())
{
// player has object
if (player.GetKitchenObject().TryGetPlate(out PlateKitchenObject plateKitchenObject))
{
// Player is holding a plate
if (plateKitchenObject.TryAddIngreedient(GetKitchenObject().GetKitchenObjectSO()))
{
GetKitchenObject().DestroySelf();
}
}
state = State.Idle;
OnStateChanged?.Invoke(this, new OnStateChangedEventArgs
{
state = state,
});
OnProgressChange?.Invoke(this, new IHasProgress.OnProgressChangeEventsArgs
{
progressNormalized = 0f,
});
}
else
{
// player has nothing
GetKitchenObject().SetKitchenObjectParent(player);
state = State.Idle;
OnStateChanged?.Invoke(this, new OnStateChangedEventArgs
{
state = state,
});
OnProgressChange?.Invoke(this, new IHasProgress.OnProgressChangeEventsArgs
{
progressNormalized = 0f,
});
}
}
}
private bool HasRecipeWithInput(KitchenObjectSO inputKitchenObjectSO)
{
FryingRecipeSO fryingRecipeSO = GetFryingRecipeSOWithInput(inputKitchenObjectSO);
return fryingRecipeSO != null;
}
private KitchenObjectSO GetOutputForInput(KitchenObjectSO inputKitchenObjectSO)
{
FryingRecipeSO fryingRecipeSO = GetFryingRecipeSOWithInput(inputKitchenObjectSO);
if (fryingRecipeSO != null)
{
return fryingRecipeSO.output;
}
else
{
return null;
}
}
private FryingRecipeSO GetFryingRecipeSOWithInput(KitchenObjectSO inputKitchenObjectSO)
{
foreach (FryingRecipeSO fryingRecipeSO in fryingRecipeSOArray)
{
if (fryingRecipeSO.input == inputKitchenObjectSO)
{
return fryingRecipeSO;
}
}
return null;
}
private BurningRecipeSO GetBurningRecipeSOWithInput(KitchenObjectSO inputKitchenObjectSO)
{
foreach (BurningRecipeSO burningRecipeSO in burningRecipeSOArray)
{
if (burningRecipeSO.input == inputKitchenObjectSO)
{
return burningRecipeSO;
}
}
return null;
}
public bool IsFried()
{
return state == State.Fried;
}
}