KitchenChaos/Assets/Scripts/Counters/PlatesCounter.cs

53 lines
1.5 KiB
C#

using System;
using Unity.Netcode;
using UnityEngine;
public class PlatesCounter : BaseCounter {
[SerializeField] private float spawnPlateTimerMax = 10f;
[SerializeField] private int platesSpawnedAmountMax = 6;
[SerializeField] private KitchenObjectSO plateKitchenObjectSO;
private int platesSpawnedAmount;
private float spawnPlateTimer;
private void Update() {
if (!IsServer) return;
spawnPlateTimer += Time.deltaTime;
if (spawnPlateTimer > spawnPlateTimerMax) {
spawnPlateTimer = 0f;
if (KitchenGameManager.Instance.IsGamePlaying() && platesSpawnedAmount < platesSpawnedAmountMax)
SpawnPlateServerRpc();
}
}
[ServerRpc]
public void SpawnPlateServerRpc() => SpawnPlateClientRpc();
[ClientRpc]
public void SpawnPlateClientRpc() {
platesSpawnedAmount++;
OnPlateSpawned?.Invoke(this, EventArgs.Empty);
}
public event EventHandler OnPlateSpawned;
public event EventHandler OnPlateRemoved;
public override void Interact(Player player) {
if (player.KitchenObject != null) return;
if (platesSpawnedAmount == 0) return;
KitchenObject.SpawnKitchenObject(plateKitchenObjectSO, player);
InteractLogicServerRpc();
}
[ServerRpc(RequireOwnership = false)]
public void InteractLogicServerRpc() {
InteractLogicClientRpc();
}
[ClientRpc]
public void InteractLogicClientRpc() {
platesSpawnedAmount--;
OnPlateRemoved?.Invoke(this, EventArgs.Empty);
}
}