Implements victory and defeat music.

This commit is contained in:
Zimmermann Gyula
2015-07-13 14:50:01 +02:00
parent 4297d06ba3
commit 57e684d1d9
9 changed files with 69 additions and 5 deletions

View File

@@ -352,6 +352,8 @@ namespace OpenRA.Traits
void OnObjectiveFailed(Player player, int objectiveID); void OnObjectiveFailed(Player player, int objectiveID);
} }
public interface IGameOver { void GameOver(World world); }
public interface IWarhead public interface IWarhead
{ {
int Delay { get; } int Delay { get; }

View File

@@ -17,14 +17,31 @@ namespace OpenRA.Traits
[Desc("Trait for music handling. Attach this to the world actor.")] [Desc("Trait for music handling. Attach this to the world actor.")]
public class MusicPlaylistInfo : ITraitInfo public class MusicPlaylistInfo : ITraitInfo
{ {
[Desc("Music to play when the map starts.", "Plays the first song on the playlist when undefined.")]
public readonly string StartingMusic = null; public readonly string StartingMusic = null;
[Desc("Should the starting music loop?")]
public readonly bool LoopStartingMusic = false; public readonly bool LoopStartingMusic = false;
[Desc("Music to play when the game has been won.")]
public readonly string VictoryMusic = null;
[Desc("Should the victory music loop?")]
public readonly bool LoopVictoryMusic = false;
[Desc("Music to play when the game has been lost.")]
public readonly string DefeatMusic = null;
[Desc("Should the defeat music loop?")]
public readonly bool LoopDefeatMusic = false;
public object Create(ActorInitializer init) { return new MusicPlaylist(init.World, this); } public object Create(ActorInitializer init) { return new MusicPlaylist(init.World, this); }
} }
public class MusicPlaylist : INotifyActorDisposing public class MusicPlaylist : INotifyActorDisposing, IGameOver
{ {
readonly MusicPlaylistInfo info;
readonly MusicInfo[] random; readonly MusicInfo[] random;
readonly MusicInfo[] playlist; readonly MusicInfo[] playlist;
@@ -35,6 +52,8 @@ namespace OpenRA.Traits
public MusicPlaylist(World world, MusicPlaylistInfo info) public MusicPlaylist(World world, MusicPlaylistInfo info)
{ {
this.info = info;
IsMusicAvailable = world.Map.Rules.InstalledMusic.Any(); IsMusicAvailable = world.Map.Rules.InstalledMusic.Any();
playlist = world.Map.Rules.InstalledMusic.Select(a => a.Value).ToArray(); playlist = world.Map.Rules.InstalledMusic.Select(a => a.Value).ToArray();
@@ -71,6 +90,40 @@ namespace OpenRA.Traits
return playlist; return playlist;
} }
public void GameOver(World world)
{
if (!IsMusicAvailable)
return;
var playedSong = currentSong;
if (world.LocalPlayer.WinState == WinState.Won)
{
if (!string.IsNullOrEmpty(info.VictoryMusic)
&& world.Map.Rules.Music.ContainsKey(info.VictoryMusic)
&& world.Map.Rules.Music[info.VictoryMusic].Exists)
{
currentSong = world.Map.Rules.Music[info.VictoryMusic];
repeat = info.LoopVictoryMusic;
}
}
else
{
// Most RTS treats observers losing the game,
// no need for a special handling involving them here.
if (!string.IsNullOrEmpty(info.DefeatMusic)
&& world.Map.Rules.Music.ContainsKey(info.DefeatMusic)
&& world.Map.Rules.Music[info.DefeatMusic].Exists)
{
currentSong = world.Map.Rules.Music[info.DefeatMusic];
repeat = info.LoopDefeatMusic;
}
}
if (playedSong != currentSong)
Play();
}
void Play() void Play()
{ {
if (currentSong == null || !IsMusicAvailable) if (currentSong == null || !IsMusicAvailable)

View File

@@ -58,6 +58,10 @@ namespace OpenRA
if (!gameOver) if (!gameOver)
{ {
gameOver = true; gameOver = true;
foreach (var t in WorldActor.TraitsImplementing<IGameOver>())
t.GameOver(this);
GameOver(); GameOver();
} }
} }

View File

@@ -72,9 +72,6 @@ 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()

View File

@@ -4,6 +4,8 @@
ScreenMap: ScreenMap:
ActorMap: ActorMap:
MusicPlaylist: MusicPlaylist:
VictoryMusic: win1
DefeatMusic: heart
TerrainGeometryOverlay: TerrainGeometryOverlay:
ShroudRenderer: ShroudRenderer:
ShroudVariants: typea, typeb, typec, typed ShroudVariants: typea, typeb, typec, typed

View File

@@ -126,7 +126,7 @@ Rules:
LuaScript: LuaScript:
Scripts: shellmap.lua Scripts: shellmap.lua
MusicPlaylist: MusicPlaylist:
StartingMusic: score StartingMusic: waitgame
GlobalLightingPaletteEffect: GlobalLightingPaletteEffect:
rockettower: rockettower:
Power: Power:

View File

@@ -4,6 +4,8 @@
ScreenMap: ScreenMap:
ActorMap: ActorMap:
MusicPlaylist: MusicPlaylist:
VictoryMusic: score
DefeatMusic: options
TerrainGeometryOverlay: TerrainGeometryOverlay:
ShroudRenderer: ShroudRenderer:
ShroudVariants: typea, typeb, typec, typed ShroudVariants: typea, typeb, typec, typed

View File

@@ -4,6 +4,8 @@
ActorMap: ActorMap:
ScreenMap: ScreenMap:
MusicPlaylist: MusicPlaylist:
VictoryMusic: score
DefeatMusic: map
TerrainGeometryOverlay: TerrainGeometryOverlay:
LoadWidgetAtGameStart: LoadWidgetAtGameStart:
ShroudRenderer: ShroudRenderer:

View File

@@ -4,6 +4,8 @@
ScreenMap: ScreenMap:
ActorMap: ActorMap:
MusicPlaylist: MusicPlaylist:
VictoryMusic: score
DefeatMusic: maps
LoadWidgetAtGameStart: LoadWidgetAtGameStart:
ShroudRenderer: ShroudRenderer:
Index: 255, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 20, 40, 56, 65, 97, 130, 148, 194, 24, 33, 66, 132, 28, 41, 67, 134, 1, 2, 4, 8, 3, 6, 12, 9, 7, 14, 13, 11, 5, 10, 15, 255 Index: 255, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 20, 40, 56, 65, 97, 130, 148, 194, 24, 33, 66, 132, 28, 41, 67, 134, 1, 2, 4, 8, 3, 6, 12, 9, 7, 14, 13, 11, 5, 10, 15, 255