Convert ProximityExternalCondition from disable-able to conditional.

This commit is contained in:
atlimit8
2017-03-11 10:33:40 -06:00
parent 770efd710b
commit 18eddf4a70
2 changed files with 30 additions and 26 deletions

View File

@@ -16,7 +16,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[Desc("Applies a condition to actors within a specified range.")] [Desc("Applies a condition to actors within a specified range.")]
public class ProximityExternalConditionInfo : ITraitInfo public class ProximityExternalConditionInfo : ConditionalTraitInfo
{ {
[FieldLoader.Require] [FieldLoader.Require]
[Desc("The condition to apply. Must be included in the target actor's ExternalConditions list.")] [Desc("The condition to apply. Must be included in the target actor's ExternalConditions list.")]
@@ -38,12 +38,11 @@ namespace OpenRA.Mods.Common.Traits
public readonly string EnableSound = null; public readonly string EnableSound = null;
public readonly string DisableSound = null; public readonly string DisableSound = null;
public object Create(ActorInitializer init) { return new ProximityExternalCondition(init.Self, this); } public override object Create(ActorInitializer init) { return new ProximityExternalCondition(init.Self, this); }
} }
public class ProximityExternalCondition : ITick, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyOtherProduction public class ProximityExternalCondition : ConditionalTrait<ProximityExternalConditionInfo>, ITick, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyOtherProduction
{ {
readonly ProximityExternalConditionInfo info;
readonly Actor self; readonly Actor self;
readonly Dictionary<Actor, int> tokens = new Dictionary<Actor, int>(); readonly Dictionary<Actor, int> tokens = new Dictionary<Actor, int>();
@@ -55,14 +54,12 @@ namespace OpenRA.Mods.Common.Traits
WDist cachedVRange; WDist cachedVRange;
WDist desiredVRange; WDist desiredVRange;
bool cachedDisabled = true;
public ProximityExternalCondition(Actor self, ProximityExternalConditionInfo info) public ProximityExternalCondition(Actor self, ProximityExternalConditionInfo info)
: base(info)
{ {
this.info = info;
this.self = self; this.self = self;
cachedRange = info.Range; cachedRange = WDist.Zero;
cachedVRange = info.MaximumVerticalOffset; cachedVRange = WDist.Zero;
} }
public void AddedToWorld(Actor self) public void AddedToWorld(Actor self)
@@ -76,18 +73,22 @@ namespace OpenRA.Mods.Common.Traits
self.World.ActorMap.RemoveProximityTrigger(proximityTrigger); self.World.ActorMap.RemoveProximityTrigger(proximityTrigger);
} }
public void Tick(Actor self) protected override void TraitEnabled(Actor self)
{ {
var disabled = self.IsDisabled(); Game.Sound.Play(SoundType.World, Info.EnableSound, self.CenterPosition);
desiredRange = Info.Range;
if (cachedDisabled != disabled) desiredVRange = Info.MaximumVerticalOffset;
{
Game.Sound.Play(SoundType.World, disabled ? info.DisableSound : info.EnableSound, self.CenterPosition);
desiredRange = disabled ? WDist.Zero : info.Range;
desiredVRange = disabled ? WDist.Zero : info.MaximumVerticalOffset;
cachedDisabled = disabled;
} }
protected override void TraitDisabled(Actor self)
{
Game.Sound.Play(SoundType.World, Info.DisableSound, self.CenterPosition);
desiredRange = WDist.Zero;
desiredVRange = WDist.Zero;
}
public void Tick(Actor self)
{
if (self.CenterPosition != cachedPosition || desiredRange != cachedRange || desiredVRange != cachedVRange) if (self.CenterPosition != cachedPosition || desiredRange != cachedRange || desiredVRange != cachedVRange)
{ {
cachedPosition = self.CenterPosition; cachedPosition = self.CenterPosition;
@@ -102,18 +103,18 @@ namespace OpenRA.Mods.Common.Traits
if (a.Disposed || self.Disposed) if (a.Disposed || self.Disposed)
return; return;
if (a == self && !info.AffectsParent) if (a == self && !Info.AffectsParent)
return; return;
if (tokens.ContainsKey(a)) if (tokens.ContainsKey(a))
return; return;
var stance = self.Owner.Stances[a.Owner]; var stance = self.Owner.Stances[a.Owner];
if (!info.ValidStances.HasStance(stance)) if (!Info.ValidStances.HasStance(stance))
return; return;
var external = a.TraitsImplementing<ExternalCondition>() var external = a.TraitsImplementing<ExternalCondition>()
.FirstOrDefault(t => t.Info.Condition == info.Condition && t.CanGrantCondition(a, self)); .FirstOrDefault(t => t.Info.Condition == Info.Condition && t.CanGrantCondition(a, self));
if (external != null) if (external != null)
tokens[a] = external.GrantCondition(a, self); tokens[a] = external.GrantCondition(a, self);
@@ -126,18 +127,18 @@ namespace OpenRA.Mods.Common.Traits
return; return;
// We don't grant conditions when disabled // We don't grant conditions when disabled
if (self.IsDisabled()) if (IsTraitDisabled)
return; return;
// Work around for actors produced within the region not triggering until the second tick // Work around for actors produced within the region not triggering until the second tick
if ((produced.CenterPosition - self.CenterPosition).HorizontalLengthSquared <= info.Range.LengthSquared) if ((produced.CenterPosition - self.CenterPosition).HorizontalLengthSquared <= Info.Range.LengthSquared)
{ {
var stance = self.Owner.Stances[produced.Owner]; var stance = self.Owner.Stances[produced.Owner];
if (!info.ValidStances.HasStance(stance)) if (!Info.ValidStances.HasStance(stance))
return; return;
var external = produced.TraitsImplementing<ExternalCondition>() var external = produced.TraitsImplementing<ExternalCondition>()
.FirstOrDefault(t => t.Info.Condition == info.Condition && t.CanGrantCondition(produced, self)); .FirstOrDefault(t => t.Info.Condition == Info.Condition && t.CanGrantCondition(produced, self));
if (external != null) if (external != null)
tokens[produced] = external.GrantCondition(produced, self); tokens[produced] = external.GrantCondition(produced, self);

View File

@@ -301,7 +301,10 @@ NASTLH:
PowerupSpeech: EnablePower PowerupSpeech: EnablePower
PowerdownSpeech: DisablePower PowerdownSpeech: DisablePower
IndicatorPalette: mouse IndicatorPalette: mouse
GrantConditionOnDisabled:
Condition: disabled
ProximityExternalCondition: ProximityExternalCondition:
RequiresCondition: !disabled
Condition: cloakgenerator Condition: cloakgenerator
Range: 12c0 Range: 12c0
EnableSound: cloak5.aud EnableSound: cloak5.aud