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

299 lines
8.2 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;
// Fallback — F5 ðàáîòàåò âñåãäà, äàæå åñëè COM ïåðåõâàòûâàåò In
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; // çàïîìèíàåì êàêîé overlay îòêðûëè
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();
}
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(turnLeftKey) || In.GetButton(turnRightKey);
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;
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);
}
/// <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);
}
}