void Attack(Transform target) { IAttackable targetBody = target.GetComponent <IAttackable>(); if (targetBody == null) { throw new ArgumentException("Target cannot be attacked."); } Attack attack = BuildAttack(); Defense targetDefense = targetBody.GetDefense(); AttackResult result = AttackResolver.ResolvePhysicalAttack(attack, targetDefense); targetBody.Attack(result); }
public AttackResult ResolvePhysicalAttack(Attack atk, Defense def) { var result = new AttackResult(); result.OriginalHealth = def.CurrentHealth; result.NameOfAttacker = atk.NameOfAttacker; result.NameOfDefender = def.NameOfDefender; if (RandomRoll > atk.Accuracy) // miss { result.IsMiss = true; Logger.Log(result.ToString()); return(result); } if (RandomRoll < def.PhysicalEvasion) // evaded { result.IsEvaded = true; Logger.Log(result.ToString()); return(result); } if (RandomRoll < atk.CritChance) { result.IsCritical = true; } int physicalReduction = def.PhysicalDamageSubtraction; // magical reduction gets distributed across the nonphysical types until it's depleted int magicalReduction = def.NonPhysicalDamageSubstraction; result.MagicDamage = ComputeDamage(atk.MagicDamage, 0, 0, magicalReduction); magicalReduction -= result.MagicDamage.Reduced; result.LightningDamage = ComputeDamage(atk.LightningDamage, def.LightningResistance, def.LightningAbsorption, magicalReduction); magicalReduction -= result.LightningDamage.Reduced; result.FireDamage = ComputeDamage(atk.FireDamage, def.FireResistance, def.FireAbsorption, magicalReduction); magicalReduction -= result.FireDamage.Reduced; result.ColdDamage = ComputeDamage(atk.ColdDamage, def.ColdResistance, def.ColdAbsorption, magicalReduction); magicalReduction -= result.ColdDamage.Reduced; result.PoisonDamage = ComputeDamage(atk.PoisonDamage, def.PoisonResistance, def.PoisonAbsoprtion, magicalReduction); magicalReduction -= result.PoisonDamage.Reduced; int critFactor = result.IsCritical ? atk.CritMultiplier : 1; result.PhysicalDamage = ComputePhysicalDamage(atk.PhysicalDamage, def.Armor, critFactor, physicalReduction); Logger.Log(result.ToString()); return(result); }
void TryMoveTo(Coord coord) { if (Map.IsWalkable(coord)) { Collider2D collision = CustomRaycast.GetMapEntityColliderAt(coord); if (collision != null) // something is in the way { IAttackable target = collision.GetComponent <IAttackable>(); if (target != null) // that something can be attacked { Defense defense = target.GetDefense(); Attack attack = attacker.BuildAttack(); AttackResult result = attackResolver.ResolvePhysicalAttack(attack, defense); target.Attack(result); time.IncreaseBasedOnSpeed(AttackSpeed); } } else { transform.position = (Vector2)coord; time.IncreaseBasedOnSpeed(MoveSpeed); } } }