mirror of
https://github.com/RHeavenStudioPlus/HeavenStudioPlus.git
synced 2024-12-05 15:57:32 +00:00
caf7d9465f
* add pause menu assets * layout and animation for pause * make play mode prefab function re-assign unused class inheritance * remove filepath * don't init medals twice * remove PlayerActionObject * initial attempt at anti-note lock TODO: circumvent inputs clearing themselves making the functionality not work * properly implement input lock prevention * fix error on editor open * functional pause menu * bugfix * make unpausing not reset current play statistics * serialize initializer components in inspector instead of procedurally generating * sanity check * note for fade * make flashes in the camera prefabs instead of in world space remove / reorganize script files address issue #411 * fix bug with perfect campaign make minigame transitions hide the game canvas adjust animation of the song credits textbox * fully functional intro scene (placeholder for future title screen) refactored entire game loading procedure re-organized some files * add interaction query to disclaimer text * reword legal * anchor section medals to section display more tempo change placement controls * operation order bugfix * prep for future ratings and stats * loading text * autoload opening scene * splash screen adjustments added setting to force enable splash screen * adjust setting entry
402 lines
No EOL
12 KiB
C#
402 lines
No EOL
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
using Starpelly;
|
|
|
|
namespace HeavenStudio
|
|
{
|
|
// [RequireComponent(typeof(AudioSource))]
|
|
public class Conductor : MonoBehaviour
|
|
{
|
|
// Song beats per minute
|
|
// This is determined by the song you're trying to sync up to
|
|
public float songBpm;
|
|
|
|
// The number of seconds for each song beat
|
|
public float secPerBeat;
|
|
|
|
// The number of seconds for each song beat, inversely scaled to song pitch (higer pitch = shorter time)
|
|
public float pitchedSecPerBeat => (secPerBeat / SongPitch);
|
|
|
|
// Current song position, in seconds
|
|
private double songPos; // for Conductor use only
|
|
public float songPosition => (float) songPos;
|
|
public double songPositionAsDouble => songPos;
|
|
|
|
// Current song position, in beats
|
|
private double songPosBeat; // for Conductor use only
|
|
public float songPositionInBeats => (float) songPosBeat;
|
|
public double songPositionInBeatsAsDouble => songPosBeat;
|
|
|
|
// Current time of the song
|
|
private double time;
|
|
|
|
double lastAbsTime;
|
|
|
|
// the dspTime we started at
|
|
private double dspStartTime;
|
|
public double dspStartTimeAsDouble => dspStartTime;
|
|
|
|
//the beat we started at
|
|
private double startBeat;
|
|
public double startBeatAsDouble => startBeat;
|
|
|
|
// an AudioSource attached to this GameObject that will play the music.
|
|
public AudioSource musicSource;
|
|
|
|
// The offset to the first beat of the song in seconds
|
|
public float firstBeatOffset;
|
|
|
|
// Conductor instance
|
|
public static Conductor instance;
|
|
|
|
// Conductor is currently playing song
|
|
public bool isPlaying;
|
|
|
|
// Conductor is currently paused, but not fully stopped
|
|
public bool isPaused;
|
|
|
|
// Last reported beat based on song position
|
|
private float lastReportedBeat = 0f;
|
|
|
|
// Metronome tick sound enabled
|
|
public bool metronome = false;
|
|
Util.Sound metronomeSound;
|
|
|
|
// pitch values
|
|
private float timelinePitch = 1f;
|
|
private float minigamePitch = 1f;
|
|
public float SongPitch { get => isPaused ? 0f : (timelinePitch * minigamePitch); }
|
|
|
|
public void SetTimelinePitch(float pitch)
|
|
{
|
|
timelinePitch = pitch;
|
|
musicSource.pitch = SongPitch;
|
|
}
|
|
|
|
public void SetMinigamePitch(float pitch)
|
|
{
|
|
minigamePitch = pitch;
|
|
musicSource.pitch = SongPitch;
|
|
}
|
|
|
|
|
|
void Awake()
|
|
{
|
|
instance = this;
|
|
}
|
|
|
|
public void SetBeat(double beat)
|
|
{
|
|
double secFromBeat = GetSongPosFromBeat(beat);
|
|
|
|
if (musicSource.clip != null)
|
|
{
|
|
if (secFromBeat < musicSource.clip.length)
|
|
musicSource.time = (float) secFromBeat;
|
|
else
|
|
musicSource.time = 0;
|
|
}
|
|
|
|
GameManager.instance.SetCurrentEventToClosest((float) beat);
|
|
songPosBeat = beat;
|
|
}
|
|
|
|
public void Play(double beat)
|
|
{
|
|
GameManager.instance.SortEventsList();
|
|
bool negativeOffset = firstBeatOffset < 0f;
|
|
bool negativeStartTime = false;
|
|
|
|
// Debug.Log("starting playback @ beat " + beat + ", offset is " + firstBeatOffset);
|
|
|
|
var startPos = GetSongPosFromBeat(beat);
|
|
if (negativeOffset)
|
|
{
|
|
time = startPos;
|
|
}
|
|
else
|
|
{
|
|
negativeStartTime = startPos - firstBeatOffset < 0f;
|
|
|
|
if (negativeStartTime)
|
|
time = startPos - firstBeatOffset;
|
|
else
|
|
time = startPos;
|
|
}
|
|
|
|
//TODO: make this take into account past tempo changes
|
|
songPosBeat = GetBeatFromSongPos(time - firstBeatOffset);
|
|
// Debug.Log("corrected starting playback @ beat " + songPosBeat);
|
|
|
|
isPlaying = true;
|
|
isPaused = false;
|
|
|
|
if (SongPosLessThanClipLength(startPos))
|
|
{
|
|
if (negativeOffset)
|
|
{
|
|
var musicStartTime = startPos + firstBeatOffset;
|
|
|
|
if (musicStartTime < 0f)
|
|
{
|
|
musicSource.time = (float) startPos;
|
|
musicSource.PlayScheduled(AudioSettings.dspTime - firstBeatOffset / SongPitch);
|
|
}
|
|
else
|
|
{
|
|
musicSource.time = (float) musicStartTime;
|
|
musicSource.PlayScheduled(AudioSettings.dspTime);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (negativeStartTime)
|
|
{
|
|
musicSource.time = (float) startPos;
|
|
}
|
|
else
|
|
{
|
|
musicSource.time = (float) startPos + firstBeatOffset;
|
|
}
|
|
|
|
musicSource.PlayScheduled(AudioSettings.dspTime);
|
|
}
|
|
}
|
|
lastAbsTime = Time.realtimeSinceStartupAsDouble;
|
|
dspStartTime = AudioSettings.dspTime;
|
|
startBeat = beat;
|
|
|
|
// GameManager.instance.SetCurrentEventToClosest(songPositionInBeats);
|
|
}
|
|
|
|
public void Pause()
|
|
{
|
|
isPlaying = false;
|
|
isPaused = true;
|
|
|
|
musicSource.Pause();
|
|
}
|
|
|
|
public void Stop(float time)
|
|
{
|
|
this.time = time;
|
|
|
|
songPosBeat = 0;
|
|
|
|
isPlaying = false;
|
|
isPaused = false;
|
|
|
|
musicSource.Stop();
|
|
}
|
|
float test;
|
|
|
|
public void Update()
|
|
{
|
|
if (isPlaying)
|
|
{
|
|
double absTime = Time.realtimeSinceStartupAsDouble;
|
|
double dt = (absTime - lastAbsTime) * SongPitch;
|
|
lastAbsTime = absTime;
|
|
|
|
time += dt;
|
|
|
|
songPos = time;
|
|
|
|
songPosBeat = GetBeatFromSongPos(songPos - firstBeatOffset);
|
|
}
|
|
}
|
|
|
|
public void LateUpdate()
|
|
{
|
|
if (metronome && isPlaying)
|
|
{
|
|
if (ReportBeat(ref lastReportedBeat))
|
|
{
|
|
metronomeSound = Util.Jukebox.PlayOneShot("metronome", lastReportedBeat);
|
|
}
|
|
else if (songPositionInBeats < lastReportedBeat)
|
|
{
|
|
lastReportedBeat = Mathf.Round(songPositionInBeats);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (metronomeSound != null)
|
|
{
|
|
metronomeSound.Delete();
|
|
metronomeSound = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool ReportBeat(ref float lastReportedBeat, float offset = 0, bool shiftBeatToOffset = true)
|
|
{
|
|
bool result = songPositionInBeats + (shiftBeatToOffset ? offset : 0f) >= (lastReportedBeat) + 1f;
|
|
if (result)
|
|
{
|
|
lastReportedBeat += 1f;
|
|
if (lastReportedBeat < songPositionInBeats)
|
|
{
|
|
lastReportedBeat = Mathf.Round(songPositionInBeats);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public float GetLoopPositionFromBeat(float beatOffset, float length)
|
|
{
|
|
return Mathf.Repeat((songPositionInBeats / length) + beatOffset, 1);
|
|
}
|
|
|
|
public float GetPositionFromBeat(float startBeat, float length)
|
|
{
|
|
float a = Mathp.Normalize(songPositionInBeats, startBeat, startBeat + length);
|
|
return a;
|
|
}
|
|
|
|
public float GetBeatFromPosition(float position, float startBeat, float length)
|
|
{
|
|
return Mathp.DeNormalize(position, startBeat, startBeat + length);
|
|
}
|
|
|
|
public float GetPositionFromMargin(float targetBeat, float margin)
|
|
{
|
|
return GetPositionFromBeat(targetBeat - margin, margin);
|
|
}
|
|
|
|
public float GetBeatFromPositionAndMargin(float position, float targetBeat, float margin)
|
|
{
|
|
return GetBeatFromPosition(position, targetBeat - margin, margin);
|
|
}
|
|
|
|
private List<DynamicBeatmap.TempoChange> GetSortedTempoChanges(DynamicBeatmap chart)
|
|
{
|
|
GameManager.instance.SortEventsList();
|
|
return GameManager.instance.Beatmap.tempoChanges;
|
|
}
|
|
|
|
public float GetBpmAtBeat(float beat)
|
|
{
|
|
var chart = GameManager.instance.Beatmap;
|
|
if (chart.tempoChanges.Count == 0)
|
|
return chart.bpm;
|
|
float bpm = chart.bpm;
|
|
|
|
foreach (DynamicBeatmap.TempoChange t in chart.tempoChanges)
|
|
{
|
|
if (t.beat > beat)
|
|
{
|
|
break;
|
|
}
|
|
bpm = t.tempo;
|
|
}
|
|
|
|
return bpm;
|
|
}
|
|
|
|
public double GetSongPosFromBeat(double beat)
|
|
{
|
|
var chart = GameManager.instance.Beatmap;
|
|
float bpm = chart.bpm;
|
|
|
|
double counter = 0f;
|
|
|
|
float lastTempoChangeBeat = 0f;
|
|
|
|
foreach (DynamicBeatmap.TempoChange t in chart.tempoChanges)
|
|
{
|
|
if (t.beat > beat)
|
|
{
|
|
break;
|
|
}
|
|
|
|
counter += (t.beat - lastTempoChangeBeat) * 60/bpm;
|
|
bpm = t.tempo;
|
|
lastTempoChangeBeat = t.beat;
|
|
}
|
|
|
|
counter += (beat - lastTempoChangeBeat) * 60/bpm;
|
|
|
|
return counter;
|
|
}
|
|
|
|
//thank you @wooningcharithri#7419 for the psuedo-code
|
|
public double BeatsToSecs(double beats, float bpm)
|
|
{
|
|
return beats / bpm * 60f;
|
|
}
|
|
public double SecsToBeats(double s, float bpm)
|
|
{
|
|
return s / 60f * bpm;
|
|
}
|
|
|
|
public double GetBeatFromSongPos(double seconds)
|
|
{
|
|
double lastTempoChangeBeat = 0f;
|
|
double counterSeconds = -firstBeatOffset;
|
|
float lastBpm = GameManager.instance.Beatmap.bpm;
|
|
|
|
foreach (DynamicBeatmap.TempoChange t in GameManager.instance.Beatmap.tempoChanges)
|
|
{
|
|
double beatToNext = t.beat - lastTempoChangeBeat;
|
|
double secToNext = BeatsToSecs(beatToNext, lastBpm);
|
|
double nextSecs = counterSeconds + secToNext;
|
|
|
|
if (nextSecs >= seconds)
|
|
break;
|
|
|
|
lastTempoChangeBeat = t.beat;
|
|
lastBpm = t.tempo;
|
|
counterSeconds = nextSecs;
|
|
}
|
|
return lastTempoChangeBeat + SecsToBeats(seconds - counterSeconds, lastBpm);
|
|
}
|
|
//
|
|
|
|
// convert real seconds to beats
|
|
public float GetRestFromRealTime(float seconds)
|
|
{
|
|
return seconds/pitchedSecPerBeat;
|
|
}
|
|
|
|
public void SetBpm(float bpm)
|
|
{
|
|
this.songBpm = bpm;
|
|
secPerBeat = 60f / songBpm;
|
|
}
|
|
|
|
public void SetVolume(float percent)
|
|
{
|
|
musicSource.volume = percent / 100f;
|
|
}
|
|
|
|
public float SongLengthInBeats()
|
|
{
|
|
if (!musicSource.clip) return 0;
|
|
return (float) GetBeatFromSongPos(musicSource.clip.length);
|
|
}
|
|
|
|
public bool SongPosLessThanClipLength(float t)
|
|
{
|
|
if (musicSource.clip != null)
|
|
return t < musicSource.clip.length;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
public bool SongPosLessThanClipLength(double t)
|
|
{
|
|
if (musicSource.clip != null)
|
|
return t < musicSource.clip.length;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
public bool NotStopped()
|
|
{
|
|
return Conductor.instance.isPlaying == true || Conductor.instance.isPaused == true;
|
|
}
|
|
}
|
|
} |