Afterparty/Assets/Scripts/DoorController.cs
2026-01-11 17:04:23 +03:00

93 lines
2.4 KiB
C#

using UnityEngine;
using System.Collections;
public class DoorController : MonoBehaviour, IInteractable
{
[Header("Settings")]
[SerializeField] private float openAngle = 90f;
[SerializeField] private float smoothSpeed = 2f;
[SerializeField] private bool isLocked = false;
[Header("Key Settings")]
[SerializeField] private ItemData keyRequired;
private bool _isOpen = false;
private Quaternion _closedRotation;
private Quaternion _openRotation;
private Coroutine _animationCoroutine;
private void Start()
{
_closedRotation = transform.localRotation;
_openRotation = _closedRotation * Quaternion.Euler(0, openAngle, 0);
}
public void Interact()
{
if (_isOpen)
{
Close();
}
else
{
TryOpen();
}
}
public string GetDescription()
{
if (isLocked) return "Заперто (Нужен ключ)";
return _isOpen ? "Закрыть" : "Открыть";
}
private void TryOpen()
{
if (isLocked)
{
if (keyRequired != null && InventorySystem.Instance.HasItem(keyRequired))
{
Debug.Log("Ключ подошел! Дверь открыта.");
isLocked = false;
Open();
}
else
{
Debug.Log("Нужен ключ!");
}
}
else
{
Open();
}
}
private void Open()
{
_isOpen = true;
StopAnimation();
_animationCoroutine = StartCoroutine(AnimateDoor(_openRotation));
}
private void Close()
{
_isOpen = false;
StopAnimation();
_animationCoroutine = StartCoroutine(AnimateDoor(_closedRotation));
}
private void StopAnimation()
{
if (_animationCoroutine != null) StopCoroutine(_animationCoroutine);
}
// Универсальная корутина для плавного вращения
private IEnumerator AnimateDoor(Quaternion targetRotation)
{
while (Quaternion.Angle(transform.localRotation, targetRotation) > 0.1f)
{
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, Time.deltaTime * smoothSpeed);
yield return null;
}
transform.localRotation = targetRotation;
}
}