TO SQUASH: menu key movement.
This commit is contained in:
@@ -10,26 +10,100 @@ public class MenuKey : MonoBehaviour
|
||||
|
||||
private Animator animator;
|
||||
|
||||
private int currentRow = 0;
|
||||
|
||||
private int targetRow = 0;
|
||||
|
||||
private bool isTranslating = false;
|
||||
|
||||
private float translationElapsed = 0f;
|
||||
|
||||
private float translationStartY = 0f;
|
||||
|
||||
private float translationTargetY = 0f;
|
||||
|
||||
private MenuCallbackHolder callbackHolder;
|
||||
|
||||
private static float animationDuration = 0.45f;
|
||||
|
||||
private static float[] rowPositions = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
audioSource = GetComponent<AudioSource>();
|
||||
animator = GetComponent<Animator>();
|
||||
callbackHolder = new MenuCallbackHolder();
|
||||
|
||||
// Precompute the row positions based on the number of rows and the desired spacing.
|
||||
for (int i = 0; i < rowPositions.Length; i++)
|
||||
{
|
||||
rowPositions[i] = 1.462f - (i * 0.69f);
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
if (!isTranslating && currentRow != targetRow)
|
||||
{
|
||||
animator.SetTrigger("buttonChanged");
|
||||
|
||||
translationElapsed = 0f;
|
||||
translationStartY = rowPositions[currentRow];
|
||||
translationTargetY = rowPositions[targetRow];
|
||||
isTranslating = true;
|
||||
}
|
||||
|
||||
if (isTranslating)
|
||||
{
|
||||
translationElapsed += Time.deltaTime;
|
||||
|
||||
float t = Mathf.Clamp01(translationElapsed / animationDuration);
|
||||
float easedT = EaseOutQuad(t);
|
||||
float y = Mathf.Lerp(translationStartY, translationTargetY, easedT);
|
||||
|
||||
Vector3 position = transform.position;
|
||||
position.y = y;
|
||||
transform.position = position;
|
||||
|
||||
if (t >= 1f)
|
||||
{
|
||||
currentRow = targetRow;
|
||||
isTranslating = false;
|
||||
|
||||
position.y = translationTargetY;
|
||||
transform.position = position;
|
||||
|
||||
animator.ResetTrigger("buttonChanged");
|
||||
animator.SetTrigger("inserted");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void playKeyTurnOpen(int _)
|
||||
public void PlayKeyTurnOpen(int _)
|
||||
{
|
||||
audioSource.PlayOneShot(keyTurnOpen);
|
||||
}
|
||||
|
||||
public void playKeyTurnClose(int _)
|
||||
public void PlayKeyTurnClose(int _)
|
||||
{
|
||||
audioSource.PlayOneShot(keyTurnClose);
|
||||
}
|
||||
|
||||
public void TranslateTo(int row)
|
||||
{
|
||||
targetRow = row;
|
||||
}
|
||||
|
||||
public void InvokeCallback(int _)
|
||||
{
|
||||
callbackHolder.InvokeCallback(currentRow);
|
||||
}
|
||||
|
||||
private float EaseOutQuad(float t)
|
||||
{
|
||||
// Quadratic easing out - deceleration until the end
|
||||
return 1 - (1 - t) * (1 - t);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user