93 lines
3.7 KiB
C#
93 lines
3.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
|
|
public class CuttingCounter : BaseCounter, IHasProgress {
|
|
[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) {
|
|
if (player.KitchenObject is null) return;
|
|
if (!HasRecipeWithInput(player.KitchenObject.KitchenObjectSO)) return;
|
|
|
|
KitchenObject kitchenObject = player.KitchenObject;
|
|
kitchenObject.SetKitchenObjectParent(this);
|
|
player.KitchenObject = null;
|
|
InteractLogicPlaceObjectOnCounterServerRpc();
|
|
}
|
|
else {
|
|
if (player.KitchenObject == null) return;
|
|
|
|
if (player.KitchenObject.TryGetPlate(out PlateKitchenObject plateKitchenObject)) {
|
|
if (plateKitchenObject.TryAddIngredient(KitchenObject.KitchenObjectSO)) KitchenObject.DestroySelf();
|
|
}
|
|
else {
|
|
KitchenObject.SetKitchenObjectParent(player);
|
|
KitchenObject = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
[ServerRpc(RequireOwnership = false)]
|
|
public void InteractLogicPlaceObjectOnCounterServerRpc() => InteractLogicPlaceObjectOnCounterClientRpc();
|
|
|
|
[ClientRpc]
|
|
public void InteractLogicPlaceObjectOnCounterClientRpc() {
|
|
cuttingProgress = 0;
|
|
OnProgressChanged?.Invoke(this, new IHasProgress.ProgressChangedEventArgs{ ProgressNormalized = 0f });
|
|
}
|
|
|
|
public override void InteractAlternate(Player player) {
|
|
if (KitchenObject == null) return;
|
|
if (!HasRecipeWithInput(KitchenObject.KitchenObjectSO)) return;
|
|
|
|
CutObjectServerRpc();
|
|
TestCuttingProgressDoneServerRpc();
|
|
}
|
|
|
|
[ServerRpc(RequireOwnership = false)]
|
|
public void CutObjectServerRpc() => CutObjectClientRpc();
|
|
|
|
[ClientRpc]
|
|
public void CutObjectClientRpc() {
|
|
cuttingProgress++;
|
|
|
|
OnCut?.Invoke(this, EventArgs.Empty);
|
|
OnAnyCut?.Invoke(this, EventArgs.Empty);
|
|
|
|
CuttingRecipeSO cuttingRecipeSO = GetCuttingRecipeSOWithInput(KitchenObject.KitchenObjectSO);
|
|
OnProgressChanged?.Invoke(this, new IHasProgress.ProgressChangedEventArgs
|
|
{ ProgressNormalized = (float)cuttingProgress / cuttingRecipeSO.cuttingProgressMax });
|
|
}
|
|
|
|
[ServerRpc(RequireOwnership = false)]
|
|
public void TestCuttingProgressDoneServerRpc() {
|
|
CuttingRecipeSO cuttingRecipeSO = GetCuttingRecipeSOWithInput(KitchenObject.KitchenObjectSO);
|
|
if (cuttingRecipeSO is null) return;
|
|
if (cuttingProgress < cuttingRecipeSO.cuttingProgressMax) return;
|
|
|
|
KitchenObjectSO outputKitchenObjectSO = GetOutputForInput(KitchenObject.KitchenObjectSO);
|
|
if (outputKitchenObjectSO is null) return;
|
|
|
|
KitchenObject.DestroyKitchenObject(KitchenObject);
|
|
KitchenObject.SpawnKitchenObject(outputKitchenObjectSO, this);
|
|
}
|
|
|
|
private bool HasRecipeWithInput(KitchenObjectSO inputKitchenObjectSO) => GetCuttingRecipeSOWithInput(inputKitchenObjectSO) is not null;
|
|
|
|
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);
|
|
|
|
public static void ResetStaticData() => OnAnyCut = null;
|
|
} |