Introduce background music concept.
This commit is contained in:
@@ -20,20 +20,15 @@ namespace OpenRA.Traits
|
||||
[Desc("Music to play when the map starts.", "Plays the first song on the playlist when undefined.")]
|
||||
public readonly string StartingMusic = null;
|
||||
|
||||
[Desc("Should the starting music loop?")]
|
||||
public readonly bool LoopStartingMusic = false;
|
||||
|
||||
[Desc("Music to play when the game has been won.")]
|
||||
public readonly string VictoryMusic = null;
|
||||
|
||||
[Desc("Should the victory music loop?")]
|
||||
public readonly bool LoopVictoryMusic = false;
|
||||
|
||||
[Desc("Music to play when the game has been lost.")]
|
||||
public readonly string DefeatMusic = null;
|
||||
|
||||
[Desc("Should the defeat music loop?")]
|
||||
public readonly bool LoopDefeatMusic = false;
|
||||
[Desc("This track is played when no other music is playing.",
|
||||
"It cannot be paused, but can be overridden by selecting a new track.")]
|
||||
public readonly string BackgroundMusic = null;
|
||||
|
||||
public object Create(ActorInitializer init) { return new MusicPlaylist(init.World, this); }
|
||||
}
|
||||
@@ -41,18 +36,21 @@ namespace OpenRA.Traits
|
||||
public class MusicPlaylist : INotifyActorDisposing, IGameOver
|
||||
{
|
||||
readonly MusicPlaylistInfo info;
|
||||
readonly World world;
|
||||
|
||||
readonly MusicInfo[] random;
|
||||
readonly MusicInfo[] playlist;
|
||||
|
||||
public readonly bool IsMusicAvailable;
|
||||
public bool CurrentSongIsBackground { get; private set; }
|
||||
|
||||
MusicInfo currentSong;
|
||||
bool repeat;
|
||||
MusicInfo currentBackgroundSong;
|
||||
|
||||
public MusicPlaylist(World world, MusicPlaylistInfo info)
|
||||
{
|
||||
this.info = info;
|
||||
this.world = world;
|
||||
|
||||
IsMusicAvailable = world.Map.Rules.InstalledMusic.Any();
|
||||
|
||||
@@ -63,22 +61,31 @@ namespace OpenRA.Traits
|
||||
|
||||
random = playlist.Shuffle(Game.CosmeticRandom).ToArray();
|
||||
|
||||
if (!string.IsNullOrEmpty(info.StartingMusic)
|
||||
&& world.Map.Rules.Music.ContainsKey(info.StartingMusic)
|
||||
&& world.Map.Rules.Music[info.StartingMusic].Exists)
|
||||
if (SongExists(info.StartingMusic))
|
||||
{
|
||||
currentSong = world.Map.Rules.Music[info.StartingMusic];
|
||||
repeat = info.LoopStartingMusic;
|
||||
}
|
||||
else
|
||||
else if (SongExists(info.BackgroundMusic))
|
||||
{
|
||||
currentSong = Game.Settings.Sound.Shuffle ? random.First() : playlist.First();
|
||||
repeat = Game.Settings.Sound.Repeat;
|
||||
currentSong = currentBackgroundSong = world.Map.Rules.Music[info.BackgroundMusic];
|
||||
CurrentSongIsBackground = true;
|
||||
}
|
||||
|
||||
Play();
|
||||
}
|
||||
|
||||
bool SongExists(string song)
|
||||
{
|
||||
return !string.IsNullOrEmpty(song)
|
||||
&& world.Map.Rules.Music.ContainsKey(song)
|
||||
&& world.Map.Rules.Music[song].Exists;
|
||||
}
|
||||
|
||||
bool SongExists(MusicInfo song)
|
||||
{
|
||||
return song != null && song.Exists;
|
||||
}
|
||||
|
||||
public MusicInfo CurrentSong()
|
||||
{
|
||||
return currentSong;
|
||||
@@ -95,43 +102,34 @@ namespace OpenRA.Traits
|
||||
if (!IsMusicAvailable)
|
||||
return;
|
||||
|
||||
var playedSong = currentSong;
|
||||
|
||||
if (world.LocalPlayer != null && world.LocalPlayer.WinState == WinState.Won)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(info.VictoryMusic)
|
||||
&& world.Map.Rules.Music.ContainsKey(info.VictoryMusic)
|
||||
&& world.Map.Rules.Music[info.VictoryMusic].Exists)
|
||||
if (SongExists(info.VictoryMusic))
|
||||
{
|
||||
currentSong = world.Map.Rules.Music[info.VictoryMusic];
|
||||
repeat = info.LoopVictoryMusic;
|
||||
currentBackgroundSong = world.Map.Rules.Music[info.VictoryMusic];
|
||||
Stop();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Most RTS treats observers losing the game,
|
||||
// no need for a special handling involving them here.
|
||||
if (!string.IsNullOrEmpty(info.DefeatMusic)
|
||||
&& world.Map.Rules.Music.ContainsKey(info.DefeatMusic)
|
||||
&& world.Map.Rules.Music[info.DefeatMusic].Exists)
|
||||
if (SongExists(info.DefeatMusic))
|
||||
{
|
||||
currentSong = world.Map.Rules.Music[info.DefeatMusic];
|
||||
repeat = info.LoopDefeatMusic;
|
||||
currentBackgroundSong = world.Map.Rules.Music[info.DefeatMusic];
|
||||
Stop();
|
||||
}
|
||||
}
|
||||
|
||||
if (playedSong != currentSong)
|
||||
Play();
|
||||
}
|
||||
|
||||
void Play()
|
||||
{
|
||||
if (currentSong == null || !IsMusicAvailable)
|
||||
if (!SongExists(currentSong) || !IsMusicAvailable)
|
||||
return;
|
||||
|
||||
Sound.PlayMusicThen(currentSong, () =>
|
||||
{
|
||||
if (!repeat)
|
||||
if (!CurrentSongIsBackground && !Game.Settings.Sound.Repeat)
|
||||
currentSong = GetNextSong();
|
||||
|
||||
Play();
|
||||
@@ -144,15 +142,9 @@ namespace OpenRA.Traits
|
||||
return;
|
||||
|
||||
currentSong = music;
|
||||
repeat = Game.Settings.Sound.Repeat;
|
||||
CurrentSongIsBackground = false;
|
||||
|
||||
Sound.PlayMusicThen(music, () =>
|
||||
{
|
||||
if (!repeat)
|
||||
currentSong = GetNextSong();
|
||||
|
||||
Play();
|
||||
});
|
||||
Play();
|
||||
}
|
||||
|
||||
public void Play(MusicInfo music, Action onComplete)
|
||||
@@ -161,6 +153,7 @@ namespace OpenRA.Traits
|
||||
return;
|
||||
|
||||
currentSong = music;
|
||||
CurrentSongIsBackground = false;
|
||||
Sound.PlayMusicThen(music, onComplete);
|
||||
}
|
||||
|
||||
@@ -181,22 +174,34 @@ namespace OpenRA.Traits
|
||||
|
||||
var songs = Game.Settings.Sound.Shuffle ? random : playlist;
|
||||
|
||||
return reverse ? songs.SkipWhile(m => m != currentSong)
|
||||
.Skip(1).FirstOrDefault() ?? songs.FirstOrDefault() :
|
||||
songs.Reverse().SkipWhile(m => m != currentSong)
|
||||
.Skip(1).FirstOrDefault() ?? songs.Reverse().FirstOrDefault();
|
||||
var next = reverse ? songs.Reverse().SkipWhile(m => m != currentSong)
|
||||
.Skip(1).FirstOrDefault() ?? songs.Reverse().FirstOrDefault() :
|
||||
songs.SkipWhile(m => m != currentSong)
|
||||
.Skip(1).FirstOrDefault() ?? songs.FirstOrDefault();
|
||||
|
||||
if (SongExists(next))
|
||||
return next;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
currentSong = null;
|
||||
Sound.StopMusic();
|
||||
|
||||
if (currentBackgroundSong != null)
|
||||
{
|
||||
currentSong = currentBackgroundSong;
|
||||
CurrentSongIsBackground = true;
|
||||
Play();
|
||||
}
|
||||
}
|
||||
|
||||
public void Disposing(Actor self)
|
||||
{
|
||||
if (currentSong != null)
|
||||
Stop();
|
||||
Sound.StopMusic();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user