89 lines
2.4 KiB
C#
89 lines
2.4 KiB
C#
using UnityEngine;
|
|
|
|
public sealed class FurnaceMetalLevelView : MonoBehaviour
|
|
{
|
|
[SerializeField] private FurnaceController furnace;
|
|
[SerializeField] private Transform metalVisual;
|
|
[SerializeField] private Transform emptyLocalPosition;
|
|
[SerializeField] private Transform fullLocalPosition;
|
|
[SerializeField, Min(0f)] private float movementSpeed = 1.5f;
|
|
|
|
private Vector3 targetLocalPosition;
|
|
|
|
private void Reset()
|
|
{
|
|
furnace = GetComponentInParent<FurnaceController>();
|
|
metalVisual = transform;
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (furnace == null)
|
|
furnace = GetComponentInParent<FurnaceController>();
|
|
|
|
if (furnace != null)
|
|
furnace.Changed += RefreshTarget;
|
|
|
|
RefreshTarget();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (furnace != null)
|
|
furnace.Changed -= RefreshTarget;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (metalVisual == null)
|
|
return;
|
|
|
|
metalVisual.localPosition = movementSpeed <= 0f
|
|
? targetLocalPosition
|
|
: Vector3.MoveTowards(
|
|
metalVisual.localPosition,
|
|
targetLocalPosition,
|
|
movementSpeed * Time.deltaTime);
|
|
}
|
|
|
|
private void RefreshTarget()
|
|
{
|
|
float normalized = furnace != null ? furnace.MetalNormalized : 0f;
|
|
targetLocalPosition = Vector3.Lerp(
|
|
GetLocalPoint(emptyLocalPosition),
|
|
GetLocalPoint(fullLocalPosition),
|
|
normalized);
|
|
}
|
|
|
|
private Vector3 GetLocalPoint(Transform point)
|
|
{
|
|
if (point == null || metalVisual == null)
|
|
return metalVisual != null ? metalVisual.localPosition : Vector3.zero;
|
|
|
|
Transform space = metalVisual.parent;
|
|
return space != null
|
|
? space.InverseTransformPoint(point.position)
|
|
: point.position;
|
|
}
|
|
|
|
[ContextMenu("Preview/Empty")]
|
|
private void PreviewEmpty() => ApplyPreview(0f);
|
|
|
|
[ContextMenu("Preview/Half")]
|
|
private void PreviewHalf() => ApplyPreview(0.5f);
|
|
|
|
[ContextMenu("Preview/Full")]
|
|
private void PreviewFull() => ApplyPreview(1f);
|
|
|
|
private void ApplyPreview(float normalized)
|
|
{
|
|
if (metalVisual == null)
|
|
return;
|
|
|
|
metalVisual.localPosition = Vector3.Lerp(
|
|
GetLocalPoint(emptyLocalPosition),
|
|
GetLocalPoint(fullLocalPosition),
|
|
normalized);
|
|
}
|
|
}
|