1 #region Copyright & License Information
2 /*
3  * Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
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 
14 namespace OpenRA.Mods.Common.Traits
15 {
16 	public class GrantConditionWhileAimingInfo : ITraitInfo
17 	{
18 		[FieldLoader.Require]
19 		[GrantedConditionReference]
20 		[Desc("The condition to grant while aiming.")]
21 		public readonly string Condition = null;
22 
ITraitInfo.Create(ActorInitializer init)23 		object ITraitInfo.Create(ActorInitializer init) { return new GrantConditionWhileAiming(this); }
24 	}
25 
26 	public class GrantConditionWhileAiming : INotifyCreated, INotifyAiming
27 	{
28 		readonly GrantConditionWhileAimingInfo info;
29 
30 		ConditionManager conditionManager;
31 		int conditionToken = ConditionManager.InvalidConditionToken;
32 
GrantConditionWhileAiming(GrantConditionWhileAimingInfo info)33 		public GrantConditionWhileAiming(GrantConditionWhileAimingInfo info)
34 		{
35 			this.info = info;
36 		}
37 
INotifyCreated.Created(Actor self)38 		void INotifyCreated.Created(Actor self)
39 		{
40 			conditionManager = self.TraitOrDefault<ConditionManager>();
41 		}
42 
INotifyAiming.StartedAiming(Actor self, AttackBase attack)43 		void INotifyAiming.StartedAiming(Actor self, AttackBase attack)
44 		{
45 			if (conditionToken == ConditionManager.InvalidConditionToken)
46 				conditionToken = conditionManager.GrantCondition(self, info.Condition);
47 		}
48 
INotifyAiming.StoppedAiming(Actor self, AttackBase attack)49 		void INotifyAiming.StoppedAiming(Actor self, AttackBase attack)
50 		{
51 			if (conditionToken != ConditionManager.InvalidConditionToken)
52 				conditionToken = conditionManager.RevokeCondition(self, conditionToken);
53 		}
54 	}
55 }
56