Skip to content

Commit

Permalink
Added invulnerability time when taking damage to IDamageable
Browse files Browse the repository at this point in the history
  • Loading branch information
treesgobark committed Jan 26, 2024
1 parent 3b59566 commit 153c0c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
23 changes: 18 additions & 5 deletions Engines/FlatRedBallXNA/FlatRedBall/Entities/IDamageable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ public interface IDamageable
Dictionary<IDamageArea, double> DamageAreaLastDamage { get; }
int TeamIndex { get; }
bool IsDamageReceivingEnabled { get; }
double InvulnerabilityTimeAfterDamage { get; }
double LastDamageTime { get; set; }
decimal CurrentHealth { get; set; }
bool IsInvulnerable { get; }
decimal MaxHealth { get; set; }

/// <summary>
/// Event raised before damave is dealt. This event can be used to modify the damage dealt.
/// Event raised before damage is dealt. This event can be used to modify the damage dealt.
/// </summary>
Func<decimal, IDamageArea, decimal> ModifyDamageReceived { get; set; }
/// <summary>
Expand All @@ -42,7 +45,8 @@ public static bool ShouldTakeDamage(this IDamageable damageable, IDamageArea dam
if (damageable.TeamIndex == damageArea.TeamIndex
|| damageable.CurrentHealth <= 0
|| !damageArea.IsDamageDealingEnabled
|| !damageable.IsDamageReceivingEnabled)
|| !damageable.IsDamageReceivingEnabled
|| damageable.IsInvulnerable)
{
return false;
}
Expand All @@ -52,6 +56,7 @@ public static bool ShouldTakeDamage(this IDamageable damageable, IDamageArea dam
// damage area, so deal damage and record the time in the damageAreaLastDamage
// dictionary.
damageable.DamageAreaLastDamage.Add(damageArea, TimeManager.CurrentScreenTime);
damageable.LastDamageTime = TimeManager.CurrentScreenTime;

// Remove the damage area from the dictionary when it is destroyed or else
// the Player may accumulate a large collection of damage areas, resulting in
Expand All @@ -69,6 +74,7 @@ public static bool ShouldTakeDamage(this IDamageable damageable, IDamageArea dam
{
// If so, update the last damage time.
damageable.DamageAreaLastDamage[damageArea] = TimeManager.CurrentScreenTime;
damageable.LastDamageTime = TimeManager.CurrentScreenTime;
return true;
}
else
Expand Down Expand Up @@ -100,14 +106,21 @@ public static decimal TakeDamage(this IDamageable damageable, IDamageArea damage
var modifiedByBoth = damageArea.ModifyDamageDealt?.Invoke(modifiedByDamageable, damageable) ?? modifiedByDamageable;

var healthBefore = damageable.CurrentHealth;

if (modifiedByBoth != 0)
{
damageable.CurrentHealth -= modifiedByBoth;
decimal newHealth = damageable.CurrentHealth - modifiedByBoth;

if (newHealth > damageable.MaxHealth)
{
newHealth = damageable.MaxHealth;
}

damageable.CurrentHealth = newHealth;
}

// We used to not raise events when taking 0 damage, but we may want
// to have some kind of logic play when taking 0 damage, likeplay a sound
// to have some kind of logic play when taking 0 damage, like play a sound
// effect to indicate that this is not a spot that an enemy can get hit.
damageable.ReactToDamageReceived?.Invoke(modifiedByBoth, damageArea);
damageArea.ReactToDamageDealt?.Invoke(modifiedByBoth, damageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public override ICodeBlock GenerateFields(ICodeBlock codeBlock, IElement element
if (UsesDamageV3)
{
codeBlock.Line("public bool IsDamageReceivingEnabled { get; set; } = true;");
codeBlock.Line("public double InvulnerabilityTimeAfterDamage { get; set; } = 0;");
codeBlock.Line("public bool IsInvulnerable => TimeManager.CurrentScreenSecondsSince(LastDamageTime) < InvulnerabilityTimeAfterDamage;");
codeBlock.Line("public double LastDamageTime { get; set; } = -999;");
}
}
}
Expand Down

0 comments on commit 153c0c6

Please sign in to comment.