CapersProject/Assets/02.Scripts/Tycoon/CustomerTableController.cs
2024-11-07 15:13:57 +09:00

78 lines
2.1 KiB
C#

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<CustomerTable> _customerTables;
[SerializeField]
private List<CustomerTable> _activeCustomerTables;
private Random _random = new();
private AstarPath _astarPath;
private void Awake()
{
_astarPath = FindAnyObjectByType<AstarPath>();
_customerTables = _customerTableRoot.GetComponentsInChildren<CustomerTable>().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<int>(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].Show();
}
public void HideCustomerTable(int index)
{
_customerTables[index].Hide();
}
}
}