diff --git a/OpenRA.FileFormats/Map/Map.cs b/OpenRA.FileFormats/Map/Map.cs index 66ac029974..4ede89ce57 100644 --- a/OpenRA.FileFormats/Map/Map.cs +++ b/OpenRA.FileFormats/Map/Map.cs @@ -50,6 +50,7 @@ namespace OpenRA.FileFormats public Dictionary Rules = new Dictionary(); public Dictionary Weapons = new Dictionary(); public Dictionary Voices = new Dictionary(); + public Dictionary Music = new Dictionary(); public Dictionary Terrain = new Dictionary(); // Binary map data public byte TileFormat = 1; diff --git a/OpenRA.Game/GameRules/MusicInfo.cs b/OpenRA.Game/GameRules/MusicInfo.cs index 651f500c1f..c1c2833aee 100644 --- a/OpenRA.Game/GameRules/MusicInfo.cs +++ b/OpenRA.Game/GameRules/MusicInfo.cs @@ -8,43 +8,33 @@ namespace OpenRA.GameRules { public class MusicInfo { - public readonly Lazy> Pools; + public readonly MusicPool Pool; public readonly string[] Music = { }; public MusicInfo( MiniYaml y ) { FieldLoader.Load(this, y); - - Pools = Lazy.New(() => - new Dictionary - { - { "Music", new MusicPool(Music) }, - }); + Pool = new MusicPool(Music); } } public class MusicPool { readonly string[] clips; - readonly List liveclips = new List(); + int playing = 0; public MusicPool(params string[] clips) { this.clips = clips; } - public string GetNext() - { - if (liveclips.Count == 0) - liveclips.AddRange(clips); - - if (liveclips.Count == 0) - return null; /* avoid crashing if there's no clips at all */ - - var i = Game.CosmeticRandom.Next(liveclips.Count); - var s = liveclips[i]; - liveclips.RemoveAt(i); - return s; + public string GetNext() { + playing = (playing + 1) % clips.Length; + return clips[playing]; + } + public string GetPrev() { + playing = (playing + clips.Length - 1) % clips.Length; + return clips[playing]; } } } diff --git a/OpenRA.Game/GameRules/Rules.cs b/OpenRA.Game/GameRules/Rules.cs index d0f6e081c2..b358893e41 100755 --- a/OpenRA.Game/GameRules/Rules.cs +++ b/OpenRA.Game/GameRules/Rules.cs @@ -68,6 +68,7 @@ namespace OpenRA Info = LoadYamlRules(m.Rules, map.Rules, (k, y) => new ActorInfo(k.Key.ToLowerInvariant(), k.Value, y)); Weapons = LoadYamlRules(m.Weapons, map.Weapons, (k, _) => new WeaponInfo(k.Key.ToLowerInvariant(), k.Value)); Voices = LoadYamlRules(m.Voices, map.Voices, (k, _) => new VoiceInfo(k.Value)); + Music = LoadYamlRules(m.Music, map.Music, (k, _) => new MusicInfo(k.Value)); TerrainTypes = LoadYamlRules(m.Terrain, map.Terrain, (k, _) => new TerrainCost(k.Value)) .ToDictionary(kv => (TerrainType)Enum.Parse(typeof(TerrainType), kv.Key, true), kv => kv.Value); diff --git a/OpenRA.Game/Widgets/Delegates/MusicPlayerDelegate.cs b/OpenRA.Game/Widgets/Delegates/MusicPlayerDelegate.cs index 7403492f3e..28c21c35e0 100644 --- a/OpenRA.Game/Widgets/Delegates/MusicPlayerDelegate.cs +++ b/OpenRA.Game/Widgets/Delegates/MusicPlayerDelegate.cs @@ -50,7 +50,7 @@ namespace OpenRA.Widgets.Delegates return true; }; bg.GetWidget("BUTTON_PREV").OnMouseUp = mi => { - Sound.PlayMusic(GetNextSong()); + Sound.PlayMusic(GetPrevSong()); Sound.MusicPaused = false; bg.GetWidget("BUTTON_PLAY").Visible = false; bg.GetWidget("BUTTON_PAUSE").Visible = true; @@ -58,10 +58,7 @@ namespace OpenRA.Widgets.Delegates }; } - string GetNextSong() - { - //goes boom here - return Rules.Music["AllMusic"].Pools.Value["Music"].GetNext(); - } + string GetNextSong() { return Rules.Music["allmusic"].Pool.GetNext(); } + string GetPrevSong() { return Rules.Music["allmusic"].Pool.GetPrev(); } } }