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\PathfinderDebugOverlay.cs" />
<Compile Include="Traits\World\PathSearch.cs" /> <Compile Include="Traits\World\PathSearch.cs" />
<Compile Include="Traits\World\PlayerPaletteFromCurrentTileset.cs" /> <Compile Include="Traits\World\PlayerPaletteFromCurrentTileset.cs" />
<Compile Include="Traits\World\PlayMusicOnMapLoad.cs" />
<Compile Include="Traits\World\RadarPings.cs" /> <Compile Include="Traits\World\RadarPings.cs" />
<Compile Include="Traits\World\ResourceClaim.cs" /> <Compile Include="Traits\World\ResourceClaim.cs" />
<Compile Include="Traits\World\ResourceClaimLayer.cs" /> <Compile Include="Traits\World\ResourceClaimLayer.cs" />

View File

@@ -10,8 +10,11 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Linq;
using Eluant; using Eluant;
using OpenRA.GameRules;
using OpenRA.Scripting; using OpenRA.Scripting;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Scripting namespace OpenRA.Mods.Common.Scripting
{ {
@@ -49,7 +52,7 @@ namespace OpenRA.Mods.Common.Scripting
try try
{ {
using (f) using (f)
f.Call(); f.Call().Dispose();
} }
catch (LuaException e) catch (LuaException e)
{ {
@@ -63,6 +66,46 @@ namespace OpenRA.Mods.Common.Scripting
Media.PlayFMVFullscreen(world, movie, onComplete); 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.")] [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 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 try
{ {
using (f) using (f)
f.Call(); f.Call().Dispose();
} }
catch (Exception e) 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);
}
}
}

View File

@@ -56,8 +56,13 @@ CheckForBase = function()
return #baseBuildings >= 3 return #baseBuildings >= 3
end end
WorldLoaded = function() initialSong = "aoi"
PlayMusic = function()
Media.PlayMusic(initialSong, PlayMusic)
initialSong = nil
end
WorldLoaded = function()
player = Player.GetPlayer("GDI") player = Player.GetPlayer("GDI")
enemy = Player.GetPlayer("Nod") 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) ReinforceWithLandingCraft(MCVReinforcements, lstStart.Location + CVec.New(2, 0), lstEnd.Location + CVec.New(2, 0), mcvTarget.Location)
Reinforce(InfantryReinforcements) Reinforce(InfantryReinforcements)
PlayMusic()
Trigger.OnIdle(Gunboat, function() SetGunboatPath(Gunboat) end) Trigger.OnIdle(Gunboat, function() SetGunboatPath(Gunboat) end)
SendNodPatrol() SendNodPatrol()

View File

@@ -424,9 +424,6 @@ Rules:
-SpawnMPUnits: -SpawnMPUnits:
-MPStartLocations: -MPStartLocations:
-CrateSpawner: -CrateSpawner:
PlayMusicOnMapLoad:
Music: aoi
Loop: false
LuaScript: LuaScript:
Scripts: gdi01.lua Scripts: gdi01.lua
ObjectivesPanel: ObjectivesPanel:

View File

@@ -992,9 +992,6 @@ Rules:
-CrateSpawner: -CrateSpawner:
MenuPaletteEffect: MenuPaletteEffect:
Effect: Desaturated Effect: Desaturated
PlayMusicOnMapLoad:
Music: map1
Loop: true
LuaScript: LuaScript:
Scripts: shellmap.lua Scripts: shellmap.lua
LST: LST:

View File

@@ -17,6 +17,7 @@ WorldLoaded = function()
for i, unit in ipairs(units) do for i, unit in ipairs(units) do
LoopTrack(unit, CPos.New(8, unit.Location.Y), CPos.New(87, unit.Location.Y)) LoopTrack(unit, CPos.New(8, unit.Location.Y), CPos.New(87, unit.Location.Y))
end end
PlayMusic()
end end
LoopTrack = function(actor, left, right) LoopTrack = function(actor, left, right)
@@ -25,6 +26,10 @@ LoopTrack = function(actor, left, right)
actor.CallFunc(function() LoopTrack(actor, left, right) end) actor.CallFunc(function() LoopTrack(actor, left, right) end)
end end
PlayMusic = function()
Media.PlayMusic("map1", PlayMusic)
end
LoadTransport = function(transport, passenger) LoadTransport = function(transport, passenger)
transport.LoadPassenger(Actor.Create(passenger, false, { Owner = transport.Owner, Facing = transport.Facing })) transport.LoadPassenger(Actor.Create(passenger, false, { Owner = transport.Owner, Facing = transport.Facing }))
end end