|
| 1 | +#region Copyright & License Information |
| 2 | +/* |
| 3 | + * Copyright (c) The OpenRA Developers and Contributors |
| 4 | + * This file is part of OpenRA, which is free software. It is made |
| 5 | + * available to you under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation, either version 3 of |
| 7 | + * the License, or (at your option) any later version. For more |
| 8 | + * information, see COPYING. |
| 9 | + */ |
| 10 | +#endregion |
| 11 | + |
| 12 | +using OpenRA.Traits; |
| 13 | +using OpenRA.Mods.Common.Traits; |
| 14 | + |
| 15 | +namespace OpenRA.Mods.CA.Traits |
| 16 | +{ |
| 17 | + [Desc("Grants a condition to this actor when the player has stored funds (cash plus resources).")] |
| 18 | + public class GrantConditionOnPlayerFundsInfo : TraitInfo |
| 19 | + { |
| 20 | + [FieldLoader.Require] |
| 21 | + [GrantedConditionReference] |
| 22 | + [Desc("Condition to grant.")] |
| 23 | + public readonly string Condition = null; |
| 24 | + |
| 25 | + [Desc("Enable condition when funds are greater than this.")] |
| 26 | + public readonly int Threshold = 0; |
| 27 | + |
| 28 | + public override object Create(ActorInitializer init) { return new GrantConditionOnPlayerFunds(this); } |
| 29 | + } |
| 30 | + |
| 31 | + public class GrantConditionOnPlayerFunds : INotifyCreated, INotifyOwnerChanged, ITick |
| 32 | + { |
| 33 | + readonly GrantConditionOnPlayerFundsInfo info; |
| 34 | + PlayerResources playerResources; |
| 35 | + |
| 36 | + int conditionToken = Actor.InvalidConditionToken; |
| 37 | + |
| 38 | + public GrantConditionOnPlayerFunds(GrantConditionOnPlayerFundsInfo info) |
| 39 | + { |
| 40 | + this.info = info; |
| 41 | + } |
| 42 | + |
| 43 | + void INotifyCreated.Created(Actor self) |
| 44 | + { |
| 45 | + playerResources = self.Owner.PlayerActor.Trait<PlayerResources>(); |
| 46 | + } |
| 47 | + |
| 48 | + void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) |
| 49 | + { |
| 50 | + playerResources = newOwner.PlayerActor.Trait<PlayerResources>(); |
| 51 | + } |
| 52 | + |
| 53 | + void ITick.Tick(Actor self) |
| 54 | + { |
| 55 | + if (string.IsNullOrEmpty(info.Condition)) |
| 56 | + return; |
| 57 | + |
| 58 | + var enabled = playerResources.GetCashAndResources() > info.Threshold; |
| 59 | + if (enabled && conditionToken == Actor.InvalidConditionToken) |
| 60 | + conditionToken = self.GrantCondition(info.Condition); |
| 61 | + else if (!enabled && conditionToken != Actor.InvalidConditionToken) |
| 62 | + conditionToken = self.RevokeCondition(conditionToken); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments