Convert AffectedByPowerOutage from disabler to conditional condition granter.

This commit is contained in:
atlimit8
2017-03-16 16:16:23 -05:00
parent 61b84e0c5a
commit 0dfdea1826
7 changed files with 65 additions and 25 deletions

View File

@@ -15,23 +15,39 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[Desc("Disables the actor when a power outage is triggered (see `InfiltrateForPowerOutage` for more information).")] [Desc("Disables the actor when a power outage is triggered (see `InfiltrateForPowerOutage` for more information).")]
public class AffectedByPowerOutageInfo : ITraitInfo public class AffectedByPowerOutageInfo : ConditionalTraitInfo
{ {
public object Create(ActorInitializer init) { return new AffectedByPowerOutage(init.Self); } [GrantedConditionReference]
[Desc("The condition to grant while there is a power outage.")]
public readonly string Condition = null;
public override object Create(ActorInitializer init) { return new AffectedByPowerOutage(init.Self, this); }
} }
public class AffectedByPowerOutage : INotifyOwnerChanged, ISelectionBar, IPowerModifier, IDisable public class AffectedByPowerOutage : ConditionalTrait<AffectedByPowerOutageInfo>, INotifyOwnerChanged, ISelectionBar, INotifyCreated, INotifyAddedToWorld
{ {
PowerManager playerPower; PowerManager playerPower;
ConditionManager conditionManager;
int token = ConditionManager.InvalidConditionToken;
public AffectedByPowerOutage(Actor self) public AffectedByPowerOutage(Actor self, AffectedByPowerOutageInfo info)
: base(info)
{ {
playerPower = self.Owner.PlayerActor.Trait<PowerManager>(); playerPower = self.Owner.PlayerActor.Trait<PowerManager>();
} }
void INotifyAddedToWorld.AddedToWorld(Actor self) { UpdateStatus(self); }
protected override void TraitEnabled(Actor self) { UpdateStatus(self); }
protected override void TraitDisabled(Actor self) { Revoke(self); }
void INotifyCreated.Created(Actor self)
{
conditionManager = self.TraitOrDefault<ConditionManager>();
}
float ISelectionBar.GetValue() float ISelectionBar.GetValue()
{ {
if (playerPower.PowerOutageRemainingTicks <= 0) if (IsTraitDisabled || playerPower.PowerOutageRemainingTicks <= 0)
return 0; return 0;
return (float)playerPower.PowerOutageRemainingTicks / playerPower.PowerOutageTotalTicks; return (float)playerPower.PowerOutageRemainingTicks / playerPower.PowerOutageTotalTicks;
@@ -44,19 +60,30 @@ namespace OpenRA.Mods.Common.Traits
bool ISelectionBar.DisplayWhenEmpty { get { return false; } } bool ISelectionBar.DisplayWhenEmpty { get { return false; } }
int IPowerModifier.GetPowerModifier() public void UpdateStatus(Actor self)
{ {
return playerPower.PowerOutageRemainingTicks > 0 ? 0 : 100; if (!IsTraitDisabled && playerPower.PowerOutageRemainingTicks > 0)
Grant(self);
else
Revoke(self);
} }
public bool Disabled void Grant(Actor self)
{ {
get { return playerPower.PowerOutageRemainingTicks > 0; } if (token == ConditionManager.InvalidConditionToken)
token = conditionManager.GrantCondition(self, Info.Condition);
}
void Revoke(Actor self)
{
if (token != ConditionManager.InvalidConditionToken)
token = conditionManager.RevokeCondition(self, token);
} }
void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{ {
playerPower = newOwner.PlayerActor.Trait<PowerManager>(); playerPower = newOwner.PlayerActor.Trait<PowerManager>();
UpdateStatus(self);
} }
} }
} }

View File

@@ -141,11 +141,11 @@ namespace OpenRA.Mods.Common.Traits
void UpdatePowerOutageActors() void UpdatePowerOutageActors()
{ {
var actors = self.World.ActorsHavingTrait<AffectedByPowerOutage>() var traitPairs = self.World.ActorsWithTrait<AffectedByPowerOutage>()
.Where(a => !a.IsDead && a.IsInWorld && a.Owner == self.Owner); .Where(p => !p.Actor.IsDead && p.Actor.IsInWorld && p.Actor.Owner == self.Owner);
foreach (var a in actors) foreach (var p in traitPairs)
UpdateActor(a); p.Trait.UpdateStatus(p.Actor);
} }
void IResolveOrder.ResolveOrder(Actor self, Order order) void IResolveOrder.ResolveOrder(Actor self, Order order)

View File

@@ -933,3 +933,12 @@
Palette: disabled Palette: disabled
GrantConditionOnDisabled@IDISABLE: GrantConditionOnDisabled@IDISABLE:
Condition: disabled Condition: disabled
^DisabledByPowerOutage:
AffectedByPowerOutage:
Condition: power-outage
InfiltrateForPowerOutage:
DisableOnCondition@POWER_OUTAGE:
RequiresCondition: power-outage
Power:
RequiresCondition: !power-outage

View File

