128 lines
4.5 KiB
C#
128 lines
4.5 KiB
C#
using UnityEngine;
|
||
|
||
/// <summary>
|
||
/// устаревший функционал, сейчас используется CameraController
|
||
/// </summary>
|
||
|
||
public class CameraPivotController : MonoBehaviour
|
||
{
|
||
[Header("Refs")]
|
||
public Transform pivot; // если не задан — возьмём свой transform
|
||
public GameManager gameManager; // чтобы блокировать в UI
|
||
|
||
|
||
[Header("Input")]
|
||
public ButtonPinMapping rotateLeftKey;
|
||
public ButtonPinMapping rotateRightKey;
|
||
|
||
|
||
[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;
|
||
|
||
private TargetCursor targetCursor;
|
||
public TargetCursor 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;
|
||
|
||
// Не вращать камеру при расстановке, если игрок не удерживает кнопку поворота
|
||
// if (targetCursor != null && targetCursor.isPlacingShips && !targetCursor.IsCameraRotMode)
|
||
// 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);
|
||
}
|
||
}
|
||
}
|