Esempio n. 1
0
    /// <summary>
    /// Resolves an attack between two units.
    /// Returns true if the defender has died.
    /// </summary>
    /// <param name="attacker"></param>
    /// <param name="defender"></param>
    /// <returns></returns>
    public static bool ResolveAttack(IUnit attacker, IUnit defender)
    {
        float hitChance = CalculateHitChance(attacker, defender);

        //If it is a hit
        if (Random.Range(0, 1.0f) < hitChance)
        {
            //Calculate combat damage
            float combatDamage = GameMath.CalculateCombatDamage(attacker, defender);

            //Distribute the damage to a random body part?
            float totalSize = 0;
            foreach (BodyPart x in defender.b_parts)
            {
                if (x.wound.gravity != Wound.WoundGravity.REMOVED)
                {
                    totalSize += x.size;
                }
            }

            float randomPlace = Random.Range(0, totalSize);

            BodyPart partHit = null;
            int      index   = 0;
            while (randomPlace > 0.0f && index < defender.b_parts.Count)
            {
                partHit      = defender.b_parts[index];
                randomPlace -= partHit.size;
                index++;
            }

            //Apply damage
            //Debug.Log(attacker.name + " struck " + defender.name);
            DamageBodyPart(defender, partHit, combatDamage);

            //Check if unit is dead
            if (partHit.partFunctions.Contains(BodyPart.BodyPartFunction.VITAL) && partHit.wound.gravity.Equals(Wound.WoundGravity.REMOVED))
            {
                defender.isAlive = false;
                Debug.Log(defender.name + " has died.");
                return(true);
            }
            if (defender.CheckIfHarmless())
            {
                Debug.Log(defender.name + " is out of combat. ");
                return(true);
            }
        }
        else
        {
            //Debug.Log(attacker.name + " missed " + defender.name);
        }
        return(false);
    }