Merge pull request #6880 from Mailaender/lua-music

Added Media.PlayMusic to the Lua API
This commit is contained in:
Oliver Brakmann
2015-01-25 15:05:47 +01:00
16 changed files with 106 additions and 61 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,52 @@ 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("Stop the current song.")]
public void StopMusic()
{
Sound.StopMusic();
}
[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

@@ -695,6 +695,18 @@ namespace OpenRA.Mods.Common.UtilityCommands
if (depth == 1 && node.Key == "MustBeDestroyed") if (depth == 1 && node.Key == "MustBeDestroyed")
node.Value.Nodes.Add(new MiniYamlNode("RequiredForShortGame", "true")); node.Value.Nodes.Add(new MiniYamlNode("RequiredForShortGame", "true"));
// Remove PlayMusicOnMapLoad
if (engineVersion < 20150125)
{
if (depth == 0 && node.Value.Nodes.Exists(n => n.Key == "PlayMusicOnMapLoad"))
{
node.Value.Nodes.RemoveAll(n => n.Key == "PlayMusicOnMapLoad");
Console.WriteLine("The 'PlayMusicOnMapLoad' trait has been removed.");
Console.WriteLine("Please use the Lua API function 'PlayMusic' instead.");
Console.WriteLine("See http://wiki.openra.net/Lua-API for details.");
}
}
UpgradeActorRules(engineVersion, ref node.Value.Nodes, node, depth + 1); UpgradeActorRules(engineVersion, ref node.Value.Nodes, node, depth + 1);
} }
} }

View File

@@ -11,6 +11,7 @@
using System; using System;
using System.Linq; using System.Linq;
using OpenRA.GameRules; using OpenRA.GameRules;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Traits; using OpenRA.Traits;
using OpenRA.Widgets; using OpenRA.Widgets;
@@ -96,6 +97,18 @@ namespace OpenRA.Mods.RA.Widgets.Logic
installButton.IsVisible = () => modRules.InstalledMusic.ToArray().Length <= installData.ShippedSoundtracks; installButton.IsVisible = () => modRules.InstalledMusic.ToArray().Length <= installData.ShippedSoundtracks;
} }
var songWatcher = widget.GetOrNull<LogicTickerWidget>("SONG_WATCHER");
if (songWatcher != null)
{
songWatcher.OnTick = () =>
{
if (Sound.CurrentMusic == null || currentSong == Sound.CurrentMusic)
return;
currentSong = Sound.CurrentMusic;
};
}
panel.Get<ButtonWidget>("BACK_BUTTON").OnClick = () => { Game.Settings.Save(); Ui.CloseWindow(); onExit(); }; panel.Get<ButtonWidget>("BACK_BUTTON").OnClick = () => { Game.Settings.Save(); Ui.CloseWindow(); onExit(); };
} }

View File

@@ -5,6 +5,7 @@ Container@MUSIC_PANEL:
Width: 360 Width: 360
Height: 435 Height: 435
Children: Children:
LogicTicker@SONG_WATCHER:
Label@TITLE: Label@TITLE:
Width: 360 Width: 360
Y: 0-25 Y: 0-25

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")
@@ -73,6 +78,9 @@ WorldLoaded = function()
Trigger.OnPlayerWon(player, function() Trigger.OnPlayerWon(player, function()
Media.PlaySpeechNotification(player, "Win") Media.PlaySpeechNotification(player, "Win")
Trigger.AfterDelay(DateTime.Seconds(1), function()
Media.PlayMusic("win1")
end)
end) end)
Trigger.OnPlayerLost(player, function() Trigger.OnPlayerLost(player, function()
@@ -85,6 +93,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

View File

@@ -16,4 +16,5 @@ WorldLoaded = function()
atreides = Player.GetPlayer("Atreides") atreides = Player.GetPlayer("Atreides")
InsertHarvester() InsertHarvester()
Media.PlayMusic("score")
end end

View File

@@ -5,6 +5,7 @@ Background@MUSIC_PANEL:
Width: 360 Width: 360
Height: 450 Height: 450
Children: Children:
LogicTicker@SONG_WATCHER:
ScrollPanel@MUSIC_LIST: ScrollPanel@MUSIC_LIST:
X: 15 X: 15
Y: 45 Y: 45

View File

@@ -162,4 +162,6 @@ WorldLoaded = function()
SendSovietUnits(Entry5.Location, UnitTypes, 50) SendSovietUnits(Entry5.Location, UnitTypes, 50)
SendSovietUnits(Entry6.Location, UnitTypes, 50) SendSovietUnits(Entry6.Location, UnitTypes, 50)
SendSovietUnits(Entry7.Location, BeachUnitTypes, 15) SendSovietUnits(Entry7.Location, BeachUnitTypes, 15)
Media.PlayMusic()
end end

View File

@@ -40,7 +40,8 @@ Rules:
-StartGameNotification: -StartGameNotification:
-SpawnMPUnits: -SpawnMPUnits:
-MPStartLocations: -MPStartLocations:
LoadWidgetAtGameStart: LuaScript:
Scripts: shellmap.lua
Sequences: Sequences:

View File

@@ -0,0 +1,7 @@
PlayMusic = function()
Media.PlayMusic("maps", PlayMusic)
end
WorldLoaded = function()
PlayMusic()
end