123 lines
3.8 KiB
C#
123 lines
3.8 KiB
C#
using UnityEngine;
|
|
|
|
public class CameraPivotController : MonoBehaviour
|
|
{
|
|
[Header("Refs")]
|
|
public Transform pivot; // åñëè íå çàäàí — âîçüì¸ì ñâîé transform
|
|
public GameManager gameManager; // ÷òîáû áëîêèðîâàòü â UI
|
|
|
|
|
|
[Header("Input")]
|
|
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 float lastTapTime = 0f;
|
|
[Tooltip("Îêíî âðåìåíè äëÿ ðàñïîçíàâàíèÿ äâîéíîãî íàæàòèÿ (ñåê)")]
|
|
public float doubleTapWindow = 0.4f;
|
|
private bool isReturning = false;
|
|
|
|
private Quaternion _homeRotation;
|
|
|
|
TargetCursor targetCursor;
|
|
|
|
void Awake()
|
|
{
|
|
if (!pivot) pivot = transform;
|
|
// ïî óìîë÷àíèþ "äîì" — òåêóùèé ïîâîðîò íà ñòàðòå
|
|
_homeRotation = pivot.rotation;
|
|
}
|
|
|
|
// Òâîé ïðèâû÷íûé èíèöèàëèçàòîð
|
|
// Setup: rotateLeft, rotateRight — ïèíû îòäåëüíûõ êíîïîê ïîâîðîòà
|
|
public void Setup(GameManager gm, Transform pivot, ButtonPinMapping left, ButtonPinMapping right, float speed, TargetCursor tc)
|
|
{
|
|
this.gameManager = gm;
|
|
this.pivot = pivot ? pivot : transform;
|
|
this.rotateLeftKey = left;
|
|
this.rotateRightKey = right;
|
|
this.rotateSpeed = speed;
|
|
// ôèêñèðóåì "äîì" ïîñëå ôèíàëüíîé óñòàíîâêè êàìåðû
|
|
_homeRotation = this.pivot.rotation;
|
|
targetCursor = tc;
|
|
|
|
// Ñáðîñèì ñîñòîÿíèå äâîéíîãî íàæàòèÿ ïðè (ïåðå)èíèöèàëèçàöèè
|
|
countDown = 0;
|
|
lastTapTime = 0f;
|
|
isReturning = false;
|
|
}
|
|
|
|
public void CaptureHomeNow() => _homeRotation = pivot.rotation;
|
|
|
|
void Update()
|
|
{
|
|
if (gameManager != null && gameManager.currentInputMode == GameManager.InputMode.UI)
|
|
return;
|
|
|
|
// Íå âðàùàòü êàìåðó ïðè ðåæèìå ðàññòàíîâêè êîðàáëåé (îãðàíè÷èâàåì êîíôëèêò ñ TargetCursor)
|
|
if (targetCursor != null && targetCursor.isPlacingShips)
|
|
return;
|
|
|
|
// Îáíàðóæåíèå äâîéíîãî íàæàòèÿ (time-window)
|
|
if (In.GetButtonDown(rotateLeftKey) || In.GetButtonDown(rotateRightKey))
|
|
{
|
|
if (Time.time - lastTapTime <= doubleTapWindow)
|
|
{
|
|
countDown++;
|
|
}
|
|
else
|
|
{
|
|
countDown = 1;
|
|
}
|
|
lastTapTime = Time.time;
|
|
if (countDown >= 2)
|
|
{
|
|
isReturning = true;
|
|
countDown = 0;
|
|
}
|
|
}
|
|
|
|
// Åñëè â ðåæèìå âîçâðàòà — äâèãàåì êàìåðó ê äîìó
|
|
if (isReturning)
|
|
{
|
|
pivot.rotation = Quaternion.RotateTowards(pivot.rotation, _homeRotation, returnSpeed * Time.deltaTime);
|
|
if (Quaternion.Angle(pivot.rotation, _homeRotation) < 0.01f)
|
|
{
|
|
Vector3 dir = pivot.rotation.eulerAngles;
|
|
if (targetCursor != null) targetCursor.SwitchButton(dir);
|
|
isReturning = false;
|
|
}
|
|
}
|
|
|
|
// Ðó÷íîå âðàùåíèå ïðè íàæàòèè îòäåëüíûõ êíîïîê
|
|
if (In.GetButton(rotateLeftKey) || In.GetButton(rotateRightKey))
|
|
{
|
|
float dir = 0f;
|
|
if (In.GetButton(rotateRightKey))
|
|
{
|
|
dir -= 1f;
|
|
}
|
|
|
|
if (In.GetButton(rotateLeftKey))
|
|
{
|
|
dir += 1f;
|
|
}
|
|
|
|
if (dir != 0f && pivot)
|
|
pivot.Rotate(Vector3.up, dir * rotateSpeed * Time.deltaTime, Space.World);
|
|
}
|
|
|
|
// Ïðè îòïóñêàíèè ëþáîé èç êíîïîê ïîâîðîòà — îáíîâëÿåì êíîïêè â òàðãåò-êóðñîð
|
|
if (In.GetButtonUp(rotateLeftKey) || In.GetButtonUp(rotateRightKey))
|
|
{
|
|
Vector3 dir = pivot.rotation.eulerAngles;
|
|
if (targetCursor != null) targetCursor.SwitchButton(dir);
|
|
}
|
|
}
|
|
}
|