Skip to content

Commit

Permalink
Merge pull request #1336 from vchelaru/DamageDealingToggles
Browse files Browse the repository at this point in the history
Added toggle properties for damage dealing and receiving
  • Loading branch information
vchelaru authored Jan 26, 2024
2 parents fa7ba93 + 153c0c6 commit f119296
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
1 change: 1 addition & 0 deletions Engines/FlatRedBallXNA/FlatRedBall/Entities/IDamageArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IDamageArea : ICollidable
{
event Action Destroyed;
double SecondsBetweenDamage { get; set; }
bool IsDamageDealingEnabled { get; }
object DamageDealer { get; }
int TeamIndex { get; }

Expand Down
29 changes: 23 additions & 6 deletions Engines/FlatRedBallXNA/FlatRedBall/Entities/IDamageable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ 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 @@ -30,15 +34,19 @@ public static class DamageableExtensionMethods
{
/// <summary>
/// Returns whether the argument IDamageable should take damage from the argument IDamageArea.
/// This returns true if the team indexes are different, ifthe damageable has > 0 CurrentHealth,
/// This returns true if the team indexes are different, if the damageable has > 0 CurrentHealth,
/// and if enough time has passed since the last damage was dealt by this particular IDamageArea instance.
/// </summary>
/// <param name="damageable">The damageable object, typically a Player or Enemy.</param>
/// <param name="damageArea">The damage dealing object, typically a bullet or enemy.</param>
/// <returns></returns>
public static bool ShouldTakeDamage(this IDamageable damageable, IDamageArea damageArea)
{
if (damageable.TeamIndex == damageArea.TeamIndex || damageable.CurrentHealth <= 0)
if (damageable.TeamIndex == damageArea.TeamIndex
|| damageable.CurrentHealth <= 0
|| !damageArea.IsDamageDealingEnabled
|| !damageable.IsDamageReceivingEnabled
|| damageable.IsInvulnerable)
{
return false;
}
Expand All @@ -48,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 @@ -65,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 @@ -96,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
2 changes: 1 addition & 1 deletion Engines/FlatRedBallXNA/FlatRedBall/FlatRedBallServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public object GetService(Type serviceType)

public class SyntaxVersionAttribute : Attribute
{
public int Version = 51;
public int Version = 52;
}

public static partial class FlatRedBallServices
Expand Down
5 changes: 3 additions & 2 deletions FRBDK/Glue/Glue/SaveClasses/GlueProjectSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ public enum GluxVersions
GumUsesSystemTypes = 49,
// Technically this isn't referencing GumCommon project, but it's referencing code from GumCommon
GumCommonCodeReferencing = 50,
GumTextSupportsBbCode = 51
GumTextSupportsBbCode = 51,
DamageDealingToggles = 52

// Stop! If adding an entry here, modify SyntaxVersionAttribute.Version
}
Expand All @@ -143,7 +144,7 @@ public enum GluxVersions

#region Versions

public const int LatestVersion = (int)GluxVersions.GumTextSupportsBbCode;
public const int LatestVersion = (int)GluxVersions.DamageDealingToggles;

public int FileVersion { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public override void AddInheritedTypesToList(List<string> listToAddTo, IElement

#endregion

public static bool UsesDamageV2 => GlueState.Self.CurrentGlueProject.FileVersion >= (int)GlueProjectSave.GluxVersions.DamageableHasHealth;
public static bool UsesDamageV2 => GlueState.Self.CurrentMainProject.IsFrbSourceLinked()
|| GlueState.Self.CurrentGlueProject.FileVersion >= (int)GlueProjectSave.GluxVersions.DamageableHasHealth;
public static bool UsesDamageV3 => GlueState.Self.CurrentMainProject.IsFrbSourceLinked()
|| GlueState.Self.CurrentGlueProject.FileVersion >= (int)GlueProjectSave.GluxVersions.DamageDealingToggles;

public override ICodeBlock GenerateFields(ICodeBlock codeBlock, IElement element)
{
Expand Down Expand Up @@ -70,6 +73,11 @@ public override ICodeBlock GenerateFields(ICodeBlock codeBlock, IElement element
codeBlock.Line("public Action<FlatRedBall.Entities.IDamageable> RemovedByCollision { get; set; }");

}

if (UsesDamageV3)
{
codeBlock.Line("public bool IsDamageDealingEnabled { get; set; } = true;");
}
}

if (shouldImplementIDamageable)
Expand All @@ -90,6 +98,14 @@ public override ICodeBlock GenerateFields(ICodeBlock codeBlock, IElement element
codeBlock.Line("public decimal CurrentHealth { get; set; }");
codeBlock.Line("public Action<decimal, FlatRedBall.Entities.IDamageArea> Died { get; set; }");
}

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 f119296

Please sign in to comment.