Files
HauntedBloodlines/Assets/Scripts/MusicBox.cs
2025-05-29 22:31:40 +03:00

247 lines
7.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MusicBox : MonoBehaviour
{
public Transform handleTransform; // Reference to the handle's Transform component
public AudioSource musicBoxAudio; // Reference to the AudioSource component
public float rotationSpeed = 50f; // Speed at which the handle rotates
public float maxVolume = 1f; // Maximum volume of the music
private bool isRotatingForward = false;
private bool isRotatingBackward = false;
[SerializeField] float currentTimeToOpenTopOfMusicBox;
public float targetTimeToOpenTopOfMusicBox = 5f;
private float lastTime;
private float lastAngle;
public float circularThreshold = 5.0f;
public bool isRotating;
public bool isUsingMusicBox;
[SerializeField] EventTrigger eventTrigger;
[Header("UI")]
public GameObject musicBoxMoveHandleIndication;
//void DetectCircularMovement()
//{
// float horizontal = Input.GetAxis("Horizontal");
// float vertical = Input.GetAxis("Vertical");
// Vector2 movementInput = new Vector2(horizontal, vertical);
// // Check if movement has a constant magnitude
// float magnitude = movementInput.magnitude;
// if (Mathf.Approximately(magnitude, 1.0f))
// {
// // Check if movement is in a circular pattern
// float angle = Mathf.Atan2(vertical, horizontal) * Mathf.Rad2Deg;
// if (angle < 0)
// {
// angle += 360;
// }
// float deltaTime = Time.time - lastTime;
// float angleChange = Mathf.DeltaAngle(lastAngle, angle);
// // Check if the angle is changing smoothly (within a certain threshold)
// if (Mathf.Abs(angleChange) < circularThreshold)
// {
// // Detect direction based on the change in angle
// float angularSpeed = angleChange / deltaTime;
// if (angularSpeed > 0)
// {
// Debug.Log("Circular Movement to the right detected");
// }
// else if (angularSpeed < 0)
// {
// Debug.Log("Circular Movement to the left detected");
// }
// }
// lastAngle = angle;
// lastTime = Time.time;
// }
//}
//void Update()
//{
// //DetectCircularMovement();
// // Check for input to rotate the handle forward
// if (Input.GetKey(KeyCode.W))
// {
// isRotatingForward = true;
// isRotatingBackward = false;
// isRotating = true;
// }
// else if (Input.GetKey(KeyCode.S)) // Check for input to rotate the handle backward
// {
// isRotatingForward = false;
// isRotatingBackward = true;
// isRotating = true;
// }
// else
// {
// isRotatingForward = false;
// isRotatingBackward = false;
// isRotating = false;
// }
// // Rotate the handle based on input
// RotateHandle();
// // Check for input to play music
// if (/*Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) ||*/ isRotating)
// {
// // Fade in music volume
// if (musicBoxAudio.volume < maxVolume)
// {
// musicBoxAudio.volume += Time.deltaTime;
// }
// // Play music if not already playing
// if (!musicBoxAudio.isPlaying)
// {
// musicBoxAudio.Play();
// }
// }
// else
// {
// // Fade out music volume
// if (musicBoxAudio.volume > 0f)
// {
// musicBoxAudio.volume -= Time.deltaTime;
// }
// else
// {
// // Stop playing music when volume reaches zero
// musicBoxAudio.Stop();
// }
// }
//}
public void UseMusicBox()
{
isUsingMusicBox = true;
musicBoxMoveHandleIndication.gameObject.SetActive(true);
StartCoroutine(UpdateMusicBox());
}
public void IsNoLongerUsingMusicBox()
{
musicBoxAudio.Stop(); //Change to make it fade out.
isUsingMusicBox = false;
musicBoxMoveHandleIndication.gameObject.SetActive(false);
}
//void Update()
//{
// // Rotate the handle based on input
// RotateHandle();
//}
IEnumerator UpdateMusicBox()
{
while (isUsingMusicBox)
{
#region Input
// Check for input to rotate the handle forward
float MoveHandleForwardControllerInput = Input.GetAxis("RightTrigger");
float MoveHandleBackwardControllerInput = Input.GetAxis("LeftTrigger");
if (Input.GetKey(KeyCode.W) || MoveHandleForwardControllerInput < 0)
{
isRotatingForward = true;
isRotatingBackward = false;
isRotating = true;
}
else if (Input.GetKey(KeyCode.S) || MoveHandleBackwardControllerInput < 0) // Check for input to rotate the handle backward
{
isRotatingForward = false;
isRotatingBackward = true;
isRotating = true;
}
else
{
isRotatingForward = false;
isRotatingBackward = false;
isRotating = false;
}
#endregion
// Rotate the handle based on input
RotateHandle();
#region Music
// Check for input to play music
if (isRotating)
{
if (currentTimeToOpenTopOfMusicBox <= targetTimeToOpenTopOfMusicBox)
{
currentTimeToOpenTopOfMusicBox += Time.unscaledDeltaTime;
}
// Fade in music volume
if (musicBoxAudio.volume < maxVolume)
{
musicBoxAudio.volume += Time.unscaledDeltaTime;
}
// Play music if not already playing
if (!musicBoxAudio.isPlaying)
{
musicBoxAudio.Play();
}
}
else
{
// Fade out music volume
if (musicBoxAudio.volume > 0f)
{
musicBoxAudio.volume -= Time.unscaledDeltaTime;
}
else
{
// Stop playing music when volume reaches zero
currentTimeToOpenTopOfMusicBox = 0f;
musicBoxAudio.Stop();
}
}
if (currentTimeToOpenTopOfMusicBox >= targetTimeToOpenTopOfMusicBox)
{
if (isUsingMusicBox)
{
eventTrigger.Invoke();
musicBoxAudio.Stop();
musicBoxMoveHandleIndication.gameObject.SetActive(false);
isUsingMusicBox = false;
}
}
#endregion
yield return null;
}
}
void RotateHandle()
{
// Rotate the handle based on input and rotation speed
if (isRotatingForward)
{
handleTransform.Rotate(Vector3.back * rotationSpeed * Time.unscaledDeltaTime);
print("Is rotating handle forward");
}
else if (isRotatingBackward)
{
handleTransform.Rotate(Vector3.forward * rotationSpeed * Time.unscaledDeltaTime);
print("Is rotating handle backwards");
}
}
}