sea-battle/Assets/Scripts/PlayerCursorUI.cs

178 lines
4.7 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class PlayerCursorUI : MonoBehaviour
{
public GameManager gameManager;
public Canvas parentCanvas; // Êàíâàñ ýòîãî èãðîêà
public int playerId = 1; // 1 èëè 2 (äëÿ óïðàâëåíèÿ)
private List<Button> buttons = new List<Button>();
private int currentIndex = 0;
private ButtonPinMapping upKey;
private ButtonPinMapping downKey;
private ButtonPinMapping leftKey;
private ButtonPinMapping rightKey;
private ButtonPinMapping selectKey;
private bool useJoystick = false;
void Start()
{
var player = GetComponentInParent<Player>();
if (player != null)
playerId = player.PlayerId;
DetectInputDevice();
BindControls();
RefreshButtons();
}
void DetectInputDevice()
{
// Ïðîâåðÿåì, åñòü ëè ïîäêëþ÷¸ííûå äæîéñòèêè
var joysticks = Input.GetJoystickNames();
if (joysticks.Length > 0 && !string.IsNullOrEmpty(joysticks[0]))
useJoystick = true;
else
useJoystick = false;
}
void BindControls()
{
if (!useJoystick) // êëàâèàòóðà
{
if (playerId == 1)
{
upKey = ButtonPinMapping.up1;
downKey = ButtonPinMapping.down1;
leftKey = ButtonPinMapping.left1;
rightKey = ButtonPinMapping.right1;
selectKey = ButtonPinMapping.action1;
}
else
{
upKey = ButtonPinMapping.up2;
downKey = ButtonPinMapping.down2;
leftKey = ButtonPinMapping.left2;
rightKey = ButtonPinMapping.right2;
selectKey = ButtonPinMapping.action2;
}
}
}
void OnEnable()
{
if (gameManager != null)
gameManager.OnInputModeChanged += HandleInputModeChanged;
}
void OnDisable()
{
if (gameManager != null)
gameManager.OnInputModeChanged -= HandleInputModeChanged;
}
void HandleInputModeChanged(GameManager.InputMode mode)
{
if (mode == GameManager.InputMode.UI)
{
RefreshButtons();
}
else
{
ClearHighlight();
}
}
void Update()
{
if (gameManager == null || gameManager.currentInputMode != GameManager.InputMode.UI)
return;
// Åñëè àêòèâíàÿ ïàíåëü ñìåíèëàñü èëè êíîïêà èñ÷åçëà — îáíîâëÿåì
if (buttons.Count == 0 || !buttons[currentIndex].gameObject.activeInHierarchy)
{
RefreshButtons();
}
if (buttons.Count == 0) return;
bool moved = false;
// Äâèæåíèå ïî êíîïêàì
if (In.GetButtonDown(upKey)) { MoveNext(-1); moved = true; }
if (In.GetButtonDown(downKey)) { MoveNext(1); moved = true; }
if (In.GetButtonDown (leftKey))
{
if (gameManager.currentState == GameState.ShipPlacement)
{
ClearHighlight();
gameManager.SetInputMode(GameManager.InputMode.ShipPlacement);
return;
}
else { MoveNext(-1); moved = true; }
}
if (In.GetButtonDown(rightKey)) { MoveNext(1); moved = true; }
if (moved) UpdateCursorHighlight();
// Âûáîð êíîïêè
if (In.GetButtonDown(selectKey))
{
if (buttons[currentIndex].interactable)
buttons[currentIndex].onClick.Invoke();
}
}
void MoveNext(int direction)
{
int startIndex = currentIndex;
do
{
currentIndex = (currentIndex + direction + buttons.Count) % buttons.Count;
}
while (!buttons[currentIndex].interactable && currentIndex != startIndex);
UpdateCursorHighlight();
}
void RefreshButtons()
{
buttons.Clear();
Button[] foundButtons = parentCanvas.GetComponentsInChildren<Button>(true);
foreach (Button btn in foundButtons)
{
if (btn.gameObject.activeInHierarchy)
buttons.Add(btn);
}
currentIndex = 0;
UpdateCursorHighlight();
}
void UpdateCursorHighlight()
{
for (int i = 0; i < buttons.Count; i++)
{
var highlight = buttons[i].GetComponent<UIButtonHighlight>();
if (highlight)
highlight.SetHighlighted(i == currentIndex);
}
}
void ClearHighlight()
{
foreach (var btn in buttons)
{
var highlight = btn.GetComponent<UIButtonHighlight>();
if (highlight)
highlight.SetHighlighted(false);
}
}
}