TO SQUASH: menu key movement.
This commit is contained in:
@@ -5,7 +5,9 @@ public class MenuButton : MonoBehaviour
|
||||
{
|
||||
public bool isButtonPressed = false;
|
||||
|
||||
public Animator keyAnimator;
|
||||
public MenuKey key;
|
||||
|
||||
public int row;
|
||||
|
||||
private SpriteRenderer spriteRenderer;
|
||||
|
||||
@@ -33,7 +35,7 @@ public class MenuButton : MonoBehaviour
|
||||
if (boxCollider.OverlapPoint(worldMousePosition))
|
||||
{
|
||||
isButtonPressed = true;
|
||||
keyAnimator.SetTrigger("buttonChanged");
|
||||
key.TranslateTo(row);
|
||||
} else {
|
||||
isButtonPressed = false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class MenuCallbackHolder
|
||||
{
|
||||
private System.Action[] callbacks;
|
||||
|
||||
public MenuCallbackHolder()
|
||||
{
|
||||
callbacks = new System.Action[]
|
||||
{
|
||||
PlayButtonCallback,
|
||||
InstruccionsButtonCallback,
|
||||
SetupButtonCallback,
|
||||
ZapButtonCallback,
|
||||
AboutButtonCallback,
|
||||
QuitButtonCallback
|
||||
};
|
||||
}
|
||||
|
||||
public void InvokeCallback(int index)
|
||||
{
|
||||
if (index >= 0 && index < callbacks.Length)
|
||||
{
|
||||
callbacks[index].Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"MenuCallbackHolder: Invalid callback index {index}");
|
||||
}
|
||||
}
|
||||
|
||||
public void PlayButtonCallback()
|
||||
{
|
||||
Debug.Log("Play button pressed");
|
||||
UnityEngine.SceneManagement.SceneManager.LoadScene("Game");
|
||||
}
|
||||
|
||||
public void InstruccionsButtonCallback()
|
||||
{
|
||||
Debug.Log("Instructions button pressed");
|
||||
// UnityEngine.SceneManagement.SceneManager.LoadScene("Instructions");
|
||||
}
|
||||
|
||||
public void SetupButtonCallback()
|
||||
{
|
||||
Debug.Log("Setup button pressed");
|
||||
// UnityEngine.SceneManagement.SceneManager.LoadScene("Setup");
|
||||
}
|
||||
|
||||
public void ZapButtonCallback()
|
||||
{
|
||||
Debug.Log("Zap Scores button pressed");
|
||||
// UnityEngine.SceneManagement.SceneManager.LoadScene("Zap");
|
||||
}
|
||||
|
||||
public void AboutButtonCallback()
|
||||
{
|
||||
Debug.Log("About button pressed");
|
||||
// UnityEngine.SceneManagement.SceneManager.LoadScene("About");
|
||||
}
|
||||
|
||||
public void QuitButtonCallback()
|
||||
{
|
||||
Debug.Log("Quit button pressed");
|
||||
Application.Quit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e6c3bcd6a2eb7ec5aab68f9b18daea7
|
||||
@@ -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