Add INotify(Activity)StanceChanged interfaces.

This commit is contained in:
Paul Chote
2019-05-19 15:29:56 +00:00
committed by abcdefg30
parent 07dc2a1132
commit 62b5d22e53
3 changed files with 39 additions and 0 deletions

View File

@@ -171,6 +171,20 @@ namespace OpenRA.Activities
act = act.childActivity; act = act.childActivity;
} }
} }
public IEnumerable<T> ActivitiesImplementing<T>() where T : IActivityInterface
{
if (childActivity != null)
foreach (var a in childActivity.ActivitiesImplementing<T>())
yield return a;
if (this is T)
yield return (T)(object)this;
if (NextActivity != null)
foreach (var a in NextActivity.ActivitiesImplementing<T>())
yield return a;
}
} }
public static class ActivityExts public static class ActivityExts

View File

@@ -350,6 +350,8 @@ namespace OpenRA.Traits
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1302:InterfaceNamesMustBeginWithI", Justification = "Not a real interface, but more like a tag.")] [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1302:InterfaceNamesMustBeginWithI", Justification = "Not a real interface, but more like a tag.")]
public interface Requires<T> where T : class, ITraitInfoInterface { } public interface Requires<T> where T : class, ITraitInfoInterface { }
public interface IActivityInterface { }
[RequireExplicitImplementation] [RequireExplicitImplementation]
public interface INotifySelected { void Selected(Actor self); } public interface INotifySelected { void Selected(Actor self); }
[RequireExplicitImplementation] [RequireExplicitImplementation]

View File

@@ -11,6 +11,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Activities;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Traits; using OpenRA.Traits;
@@ -18,6 +19,18 @@ namespace OpenRA.Mods.Common.Traits
{ {
public enum UnitStance { HoldFire, ReturnFire, Defend, AttackAnything } public enum UnitStance { HoldFire, ReturnFire, Defend, AttackAnything }
[RequireExplicitImplementation]
public interface IActivityNotifyStanceChanged : IActivityInterface
{
void StanceChanged(Actor self, AutoTarget autoTarget, UnitStance oldStance, UnitStance newStance);
}
[RequireExplicitImplementation]
public interface INotifyStanceChanged
{
void StanceChanged(Actor self, AutoTarget autoTarget, UnitStance oldStance, UnitStance newStance);
}
[Desc("The actor will automatically engage the enemy when it is in range.")] [Desc("The actor will automatically engage the enemy when it is in range.")]
public class AutoTargetInfo : ConditionalTraitInfo, Requires<AttackBaseInfo>, IEditorActorOptions public class AutoTargetInfo : ConditionalTraitInfo, Requires<AttackBaseInfo>, IEditorActorOptions
{ {
@@ -126,6 +139,7 @@ namespace OpenRA.Mods.Common.Traits
UnitStance stance; UnitStance stance;
ConditionManager conditionManager; ConditionManager conditionManager;
IDisableAutoTarget[] disableAutoTarget; IDisableAutoTarget[] disableAutoTarget;
INotifyStanceChanged[] notifyStanceChanged;
IEnumerable<AutoTargetPriorityInfo> activeTargetPriorities; IEnumerable<AutoTargetPriorityInfo> activeTargetPriorities;
int conditionToken = ConditionManager.InvalidConditionToken; int conditionToken = ConditionManager.InvalidConditionToken;
@@ -134,8 +148,16 @@ namespace OpenRA.Mods.Common.Traits
if (stance == value) if (stance == value)
return; return;
var oldStance = stance;
stance = value; stance = value;
ApplyStanceCondition(self); ApplyStanceCondition(self);
foreach (var nsc in notifyStanceChanged)
nsc.StanceChanged(self, this, oldStance, stance);
if (self.CurrentActivity != null)
foreach (var a in self.CurrentActivity.ActivitiesImplementing<IActivityNotifyStanceChanged>())
a.StanceChanged(self, this, oldStance, stance);
} }
void ApplyStanceCondition(Actor self) void ApplyStanceCondition(Actor self)
@@ -176,6 +198,7 @@ namespace OpenRA.Mods.Common.Traits
conditionManager = self.TraitOrDefault<ConditionManager>(); conditionManager = self.TraitOrDefault<ConditionManager>();
disableAutoTarget = self.TraitsImplementing<IDisableAutoTarget>().ToArray(); disableAutoTarget = self.TraitsImplementing<IDisableAutoTarget>().ToArray();
notifyStanceChanged = self.TraitsImplementing<INotifyStanceChanged>().ToArray();
ApplyStanceCondition(self); ApplyStanceCondition(self);
} }