sea-battle/Assets/Scripts/CameraPivotController.cs
2026-01-09 08:45:22 +03:00

100 lines
2.7 KiB
C#

using UnityEngine;
public class CameraPivotController : MonoBehaviour
{
[Header("Refs")]
public Transform pivot; // åñëè íå çàäàí — âîçüì¸ì ñâîé transform
public GameManager gameManager; // ÷òîáû áëîêèðîâàòü â UI
[Header("Input")]
public ButtonPinMapping modifierKey = ButtonPinMapping.turn1;
public ButtonPinMapping rotateLeftKey = ButtonPinMapping.left1;
public ButtonPinMapping rotateRightKey = ButtonPinMapping.right1;
[Header("Speeds")]
public float rotateSpeed = 90f; // ðó÷íîå âðàùåíèå
public float returnSpeed = 120; // âîçâðàò â "äîìîé"
private int countDown = 0;
private Quaternion _homeRotation;
TargetCursor targetCursor;
void Awake()
{
if (!pivot) pivot = transform;
// ïî óìîë÷àíèþ "äîì" — òåêóùèé ïîâîðîò íà ñòàðòå
_homeRotation = pivot.rotation;
}
// Òâîé ïðèâû÷íûé èíèöèàëèçàòîð
public void Setup(GameManager gm, Transform pivot, ButtonPinMapping left, ButtonPinMapping right, ButtonPinMapping modifier, float speed, TargetCursor tc)
{
this.gameManager = gm;
this.pivot = pivot ? pivot : transform;
this.rotateLeftKey = left;
this.rotateRightKey = right;
this.modifierKey = modifier;
this.rotateSpeed = speed;
// ôèêñèðóåì "äîì" ïîñëå ôèíàëüíîé óñòàíîâêè êàìåðû
_homeRotation = this.pivot.rotation;
targetCursor = tc;
}
public void CaptureHomeNow() => _homeRotation = pivot.rotation;
void Update()
{
if (gameManager != null && gameManager.currentInputMode == GameManager.InputMode.UI)
return;
if (In.GetButtonDown(modifierKey)) countDown++;
if (countDown >= 2)
{
pivot.rotation = Quaternion.RotateTowards(pivot.rotation, _homeRotation, rotateSpeed * Time.deltaTime);
if (pivot.rotation == _homeRotation)
{
Vector3 dir = pivot.rotation.eulerAngles;
targetCursor.SwitchButton(dir);
countDown = 0;
}
}
if (In.GetButton(modifierKey))
{
float dir = 0f;
if (In.GetButton(rotateRightKey))
{
dir -= 1f;
countDown = 0;
}
if (In.GetButton(rotateLeftKey))
{
dir += 1f;
countDown = 0;
}
if (dir != 0f && pivot)
pivot.Rotate(Vector3.up, dir * rotateSpeed * Time.deltaTime, Space.World);
}
if (In.GetButtonUp(modifierKey))
{
// èíâåíòèðîâàíèå óïðàâëåíèÿ
Vector3 dir = pivot.rotation.eulerAngles;
targetCursor.SwitchButton(dir);
}
}
}