using System; using System.Collections.Generic; using System.Linq; using BlueWater.Utility; using UnityEngine; using Random = System.Random; namespace BlueWater.Tycoons { [DefaultExecutionOrder(-1)] public class CustomerTableController : MonoBehaviour { [SerializeField] private Transform _customerTableRoot; [SerializeField] private List _customerTables; [SerializeField] private List _activeCustomerTables; private Random _random = new(); private AstarPath _astarPath; private void Awake() { _astarPath = FindAnyObjectByType(); _customerTables = _customerTableRoot.GetComponentsInChildren().ToList(); for (var i = 0; i < _customerTables.Count; i++) { HideCustomerTable(i); } } public void RegisterTable(CustomerTable table) { Utils.RegisterList(_activeCustomerTables, table); _astarPath.Scan(); } public void UnregisterTable(CustomerTable table) { Utils.UnregisterList(_activeCustomerTables, table); _astarPath.Scan(); } public TableSeat FindEmptySeat() { var tableCount = _activeCustomerTables.Count; var indices = new List(Enumerable.Range(0, tableCount)); while (indices.Count > 0) { var randomIndex = _random.Next(indices.Count); var tableIndex = indices[randomIndex]; indices.RemoveAt(randomIndex); var emptySeat = _activeCustomerTables[tableIndex].FindEmptySeat(); if (emptySeat != null) { return emptySeat; } } return null; } public void ShowCustomerTable(int index) { _customerTables[index].gameObject.SetActive(true); } public void HideCustomerTable(int index) { _customerTables[index].gameObject.SetActive(false); } } }