164 lines
4.7 KiB
C#
164 lines
4.7 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
/// <summary>
|
|
/// Терминал лифта в 3D мире.
|
|
/// Игрок может подойти, увидеть подсказку и нажать [E] для открытия UI цифрового ввода кода.
|
|
/// </summary>
|
|
public class ElevatorTerminal : MonoBehaviour
|
|
{
|
|
[Header("Дистанция взаимодействия")]
|
|
public float interactionDistance = 3.8f;
|
|
|
|
[Header("Индикаторы")]
|
|
public Light statusLight;
|
|
public MeshRenderer screenRenderer;
|
|
|
|
private static Transform cachedPlayerTransform;
|
|
|
|
private void Start()
|
|
{
|
|
LocatePlayer();
|
|
UpdateLightColor(false);
|
|
|
|
if (LevelCodeManager.Instance != null)
|
|
{
|
|
LevelCodeManager.Instance.OnElevatorUnlocked += HandleUnlocked;
|
|
}
|
|
|
|
var col = GetComponent<Collider>();
|
|
if (col == null)
|
|
{
|
|
var sc = gameObject.AddComponent<SphereCollider>();
|
|
sc.isTrigger = true;
|
|
sc.radius = interactionDistance;
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (LevelCodeManager.Instance != null)
|
|
{
|
|
LevelCodeManager.Instance.OnElevatorUnlocked -= HandleUnlocked;
|
|
}
|
|
}
|
|
|
|
private Transform LocatePlayer()
|
|
{
|
|
if (cachedPlayerTransform != null) return cachedPlayerTransform;
|
|
|
|
var pm = Object.FindAnyObjectByType<PlayerMovement>();
|
|
if (pm != null) { cachedPlayerTransform = pm.transform; return cachedPlayerTransform; }
|
|
|
|
var ph = Object.FindAnyObjectByType<PlayerHealth>();
|
|
if (ph != null) { cachedPlayerTransform = ph.transform; return cachedPlayerTransform; }
|
|
|
|
var pTag = GameObject.FindWithTag("Player");
|
|
if (pTag != null) { cachedPlayerTransform = pTag.transform; return cachedPlayerTransform; }
|
|
|
|
var pName = GameObject.Find("Player");
|
|
if (pName != null) { cachedPlayerTransform = pName.transform; return cachedPlayerTransform; }
|
|
|
|
if (Camera.main != null) { return Camera.main.transform; }
|
|
|
|
return null;
|
|
}
|
|
|
|
private void HandleUnlocked()
|
|
{
|
|
UpdateLightColor(true);
|
|
}
|
|
|
|
private void UpdateLightColor(bool isUnlocked)
|
|
{
|
|
if (statusLight != null)
|
|
{
|
|
statusLight.color = isUnlocked ? new Color(0.2f, 1f, 0.4f) : new Color(1f, 0.85f, 0.1f);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (ElevatorKeypadUI.Instance != null && ElevatorKeypadUI.Instance.IsOpen) return;
|
|
if (Time.timeScale == 0f) return;
|
|
|
|
Transform player = LocatePlayer();
|
|
if (player == null) return;
|
|
|
|
float dist3D = Vector3.Distance(transform.position, player.position);
|
|
float distXZ = Vector2.Distance(new Vector2(transform.position.x, transform.position.z),
|
|
new Vector2(player.position.x, player.position.z));
|
|
|
|
bool inRange = dist3D <= interactionDistance || distXZ <= (interactionDistance * 0.9f);
|
|
|
|
if (inRange)
|
|
{
|
|
if (CheckInteractInput())
|
|
{
|
|
Interact();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnTriggerStay(Collider other)
|
|
{
|
|
if (ElevatorKeypadUI.Instance != null && ElevatorKeypadUI.Instance.IsOpen) return;
|
|
if (Time.timeScale == 0f) return;
|
|
|
|
if (other.CompareTag("Player") || other.GetComponentInParent<PlayerMovement>() != null)
|
|
{
|
|
if (CheckInteractInput())
|
|
{
|
|
Interact();
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool CheckInteractInput()
|
|
{
|
|
var keyboard = Keyboard.current;
|
|
if (keyboard != null)
|
|
{
|
|
if (keyboard.eKey.wasPressedThisFrame ||
|
|
keyboard.fKey.wasPressedThisFrame ||
|
|
keyboard.enterKey.wasPressedThisFrame)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
if (Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.F) || Input.GetKeyDown(KeyCode.Return))
|
|
{
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
return false;
|
|
}
|
|
|
|
public void Interact()
|
|
{
|
|
bool isUnlocked = LevelCodeManager.Instance != null && LevelCodeManager.Instance.IsElevatorUnlocked;
|
|
|
|
if (isUnlocked)
|
|
{
|
|
if (LevelSuccessUI.Instance != null)
|
|
{
|
|
LevelSuccessUI.Instance.ShowSuccessScreen();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (ElevatorKeypadUI.Instance != null)
|
|
{
|
|
ElevatorKeypadUI.Instance.OpenKeypad();
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("[ElevatorTerminal] ElevatorKeypadUI.Instance is NULL!");
|
|
}
|
|
}
|
|
}
|
|
}
|