KitchenChaos/Assets/Scripts/DeliveryManager.cs

102 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using Random = UnityEngine.Random;
public class DeliveryManager : NetworkBehaviour {
private const float spawnRecipeTimerMax = 3f;
private const int waitingRecipesMax = 5;
[SerializeField] private RecipeListSO recipeListSO;
private float spawnRecipeTimer = 3f;
public static DeliveryManager Instance { get; private set; }
public List<RecipeSO> WaitingRecipeSOList { get; private set; }
public int SuccessfulRecipes { get; private set; }
public int Points { get; private set; }
private void Awake() {
Instance = this;
WaitingRecipeSOList = new List<RecipeSO>();
}
private void Update() {
if (!IsServer) return;
spawnRecipeTimer -= Time.deltaTime;
if (!(spawnRecipeTimer <= 0f)) return;
spawnRecipeTimer = spawnRecipeTimerMax;
if (WaitingRecipeSOList.Count >= waitingRecipesMax) return;
if (KitchenGameManager.Instance.IsGamePlaying()) {
int waitingRecipeSOIndex = Random.Range(0, recipeListSO.recipeSOList.Count);
SpawnNewWaitingRecipeClientRpc(waitingRecipeSOIndex);
}
}
public event EventHandler OnRecipeSpawned;
public event EventHandler OnRecipeCompleted;
public event EventHandler<RecipeEventArgs> OnRecipeSuccess;
public event EventHandler OnRecipeFailed;
[ClientRpc]
public void SpawnNewWaitingRecipeClientRpc(int waitingRecipeSOIndex) {
WaitingRecipeSOList.Add(recipeListSO.recipeSOList[waitingRecipeSOIndex]);
OnRecipeSpawned?.Invoke(this, EventArgs.Empty);
}
public void DeliverRecipe(PlateKitchenObject plateKitchenObject) {
for (int i = 0; i < WaitingRecipeSOList.Count; i++) {
if (WaitingRecipeSOList[i].KitchenObjectSOList.Count !=
plateKitchenObject.GetKitchenObjectSOList().Count) continue;
bool plateContentsMatchesRecipe = true;
foreach (KitchenObjectSO recipeKitchenObjectSO in WaitingRecipeSOList[i].KitchenObjectSOList) {
bool ingredientFound = false;
foreach (KitchenObjectSO plateKitchenObjectSO in plateKitchenObject.GetKitchenObjectSOList()) {
if (plateKitchenObjectSO != recipeKitchenObjectSO) continue;
ingredientFound = true;
break;
}
if (ingredientFound) continue;
plateContentsMatchesRecipe = false;
}
if (!plateContentsMatchesRecipe) {
DeliverIncorrectRecipeServerRpc();
continue;
}
DeliverCorrectRecipeServerRpc(i);
return;
}
}
[ServerRpc(RequireOwnership = false)]
public void DeliverIncorrectRecipeServerRpc() {
DeliverIncorrectRecipeClientRpc();
}
[ClientRpc]
public void DeliverIncorrectRecipeClientRpc() {
Points -= 1;
OnRecipeFailed?.Invoke(this, EventArgs.Empty);
}
[ServerRpc(RequireOwnership = false)]
public void DeliverCorrectRecipeServerRpc(int waitingRecipeSOListIndex) {
DeliverCorrectRecipeClientRpc(waitingRecipeSOListIndex);
}
[ClientRpc]
public void DeliverCorrectRecipeClientRpc(int waitingRecipeSOListIndex) {
Debug.Log($"Player delievered a correct {WaitingRecipeSOList[waitingRecipeSOListIndex].RecipeName}.");
SuccessfulRecipes++;
Points += WaitingRecipeSOList[waitingRecipeSOListIndex].Points;
WaitingRecipeSOList.RemoveAt(waitingRecipeSOListIndex);
OnRecipeCompleted?.Invoke(this, EventArgs.Empty);
OnRecipeSuccess?.Invoke(this, new RecipeEventArgs{ RecipeSO = WaitingRecipeSOList[waitingRecipeSOListIndex] });
}
}