From 3489794713251aa42a8a1d6bd56655a2b55e5985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sat, 1 Nov 2014 19:11:39 +0100 Subject: [PATCH] replace PlayMusicOnMapLoad trait with Lua Media.PlayMusic method --- OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 1 - .../Scripting/Global/MediaGlobal.cs | 45 ++++++++++++++++- .../Scripting/Global/TriggerGlobal.cs | 2 +- .../Traits/World/PlayMusicOnMapLoad.cs | 50 ------------------- mods/cnc/maps/gdi01/gdi01.lua | 9 +++- mods/cnc/maps/gdi01/map.yaml | 3 -- mods/cnc/maps/shellmap/map.yaml | 3 -- mods/cnc/maps/shellmap/shellmap.lua | 5 ++ 8 files changed, 58 insertions(+), 60 deletions(-) delete mode 100644 OpenRA.Mods.Common/Traits/World/PlayMusicOnMapLoad.cs diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index e8f263effe..80738fe31c 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -416,7 +416,6 @@ - diff --git a/OpenRA.Mods.Common/Scripting/Global/MediaGlobal.cs b/OpenRA.Mods.Common/Scripting/Global/MediaGlobal.cs index 207df3f7ef..a72afb854a 100644 --- a/OpenRA.Mods.Common/Scripting/Global/MediaGlobal.cs +++ b/OpenRA.Mods.Common/Scripting/Global/MediaGlobal.cs @@ -10,8 +10,11 @@ using System; using System.Drawing; +using System.Linq; using Eluant; +using OpenRA.GameRules; using OpenRA.Scripting; +using OpenRA.Traits; namespace OpenRA.Mods.Common.Scripting { @@ -49,7 +52,7 @@ namespace OpenRA.Mods.Common.Scripting try { using (f) - f.Call(); + f.Call().Dispose(); } catch (LuaException e) { @@ -63,6 +66,46 @@ namespace OpenRA.Mods.Common.Scripting Media.PlayFMVFullscreen(world, movie, onComplete); } + MusicInfo previousMusic; + [Desc("Play track defined in music.yaml or keep it empty for a random song.")] + public void PlayMusic(string track = null, LuaFunction func = null) + { + if (!Game.Settings.Sound.MapMusic) + return; + + var music = world.Map.Rules.InstalledMusic.Select(a => a.Value).ToArray(); + if (!music.Any()) + return; + + var musicInfo = !string.IsNullOrEmpty(track) ? world.Map.Rules.Music[track] + : Game.Settings.Sound.Repeat && previousMusic != null ? previousMusic + : Game.Settings.Sound.Shuffle ? music.Random(Game.CosmeticRandom) + : previousMusic == null ? music.First() + : music.SkipWhile(s => s != previousMusic).Skip(1).First(); + + if (func != null) + { + var f = func.CopyReference() as LuaFunction; + onComplete = () => + { + try + { + using (f) + f.Call().Dispose(); + } + catch (LuaException e) + { + Context.FatalError(e.Message); + } + }; + } + else + onComplete = () => { }; + + Sound.PlayMusicThen(musicInfo, onComplete); + previousMusic = Sound.CurrentMusic; + } + [Desc("Display a text message to the player.")] public void DisplayMessage(string text, string prefix = "Mission") // TODO: expose HSLColor to Lua and add as parameter { diff --git a/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs b/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs index 304f905732..d490f9ceb1 100644 --- a/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs +++ b/OpenRA.Mods.Common/Scripting/Global/TriggerGlobal.cs @@ -41,7 +41,7 @@ namespace OpenRA.Mods.Common.Scripting try { using (f) - f.Call(); + f.Call().Dispose(); } catch (Exception e) { diff --git a/OpenRA.Mods.Common/Traits/World/PlayMusicOnMapLoad.cs b/OpenRA.Mods.Common/Traits/World/PlayMusicOnMapLoad.cs deleted file mode 100644 index 6fa5ded5db..0000000000 --- a/OpenRA.Mods.Common/Traits/World/PlayMusicOnMapLoad.cs +++ /dev/null @@ -1,50 +0,0 @@ -#region Copyright & License Information -/* - * Copyright 2007-2015 The OpenRA Developers (see AUTHORS) - * This file is part of OpenRA, which is free software. It is made - * available to you under the terms of the GNU General Public License - * as published by the Free Software Foundation. For more information, - * see COPYING. - */ -#endregion - -using System; -using OpenRA.Graphics; -using OpenRA.Traits; - -namespace OpenRA.Mods.Common.Traits -{ - class PlayMusicOnMapLoadInfo : ITraitInfo - { - public readonly string Music = null; - public readonly bool Loop = false; - - public object Create(ActorInitializer init) { return new PlayMusicOnMapLoad(init.World, this); } - } - - class PlayMusicOnMapLoad : IWorldLoaded - { - readonly PlayMusicOnMapLoadInfo info; - readonly World world; - - public PlayMusicOnMapLoad(World world, PlayMusicOnMapLoadInfo info) - { - this.world = world; - this.info = info; - } - - public void WorldLoaded(World world, WorldRenderer wr) - { - PlayMusic(); - } - - void PlayMusic() - { - var onComplete = info.Loop ? (Action)PlayMusic : () => { }; - - if (Game.Settings.Sound.MapMusic && - world.Map.Rules.Music.ContainsKey(info.Music)) - Sound.PlayMusicThen(world.Map.Rules.Music[info.Music], onComplete); - } - } -} \ No newline at end of file diff --git a/mods/cnc/maps/gdi01/gdi01.lua b/mods/cnc/maps/gdi01/gdi01.lua index 7dfb13fc1c..ad9cf8dd49 100644 --- a/mods/cnc/maps/gdi01/gdi01.lua +++ b/mods/cnc/maps/gdi01/gdi01.lua @@ -56,8 +56,13 @@ CheckForBase = function() return #baseBuildings >= 3 end -WorldLoaded = function() +initialSong = "aoi" +PlayMusic = function() + Media.PlayMusic(initialSong, PlayMusic) + initialSong = nil +end +WorldLoaded = function() player = Player.GetPlayer("GDI") enemy = Player.GetPlayer("Nod") @@ -85,6 +90,8 @@ WorldLoaded = function() ReinforceWithLandingCraft(MCVReinforcements, lstStart.Location + CVec.New(2, 0), lstEnd.Location + CVec.New(2, 0), mcvTarget.Location) Reinforce(InfantryReinforcements) + PlayMusic() + Trigger.OnIdle(Gunboat, function() SetGunboatPath(Gunboat) end) SendNodPatrol() diff --git a/mods/cnc/maps/gdi01/map.yaml b/mods/cnc/maps/gdi01/map.yaml index 1b106912a6..c9db5232a7 100644 --- a/mods/cnc/maps/gdi01/map.yaml +++ b/mods/cnc/maps/gdi01/map.yaml @@ -424,9 +424,6 @@ Rules: -SpawnMPUnits: -MPStartLocations: -CrateSpawner: - PlayMusicOnMapLoad: - Music: aoi - Loop: false LuaScript: Scripts: gdi01.lua ObjectivesPanel: diff --git a/mods/cnc/maps/shellmap/map.yaml b/mods/cnc/maps/shellmap/map.yaml index 0bd3072e06..2596ac7b31 100644 --- a/mods/cnc/maps/shellmap/map.yaml +++ b/mods/cnc/maps/shellmap/map.yaml @@ -992,9 +992,6 @@ Rules: -CrateSpawner: MenuPaletteEffect: Effect: Desaturated - PlayMusicOnMapLoad: - Music: map1 - Loop: true LuaScript: Scripts: shellmap.lua LST: diff --git a/mods/cnc/maps/shellmap/shellmap.lua b/mods/cnc/maps/shellmap/shellmap.lua index 2b72205e06..3a847feaa9 100644 --- a/mods/cnc/maps/shellmap/shellmap.lua +++ b/mods/cnc/maps/shellmap/shellmap.lua @@ -17,6 +17,7 @@ WorldLoaded = function() for i, unit in ipairs(units) do LoopTrack(unit, CPos.New(8, unit.Location.Y), CPos.New(87, unit.Location.Y)) end + PlayMusic() end LoopTrack = function(actor, left, right) @@ -25,6 +26,10 @@ LoopTrack = function(actor, left, right) actor.CallFunc(function() LoopTrack(actor, left, right) end) end +PlayMusic = function() + Media.PlayMusic("map1", PlayMusic) +end + LoadTransport = function(transport, passenger) transport.LoadPassenger(Actor.Create(passenger, false, { Owner = transport.Owner, Facing = transport.Facing })) end \ No newline at end of file