Skip to content

Commit

Permalink
Song Selection goes back to Last Song Played
Browse files Browse the repository at this point in the history
  • Loading branch information
JayDiddyThaGOAT committed May 12, 2023
1 parent 466640b commit efd7122
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 118 deletions.
2 changes: 2 additions & 0 deletions Assets/Script/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class GameManager : MonoBehaviour {
private AudioMixerGroup vocalGroup;

public SceneIndex CurrentScene { get; private set; } = SceneIndex.PERSISTANT;

public SongEntry SelectedSong { get; set; }

private void Awake() {
Instance = this;
Expand Down
129 changes: 38 additions & 91 deletions Assets/Script/PlayMode/Play.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using YARG.Settings;
using YARG.Song;
using YARG.UI;
using YARG.Util;
using YARG.Venue;

namespace YARG.PlayMode {
Expand All @@ -28,8 +27,6 @@ public static Play Instance {

public const float SONG_START_OFFSET = -2f;

public static SongEntry song = null;

public delegate void BeatAction();
public static event BeatAction BeatEvent;

Expand All @@ -41,7 +38,7 @@ public static Play Instance {
public static event PauseStateChangeAction OnPauseToggle;

[SerializeField]
private RenderTexture backgroundRenderTexture;
private GameObject soundAudioPrefab;

public bool SongStarted {
get;
Expand Down Expand Up @@ -89,20 +86,12 @@ public bool Paused {

GameManager.AudioManager.Pause();

if (GameUI.Instance.videoPlayer.enabled) {
GameUI.Instance.videoPlayer.Pause();
}
} else {
Time.timeScale = 1f;

GameManager.AudioManager.Play();

if (GameUI.Instance.videoPlayer.enabled) {
GameUI.Instance.videoPlayer.Play();
}
}

OnPauseToggle?.Invoke(_paused);
OnPauseToggle(_paused);
}
}

Expand All @@ -114,8 +103,6 @@ private void Awake() {
ScoreKeeper.Reset();
StarScoreKeeper.Reset();

backgroundRenderTexture.ClearTexture();

// Song
StartSong();
}
Expand All @@ -127,10 +114,10 @@ private void StartSong() {
bool isSpeedUp = Math.Abs(speed - 1) > float.Epsilon;

// Load MOGG if CON, otherwise load stems
if (song is ExtractedConSongEntry rawConSongEntry) {
if (GameManager.Instance.SelectedSong is ExtractedConSongEntry rawConSongEntry) {
GameManager.AudioManager.LoadMogg(rawConSongEntry, isSpeedUp);
} else {
var stems = AudioHelpers.GetSupportedStems(song.Location);
var stems = AudioHelpers.GetSupportedStems(GameManager.Instance.SelectedSong.Location);

GameManager.AudioManager.LoadSong(stems, isSpeedUp);
}
Expand Down Expand Up @@ -172,8 +159,32 @@ private void StartSong() {
i++;
}

// Load background (venue, video, image, etc.)
LoadBackground();
// Load Background
string backgroundPath = Path.Combine(GameManager.Instance.SelectedSong.Location, "bg.yarground");
string mp4Path = Path.Combine(GameManager.Instance.SelectedSong.Location, "bg.mp4");
string pngPath = Path.Combine(GameManager.Instance.SelectedSong.Location, "bg.png");

if (File.Exists(backgroundPath)) {
// First check for a yarground
var bundle = AssetBundle.LoadFromFile(backgroundPath);
var bg = bundle.LoadAsset<GameObject>("Assets/_Background.prefab");
var bgInstance = Instantiate(bg);

bgInstance.GetComponent<BundleBackgroundManager>().Bundle = bundle;
} else if (File.Exists(mp4Path)) {
// If not, check for a video
GameUI.Instance.videoPlayer.url = mp4Path;
GameUI.Instance.videoPlayer.enabled = true;
GameUI.Instance.videoPlayer.Play();
} else if (File.Exists(pngPath)) {
// Otherwise, load an image
var png = ImageHelper.LoadTextureFromFile(pngPath);

GameUI.Instance.background.texture = png;
} else {
// No background file, we load a random video
// TODO: Add custom videos folder, load here
}

SongStarted = true;

Expand All @@ -192,74 +203,16 @@ private void StartSong() {
break;
}
}

OnSongStart?.Invoke(song);
}

private void LoadBackground() {
// Try a yarground first

string backgroundPath = Path.Combine(song.Location, "bg.yarground");
if (File.Exists(backgroundPath)) {
var bundle = AssetBundle.LoadFromFile(backgroundPath);

// KEEP THIS PATH LOWERCASE
// Breaks things for other platforms, because Unity
var bg = bundle.LoadAsset<GameObject>("assets/_background.prefab");

var bgInstance = Instantiate(bg);

bgInstance.GetComponent<BundleBackgroundManager>().Bundle = bundle;
return;
}

// Next, a video

string[] videoPaths = {
"bg.mp4",
"bg.mov",
"bg.webm",
};

foreach (var file in videoPaths) {
var path = Path.Combine(song.Location, file);

if (File.Exists(path)) {
GameUI.Instance.videoPlayer.url = path;
GameUI.Instance.videoPlayer.enabled = true;

return;
}
}

// Finally, an image

string[] imagePaths = {
"bg.png",
"bg.jpg",
"bg.jpeg",
};

foreach (var file in imagePaths) {
var path = Path.Combine(song.Location, file);

if (File.Exists(path)) {
var png = ImageHelper.LoadTextureFromFile(path);

GameUI.Instance.background.texture = png;
return;
}
}
}

private void LoadChart() {
// Add main file
var files = new List<string> {
Path.Combine(song.Location, song.NotesFile)
Path.Combine(GameManager.Instance.SelectedSong.Location, GameManager.Instance.SelectedSong.NotesFile)
};

// Look for upgrades and add
// var upgradeFolder = new DirectoryInfo(Path.Combine(song.RootFolder, "yarg_upgrade"));
// var upgradeFolder = new DirectoryInfo(Path.Combine(GameManager.Instance.ChosenSong.RootFolder, "yarg_upgrade"));
// if (upgradeFolder.Exists) {
// foreach (var midi in upgradeFolder.GetFiles("*.mid")) {
// files.Add(midi.FullName);
Expand All @@ -269,18 +222,18 @@ private void LoadChart() {
// Parse

MoonSong moonSong = null;
if (song.NotesFile.EndsWith(".chart")) {
if (GameManager.Instance.SelectedSong.NotesFile.EndsWith(".chart")) {
Debug.Log("Reading .chart file");
moonSong = ChartReader.ReadChart(files[0]);
}

chart = new YargChart(moonSong);
if (song.NotesFile.EndsWith(".mid")) {
if (GameManager.Instance.SelectedSong.NotesFile.EndsWith(".mid")) {
// Parse
var parser = new MidiParser(song, files.ToArray());
var parser = new MidiParser(GameManager.Instance.SelectedSong, files.ToArray());
chart.InitializeArrays();
parser.Parse(chart);
} else if (song.NotesFile.EndsWith(".chart")) {
} else if (GameManager.Instance.SelectedSong.NotesFile.EndsWith(".chart")) {
var handler = new BeatHandler(moonSong);
handler.GenerateBeats();
chart.beats = handler.Beats;
Expand All @@ -298,10 +251,6 @@ private IEnumerator StartAudio() {
yield return null;
}

if (GameUI.Instance.videoPlayer.enabled) {
GameUI.Instance.videoPlayer.Play();
}

GameManager.AudioManager.Play();
audioStarted = true;
}
Expand Down Expand Up @@ -427,7 +376,7 @@ private void Update() {
}
}

// End song
// End GameManager.Instance.ChosenSong
if (realSongTime >= SongLength) {
MainMenu.isPostSong = true;
Exit();
Expand Down Expand Up @@ -491,15 +440,13 @@ public void Exit() {
GameManager.AudioManager.UnloadSong();

// Call events
OnSongEnd?.Invoke(song);
OnSongEnd?.Invoke(GameManager.Instance.SelectedSong);

// Unpause just in case
Time.timeScale = 1f;

backgroundRenderTexture.ClearTexture();
_tracks.Clear();

OnSongEnd?.Invoke(song);
GameManager.Instance.LoadScene(SceneIndex.MENU);
}

Expand Down
22 changes: 7 additions & 15 deletions Assets/Script/UI/DifficultySelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ private void IncreasePlayerIndex() {
}

// Play song
Play.song = MainMenu.Instance.chosenSong;
GameManager.Instance.LoadScene(SceneIndex.PLAY);
} else {
UpdateInstrument();
Expand All @@ -224,7 +223,9 @@ private void UpdateInstrument() {

// Get available instruments
var availableInstruments = allInstruments
.Where(instrument => MainMenu.Instance.chosenSong.HasInstrument(instrument)).ToList();
.Where(instrument => GameManager.Instance.SelectedSong.HasInstrument(instrument)).ToList();

Debug.Log(GameManager.Instance.SelectedSong.AvailableParts);

// Force add pro drums and five lane
if (availableInstruments.Contains(Instrument.DRUMS)) {
Expand All @@ -242,25 +243,16 @@ private void UpdateInstrument() {
// Filter out to only allowed instruments
availableInstruments.RemoveAll(i => !player.inputStrategy.GetAllowedInstruments().Contains(i));

bool showSitOut = availableInstruments.Count <= 0 || PlayerManager.players.Count > 1;

optionCount = availableInstruments.Count + (showSitOut ? 1 : 0);
if (showSitOut) {
optionCount++;
}
optionCount = availableInstruments.Count + 1;

// Add to options
var ops = new string[optionCount];
var ops = new string[availableInstruments.Count + 1];
instruments = new string[availableInstruments.Count];
for (int i = 0; i < instruments.Length; i++) {
instruments[i] = availableInstruments[i].ToStringName();
ops[i] = availableInstruments[i].ToLocalizedName();
}

// Add sit out (only if there are more than 1 player)
if (showSitOut) {
ops[^1] = "Sit Out";
}
ops[^1] = "Sit Out";

// Set text and sprites
for (int i = 0; i < 6; i++) {
Expand Down Expand Up @@ -294,7 +286,7 @@ private void UpdateDifficulty(string chosenInstrument, bool showExpertPlus) {
// Get the available difficulties
var availableDifficulties = new List<Difficulty>();
for (int i = 0; i < (int) Difficulty.EXPERT_PLUS; i++) {
if (!MainMenu.Instance.chosenSong.HasPart(instrument, (Difficulty) i)) {
if (!GameManager.Instance.SelectedSong.HasPart(instrument, (Difficulty) i)) {
continue;
}
availableDifficulties.Add((Difficulty) i);
Expand Down
8 changes: 4 additions & 4 deletions Assets/Script/UI/GameUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ private void Awake() {

private void Start() {
if (Play.speed == 1f) {
songTitle.text = $"{Play.song.Name}";
bandName.text = $"{Play.song.Artist}";
songTitle.text = $"{GameManager.Instance.SelectedSong.Name}";
bandName.text = $"{GameManager.Instance.SelectedSong.Artist}";
} else {
songTitle.text = $"{Play.song.Name} ({Play.speed * 100}%)";
bandName.text = $"{Play.song.Artist}";
songTitle.text = $"{GameManager.Instance.SelectedSong.Name} ({Play.speed * 100}%)";
bandName.text = $"{GameManager.Instance.SelectedSong.Artist}";
}
}

Expand Down
2 changes: 0 additions & 2 deletions Assets/Script/UI/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ private enum ButtonIndex {
EXIT
}

public SongEntry chosenSong = null;

[SerializeField]
private Canvas mainMenu;
[SerializeField]
Expand Down
16 changes: 15 additions & 1 deletion Assets/Script/UI/MusicLibrary/SongSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ private set {
_selectedIndex -= _songs.Count;
}

if (_songs[value] is SongViewType song) {
GameManager.Instance.SelectedSong = song.SongEntry;
}

UpdateScrollbar();
UpdateSongViews();
}
Expand Down Expand Up @@ -301,7 +305,17 @@ public void UpdateSearch() {
_songs.Clear();
}

SelectedIndex = 1;
if (GameManager.Instance.SelectedSong == null) {
SelectedIndex = 1;
} else {
var index = _songs.FindIndex(song => {
var songType = song as SongViewType;
return songType != null && songType.SongEntry == GameManager.Instance.SelectedSong;
});

SelectedIndex = Mathf.Max(1, index);
}

UpdateSongViews();
UpdateScrollbar();
}
Expand Down
1 change: 0 additions & 1 deletion Assets/Script/UI/MusicLibrary/ViewTypes/SongViewType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public override void SecondaryTextClick() {
public override void PrimaryButtonClick() {
base.PrimaryButtonClick();

MainMenu.Instance.chosenSong = SongEntry;
MainMenu.Instance.ShowPreSong();
}

Expand Down
Loading

0 comments on commit efd7122

Please sign in to comment.