326 lines
9.3 KiB
C#

using System;
using UnityEngine;
using UnityEngine.UI;
using static GameManager;
public class SettingsInput : MonoBehaviour
{
[SerializeField] private SettingsScreen screen;
public GameManager gameManager;
private bool isPaused = false;
private bool pushUp = false;
private bool leftHeld;
private bool rightHeld;
private bool downHeld;
private bool turn;
private bool isActivePanel = false;
private bool waitForKeyRelease = false;
private float unifiedTimer = 0f;
private float timerThreshold = 1.25f;
private float secretButtonTimer = 0f;
private ButtonPinMapping secretKey = ButtonPinMapping.secret1;
private ButtonPinMapping downKey = ButtonPinMapping.down1;
private ButtonPinMapping upKey = ButtonPinMapping.up1;
private ButtonPinMapping leftKey = ButtonPinMapping.left1;
private ButtonPinMapping rightKey = ButtonPinMapping.right1;
private ButtonPinMapping action1 = ButtonPinMapping.action1;
private ButtonPinMapping turnLeftKey = ButtonPinMapping.turnLeft1;
private ButtonPinMapping turnRightKey = ButtonPinMapping.turnRight1;
private bool IsSecretHeld() => In.GetButton(secretKey) || Input.GetKey(KeyCode.F5);
private bool IsSecretReleased() => In.GetButtonUp(secretKey) || Input.GetKeyUp(KeyCode.F5);
private SettingsPanelBase panel = null;
private string currentOverlayId = null;
private void OnEnable()
{
CreditSystem.OnInitialize += Subscribe;
}
private void OnDisable()
{
if (CreditSystem.onCoinAdded != null) CreditSystem.onCoinAdded.RemoveListener(View);
CreditSystem.OnInitialize -= Subscribe;
}
private void Subscribe()
{
CreditSystem.onCoinAdded.AddListener(View);
}
private void View(int __, int _)
{
screen.AcceptView();
}
void Start()
{
screen.Open();
}
void Update()
{
// --- Âõîä â íàñòðîéêè ÷åðåç ñåêðåòíóþ êíîïêó ---
if (IsSecretHeld())
{
secretButtonTimer += Time.deltaTime;
if (secretButtonTimer <= 3f) return;
if (isActivePanel)
{
ClosePanel();
waitForKeyRelease = true;
}
isPaused = !isPaused;
if (isPaused)
{
gameManager.SetState(GameState.Settings);
gameManager.SetInputMode(InputMode.Settings);
}
else
{
gameManager.ReturnToMainMenu();
if (gameManager.gameMode == GameMode.PlayerVsPlayer)
gameManager.opponentManager.ReturnToMainMenu();
}
secretButtonTimer = 0f;
}
if (IsSecretReleased())
{
secretButtonTimer = 0f;
return;
}
if (!isPaused) return;
if (isActivePanel)
{
HandelPanelInput();
return;
}
HandleSettingsLogic();
}
// -------------------------------------------------------
// Óïðàâëåíèå âíóòðè ïîäïàíåëè (TextInput è äð.)
// -------------------------------------------------------
private void HandelPanelInput()
{
if (waitForKeyRelease)
{
if (!In.GetButton(downKey))
waitForKeyRelease = false;
return;
}
// Action1 — çàêðûòü ïîäïàíåëü è âåðíóòüñÿ â íàñòðîéêè
if (In.GetButtonDown(action1))
{
ClosePanel();
return;
}
int dirX = 0;
if (panel.UsesContinuousInput)
{
if (In.GetButton(upKey)) dirX++;
if (In.GetButton(downKey)) dirX--;
}
else
{
if (In.GetButtonDown(upKey)) dirX++;
if (In.GetButtonDown(downKey)) dirX--;
}
panel.SwitchVertical(dirX);
bool changeAmount = In.GetButton(turnLeftKey) || In.GetButton(turnRightKey);
int dirY = 0;
if (In.GetButtonDown(leftKey)) dirY--;
if (In.GetButtonDown(rightKey)) dirY++;
panel.SwitchHorizontal(dirY, changeAmount);
}
// -------------------------------------------------------
// Îñíîâíàÿ ëîãèêà íàñòðîåê
// -------------------------------------------------------
private void HandleSettingsLogic()
{
// --- Ðåæèì âûáîðà îïöèè (âòîðîé øàã) ---
if (screen.IsChoosingOption)
{
// Left/Right — íàâèãàöèÿ ìåæäó îïöèÿìè (îíè ðàñïîëîæåíû ãîðèçîíòàëüíî)
if (In.GetButtonDown(leftKey)) screen.Switch(-1);
if (In.GetButtonDown(rightKey)) screen.Switch(+1);
// Down — ïîäòâåðäèòü è ñîõðàíèòü âûáðàííóþ îïöèþ
if (In.GetButtonDown(downKey))
screen.Accept();
// Action1 — îòìåíà áåç ñîõðàíåíèÿ, âåðíóòüñÿ ê ñïèñêó íàñòðîåê
if (In.GetButtonDown(action1))
screen.CancelOption();
return;
}
// --- Ðåæèì íàâèãàöèè ïî ñïèñêó íàñòðîåê (ïåðâûé øàã) ---
// Action1 = âûõîä èç ìåíþ íàñòðîåê
if (In.GetButtonDown(action1))
{
ExitSettings();
return;
}
SettingType currentType = screen.GetCurrentSettingType();
// Up / Down — íàâèãàöèÿ ïî ñïèñêó
if (In.GetButtonDown(upKey)) screen.Switch(-1);
if (In.GetButtonDown(downKey)) screen.Switch(+1);
// Panel / Action: óäåðæàíèå Right îòêðûâàåò ïàíåëü / âûïîëíÿåò ýêøí
if (currentType == SettingType.Panel || currentType == SettingType.Action)
{
if (In.GetButton(rightKey))
{
unifiedTimer += Time.deltaTime;
screen.UpdateProgressBar(unifiedTimer / timerThreshold);
if (unifiedTimer >= timerThreshold)
{
int id = screen.GetCurrentPanelId();
if (currentType == SettingType.Panel)
OpenPanel(id);
else
ExecuteAction(id);
ResetTimer();
}
}
else
{
ResetTimer();
}
return; // ó Panel/Action íåò Accept ÷åðåç Right-òàï
}
ResetTimer();
// Svitcher: Left / Right ïåðåêëþ÷àåò ìãíîâåííî
if (currentType == SettingType.Svitcher)
{
if (In.GetButtonDown(leftKey) || In.GetButtonDown(rightKey))
screen.ToggleSwitcher();
return;
}
// Normal: Right îòêðûâàåò ïàíåëü îïöèé (øàã 1)
if (In.GetButtonDown(rightKey))
screen.Accept();
}
// -------------------------------------------------------
// Îòêðûòèå / çàêðûòèå ïîäïàíåëè
// -------------------------------------------------------
private void OpenPanel(int idPanel)
{
if (panel != null) return;
if (!PanelRouter.Instance.GetPanelInfo(idPanel, out var foundPanel, out var overlayId))
return;
isActivePanel = true;
waitForKeyRelease = true;
panel = foundPanel;
currentOverlayId = overlayId;
if (!string.IsNullOrEmpty(currentOverlayId))
gameManager.uiManager.ShowOverlay(currentOverlayId, true, hideOtherOverlays: false);
gameManager.SetInputMode(InputMode.Settings);
panel.Initialize(idPanel);
}
private void ClosePanel()
{
isActivePanel = false;
panel.Accept();
if (!string.IsNullOrEmpty(currentOverlayId))
gameManager.uiManager.ShowOverlay(currentOverlayId, false);
panel = null;
currentOverlayId = null;
gameManager.SetState(GameState.Settings);
gameManager.SetInputMode(InputMode.Settings);
}
// -------------------------------------------------------
// Âûõîä èç íàñòðîåê
// -------------------------------------------------------
private void ExitSettings()
{
if (isActivePanel)
ClosePanel();
isPaused = false;
gameManager.ReturnToMainMenu();
if (gameManager.gameMode == GameMode.PlayerVsPlayer)
gameManager.opponentManager.ReturnToMainMenu();
}
// -------------------------------------------------------
// Ýêøåíû
// -------------------------------------------------------
private void ExecuteAction(int actionId)
{
switch (actionId)
{
case 100:
Zeroize(PrizeSystem.key_startsNul);
screen.AcceptView();
Debug.Log("Ñáðîñ ñòàòèñòèêè èãð âûïîëíåí");
break;
case 101:
Zeroize(PrizeSystem.key_prizesgivenNul);
screen.AcceptView();
Debug.Log("Ñáðîñ ñòàòèñòèêè ïðèçîâ âûïîëíåí");
break;
case 102:
DifficultyManager.SyncChanceToMax();
screen.AcceptView();
Debug.Log("Ñáðîñ ïðîãðåññà äî ñóïåðïðèçà");
break;
default:
Debug.LogWarning($"Íåèçâåñòíûé actionId: {actionId}");
break;
}
}
private void ResetTimer()
{
unifiedTimer = 0f;
screen.UpdateProgressBar(0f);
}
private void Zeroize(string key)
{
PlayerPrefs.SetInt(key, 0);
}
}