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

@@ -416,7 +416,6 @@
<Compile Include="Traits\World\PathfinderDebugOverlay.cs" />
<Compile Include="Traits\World\PathSearch.cs" />
<Compile Include="Traits\World\PlayerPaletteFromCurrentTileset.cs" />
<Compile Include="Traits\World\PlayMusicOnMapLoad.cs" />
<Compile Include="Traits\World\RadarPings.cs" />
<Compile Include="Traits\World\ResourceClaim.cs" />
<Compile Include="Traits\World\ResourceClaimLayer.cs" />

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
{

View File

@@ -41,7 +41,7 @@ namespace OpenRA.Mods.Common.Scripting
try
{
using (f)
f.Call();
f.Call().Dispose();
}
catch (Exception e)
{

View File

@@ -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);
}
}
}