Convert damage modifiers to integer percentages.

This commit is contained in:
Paul Chote
2014-08-09 21:27:37 +12:00
parent c5519a7f3b
commit 0425416ce2
11 changed files with 59 additions and 27 deletions

View File

@@ -100,16 +100,21 @@ namespace OpenRA.Traits
public void InflictDamage(Actor self, Actor attacker, int damage, DamageWarhead warhead, bool ignoreModifiers)
{
if (IsDead) return; /* overkill! don't count extra hits as more kills! */
// Overkill! don't count extra hits as more kills!
if (IsDead)
return;
var oldState = this.DamageState;
/* apply the damage modifiers, if we have any. */
var modifier = self.TraitsImplementing<IDamageModifier>()
.Concat(self.Owner.PlayerActor.TraitsImplementing<IDamageModifier>())
.Select(t => t.GetDamageModifier(attacker, warhead)).Product();
if (!ignoreModifiers)
damage = damage > 0 ? (int)(damage * modifier) : damage;
// Apply any damage modifiers
if (!ignoreModifiers && damage > 0)
{
var modifiers = self.TraitsImplementing<IDamageModifier>()
.Concat(self.Owner.PlayerActor.TraitsImplementing<IDamageModifier>())
.Select(t => t.GetDamageModifier(attacker, warhead));
damage = Util.ApplyPercentageModifiers(damage, modifiers);
}
hp = Exts.Clamp(hp - damage, 0, MaxHP);

View File

@@ -155,7 +155,7 @@ namespace OpenRA.Traits
}
public interface IRenderModifier { IEnumerable<IRenderable> ModifyRender(Actor self, WorldRenderer wr, IEnumerable<IRenderable> r); }
public interface IDamageModifier { float GetDamageModifier(Actor attacker, DamageWarhead warhead); }
public interface IDamageModifier { int GetDamageModifier(Actor attacker, DamageWarhead warhead); }
public interface ISpeedModifier { decimal GetSpeedModifier(); }
public interface IFirepowerModifier { float GetFirepowerModifier(); }
public interface ILoadsPalettes { void LoadPalettes(WorldRenderer wr); }

View File

@@ -138,5 +138,15 @@ namespace OpenRA.Traits
var cells = target.Positions.Select(p => w.Map.CellContaining(p)).Distinct();
return ExpandFootprint(cells, true);
}
public static int ApplyPercentageModifiers(int number, IEnumerable<int> percentages)
{
// See the comments of PR#6079 for a faster algorithm if this becomes a performance bottleneck
var a = (decimal)number;
foreach (var p in percentages)
a *= p / 100m;
return (int)a;
}
}
}