From 80842fd4b855f71bea8a0c7585ca827212132036 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 18 Nov 2018 16:01:58 +0000 Subject: [PATCH] Add GrantConditionOnPlayerResources trait. --- OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 1 + .../GrantConditionOnPlayerResources.cs | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnPlayerResources.cs diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index bb30a78172..658208b07b 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -314,6 +314,7 @@ + diff --git a/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnPlayerResources.cs b/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnPlayerResources.cs new file mode 100644 index 0000000000..45a7263fbb --- /dev/null +++ b/OpenRA.Mods.Common/Traits/Conditions/GrantConditionOnPlayerResources.cs @@ -0,0 +1,72 @@ +#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 this actor when the player has stored resources.")] + public class GrantConditionOnPlayerResourcesInfo : ITraitInfo + { + [FieldLoader.Require] + [GrantedConditionReference] + [Desc("Condition to grant.")] + public readonly string Condition = null; + + [Desc("Enable condition when the amount of stored resources is greater than this.")] + public readonly int Threshold = 0; + + public object Create(ActorInitializer init) { return new GrantConditionOnPlayerResources(this); } + } + + public class GrantConditionOnPlayerResources : INotifyCreated, INotifyOwnerChanged, ITick + { + readonly GrantConditionOnPlayerResourcesInfo info; + PlayerResources playerResources; + + ConditionManager conditionManager; + int conditionToken = ConditionManager.InvalidConditionToken; + + public GrantConditionOnPlayerResources(GrantConditionOnPlayerResourcesInfo info) + { + this.info = info; + } + + void INotifyCreated.Created(Actor self) + { + // Special case handling is required for the Player actor. + // Created is called before Player.PlayerActor is assigned, + // so we must query other player traits from self, knowing that + // it refers to the same actor as self.Owner.PlayerActor + var playerActor = self.Info.Name == "player" ? self : self.Owner.PlayerActor; + playerResources = playerActor.Trait(); + conditionManager = self.TraitOrDefault(); + } + + void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) + { + playerResources = newOwner.PlayerActor.Trait(); + } + + void ITick.Tick(Actor self) + { + if (string.IsNullOrEmpty(info.Condition) || conditionManager == null) + return; + + var enabled = playerResources.Resources > info.Threshold; + if (enabled && conditionToken == ConditionManager.InvalidConditionToken) + conditionToken = conditionManager.GrantCondition(self, info.Condition); + else if (!enabled && conditionToken != ConditionManager.InvalidConditionToken) + conditionToken = conditionManager.RevokeCondition(self, conditionToken); + } + } +}