Merge pull request #11816 from reaperrr/fix-UpgradeOnDamage

Fix UpgradeOnDamage to apply if initial DamageState is valid
This commit is contained in:
Oliver Brakmann
2016-08-20 15:25:10 +02:00
committed by GitHub
4 changed files with 44 additions and 22 deletions

View File

@@ -507,7 +507,7 @@
<Compile Include="Traits\Upgrades\DisableOnUpgrade.cs" />
<Compile Include="Traits\Upgrades\UpgradableTrait.cs" />
<Compile Include="Traits\Upgrades\UpgradeActorsNear.cs" />
<Compile Include="Traits\Upgrades\UpgradeOnDamage.cs" />
<Compile Include="Traits\Upgrades\UpgradeOnDamageState.cs" />
<Compile Include="Traits\Upgrades\UpgradeOnTerrain.cs" />
<Compile Include="Traits\Upgrades\UpgradeManager.cs" />
<Compile Include="Traits\Valued.cs" />

View File

@@ -14,7 +14,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Applies an upgrade to the actor at specified damage states.")]
public class UpgradeOnDamageInfo : ITraitInfo, Requires<UpgradeManagerInfo>
public class UpgradeOnDamageStateInfo : ITraitInfo, Requires<UpgradeManagerInfo>, Requires<HealthInfo>
{
[UpgradeGrantedReference, FieldLoader.Require]
[Desc("The upgrades to grant.")]
@@ -32,41 +32,52 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Are upgrades irrevocable once the conditions have been met?")]
public readonly bool GrantPermanently = false;
public object Create(ActorInitializer init) { return new UpgradeOnDamage(init.Self, this); }
public object Create(ActorInitializer init) { return new UpgradeOnDamageState(init.Self, this); }
}
public class UpgradeOnDamage : INotifyDamageStateChanged
public class UpgradeOnDamageState : INotifyDamageStateChanged, INotifyCreated
{
readonly UpgradeOnDamageInfo info;
readonly UpgradeOnDamageStateInfo info;
readonly UpgradeManager um;
readonly Health health;
bool granted;
public UpgradeOnDamage(Actor self, UpgradeOnDamageInfo info)
public UpgradeOnDamageState(Actor self, UpgradeOnDamageStateInfo info)
{
this.info = info;
um = self.TraitOrDefault<UpgradeManager>();
um = self.Trait<UpgradeManager>();
health = self.Trait<Health>();
}
void INotifyCreated.Created(Actor self)
{
GrantUpgradeOnValidDamageState(self, health.DamageState);
}
void GrantUpgradeOnValidDamageState(Actor self, DamageState state)
{
if (!info.ValidDamageStates.HasFlag(state))
return;
granted = true;
var rand = Game.CosmeticRandom;
var sound = info.EnabledSounds.RandomOrDefault(rand);
Game.Sound.Play(sound, self.CenterPosition);
foreach (var u in info.Upgrades)
um.GrantUpgrade(self, u, this);
}
void INotifyDamageStateChanged.DamageStateChanged(Actor self, AttackInfo e)
{
if (um == null)
return;
if (granted && info.GrantPermanently)
return;
var rand = Game.CosmeticRandom;
if (!granted && info.ValidDamageStates.HasFlag(e.DamageState) && !info.ValidDamageStates.HasFlag(e.PreviousDamageState))
{
granted = true;
var sound = info.EnabledSounds.RandomOrDefault(rand);
Game.Sound.Play(sound, self.CenterPosition);
foreach (var u in info.Upgrades)
um.GrantUpgrade(self, u, this);
}
if (!granted && !info.ValidDamageStates.HasFlag(e.PreviousDamageState))
GrantUpgradeOnValidDamageState(self, health.DamageState);
else if (granted && !info.ValidDamageStates.HasFlag(e.DamageState) && info.ValidDamageStates.HasFlag(e.PreviousDamageState))
{
granted = false;
var rand = Game.CosmeticRandom;
var sound = info.DisabledSounds.RandomOrDefault(rand);
Game.Sound.Play(sound, self.CenterPosition);
foreach (var u in info.Upgrades)

View File

@@ -280,6 +280,17 @@ namespace OpenRA.Mods.Common.UtilityCommands
}
}
if (engineVersion < 20160818)
{
if (depth == 1 && node.Key.StartsWith("UpgradeOnDamage"))
{
var parts = node.Key.Split('@');
node.Key = "UpgradeOnDamageState";
if (parts.Length > 1)
node.Key += "@" + parts[1];
}
}
UpgradeActorRules(modData, engineVersion, ref node.Value.Nodes, node, depth + 1);
}

View File

@@ -397,7 +397,7 @@
WithInfantryBody:
AttackSequence: attack
IdleSequences: idle1,idle2
UpgradeOnDamage@CRITICAL:
UpgradeOnDamageState@CRITICAL:
Upgrades: criticalspeed
ValidDamageStates: Critical
GrantPermanently: true
@@ -476,10 +476,10 @@
Weapons: SmallDebris
Pieces: 3, 7
Range: 2c0, 5c0
UpgradeOnDamage@DAMAGED:
UpgradeOnDamageState@DAMAGED:
Upgrades: damagedspeed
ValidDamageStates: Heavy
UpgradeOnDamage@CRITICAL:
UpgradeOnDamageState@CRITICAL:
Upgrades: criticalspeed
ValidDamageStates: Critical
SpeedMultiplier@DAMAGED: