Skip to content

Commit

Permalink
Add song length to Play.cs for song end (YARC-Official#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
RileyTheFox authored Apr 14, 2023
1 parent 3ff7b03 commit dd6fe48
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion Assets/Script/PlayMode/Play.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public float SongTime {
get => realSongTime + PlayerManager.GlobalCalibration * speed;
}

public float SongLength {
get;
private set;
}

public Chart chart;

private int beatIndex = 0;
Expand Down Expand Up @@ -177,6 +182,12 @@ private IEnumerator StartSong() {
// Start all audio at the same time
foreach (var (_, audioSource) in audioSources) {
audioSource.pitch = speed;

// Gets the longest audio file and sets the song length to that length
if (audioSource.clip.length > SongLength) {
SongLength = audioSource.clip.length;
}

audioSource.Play();
}
realSongTime = audioSources.First().Value.time;
Expand All @@ -185,6 +196,14 @@ private IEnumerator StartSong() {
// Hide loading screen
GameUI.Instance.loadingContainer.SetActive(false);

// End events override the audio length
foreach (var chartEvent in chart.events) {
if (chartEvent.name is "end" or "[end]") {
SongLength = chartEvent.time;
break;
}
}

// Call events
OnSongStart?.Invoke(song);
}
Expand Down Expand Up @@ -306,7 +325,7 @@ private void Update() {
}

// End song
if (realSongTime > song.songLength + 0.5f) {
if (realSongTime > SongLength + 0.5f) {
MainMenu.isPostSong = true;
Exit();
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Script/UI/GameUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private void Start() {
}

private void Update() {
songProgress.fillAmount = Play.Instance.SongTime / Play.song.songLength;
songProgress.fillAmount = Play.Instance.SongTime / Play.Instance.SongLength;
}

public void AddTrackImage(RenderTexture rt) {
Expand Down

0 comments on commit dd6fe48

Please sign in to comment.