lucid-super-dream/Assets/Scripts/AudioBeatManager.cs

45 lines
1.0 KiB
C#
Raw Normal View History

2021-01-06 16:10:11 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2021-01-07 03:00:17 +01:00
using UnityEngine.Serialization;
2021-01-06 16:10:11 +01:00
2021-01-07 03:00:17 +01:00
public class AudioBeatManager : MonoBehaviour, IAudioBeatManager
2021-01-06 16:10:11 +01:00
{
[SerializeField] private float bpm;
public float TimeBetweenBeats => _bps * Time.deltaTime;
private float _bps;
private int _currentBeat = 0;
private float _timer;
2021-01-07 03:00:17 +01:00
[SerializeField] [FormerlySerializedAs("OnBeat")]
private IntEvent _onBeat;
public IntEvent OnBeat => _onBeat;
2021-01-06 16:10:11 +01:00
public event Action<int> OnBeatEvent;
private void Awake()
{
_bps = bpm / 60f;
_timer = 0;
}
// Update is called once per frame
void Update()
{
_timer += Time.deltaTime;
if (_timer >= TimeBetweenBeats)
{
_timer = 0;
++_currentBeat;
OnBeat?.Invoke(_currentBeat);
OnBeatEvent?.Invoke(_currentBeat);
}
}
2021-01-07 03:00:17 +01:00
}
public interface IAudioBeatManager
{
public IntEvent OnBeat { get; }
public event Action<int> OnBeatEvent;
2021-01-06 16:10:11 +01:00
}