From d9f8afdbe51d81b4cbf49b928e200e85dfbdadce Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Wed, 5 Dec 2018 14:21:48 +1300 Subject: [PATCH] Add GrantExternalConditionToProduced trait. --- OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 1 + .../GrantExternalConditionToProduced.cs | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 OpenRA.Mods.Common/Traits/GrantExternalConditionToProduced.cs diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index 24d17a8fe8..6f06730afd 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -312,6 +312,7 @@ + diff --git a/OpenRA.Mods.Common/Traits/GrantExternalConditionToProduced.cs b/OpenRA.Mods.Common/Traits/GrantExternalConditionToProduced.cs new file mode 100644 index 0000000000..930e958c16 --- /dev/null +++ b/OpenRA.Mods.Common/Traits/GrantExternalConditionToProduced.cs @@ -0,0 +1,47 @@ +#region Copyright & License Information +/* + * Copyright 2007-2018 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, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System.Linq; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits +{ + [Desc("Grants a condition to actors produced by this actor.")] + public class GrantExternalConditionToProducedInfo : ConditionalTraitInfo + { + [FieldLoader.Require] + [Desc("The condition to apply. Must be included in the produced actor's ExternalConditions list.")] + public readonly string Condition = null; + + [Desc("Duration of the condition (in ticks). Set to 0 for a permanent condition.")] + public readonly int Duration = 0; + + public override object Create(ActorInitializer init) { return new GrantExternalConditionToProduced(init.Self, this); } + } + + public class GrantExternalConditionToProduced : ConditionalTrait, INotifyProduction + { + public GrantExternalConditionToProduced(Actor self, GrantExternalConditionToProducedInfo info) + : base(info) { } + + void INotifyProduction.UnitProduced(Actor self, Actor other, CPos exit) + { + if (IsTraitDisabled || other.IsDead) + return; + + var external = other.TraitsImplementing() + .FirstOrDefault(t => t.Info.Condition == Info.Condition && t.CanGrantCondition(other, self)); + + if (external != null) + external.GrantCondition(other, self, Info.Duration); + } + } +} \ No newline at end of file