Add support for hiding music tracks.

This commit is contained in:
Zimmermann Gyula
2015-07-20 14:41:55 +02:00
committed by Paul Chote
parent 21254db120
commit 1d80c37fda
2 changed files with 25 additions and 13 deletions

View File

@@ -15,8 +15,10 @@ namespace OpenRA.GameRules
{
public class MusicInfo
{
public readonly string Filename = null;
public readonly string Title = null;
public readonly string Filename;
public readonly string Title;
public readonly bool Hidden;
public int Length { get; private set; } // seconds
public bool Exists { get; private set; }
@@ -25,18 +27,24 @@ namespace OpenRA.GameRules
Title = value.Value;
var nd = value.ToDictionary();
if (nd.ContainsKey("Hidden"))
bool.TryParse(nd["Hidden"].Value, out Hidden);
var ext = nd.ContainsKey("Extension") ? nd["Extension"].Value : "aud";
Filename = (nd.ContainsKey("Filename") ? nd["Filename"].Value : key) + "." + ext;
if (!GlobalFileSystem.Exists(Filename))
return;
Exists = true;
using (var s = GlobalFileSystem.Open(Filename))
{
if (Filename.ToLowerInvariant().EndsWith("wav"))
Length = (int)WavLoader.WaveLength(s);
else
Length = (int)AudLoader.SoundLength(s);
}
}
public void Reload()
{
@@ -45,10 +53,12 @@ namespace OpenRA.GameRules
Exists = true;
using (var s = GlobalFileSystem.Open(Filename))
{
if (Filename.ToLowerInvariant().EndsWith("wav"))
Length = (int)WavLoader.WaveLength(s);
else
Length = (int)AudLoader.SoundLength(s);
}
}
}
}

View File

@@ -41,6 +41,7 @@ namespace OpenRA.Traits
readonly MusicInfo[] random;
readonly MusicInfo[] playlist;
public readonly bool IsMusicInstalled;
public readonly bool IsMusicAvailable;
public bool CurrentSongIsBackground { get; private set; }
@@ -52,13 +53,17 @@ namespace OpenRA.Traits
this.info = info;
this.world = world;
IsMusicAvailable = world.Map.Rules.InstalledMusic.Any();
playlist = world.Map.Rules.InstalledMusic.Select(a => a.Value).ToArray();
if (!IsMusicAvailable)
IsMusicInstalled = world.Map.Rules.InstalledMusic.Any();
if (!IsMusicInstalled)
return;
playlist = world.Map.Rules.InstalledMusic
.Where(a => !a.Value.Hidden)
.Select(a => a.Value)
.ToArray();
IsMusicAvailable = playlist.Any();
random = playlist.Shuffle(Game.CosmeticRandom).ToArray();
// Always start with a random song
@@ -100,9 +105,6 @@ namespace OpenRA.Traits
public void GameOver(World world)
{
if (!IsMusicAvailable)
return;
if (world.LocalPlayer != null && world.LocalPlayer.WinState == WinState.Won)
{
if (SongExists(info.VictoryMusic))
@@ -125,7 +127,7 @@ namespace OpenRA.Traits
void Play()
{
if (!SongExists(currentSong) || !IsMusicAvailable)
if (!SongExists(currentSong))
return;
Sound.PlayMusicThen(currentSong, () =>
@@ -139,7 +141,7 @@ namespace OpenRA.Traits
public void Play(MusicInfo music)
{
if (music == null || !IsMusicAvailable)
if (music == null)
return;
currentSong = music;
@@ -150,7 +152,7 @@ namespace OpenRA.Traits
public void Play(MusicInfo music, Action onComplete)
{
if (music == null || !IsMusicAvailable)
if (music == null)
return;
currentSong = music;