sea-battle/Assets/Scripts/Settings/SettingsInput.cs

251 lines
6.7 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 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 turn1 = ButtonPinMapping.turn1;
private SettingsPanelBase panel = null ; // ïåðåêëþ÷àøêà ïàíåëè
private void OnEnable()
{
ComWrapper.OnInitialize += Subscribe;
}
private void OnDisable()
{
ComWrapper.onCoinAdded.RemoveListener(View);
ComWrapper.OnInitialize -= Subscribe;
}
private void Subscribe()
{
ComWrapper.onCoinAdded.AddListener(View);
}
private void View(int __,int _)
{
screen.AcceptView();
}
void Start()
{
screen.Open();
}
void Update()
{
// --- Ïåðåêëþ÷åíèå ïàóçû / âõîä â íàñòðîéêè ---
if (In.GetButtonDown(secretKey))
{
if (gameManager.currentState != GameState.MainMenu && gameManager.currentState != GameState.Setting)
return;
isPaused = !isPaused;
gameManager.SetState(isPaused ? GameState.Setting : GameState.MainMenu);
gameManager.SetInputMode(isPaused ? InputMode.Settings : InputMode.UI);
}
if (!isPaused) return;
if (isActivePanel)
{
HandelPanelInput();
return;
}
// --- Èíà÷å îáû÷íàÿ ëîãèêà íàñòðîåê ---
HandleSettingsLogic();
}
private void HandelPanelInput()
{
// Æä¸ì îòïóñêàíèÿ êíîïêè ïîñëå îòêðûòèÿ ïàíåëè
if (waitForKeyRelease)
{
if (!In.GetButton(downKey))
{
waitForKeyRelease = false; // Êíîïêà îòïóùåíà, ðàçðåøàåì ââîä
}
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(turn1);
int dirY = 0;
if (In.GetButtonDown(leftKey)) dirY--;
if (In.GetButtonDown(rightKey)) dirY++;
panel.SwitchHorizontal(dirY, changeAmount);
if (In.GetButtonDown(action1))
{
ClosePanel();
}
}
private void HandleSettingsLogic()
{
// Îáðàáîòêà óäåðæàíèÿ êíîïêè äëÿ îòêðûòèÿ ïàíåëè èëè âûïîëíåíèÿ ýêøåíà
if (In.GetButton(downKey) && screen.CanOpenPanel())
{
unifiedTimer += Time.deltaTime;
float fillAmount = unifiedTimer / timerThreshold;
screen.UpdateProgressBar(fillAmount); // îáíîâëÿåì ïðîãðåññ-áàð â íàñòðîéêå
if (unifiedTimer >= timerThreshold)
{
int id = screen.GetCurrentPanelId();
SettingType currentType = screen.GetCurrentSettingType();
if (currentType == SettingType.Panel)
OpenPanel(id);
else if (currentType == SettingType.Action)
ExecuteAction(id);
ResetTimer();
}
return;
}
// Ñáðîñ òàéìåðà åñëè êíîïêà íå çàæàòà
ResetTimer();
// Îáû÷íîå óïðàâëåíèå íàñòðîéêàìè
int direction = 0;
if (In.GetButtonDown(leftKey)) direction--;
if (In.GetButtonDown(rightKey)) direction++;
if (direction != 0)
screen.Switch(direction);
// Ïåðåêëþ÷åíèå ñëàéäåðà äëÿ íàñòðîåê òèïà Svitcher
if (screen.GetCurrentSettingType() == SettingType.Svitcher)
{
if (In.GetButtonDown(downKey))
{
screen.ToggleSwitcher();
}
}
else
{
// Îáû÷íûé Accept òîëüêî äëÿ íå-ñâèò÷åðîâ
if (In.GetButtonDown(downKey) && screen.isActiveAndEnabled)
screen.Accept();
}
}
private void OpenPanel(int idPanel)
{
if (panel != null) return;
isActivePanel = true;
waitForKeyRelease = true; // Áëîêèðóåì ââîä äî îòïóñêàíèÿ êíîïêè
panel = PanelRouter.Instance.GetPanel(idPanel);
gameManager.SetState(panel.State);
gameManager.SetInputMode(InputMode.Settings);
panel.Initialize(idPanel);
}
private void ClosePanel()
{
isActivePanel = false;
panel.Accept();
panel = null;
gameManager.SetState(GameState.Setting);
gameManager.SetInputMode(InputMode.Settings);
}
/// <summary>
/// Âûïîëíÿåò ýêøåí ïî ID (äëÿ íàñòðîåê òèïà Action)
/// </summary>
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);
}
}