replace PlayMusicOnMapLoad trait with Lua Media.PlayMusic method

This commit is contained in:
Matthias Mailänder
2014-11-01 19:11:39 +01:00
parent 98fd875a43
commit 3489794713
8 changed files with 58 additions and 60 deletions

View File

@@ -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
{