Replace IConditionConsumer w/ variable observers for multiple variable expressions for traits.

This commit is contained in:
atlimit8
2017-03-10 22:10:46 -06:00
parent 843ac85c92
commit d433473249
4 changed files with 49 additions and 40 deletions

View File

@@ -10,14 +10,13 @@
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Support;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
/// <summary>Use as base class for *Info to subclass of UpgradableTrait. (See UpgradableTrait.)</summary>
public abstract class ConditionalTraitInfo : IConditionConsumerInfo, IRulesetLoaded
public abstract class ConditionalTraitInfo : IObservesVariablesInfo, IRulesetLoaded
{
static readonly IReadOnlyDictionary<string, int> NoConditions = new ReadOnlyDictionary<string, int>(new Dictionary<string, int>());
@@ -43,19 +42,15 @@ namespace OpenRA.Mods.Common.Traits
/// Requires basing *Info on UpgradableTraitInfo and using base(info) constructor.
/// TraitEnabled will be called at creation if the trait starts enabled or does not use conditions.
/// </summary>
public abstract class ConditionalTrait<InfoType> : IConditionConsumer, IDisabledTrait, INotifyCreated, ISync where InfoType : ConditionalTraitInfo
public abstract class ConditionalTrait<InfoType> : IObservesVariables, IDisabledTrait, INotifyCreated, ISync where InfoType : ConditionalTraitInfo
{
public readonly InfoType Info;
IEnumerable<string> IConditionConsumer.Conditions
// Overrides must call `base.GetVariableObservers()` to avoid breaking RequiresCondition.
public virtual IEnumerable<VariableObserver> GetVariableObservers()
{
get
{
if (Info.RequiresCondition != null)
return Info.RequiresCondition.Variables;
return Enumerable.Empty<string>();
}
if (Info.RequiresCondition != null)
yield return new VariableObserver(RequiredConditionsChanged, Info.RequiresCondition.Variables);
}
[Sync] public bool IsTraitDisabled { get; private set; }
@@ -65,7 +60,7 @@ namespace OpenRA.Mods.Common.Traits
Info = info;
// Conditional traits will be enabled (if appropriate) by the ConditionManager
// calling IConditionConsumer.ConditionsChanged at the end of INotifyCreated.
// calling ConditionConsumers at the end of INotifyCreated.
IsTraitDisabled = Info.RequiresCondition != null;
}
@@ -77,7 +72,7 @@ namespace OpenRA.Mods.Common.Traits
void INotifyCreated.Created(Actor self) { Created(self); }
void IConditionConsumer.ConditionsChanged(Actor self, IReadOnlyDictionary<string, int> conditions)
void RequiredConditionsChanged(Actor self, IReadOnlyDictionary<string, int> conditions)
{
if (Info.RequiresCondition == null)
return;