using UnityEngine;
///
/// Панель правил — показывает набор страниц (GameObject) одну за другой.
/// Навигация: left → предыдущая, right → следующая, action → закрыть.
/// Кнопки читаются по playerId (берётся из родительского Player или GameManager).
/// Не нужно ничего делать с PlayerCursorUI — страницы не содержат Button-компонентов.
///
public class RulesPanel : MonoBehaviour
{
[Header("Страницы (назначьте дочерние GameObject в нужном порядке)")]
[SerializeField] private GameObject[] pages;
[Header("Ссылки")]
[SerializeField] private GameManager gameManager;
// физические кнопки конкретного игрока
private ButtonPinMapping leftKey;
private ButtonPinMapping rightKey;
private ButtonPinMapping actionKey;
private int currentPage;
private void Awake()
{
// определяем playerId: сначала через GameManager, затем через Player в родителе
int pid = 1;
if (gameManager != null)
pid = gameManager.playerId;
else
{
var player = GetComponentInParent();
if (player != null) pid = player.PlayerId;
}
if (pid == 1)
{
leftKey = ButtonPinMapping.left1;
rightKey = ButtonPinMapping.right1;
actionKey = ButtonPinMapping.action1;
}
else
{
leftKey = ButtonPinMapping.left2;
rightKey = ButtonPinMapping.right2;
actionKey = ButtonPinMapping.action2;
}
}
// ─── Public API ───────────────────────────────────────────────
///
/// Срабатывает автоматически когда UIManager делает SetActive(true).
/// Всегда начинаем с первой страницы.
///
private void OnEnable()
{
currentPage = 0;
ShowPage(currentPage);
}
/// Закрыть панель. Вызывается по кнопке action.
public void Close()
{
gameObject.SetActive(false);
}
// ─── Navigation ───────────────────────────────────────────────
private void Update()
{
if (In.GetButtonDown(leftKey))
{
PrevPage();
return;
}
if (In.GetButtonDown(rightKey))
{
NextPage();
return;
}
if (In.GetButtonDown(actionKey))
{
Close();
}
}
private void NextPage()
{
if (pages == null || pages.Length == 0) return;
int next = (currentPage + 1) % pages.Length;
ShowPage(next);
}
private void PrevPage()
{
if (pages == null || pages.Length == 0) return;
int prev = (currentPage - 1 + pages.Length) % pages.Length;
ShowPage(prev);
}
private void ShowPage(int index)
{
if (pages == null) return;
for (int i = 0; i < pages.Length; i++)
{
if (pages[i] != null)
pages[i].SetActive(i == index);
}
currentPage = index;
}
}