Armour before Health!

So I also would like to invent a new stats called “Armour” and it should work simular to the Health stats. Right now when we are taking damage, we actually refering directly on the Health component (when I am not super wrong)

So what I would actually like to do is checking first, if I still/have armour points and only taking damage on the health when the armour points are actually at 0. So something like that:

if(armourPoints <= 0)
{
attack my Health Points
}
else
{
Attack my armour Points
}

how is it actually possible to achieve that, when right now we are refering directly to the Health component itself? Cause I really would like to make the armour we put on in the inventory to really work as an armour and not only boosting up the health stats.

I was thinking of a similar thing (a little more complex though, armour, resistance, health etc)

One of the issues depends on how you’re going to handle things. Take yours for instance, lets say you’re doing 10 points of damage but only have 1 point of armour, it wipes out all the damage instead of having the remaining 9 points take from the health.

I don’t know a great way to do it, but what I came up with is to have a damage handler (think of it like IDamagable) and it checks for the appropriate components to see how it behaves each time damage comes in and process it appropriately.

@MichaelP It doesn’t have to wipe out all the damage. Armor could be ‘composed’ of, for instance, an IProtection interface like this

public interface IProtection
{
    int AbsorbDamage(int amount);
}

This interface method can return the amount of damage not absorbed. When taking damage, the IProtection items can be retrieved, and used in a chain

public void TakeDamage(int amount)
{
    foreach (var protectionItem in GetProtectionItems())  // this gets all the items that implements IProtection
    {
        amount = protectionItem.AbsorbDamage(amount);
        if (amount <= 0) return; // all damage was absorbed. we can stop
    }
    _health -= amount; // apply remaining damage
}

Armor could then implement IProtection

public class MyArmor : MonoBehaviour, IProtection
{
    private int _armorAmount = 10;

    public int AbsorbDamage(int amount)
    {
        return Mathf.Max(amount - _armorAmount, 0); // keep remaining amount >= 0
    }
}

I haven’t done the Shops and Abilities course yet (it’s on my list), so I’m guessing a few things here

2 Likes

I was talking specifically with the OPs code logic inside their if/else. Your way is a way around it.

1 Like

@bixarrio 's approach is the best one, and pretty much what I would have suggested.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms