// Applies a single instance of damage to the character // returns true if the damage is successful (including if the char is already at 0) // If we want to keep track of death saves or potential oneshots I'd probably want a damage result return value public static bool ApplyDamage(this Character character, DealtDamageInformation damageInformation) { if (character == null) { return(false); } if (character.HitPoints == null) { return(false); } if (damageInformation == null) { return(false); } if (damageInformation.Value < 0) { return(false); } if (String.IsNullOrEmpty(damageInformation.Type)) { return(false); } DamageResistance typeResistance = character.damageResistanceForDamageInfo(damageInformation); int damageRemaining = damageValueForResistance(damageInformation.Value, typeResistance); if (character.HitPoints.Temporary > 0) { if (character.HitPoints.Temporary > damageRemaining) { character.HitPoints.Temporary -= damageRemaining; damageRemaining = 0; } else { damageRemaining -= character.HitPoints.Temporary; character.HitPoints.Temporary = 0; } } if ((damageRemaining > 0) && (character.HitPoints.Current > 0)) { if (damageRemaining > character.HitPoints.Current) { damageRemaining = damageRemaining - character.HitPoints.Current; character.HitPoints.Current = 0; } else { character.HitPoints.Current -= damageRemaining; damageRemaining = 0; } } // damageRemaining might be useful for determining whether this char got one-shot // But I'm not planning to implement that for this. return(true); }
// Figure out what sort of resistance the character has for the given damage type private static DamageResistance damageResistanceForDamageInfo(this Character character, DealtDamageInformation damageInformation) { Dictionary <string, DamageResistance> resistances = character.DamageResistances(); string type = damageInformation.Type.ToLower(); if (resistances.ContainsKey(type)) { return(resistances[type]); } return(DamageResistance.None); }