using System; using System.Collections; using UnityEngine; using UnityEngine.Events; using UnityEngine.InputSystem; using RR.Common.Interfaces; namespace RR.Game2D.Entities { [RequireComponent(typeof(Collider2D))] public class Lever : MonoBehaviour, IActionable { [Header("Управление")] [SerializeField] private InputActionReference activateKey; [Header("Настройки частей рычага")] [SerializeField] private Transform stick; [SerializeField] private Transform leverBase; [Header("Настройки поворота рычага")] [SerializeField] private float rotationAngle = 90f; [SerializeField] private float rotationSpeed = 5f; [Header("События после активации")] [SerializeField] private UnityEvent onActivate; private Quaternion _initialRotation; private Collider2D _collider; private bool _isActivated; private bool _canBeActivated = false; private void Start() { _isActivated = false; // onActivate = new UnityEvent(); if (_collider == null) _collider = GetComponent(); if (_collider == null) _collider = gameObject.AddComponent(); if (!_collider.isTrigger) _collider.isTrigger = true; if (stick == null) { Debug.LogError("Lever: Stick transform is not assigned!", this); } else { _initialRotation = stick.rotation; } // activateKey.action.performed += OnActivateKey; activateKey.action.started += OnActivateKey; activateKey.action.Enable(); } private void OnTriggerEnter2D(Collider2D other) { if (!other.CompareTag("Player")) return; _canBeActivated = true; } private void OnTriggerExit2D(Collider2D other) { if (!other.CompareTag("Player")) return; _canBeActivated = false; } public void PerformAction() { StopAllCoroutines(); StartCoroutine(RotateLever()); onActivate?.Invoke(); } private IEnumerator RotateLever() { // initialRotation = stick.rotation; Quaternion targetRotation = _initialRotation * Quaternion.Euler(0f, 0f, rotationAngle); float elapsedTime = 0f; while (elapsedTime < rotationSpeed) { stick.rotation = Quaternion.Slerp(_initialRotation, targetRotation, elapsedTime / rotationSpeed); elapsedTime += Time.deltaTime; yield return null; } stick.rotation = targetRotation; _isActivated = true; } private void OnActivateKey(InputAction.CallbackContext obj) { if (!_canBeActivated) return; PerformAction(); } } }