@@ -1304,6 +1304,7 @@ AFLD:
POWR: POWR:
Inherits: ^Building Inherits: ^Building
Inherits@IDISABLE: ^DisabledOverlay Inherits@IDISABLE: ^DisabledOverlay
Inherits@POWER_OUTAGE: ^DisabledByPowerOutage
Buildable: Buildable:
Queue: Building Queue: Building
BuildPaletteOrder: 10 BuildPaletteOrder: 10
@@ -1327,8 +1328,6 @@ POWR:
Bib: Bib:
Power: Power:
Amount: 100 Amount: 100
InfiltrateForPowerOutage:
AffectedByPowerOutage:
Targetable: Targetable:
TargetTypes: Ground, Structure, C4, DetonateAttack, SpyInfiltrate TargetTypes: Ground, Structure, C4, DetonateAttack, SpyInfiltrate
ScalePowerWithHealth: ScalePowerWithHealth:
@@ -1339,6 +1338,7 @@ POWR:
APWR: APWR:
Inherits: ^Building Inherits: ^Building
Inherits@IDISABLE: ^DisabledOverlay Inherits@IDISABLE: ^DisabledOverlay
Inherits@POWER_OUTAGE: ^DisabledByPowerOutage
Buildable: Buildable:
Queue: Building Queue: Building
BuildPaletteOrder: 110 BuildPaletteOrder: 110
@@ -1366,8 +1366,6 @@ APWR:
Bib: Bib:
Power: Power:
Amount: 200 Amount: 200
InfiltrateForPowerOutage:
AffectedByPowerOutage:
Targetable: Targetable:
TargetTypes: Ground, Structure, C4, DetonateAttack, SpyInfiltrate TargetTypes: Ground, Structure, C4, DetonateAttack, SpyInfiltrate
ScalePowerWithHealth: ScalePowerWithHealth:

View File

@@ -954,3 +954,12 @@
Palette: disabled Palette: disabled
GrantConditionOnDisabled@IDISABLE: GrantConditionOnDisabled@IDISABLE:
Condition: disabled Condition: disabled
^DisabledByPowerOutage:
AffectedByPowerOutage:
Condition: power-outage
InfiltrateForPowerOutage:
DisableOnCondition@POWER_OUTAGE:
RequiresCondition: power-outage
Power:
RequiresCondition: !power-outage

View File

@@ -1,6 +1,7 @@
GAPOWR: GAPOWR:
Inherits: ^Building Inherits: ^Building
Inherits@IDISABLE: ^DisabledOverlay Inherits@IDISABLE: ^DisabledOverlay
Inherits@POWER_OUTAGE: ^DisabledByPowerOutage
Buildable: Buildable:
Queue: Building Queue: Building
BuildPaletteOrder: 10 BuildPaletteOrder: 10
@@ -32,8 +33,6 @@ GAPOWR:
Bounds: 90, 48, 0, -6 Bounds: 90, 48, 0, -6
Power: Power:
Amount: 100 Amount: 100
InfiltrateForPowerOutage:
AffectedByPowerOutage:
PowerTooltip: PowerTooltip:
Targetable: Targetable:
TargetTypes: Ground, C4, DetonateAttack, SpyInfiltrate TargetTypes: Ground, C4, DetonateAttack, SpyInfiltrate
@@ -43,7 +42,7 @@ GAPOWR:
Conditions: Conditions:
powrup: powrup.a powrup: powrup.a
Power@pluga: Power@pluga:
RequiresCondition: powrup.a RequiresCondition: powrup.a && !power-outage
Amount: 50 Amount: 50
WithIdleOverlay@pluga: WithIdleOverlay@pluga:
RequiresCondition: powrup.a RequiresCondition: powrup.a
@@ -58,7 +57,7 @@ GAPOWR:
PauseOnCondition: disabled PauseOnCondition: disabled
Sequence: idle-powrupb Sequence: idle-powrupb
Power@plugb: Power@plugb:
RequiresCondition: powrup.b RequiresCondition: powrup.b && !power-outage
Amount: 50 Amount: 50
ProvidesPrerequisite@buildingname: ProvidesPrerequisite@buildingname:
SelectionDecorations: SelectionDecorations:

View File

@@ -1,6 +1,7 @@
NAPOWR: NAPOWR:
Inherits: ^Building Inherits: ^Building
Inherits@IDISABLE: ^DisabledOverlay Inherits@IDISABLE: ^DisabledOverlay
Inherits@POWER_OUTAGE: ^DisabledByPowerOutage
Buildable: Buildable:
Queue: Building Queue: Building
BuildPaletteOrder: 20 BuildPaletteOrder: 20
@@ -29,8 +30,6 @@ NAPOWR:
Sequence: idle-lights Sequence: idle-lights
Power: Power:
Amount: 100 Amount: 100
InfiltrateForPowerOutage:
AffectedByPowerOutage:
Targetable: Targetable:
TargetTypes: Ground, C4, DetonateAttack, SpyInfiltrate TargetTypes: Ground, C4, DetonateAttack, SpyInfiltrate
ScalePowerWithHealth: ScalePowerWithHealth:
@@ -41,6 +40,7 @@ NAPOWR:
NAAPWR: NAAPWR:
Inherits: ^Building Inherits: ^Building
Inherits@IDISABLE: ^DisabledOverlay Inherits@IDISABLE: ^DisabledOverlay
Inherits@POWER_OUTAGE: ^DisabledByPowerOutage
Buildable: Buildable:
Queue: Building Queue: Building
BuildPaletteOrder: 120 BuildPaletteOrder: 120
@@ -69,8 +69,6 @@ NAAPWR:
Sequence: idle-lights Sequence: idle-lights
Power: Power:
Amount: 200 Amount: 200
InfiltrateForPowerOutage:
AffectedByPowerOutage:
Targetable: Targetable:
TargetTypes: Ground, C4, DetonateAttack, SpyInfiltrate TargetTypes: Ground, C4, DetonateAttack, SpyInfiltrate
ScalePowerWithHealth: ScalePowerWithHealth: