#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.Linq; using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits { public class ConquestVictoryConditionsInfo : ITraitInfo, Requires { [Desc("Delay for the end game notification in milliseconds.")] public readonly int NotificationDelay = 1500; [Desc("Description of the objective.")] [Translate] public readonly string Objective = "Destroy all opposition!"; public object Create(ActorInitializer init) { return new ConquestVictoryConditions(init.Self, this); } } public class ConquestVictoryConditions : ITick, INotifyObjectivesUpdated { readonly ConquestVictoryConditionsInfo info; readonly MissionObjectives mo; int objectiveID = -1; public ConquestVictoryConditions(Actor self, ConquestVictoryConditionsInfo cvcInfo) { info = cvcInfo; mo = self.Trait(); } public void Tick(Actor self) { if (self.Owner.WinState != WinState.Undefined || self.Owner.NonCombatant) return; if (objectiveID < 0) objectiveID = mo.Add(self.Owner, info.Objective, ObjectiveType.Primary, true); if (!self.Owner.NonCombatant && self.Owner.HasNoRequiredUnits()) mo.MarkFailed(self.Owner, objectiveID); var others = self.World.Players.Where(p => !p.NonCombatant && !p.IsAlliedWith(self.Owner)); if (!others.Any()) return; if (others.All(p => p.WinState == WinState.Lost)) mo.MarkCompleted(self.Owner, objectiveID); } public void OnPlayerLost(Player player) { Game.Debug("{0} is defeated.", player.PlayerName); foreach (var a in player.World.Actors.Where(a => a.Owner == player)) a.Kill(a); if (player == player.World.LocalPlayer) { Game.RunAfterDelay(info.NotificationDelay, () => { if (Game.IsCurrentWorld(player.World)) Sound.PlayNotification(player.World.Map.Rules, player, "Speech", "Lose", player.Country.Race); }); } } public void OnPlayerWon(Player player) { Game.Debug("{0} is victorious.", player.PlayerName); if (player == player.World.LocalPlayer) Game.RunAfterDelay(info.NotificationDelay, () => Sound.PlayNotification(player.World.Map.Rules, player, "Speech", "Win", player.Country.Race)); } public void OnObjectiveAdded(Player player, int id) { } public void OnObjectiveCompleted(Player player, int id) { } public void OnObjectiveFailed(Player player, int id) { } } }