114 lines
3.3 KiB
C#
114 lines
3.3 KiB
C#
using UnityEngine;
|
|
using Unheimlich.Interactions.Core;
|
|
using Unheimlich.Interactions.FloatingWords;
|
|
|
|
namespace Unheimlich.Gameplay.Items
|
|
{
|
|
/// <summary>
|
|
/// Interactable item that can be picked up and examined closely by the player.
|
|
/// Temporarily moves to inspection position and allows rotation via look input.
|
|
/// </summary>
|
|
[RequireComponent(typeof(Collider), typeof(Rigidbody))]
|
|
public class InspectableItem : InteractableBase
|
|
{
|
|
[Header("Inspection Settings")]
|
|
[SerializeField] private float _inspectionDistance = 1f;
|
|
[SerializeField] private float _rotationSpeed = 100f;
|
|
|
|
[Header("Animation Settings")]
|
|
[SerializeField] private bool _blockInteractionDuringAnimation = true;
|
|
|
|
private Vector3 _originalPosition;
|
|
private Quaternion _originalRotation;
|
|
private Transform _originalParent;
|
|
private Rigidbody _rigidbody;
|
|
private bool _isAnimationPlaying = false;
|
|
private ObjectDescriptionTrigger _objectDescriptionTrigger;
|
|
private LevitatingObject _levitatingObject;
|
|
|
|
public bool IsAnimationPlaying => _isAnimationPlaying;
|
|
|
|
private void Start()
|
|
{
|
|
_rigidbody = GetComponent<Rigidbody>();
|
|
_rigidbody.isKinematic = true;
|
|
|
|
_originalPosition = transform.position;
|
|
_originalRotation = transform.rotation;
|
|
_originalParent = transform.parent;
|
|
|
|
_objectDescriptionTrigger = GetComponent<ObjectDescriptionTrigger>();
|
|
_levitatingObject = GetComponent<LevitatingObject>();
|
|
}
|
|
|
|
public override void Interact(GameObject interactor)
|
|
{
|
|
if (_blockInteractionDuringAnimation)
|
|
{
|
|
if (_isAnimationPlaying)
|
|
return;
|
|
|
|
if (_objectDescriptionTrigger != null &&
|
|
_objectDescriptionTrigger.IsInMergeOnceMode &&
|
|
_objectDescriptionTrigger.IsAnimationActive)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (PlayerInspectionHandler.Instance != null)
|
|
PlayerInspectionHandler.Instance.ToggleInspection(this);
|
|
}
|
|
|
|
public void SetAnimationState(bool isPlaying)
|
|
{
|
|
_isAnimationPlaying = isPlaying;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Prepares the item for inspection by parenting it to the hold point and making it kinematic.
|
|
/// </summary>
|
|
public void PrepareForInspection(Transform holdPoint)
|
|
{
|
|
_originalPosition = transform.position;
|
|
_originalRotation = transform.rotation;
|
|
_originalParent = transform.parent;
|
|
|
|
_rigidbody.isKinematic = true;
|
|
transform.SetParent(holdPoint);
|
|
transform.localPosition = Vector3.forward * _inspectionDistance;
|
|
transform.localRotation = Quaternion.identity;
|
|
|
|
if (_levitatingObject != null)
|
|
{
|
|
_levitatingObject.UpdateBasePosition();
|
|
}
|
|
}
|
|
|
|
public void Release()
|
|
{
|
|
transform.SetParent(_originalParent);
|
|
transform.position = _originalPosition;
|
|
transform.rotation = _originalRotation;
|
|
|
|
_rigidbody.isKinematic = true;
|
|
|
|
if (_levitatingObject != null)
|
|
{
|
|
_levitatingObject.UpdateBasePosition();
|
|
}
|
|
}
|
|
|
|
public float RotationSpeed => _rotationSpeed;
|
|
|
|
private void OnEnable()
|
|
{
|
|
LeanTween.init();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
LeanTween.cancel(gameObject);
|
|
}
|
|
}
|
|
} |