2024-09-30 09:41:55 +00:00
|
|
|
using System;
|
2024-06-18 18:16:19 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using BlueWater.Utility;
|
|
|
|
using UnityEngine;
|
2024-09-12 04:17:34 +00:00
|
|
|
using Random = System.Random;
|
2024-06-18 18:16:19 +00:00
|
|
|
|
2024-07-02 18:27:56 +00:00
|
|
|
namespace BlueWater.Tycoons
|
2024-06-18 18:16:19 +00:00
|
|
|
{
|
2024-09-30 09:41:55 +00:00
|
|
|
[DefaultExecutionOrder(-1)]
|
2024-07-08 20:06:22 +00:00
|
|
|
public class CustomerTableController : MonoBehaviour
|
2024-06-18 18:16:19 +00:00
|
|
|
{
|
2024-09-30 09:41:55 +00:00
|
|
|
[SerializeField]
|
|
|
|
private Transform _customerTableRoot;
|
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
[SerializeField]
|
|
|
|
private List<CustomerTable> _customerTables;
|
2024-09-30 09:41:55 +00:00
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private List<CustomerTable> _activeCustomerTables;
|
2024-07-22 00:42:29 +00:00
|
|
|
|
|
|
|
private Random _random = new();
|
2024-09-30 09:41:55 +00:00
|
|
|
private AstarPath _astarPath;
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
_astarPath = FindAnyObjectByType<AstarPath>();
|
|
|
|
_customerTables = _customerTableRoot.GetComponentsInChildren<CustomerTable>().ToList();
|
|
|
|
|
|
|
|
for (var i = 0; i < _customerTables.Count; i++)
|
|
|
|
{
|
|
|
|
HideCustomerTable(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:16:19 +00:00
|
|
|
public void RegisterTable(CustomerTable table)
|
|
|
|
{
|
2024-09-30 09:41:55 +00:00
|
|
|
Utils.RegisterList(_activeCustomerTables, table);
|
|
|
|
_astarPath.Scan();
|
2024-06-18 18:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void UnregisterTable(CustomerTable table)
|
|
|
|
{
|
2024-09-30 09:41:55 +00:00
|
|
|
Utils.UnregisterList(_activeCustomerTables, table);
|
|
|
|
_astarPath.Scan();
|
2024-06-18 18:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public TableSeat FindEmptySeat()
|
|
|
|
{
|
2024-09-30 09:41:55 +00:00
|
|
|
var tableCount = _activeCustomerTables.Count;
|
2024-07-22 00:42:29 +00:00
|
|
|
var indices = new List<int>(Enumerable.Range(0, tableCount));
|
|
|
|
|
|
|
|
while (indices.Count > 0)
|
|
|
|
{
|
|
|
|
var randomIndex = _random.Next(indices.Count);
|
|
|
|
var tableIndex = indices[randomIndex];
|
|
|
|
indices.RemoveAt(randomIndex);
|
|
|
|
|
2024-09-30 09:41:55 +00:00
|
|
|
var emptySeat = _activeCustomerTables[tableIndex].FindEmptySeat();
|
2024-07-22 00:42:29 +00:00
|
|
|
if (emptySeat != null)
|
|
|
|
{
|
|
|
|
return emptySeat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2024-06-18 18:16:19 +00:00
|
|
|
}
|
2024-09-30 09:41:55 +00:00
|
|
|
|
|
|
|
public void ShowCustomerTable(int index)
|
|
|
|
{
|
|
|
|
_customerTables[index].gameObject.SetActive(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void HideCustomerTable(int index)
|
|
|
|
{
|
|
|
|
_customerTables[index].gameObject.SetActive(false);
|
|
|
|
}
|
2024-06-18 18:16:19 +00:00
|
|
|
}
|
|
|
|
}
|